blob: ea4a8e10db4470599a9a63571bd4ed0f8a8f4eb8 [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{
127 psa_mac_operation_t operation;
128 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{
168 psa_cipher_operation_t operation;
169 unsigned char iv[16] = {0};
170 size_t iv_length = sizeof( iv );
171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
177 if( usage & PSA_KEY_USAGE_ENCRYPT )
178 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100179 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
180 handle, alg ) );
181 PSA_ASSERT( psa_cipher_generate_iv( &operation,
182 iv, sizeof( iv ),
183 &iv_length ) );
184 PSA_ASSERT( psa_cipher_update( &operation,
185 plaintext, sizeof( plaintext ),
186 ciphertext, sizeof( ciphertext ),
187 &ciphertext_length ) );
188 PSA_ASSERT( psa_cipher_finish( &operation,
189 ciphertext + ciphertext_length,
190 sizeof( ciphertext ) - ciphertext_length,
191 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 ciphertext_length += part_length;
193 }
194
195 if( usage & PSA_KEY_USAGE_DECRYPT )
196 {
197 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700198 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
200 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100202 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200203 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
204 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100205 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
206 handle, alg ) );
207 PSA_ASSERT( psa_cipher_set_iv( &operation,
208 iv, iv_length ) );
209 PSA_ASSERT( psa_cipher_update( &operation,
210 ciphertext, ciphertext_length,
211 decrypted, sizeof( decrypted ),
212 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200213 status = psa_cipher_finish( &operation,
214 decrypted + part_length,
215 sizeof( decrypted ) - part_length,
216 &part_length );
217 /* For a stream cipher, all inputs are valid. For a block cipher,
218 * if the input is some aribtrary data rather than an actual
219 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700220 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700221 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100222 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200223 else
224 TEST_ASSERT( status == PSA_SUCCESS ||
225 status == PSA_ERROR_INVALID_PADDING );
226 }
227
228 return( 1 );
229
230exit:
231 psa_cipher_abort( &operation );
232 return( 0 );
233}
234
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100235static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200236 psa_key_usage_t usage,
237 psa_algorithm_t alg )
238{
239 unsigned char nonce[16] = {0};
240 size_t nonce_length = sizeof( nonce );
241 unsigned char plaintext[16] = "Hello, world...";
242 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
243 size_t ciphertext_length = sizeof( ciphertext );
244 size_t plaintext_length = sizeof( ciphertext );
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100248 PSA_ASSERT( psa_aead_encrypt( handle, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100262 TEST_EQUAL( psa_aead_decrypt( handle, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100267 &plaintext_length ),
268 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100277static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200283 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 size_t signature_length = sizeof( signature );
285
286 if( usage & PSA_KEY_USAGE_SIGN )
287 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200288 /* Some algorithms require the payload to have the size of
289 * the hash encoded in the algorithm. Use this input size
290 * even for algorithms that allow other input sizes. */
291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
292 if( hash_alg != 0 )
293 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100294 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
295 payload, payload_length,
296 signature, sizeof( signature ),
297 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100306 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
307 payload, payload_length,
308 signature, signature_length ),
309 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200310 }
311
312 return( 1 );
313
314exit:
315 return( 0 );
316}
317
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100318static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200319 psa_key_usage_t usage,
320 psa_algorithm_t alg )
321{
322 unsigned char plaintext[256] = "Hello, world...";
323 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 size_t plaintext_length = 16;
326
327 if( usage & PSA_KEY_USAGE_ENCRYPT )
328 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100329 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100339 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100356static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361 unsigned char label[16] = "This is a label.";
362 size_t label_length = sizeof( label );
363 unsigned char seed[16] = "abcdefghijklmnop";
364 size_t seed_length = sizeof( seed );
365 unsigned char output[1];
366
367 if( usage & PSA_KEY_USAGE_DERIVE )
368 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100369 PSA_ASSERT( psa_key_derivation( &generator,
370 handle, alg,
371 label, label_length,
372 seed, seed_length,
373 sizeof( output ) ) );
374 PSA_ASSERT( psa_generator_read( &generator,
375 output,
376 sizeof( output ) ) );
377 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200378 }
379
380 return( 1 );
381
382exit:
383 return( 0 );
384}
385
Gilles Peskinec7998b72018-11-07 18:45:02 +0100386/* We need two keys to exercise key agreement. Exercise the
387 * private key against its own public key. */
388static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100390 psa_algorithm_t alg )
391{
392 psa_key_type_t private_key_type;
393 psa_key_type_t public_key_type;
394 size_t key_bits;
395 uint8_t *public_key = NULL;
396 size_t public_key_length;
397 /* Return UNKNOWN_ERROR if something other than the final call to
398 * psa_key_agreement fails. This isn't fully satisfactory, but it's
399 * good enough: callers will report it as a failed test anyway. */
400 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
401
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_get_key_information( handle,
403 &private_key_type,
404 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
406 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
407 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100408 PSA_ASSERT( psa_export_public_key( handle,
409 public_key, public_key_length,
410 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100412 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key, public_key_length,
414 alg );
415exit:
416 mbedtls_free( public_key );
417 return( status );
418}
419
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 psa_key_usage_t usage,
422 psa_algorithm_t alg )
423{
424 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200425 unsigned char output[1];
426 int ok = 0;
427
428 if( usage & PSA_KEY_USAGE_DERIVE )
429 {
430 /* We need two keys to exercise key agreement. Exercise the
431 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100432 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
433 PSA_ASSERT( psa_generator_read( &generator,
434 output,
435 sizeof( output ) ) );
436 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 }
438 ok = 1;
439
440exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200441 return( ok );
442}
443
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200444static int is_oid_of_key_type( psa_key_type_t type,
445 const uint8_t *oid, size_t oid_length )
446{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200447 const uint8_t *expected_oid = NULL;
448 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200449#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200450 if( PSA_KEY_TYPE_IS_RSA( type ) )
451 {
452 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
453 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
454 }
455 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200456#endif /* MBEDTLS_RSA_C */
457#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 if( PSA_KEY_TYPE_IS_ECC( type ) )
459 {
460 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
461 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
462 }
463 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200464#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465 {
466 char message[40];
467 mbedtls_snprintf( message, sizeof( message ),
468 "OID not known for key type=0x%08lx",
469 (unsigned long) type );
470 test_fail( message, __LINE__, __FILE__ );
471 return( 0 );
472 }
473
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200474 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
481static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
482 size_t min_bits, size_t max_bits,
483 int must_be_odd )
484{
485 size_t len;
486 size_t actual_bits;
487 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100488 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100489 MBEDTLS_ASN1_INTEGER ),
490 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 /* Tolerate a slight departure from DER encoding:
492 * - 0 may be represented by an empty string or a 1-byte string.
493 * - The sign bit may be used as a value bit. */
494 if( ( len == 1 && ( *p )[0] == 0 ) ||
495 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
496 {
497 ++( *p );
498 --len;
499 }
500 if( min_bits == 0 && len == 0 )
501 return( 1 );
502 msb = ( *p )[0];
503 TEST_ASSERT( msb != 0 );
504 actual_bits = 8 * ( len - 1 );
505 while( msb != 0 )
506 {
507 msb >>= 1;
508 ++actual_bits;
509 }
510 TEST_ASSERT( actual_bits >= min_bits );
511 TEST_ASSERT( actual_bits <= max_bits );
512 if( must_be_odd )
513 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
514 *p += len;
515 return( 1 );
516exit:
517 return( 0 );
518}
519
520static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
521 size_t *len,
522 unsigned char n, unsigned char tag )
523{
524 int ret;
525 ret = mbedtls_asn1_get_tag( p, end, len,
526 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
528 if( ret != 0 )
529 return( ret );
530 end = *p + *len;
531 ret = mbedtls_asn1_get_tag( p, end, len, tag );
532 if( ret != 0 )
533 return( ret );
534 if( *p + *len != end )
535 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
536 return( 0 );
537}
538
539static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
540 uint8_t *exported, size_t exported_length )
541{
542 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100543 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200544 else
545 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200546
547#if defined(MBEDTLS_DES_C)
548 if( type == PSA_KEY_TYPE_DES )
549 {
550 /* Check the parity bits. */
551 unsigned i;
552 for( i = 0; i < bits / 8; i++ )
553 {
554 unsigned bit_count = 0;
555 unsigned m;
556 for( m = 1; m <= 0x100; m <<= 1 )
557 {
558 if( exported[i] & m )
559 ++bit_count;
560 }
561 TEST_ASSERT( bit_count % 2 != 0 );
562 }
563 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200565#endif
566
567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
568 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
569 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 uint8_t *p = exported;
571 uint8_t *end = exported + exported_length;
572 size_t len;
573 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200574 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 * modulus INTEGER, -- n
576 * publicExponent INTEGER, -- e
577 * privateExponent INTEGER, -- d
578 * prime1 INTEGER, -- p
579 * prime2 INTEGER, -- q
580 * exponent1 INTEGER, -- d mod (p-1)
581 * exponent2 INTEGER, -- d mod (q-1)
582 * coefficient INTEGER, -- (inverse of q) mod p
583 * }
584 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100585 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
586 MBEDTLS_ASN1_SEQUENCE |
587 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
588 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
590 goto exit;
591 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
592 goto exit;
593 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
594 goto exit;
595 /* Require d to be at least half the size of n. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
597 goto exit;
598 /* Require p and q to be at most half the size of n, rounded up. */
599 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
606 goto exit;
607 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100609 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100610 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200611 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200612#endif /* MBEDTLS_RSA_C */
613
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
616 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100617 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_ECP_C */
622
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
624 {
625 uint8_t *p = exported;
626 uint8_t *end = exported + exported_length;
627 size_t len;
628 mbedtls_asn1_buf alg;
629 mbedtls_asn1_buf params;
630 mbedtls_asn1_bitstring bitstring;
631 /* SubjectPublicKeyInfo ::= SEQUENCE {
632 * algorithm AlgorithmIdentifier,
633 * subjectPublicKey BIT STRING }
634 * AlgorithmIdentifier ::= SEQUENCE {
635 * algorithm OBJECT IDENTIFIER,
636 * parameters ANY DEFINED BY algorithm OPTIONAL }
637 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100638 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
639 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100640 MBEDTLS_ASN1_CONSTRUCTED ),
641 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100642 TEST_EQUAL( p + len, end );
643 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200644 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
645 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100646 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
647 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200648 p = bitstring.p;
649#if defined(MBEDTLS_RSA_C)
650 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
651 {
652 /* RSAPublicKey ::= SEQUENCE {
653 * modulus INTEGER, -- n
654 * publicExponent INTEGER } -- e
655 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100656 TEST_EQUAL( bitstring.unused_bits, 0 );
657 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
658 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100659 MBEDTLS_ASN1_CONSTRUCTED ),
660 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100661 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200662 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
663 goto exit;
664 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
665 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100666 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200667 }
668 else
669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
671 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
672 {
673 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200674 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200675 * -- then x_P as an n-bit string, big endian;
676 * -- then y_P as a n-bit string, big endian,
677 * -- where n is the order of the curve.
678 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100679 TEST_EQUAL( bitstring.unused_bits, 0 );
680 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
681 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 }
683 else
684#endif /* MBEDTLS_ECP_C */
685 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100686 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200687 mbedtls_snprintf( message, sizeof( message ),
688 "No sanity check for public key type=0x%08lx",
689 (unsigned long) type );
690 test_fail( message, __LINE__, __FILE__ );
691 return( 0 );
692 }
693 }
694 else
695
696 {
697 /* No sanity checks for other types */
698 }
699
700 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200701
702exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200703 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200704}
705
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100706static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 psa_key_usage_t usage )
708{
709 psa_key_type_t type;
710 size_t bits;
711 uint8_t *exported = NULL;
712 size_t exported_size = 0;
713 size_t exported_length = 0;
714 int ok = 0;
715
Gilles Peskine8817f612018-12-18 00:18:46 +0100716 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200717
718 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
719 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200720 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100721 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
722 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723 return( 1 );
724 }
725
Gilles Peskined14664a2018-08-10 19:07:32 +0200726 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200727 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200728
Gilles Peskine8817f612018-12-18 00:18:46 +0100729 PSA_ASSERT( psa_export_key( handle,
730 exported, exported_size,
731 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200732 ok = exported_key_sanity_check( type, bits, exported, exported_length );
733
734exit:
735 mbedtls_free( exported );
736 return( ok );
737}
738
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100739static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200740{
741 psa_key_type_t type;
742 psa_key_type_t public_type;
743 size_t bits;
744 uint8_t *exported = NULL;
745 size_t exported_size = 0;
746 size_t exported_length = 0;
747 int ok = 0;
748
Gilles Peskine8817f612018-12-18 00:18:46 +0100749 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200750 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
751 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100752 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100753 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200754 return( 1 );
755 }
756
757 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
758 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200759 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200760
Gilles Peskine8817f612018-12-18 00:18:46 +0100761 PSA_ASSERT( psa_export_public_key( handle,
762 exported, exported_size,
763 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200764 ok = exported_key_sanity_check( public_type, bits,
765 exported, exported_length );
766
767exit:
768 mbedtls_free( exported );
769 return( ok );
770}
771
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100772static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 psa_key_usage_t usage,
774 psa_algorithm_t alg )
775{
776 int ok;
777 if( alg == 0 )
778 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
779 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200781 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100782 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100784 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200785 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100786 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200787 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100788 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200789 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200791 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100792 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200793 else
794 {
795 char message[40];
796 mbedtls_snprintf( message, sizeof( message ),
797 "No code to exercise alg=0x%08lx",
798 (unsigned long) alg );
799 test_fail( message, __LINE__, __FILE__ );
800 ok = 0;
801 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200802
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100803 ok = ok && exercise_export_key( handle, usage );
804 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200805
Gilles Peskine02b75072018-07-01 22:31:34 +0200806 return( ok );
807}
808
Gilles Peskine10df3412018-10-25 22:35:43 +0200809static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
810 psa_algorithm_t alg )
811{
812 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
813 {
814 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
815 PSA_KEY_USAGE_VERIFY :
816 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
817 }
818 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
819 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
820 {
821 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
822 PSA_KEY_USAGE_ENCRYPT :
823 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
824 }
825 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
826 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
827 {
828 return( PSA_KEY_USAGE_DERIVE );
829 }
830 else
831 {
832 return( 0 );
833 }
834
835}
Darryl Green0c6575a2018-11-07 16:05:30 +0000836
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100837/* An overapproximation of the amount of storage needed for a key of the
838 * given type and with the given content. The API doesn't make it easy
839 * to find a good value for the size. The current implementation doesn't
840 * care about the value anyway. */
841#define KEY_BITS_FROM_DATA( type, data ) \
842 ( data )->len
843
Darryl Green0c6575a2018-11-07 16:05:30 +0000844typedef enum {
845 IMPORT_KEY = 0,
846 GENERATE_KEY = 1,
847 DERIVE_KEY = 2
848} generate_method;
849
Gilles Peskinee59236f2018-01-27 23:32:46 +0100850/* END_HEADER */
851
852/* BEGIN_DEPENDENCIES
853 * depends_on:MBEDTLS_PSA_CRYPTO_C
854 * END_DEPENDENCIES
855 */
856
857/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200858void static_checks( )
859{
860 size_t max_truncated_mac_size =
861 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
862
863 /* Check that the length for a truncated MAC always fits in the algorithm
864 * encoding. The shifted mask is the maximum truncated value. The
865 * untruncated algorithm may be one byte larger. */
866 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
867}
868/* END_CASE */
869
870/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100873 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200874 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
Gilles Peskine8817f612018-12-18 00:18:46 +0100877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
880 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100881 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100882 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100884 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885
886exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 mbedtls_psa_crypto_free( );
888}
889/* END_CASE */
890
891/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100892void import_twice( int alg_arg, int usage_arg,
893 int type1_arg, data_t *data1,
894 int expected_import1_status_arg,
895 int type2_arg, data_t *data2,
896 int expected_import2_status_arg )
897{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100898 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100899 psa_algorithm_t alg = alg_arg;
900 psa_key_usage_t usage = usage_arg;
901 psa_key_type_t type1 = type1_arg;
902 psa_status_t expected_import1_status = expected_import1_status_arg;
903 psa_key_type_t type2 = type2_arg;
904 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000905 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100906 psa_status_t status;
907
Gilles Peskine8817f612018-12-18 00:18:46 +0100908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100909
Gilles Peskine8817f612018-12-18 00:18:46 +0100910 PSA_ASSERT( psa_allocate_key( type1,
911 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
912 KEY_BITS_FROM_DATA( type2, data2 ) ),
913 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100914 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100915 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100916
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100917 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100918 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100919 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100921
922 if( expected_import1_status == PSA_SUCCESS ||
923 expected_import2_status == PSA_SUCCESS )
924 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100926 }
927
928exit:
929 mbedtls_psa_crypto_free( );
930}
931/* END_CASE */
932
933/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200934void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
935{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100936 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200937 size_t bits = bits_arg;
938 psa_status_t expected_status = expected_status_arg;
939 psa_status_t status;
940 psa_key_type_t type =
941 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
942 size_t buffer_size = /* Slight overapproximations */
943 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200944 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200945 unsigned char *p;
946 int ret;
947 size_t length;
948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200950 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200951
952 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
953 bits, keypair ) ) >= 0 );
954 length = ret;
955
956 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100957 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100958 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200960 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100961 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200962
963exit:
964 mbedtls_free( buffer );
965 mbedtls_psa_crypto_free( );
966}
967/* END_CASE */
968
969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300970void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300971 int type_arg,
972 int alg_arg,
973 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 int expected_bits,
975 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200976 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 int canonical_input )
978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100979 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200981 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200982 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 unsigned char *exported = NULL;
985 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100987 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 size_t reexported_length;
989 psa_key_type_t got_type;
990 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000991 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
Moran Pekercb088e72018-07-17 17:36:59 +0300993 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200994 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200996 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998
Gilles Peskine8817f612018-12-18 00:18:46 +0100999 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001000 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001001 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001002
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001003 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1004 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001005
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001007 PSA_ASSERT( psa_import_key( handle, type,
1008 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009
1010 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001011 PSA_ASSERT( psa_get_key_information( handle,
1012 &got_type,
1013 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001014 TEST_EQUAL( got_type, type );
1015 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016
1017 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001018 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019 exported, export_size,
1020 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001022
1023 /* The exported length must be set by psa_export_key() to a value between 0
1024 * and export_size. On errors, the exported length must be 0. */
1025 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1026 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1027 TEST_ASSERT( exported_length <= export_size );
1028
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001029 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001030 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001032 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001035 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001038 goto exit;
1039
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001041 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 else
1043 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001045 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1046 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001047
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 PSA_ASSERT( psa_import_key( handle2, type,
1049 exported,
1050 exported_length ) );
1051 PSA_ASSERT( psa_export_key( handle2,
1052 reexported,
1053 export_size,
1054 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001055 ASSERT_COMPARE( exported, exported_length,
1056 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001057 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001059 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060
1061destroy:
1062 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001063 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001064 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1065 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066
1067exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001068 mbedtls_free( exported );
1069 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070 mbedtls_psa_crypto_free( );
1071}
1072/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001073
Moran Pekerf709f4a2018-06-06 17:26:04 +03001074/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001075void import_key_nonempty_slot( )
1076{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001077 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001078 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1079 psa_status_t status;
1080 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001081 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001082
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1084 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001085
Moran Peker28a38e62018-11-07 16:18:24 +02001086 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001087 PSA_ASSERT( psa_import_key( handle, type,
1088 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001089
1090 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001091 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001092 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001093
1094exit:
1095 mbedtls_psa_crypto_free( );
1096}
1097/* END_CASE */
1098
1099/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001101{
1102 psa_status_t status;
1103 unsigned char *exported = NULL;
1104 size_t export_size = 0;
1105 size_t exported_length = INVALID_EXPORT_LENGTH;
1106 psa_status_t expected_export_status = expected_export_status_arg;
1107
Gilles Peskine8817f612018-12-18 00:18:46 +01001108 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001109
1110 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001111 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001112 exported, export_size,
1113 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001114 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001115
1116exit:
1117 mbedtls_psa_crypto_free( );
1118}
1119/* END_CASE */
1120
1121/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001122void export_with_no_key_activity( )
1123{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001125 psa_algorithm_t alg = PSA_ALG_CTR;
1126 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001127 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001128 unsigned char *exported = NULL;
1129 size_t export_size = 0;
1130 size_t exported_length = INVALID_EXPORT_LENGTH;
1131
Gilles Peskine8817f612018-12-18 00:18:46 +01001132 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1135 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001136 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001137 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001138
1139 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001140 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001141 exported, export_size,
1142 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001143 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001144
1145exit:
1146 mbedtls_psa_crypto_free( );
1147}
1148/* END_CASE */
1149
1150/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001151void cipher_with_no_key_activity( )
1152{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001153 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001154 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001155 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001156 psa_cipher_operation_t operation;
1157 int exercise_alg = PSA_ALG_CTR;
1158
Gilles Peskine8817f612018-12-18 00:18:46 +01001159 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001160
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1162 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001163 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001165
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001166 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001167 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001168
1169exit:
1170 psa_cipher_abort( &operation );
1171 mbedtls_psa_crypto_free( );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001176void export_after_import_failure( data_t *data, int type_arg,
1177 int expected_import_status_arg )
1178{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001179 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001180 psa_key_type_t type = type_arg;
1181 psa_status_t status;
1182 unsigned char *exported = NULL;
1183 size_t export_size = 0;
1184 psa_status_t expected_import_status = expected_import_status_arg;
1185 size_t exported_length = INVALID_EXPORT_LENGTH;
1186
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001188
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1190 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001191
Moran Peker34550092018-11-07 16:19:34 +02001192 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001193 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001194 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001195 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001196
1197 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001198 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001199 exported, export_size,
1200 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001201 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001202
1203exit:
1204 mbedtls_psa_crypto_free( );
1205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001209void cipher_after_import_failure( data_t *data, int type_arg,
1210 int expected_import_status_arg )
1211{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001212 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001213 psa_cipher_operation_t operation;
1214 psa_key_type_t type = type_arg;
1215 psa_status_t status;
1216 psa_status_t expected_import_status = expected_import_status_arg;
1217 int exercise_alg = PSA_ALG_CTR;
1218
Gilles Peskine8817f612018-12-18 00:18:46 +01001219 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001220
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1222 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001223
Moran Pekerce500072018-11-07 16:20:07 +02001224 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001225 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001226 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001227 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001228
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001231
1232exit:
1233 psa_cipher_abort( &operation );
1234 mbedtls_psa_crypto_free( );
1235}
1236/* END_CASE */
1237
1238/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001239void export_after_destroy_key( data_t *data, int type_arg )
1240{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001241 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001242 psa_key_type_t type = type_arg;
1243 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001244 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001245 psa_algorithm_t alg = PSA_ALG_CTR;
1246 unsigned char *exported = NULL;
1247 size_t export_size = 0;
1248 size_t exported_length = INVALID_EXPORT_LENGTH;
1249
Gilles Peskine8817f612018-12-18 00:18:46 +01001250 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1253 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001254 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001256 export_size = (ptrdiff_t) data->len;
1257 ASSERT_ALLOC( exported, export_size );
1258
1259 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_import_key( handle, type,
1261 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001262
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1264 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001265
1266 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001267 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001268
1269 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001271 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001272 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001273
1274exit:
1275 mbedtls_free( exported );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001281void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001282 int type_arg,
1283 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001284 int export_size_delta,
1285 int expected_export_status_arg,
1286 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001288 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001289 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001290 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001291 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001293 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001294 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001295 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001296 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001297
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001299
Gilles Peskine8817f612018-12-18 00:18:46 +01001300 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1301 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001302 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001303 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001304
1305 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001306 PSA_ASSERT( psa_import_key( handle, type,
1307 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001308
Gilles Peskine49c25912018-10-29 15:15:31 +01001309 /* Export the public key */
1310 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001311 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001312 exported, export_size,
1313 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001314 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001315 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001316 {
1317 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1318 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001320 TEST_ASSERT( expected_public_key->len <=
1321 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001322 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1323 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001324 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001325
1326exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001327 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001328 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001329 mbedtls_psa_crypto_free( );
1330}
1331/* END_CASE */
1332
Gilles Peskine20035e32018-02-03 22:44:14 +01001333/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001334void import_and_exercise_key( data_t *data,
1335 int type_arg,
1336 int bits_arg,
1337 int alg_arg )
1338{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001339 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001340 psa_key_type_t type = type_arg;
1341 size_t bits = bits_arg;
1342 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001343 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001344 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001345 psa_key_type_t got_type;
1346 size_t got_bits;
1347 psa_status_t status;
1348
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001350
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1352 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001354 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001355
1356 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001358 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359
1360 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001361 PSA_ASSERT( psa_get_key_information( handle,
1362 &got_type,
1363 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001364 TEST_EQUAL( got_type, type );
1365 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001366
1367 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001368 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001369 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001370
1371exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001372 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001373 mbedtls_psa_crypto_free( );
1374}
1375/* END_CASE */
1376
1377/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001378void key_policy( int usage_arg, int alg_arg )
1379{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001380 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001381 psa_algorithm_t alg = alg_arg;
1382 psa_key_usage_t usage = usage_arg;
1383 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1384 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001385 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1386 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001387
1388 memset( key, 0x2a, sizeof( key ) );
1389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001391
Gilles Peskine8817f612018-12-18 00:18:46 +01001392 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1393 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001394 psa_key_policy_set_usage( &policy_set, usage, alg );
1395
Gilles Peskinefe11b722018-12-18 00:24:04 +01001396 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1397 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001398 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001399
Gilles Peskine8817f612018-12-18 00:18:46 +01001400 PSA_ASSERT( psa_import_key( handle, key_type,
1401 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001402
Gilles Peskine8817f612018-12-18 00:18:46 +01001403 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001404
Gilles Peskinefe11b722018-12-18 00:24:04 +01001405 TEST_EQUAL( policy_get.usage, policy_set.usage );
1406 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001407
1408exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001409 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001410 mbedtls_psa_crypto_free( );
1411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001415void key_policy_init( )
1416{
1417 /* Test each valid way of initializing the object, except for `= {0}`, as
1418 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1419 * though it's OK by the C standard. We could test for this, but we'd need
1420 * to supress the Clang warning for the test. */
1421 psa_key_policy_t func = psa_key_policy_init( );
1422 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1423 psa_key_policy_t zero;
1424
1425 memset( &zero, 0, sizeof( zero ) );
1426
1427 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1428 * specification, we test that all valid ways of initializing the object
1429 * have the same bit pattern. This is a stronger requirement that may not
1430 * be valid on all platforms or PSA Crypto implementations, but implies the
1431 * weaker actual requirement is met: that a freshly initialized object, no
1432 * matter how it was initialized, acts the same as any other valid
1433 * initialization. */
1434 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1435 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440void mac_key_policy( int policy_usage,
1441 int policy_alg,
1442 int key_type,
1443 data_t *key_data,
1444 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001447 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448 psa_mac_operation_t operation;
1449 psa_status_t status;
1450 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001451
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001453
Gilles Peskine8817f612018-12-18 00:18:46 +01001454 PSA_ASSERT( psa_allocate_key( key_type,
1455 KEY_BITS_FROM_DATA( key_type, key_data ),
1456 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001457 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001458 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001459
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_import_key( handle, key_type,
1461 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001463 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464 if( policy_alg == exercise_alg &&
1465 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001468 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001470
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001472 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473 if( policy_alg == exercise_alg &&
1474 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001477 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478
1479exit:
1480 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001481 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 mbedtls_psa_crypto_free( );
1483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
1487void cipher_key_policy( int policy_usage,
1488 int policy_alg,
1489 int key_type,
1490 data_t *key_data,
1491 int exercise_alg )
1492{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001493 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001494 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495 psa_cipher_operation_t operation;
1496 psa_status_t status;
1497
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499
Gilles Peskine8817f612018-12-18 00:18:46 +01001500 PSA_ASSERT( psa_allocate_key( key_type,
1501 KEY_BITS_FROM_DATA( key_type, key_data ),
1502 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001503 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001504 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001505
Gilles Peskine8817f612018-12-18 00:18:46 +01001506 PSA_ASSERT( psa_import_key( handle, key_type,
1507 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001508
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001509 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001510 if( policy_alg == exercise_alg &&
1511 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001512 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001514 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001515 psa_cipher_abort( &operation );
1516
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001517 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001518 if( policy_alg == exercise_alg &&
1519 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001520 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001521 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001522 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001523
1524exit:
1525 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001526 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001527 mbedtls_psa_crypto_free( );
1528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
1532void aead_key_policy( int policy_usage,
1533 int policy_alg,
1534 int key_type,
1535 data_t *key_data,
1536 int nonce_length_arg,
1537 int tag_length_arg,
1538 int exercise_alg )
1539{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001540 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001541 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542 psa_status_t status;
1543 unsigned char nonce[16] = {0};
1544 size_t nonce_length = nonce_length_arg;
1545 unsigned char tag[16];
1546 size_t tag_length = tag_length_arg;
1547 size_t output_length;
1548
1549 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1550 TEST_ASSERT( tag_length <= sizeof( tag ) );
1551
Gilles Peskine8817f612018-12-18 00:18:46 +01001552 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_allocate_key( key_type,
1555 KEY_BITS_FROM_DATA( key_type, key_data ),
1556 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001557 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_import_key( handle, key_type,
1561 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 nonce, nonce_length,
1565 NULL, 0,
1566 NULL, 0,
1567 tag, tag_length,
1568 &output_length );
1569 if( policy_alg == exercise_alg &&
1570 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001573 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
1575 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001576 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 nonce, nonce_length,
1578 NULL, 0,
1579 tag, tag_length,
1580 NULL, 0,
1581 &output_length );
1582 if( policy_alg == exercise_alg &&
1583 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001584 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001585 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001586 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001587
1588exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001590 mbedtls_psa_crypto_free( );
1591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
1595void asymmetric_encryption_key_policy( int policy_usage,
1596 int policy_alg,
1597 int key_type,
1598 data_t *key_data,
1599 int exercise_alg )
1600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001602 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 psa_status_t status;
1604 size_t key_bits;
1605 size_t buffer_length;
1606 unsigned char *buffer = NULL;
1607 size_t output_length;
1608
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( psa_allocate_key( key_type,
1612 KEY_BITS_FROM_DATA( key_type, key_data ),
1613 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616
Gilles Peskine8817f612018-12-18 00:18:46 +01001617 PSA_ASSERT( psa_import_key( handle, key_type,
1618 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_get_key_information( handle,
1621 NULL,
1622 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1624 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001625 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 NULL, 0,
1629 NULL, 0,
1630 buffer, buffer_length,
1631 &output_length );
1632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001638 if( buffer_length != 0 )
1639 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 buffer, buffer_length,
1642 NULL, 0,
1643 buffer, buffer_length,
1644 &output_length );
1645 if( policy_alg == exercise_alg &&
1646 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001647 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650
1651exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001652 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 mbedtls_psa_crypto_free( );
1654 mbedtls_free( buffer );
1655}
1656/* END_CASE */
1657
1658/* BEGIN_CASE */
1659void asymmetric_signature_key_policy( int policy_usage,
1660 int policy_alg,
1661 int key_type,
1662 data_t *key_data,
1663 int exercise_alg )
1664{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001665 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001666 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 psa_status_t status;
1668 unsigned char payload[16] = {1};
1669 size_t payload_length = sizeof( payload );
1670 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1671 size_t signature_length;
1672
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674
Gilles Peskine8817f612018-12-18 00:18:46 +01001675 PSA_ASSERT( psa_allocate_key( key_type,
1676 KEY_BITS_FROM_DATA( key_type, key_data ),
1677 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_import_key( handle, key_type,
1682 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 signature, sizeof( signature ),
1687 &signature_length );
1688 if( policy_alg == exercise_alg &&
1689 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693
1694 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001695 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001697 signature, sizeof( signature ) );
1698 if( policy_alg == exercise_alg &&
1699 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001700 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001701 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001703
1704exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001705 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001706 mbedtls_psa_crypto_free( );
1707}
1708/* END_CASE */
1709
1710/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001711void derive_key_policy( int policy_usage,
1712 int policy_alg,
1713 int key_type,
1714 data_t *key_data,
1715 int exercise_alg )
1716{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001717 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001718 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001719 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1720 psa_status_t status;
1721
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001723
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_allocate_key( key_type,
1725 KEY_BITS_FROM_DATA( key_type, key_data ),
1726 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001727 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001728 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001729
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 PSA_ASSERT( psa_import_key( handle, key_type,
1731 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001732
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001734 exercise_alg,
1735 NULL, 0,
1736 NULL, 0,
1737 1 );
1738 if( policy_alg == exercise_alg &&
1739 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001740 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001741 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001742 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001743
1744exit:
1745 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001746 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001747 mbedtls_psa_crypto_free( );
1748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001752void agreement_key_policy( int policy_usage,
1753 int policy_alg,
1754 int key_type_arg,
1755 data_t *key_data,
1756 int exercise_alg )
1757{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001758 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001759 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001760 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001761 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1762 psa_status_t status;
1763
Gilles Peskine8817f612018-12-18 00:18:46 +01001764 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765
Gilles Peskine8817f612018-12-18 00:18:46 +01001766 PSA_ASSERT( psa_allocate_key( key_type,
1767 KEY_BITS_FROM_DATA( key_type, key_data ),
1768 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001769 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001771
Gilles Peskine8817f612018-12-18 00:18:46 +01001772 PSA_ASSERT( psa_import_key( handle, key_type,
1773 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001774
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001775 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001776
Gilles Peskine01d718c2018-09-18 12:01:02 +02001777 if( policy_alg == exercise_alg &&
1778 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001781 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001782
1783exit:
1784 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001785 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001786 mbedtls_psa_crypto_free( );
1787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001791void hash_operation_init( )
1792{
1793 /* Test each valid way of initializing the object, except for `= {0}`, as
1794 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1795 * though it's OK by the C standard. We could test for this, but we'd need
1796 * to supress the Clang warning for the test. */
1797 psa_hash_operation_t func = psa_hash_operation_init( );
1798 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1799 psa_hash_operation_t zero;
1800
1801 memset( &zero, 0, sizeof( zero ) );
1802
1803 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1804 * specification, we test that all valid ways of initializing the object
1805 * have the same bit pattern. This is a stronger requirement that may not
1806 * be valid on all platforms or PSA Crypto implementations, but implies the
1807 * weaker actual requirement is met: that a freshly initialized object, no
1808 * matter how it was initialized, acts the same as any other valid
1809 * initialization. */
1810 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1811 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1812}
1813/* END_CASE */
1814
1815/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001816void hash_setup( int alg_arg,
1817 int expected_status_arg )
1818{
1819 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001820 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001821 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001822 psa_status_t status;
1823
Gilles Peskine8817f612018-12-18 00:18:46 +01001824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001825
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001826 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001827 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001828 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829
1830exit:
1831 mbedtls_psa_crypto_free( );
1832}
1833/* END_CASE */
1834
1835/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001836void hash_bad_order( )
1837{
1838 unsigned char input[] = "";
1839 /* SHA-256 hash of an empty string */
1840 unsigned char hash[] = {
1841 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1842 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1843 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1844 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001845 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001846
Gilles Peskine8817f612018-12-18 00:18:46 +01001847 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001848
1849 /* psa_hash_update without calling psa_hash_setup beforehand */
1850 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001851 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001852 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001853
1854 /* psa_hash_verify without calling psa_hash_setup beforehand */
1855 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001856 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001857 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001858
1859 /* psa_hash_finish without calling psa_hash_setup beforehand */
1860 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001861 TEST_EQUAL( psa_hash_finish( &operation,
1862 hash, sizeof( hash ), &hash_len ),
1863 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001864
1865exit:
1866 mbedtls_psa_crypto_free( );
1867}
1868/* END_CASE */
1869
itayzafrir27e69452018-11-01 14:26:34 +02001870/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1871void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001872{
1873 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001874 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1875 * appended to it */
1876 unsigned char hash[] = {
1877 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1878 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1879 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001880 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001881 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001882
Gilles Peskine8817f612018-12-18 00:18:46 +01001883 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001884
itayzafrir27e69452018-11-01 14:26:34 +02001885 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001886 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001887 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001888 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001889
itayzafrir27e69452018-11-01 14:26:34 +02001890 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001892 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001894
itayzafrir27e69452018-11-01 14:26:34 +02001895 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001896 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001897 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001898 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001899
itayzafrirec93d302018-10-18 18:01:10 +03001900exit:
1901 mbedtls_psa_crypto_free( );
1902}
1903/* END_CASE */
1904
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001905/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1906void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001907{
1908 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001909 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001910 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001911 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001912 size_t hash_len;
1913
Gilles Peskine8817f612018-12-18 00:18:46 +01001914 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001915
itayzafrir58028322018-10-25 10:22:01 +03001916 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001917 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001918 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001919 hash, expected_size - 1, &hash_len ),
1920 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001921
1922exit:
1923 mbedtls_psa_crypto_free( );
1924}
1925/* END_CASE */
1926
1927/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001928void mac_setup( int key_type_arg,
1929 data_t *key,
1930 int alg_arg,
1931 int expected_status_arg )
1932{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001933 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001934 psa_key_type_t key_type = key_type_arg;
1935 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001936 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001937 psa_mac_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00001938 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001939 psa_status_t status;
1940
Gilles Peskine8817f612018-12-18 00:18:46 +01001941 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001942
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1944 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001945 psa_key_policy_set_usage( &policy,
1946 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1947 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001948 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001949
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( psa_import_key( handle, key_type,
1951 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001952
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001953 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001954 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001955 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001956
1957exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001958 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001959 mbedtls_psa_crypto_free( );
1960}
1961/* END_CASE */
1962
1963/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001964void mac_sign( int key_type_arg,
1965 data_t *key,
1966 int alg_arg,
1967 data_t *input,
1968 data_t *expected_mac )
1969{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001970 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001971 psa_key_type_t key_type = key_type_arg;
1972 psa_algorithm_t alg = alg_arg;
1973 psa_mac_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00001974 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001975 /* Leave a little extra room in the output buffer. At the end of the
1976 * test, we'll check that the implementation didn't overwrite onto
1977 * this extra room. */
1978 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1979 size_t mac_buffer_size =
1980 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1981 size_t mac_length = 0;
1982
1983 memset( actual_mac, '+', sizeof( actual_mac ) );
1984 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1985 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1986
Gilles Peskine8817f612018-12-18 00:18:46 +01001987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001988
Gilles Peskine8817f612018-12-18 00:18:46 +01001989 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1990 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001991 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001992 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001993
Gilles Peskine8817f612018-12-18 00:18:46 +01001994 PSA_ASSERT( psa_import_key( handle, key_type,
1995 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001996
1997 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01001998 PSA_ASSERT( psa_mac_sign_setup( &operation,
1999 handle, alg ) );
2000 PSA_ASSERT( psa_mac_update( &operation,
2001 input->x, input->len ) );
2002 PSA_ASSERT( psa_mac_sign_finish( &operation,
2003 actual_mac, mac_buffer_size,
2004 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005
2006 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002007 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2008 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002009
2010 /* Verify that the end of the buffer is untouched. */
2011 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2012 sizeof( actual_mac ) - mac_length ) );
2013
2014exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002015 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016 mbedtls_psa_crypto_free( );
2017}
2018/* END_CASE */
2019
2020/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002021void mac_verify( int key_type_arg,
2022 data_t *key,
2023 int alg_arg,
2024 data_t *input,
2025 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002027 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028 psa_key_type_t key_type = key_type_arg;
2029 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030 psa_mac_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002031 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002032
Gilles Peskine69c12672018-06-28 00:07:19 +02002033 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2034
Gilles Peskine8817f612018-12-18 00:18:46 +01002035 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002036
Gilles Peskine8817f612018-12-18 00:18:46 +01002037 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2038 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002039 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002040 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002041
Gilles Peskine8817f612018-12-18 00:18:46 +01002042 PSA_ASSERT( psa_import_key( handle, key_type,
2043 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002044
Gilles Peskine8817f612018-12-18 00:18:46 +01002045 PSA_ASSERT( psa_mac_verify_setup( &operation,
2046 handle, alg ) );
2047 PSA_ASSERT( psa_destroy_key( handle ) );
2048 PSA_ASSERT( psa_mac_update( &operation,
2049 input->x, input->len ) );
2050 PSA_ASSERT( psa_mac_verify_finish( &operation,
2051 expected_mac->x,
2052 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053
2054exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002055 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002056 mbedtls_psa_crypto_free( );
2057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002061void cipher_setup( int key_type_arg,
2062 data_t *key,
2063 int alg_arg,
2064 int expected_status_arg )
2065{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002066 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002067 psa_key_type_t key_type = key_type_arg;
2068 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002069 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002070 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002071 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002072 psa_status_t status;
2073
Gilles Peskine8817f612018-12-18 00:18:46 +01002074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002075
Gilles Peskine8817f612018-12-18 00:18:46 +01002076 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2077 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002078 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002079 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002080
Gilles Peskine8817f612018-12-18 00:18:46 +01002081 PSA_ASSERT( psa_import_key( handle, key_type,
2082 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002083
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002084 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002085 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002086 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002087
2088exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002089 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002090 mbedtls_psa_crypto_free( );
2091}
2092/* END_CASE */
2093
2094/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002095void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002096 data_t *key,
2097 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002098 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002099{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002100 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002101 psa_status_t status;
2102 psa_key_type_t key_type = key_type_arg;
2103 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002104 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002105 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002106 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002107 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002108 size_t output_buffer_size = 0;
2109 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002110 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002111 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002112 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002114 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2115 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002116
Gilles Peskine8817f612018-12-18 00:18:46 +01002117 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002118
Gilles Peskine8817f612018-12-18 00:18:46 +01002119 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2120 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002121 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002122 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002123
Gilles Peskine8817f612018-12-18 00:18:46 +01002124 PSA_ASSERT( psa_import_key( handle, key_type,
2125 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002126
Gilles Peskine8817f612018-12-18 00:18:46 +01002127 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2128 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002129
Gilles Peskine8817f612018-12-18 00:18:46 +01002130 PSA_ASSERT( psa_cipher_set_iv( &operation,
2131 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002132 output_buffer_size = ( (size_t) input->len +
2133 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002134 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002135
Gilles Peskine8817f612018-12-18 00:18:46 +01002136 PSA_ASSERT( psa_cipher_update( &operation,
2137 input->x, input->len,
2138 output, output_buffer_size,
2139 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002140 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002141 status = psa_cipher_finish( &operation,
2142 output + function_output_length,
2143 output_buffer_size,
2144 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002145 total_output_length += function_output_length;
2146
Gilles Peskinefe11b722018-12-18 00:24:04 +01002147 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002148 if( expected_status == PSA_SUCCESS )
2149 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002150 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002151 ASSERT_COMPARE( expected_output->x, expected_output->len,
2152 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002154
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002156 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002157 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002158 mbedtls_psa_crypto_free( );
2159}
2160/* END_CASE */
2161
2162/* BEGIN_CASE */
2163void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002164 data_t *key,
2165 data_t *input,
2166 int first_part_size,
2167 data_t *expected_output )
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_key_type_t key_type = key_type_arg;
2171 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002172 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002173 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002174 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002175 size_t output_buffer_size = 0;
2176 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002177 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002178 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002179 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002181 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2182 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002183
Gilles Peskine8817f612018-12-18 00:18:46 +01002184 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2187 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002188 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002189 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002190
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( psa_import_key( handle, key_type,
2192 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002193
Gilles Peskine8817f612018-12-18 00:18:46 +01002194 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2195 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002196
Gilles Peskine8817f612018-12-18 00:18:46 +01002197 PSA_ASSERT( psa_cipher_set_iv( &operation,
2198 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002199 output_buffer_size = ( (size_t) input->len +
2200 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002201 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202
Gilles Peskine4abf7412018-06-18 16:35:34 +02002203 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002204 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2205 output, output_buffer_size,
2206 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002207 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002208 PSA_ASSERT( psa_cipher_update( &operation,
2209 input->x + first_part_size,
2210 input->len - first_part_size,
2211 output, output_buffer_size,
2212 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002213 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002214 PSA_ASSERT( psa_cipher_finish( &operation,
2215 output + function_output_length,
2216 output_buffer_size,
2217 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002218 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002219 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002220
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002221 ASSERT_COMPARE( expected_output->x, expected_output->len,
2222 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002223
2224exit:
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_decrypt_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
2240 psa_key_type_t key_type = key_type_arg;
2241 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002242 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002243 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002244 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245 size_t output_buffer_size = 0;
2246 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002247 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002248 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002249 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002250
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002251 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2252 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002253
Gilles Peskine8817f612018-12-18 00:18:46 +01002254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002255
Gilles Peskine8817f612018-12-18 00:18:46 +01002256 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2257 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002258 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002259 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002260
Gilles Peskine8817f612018-12-18 00:18:46 +01002261 PSA_ASSERT( psa_import_key( handle, key_type,
2262 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002263
Gilles Peskine8817f612018-12-18 00:18:46 +01002264 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2265 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002266
Gilles Peskine8817f612018-12-18 00:18:46 +01002267 PSA_ASSERT( psa_cipher_set_iv( &operation,
2268 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002269
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002270 output_buffer_size = ( (size_t) input->len +
2271 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002272 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002273
Gilles Peskine4abf7412018-06-18 16:35:34 +02002274 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( psa_cipher_update( &operation,
2276 input->x, first_part_size,
2277 output, output_buffer_size,
2278 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002279 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002280 PSA_ASSERT( psa_cipher_update( &operation,
2281 input->x + first_part_size,
2282 input->len - first_part_size,
2283 output, output_buffer_size,
2284 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002285 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_cipher_finish( &operation,
2287 output + function_output_length,
2288 output_buffer_size,
2289 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002290 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002291 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002292
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002293 ASSERT_COMPARE( expected_output->x, expected_output->len,
2294 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002295
2296exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002297 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002298 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002299 mbedtls_psa_crypto_free( );
2300}
2301/* END_CASE */
2302
Gilles Peskine50e586b2018-06-08 14:28:46 +02002303/* BEGIN_CASE */
2304void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002305 data_t *key,
2306 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002307 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002308{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002309 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002310 psa_status_t status;
2311 psa_key_type_t key_type = key_type_arg;
2312 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002313 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002314 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002315 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002316 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002317 size_t output_buffer_size = 0;
2318 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002319 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002320 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002321 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002322
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002323 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2324 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002325
Gilles Peskine8817f612018-12-18 00:18:46 +01002326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002327
Gilles Peskine8817f612018-12-18 00:18:46 +01002328 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2329 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002330 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002331 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002332
Gilles Peskine8817f612018-12-18 00:18:46 +01002333 PSA_ASSERT( psa_import_key( handle, key_type,
2334 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002335
Gilles Peskine8817f612018-12-18 00:18:46 +01002336 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2337 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338
Gilles Peskine8817f612018-12-18 00:18:46 +01002339 PSA_ASSERT( psa_cipher_set_iv( &operation,
2340 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002341
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002342 output_buffer_size = ( (size_t) input->len +
2343 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002344 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002345
Gilles Peskine8817f612018-12-18 00:18:46 +01002346 PSA_ASSERT( psa_cipher_update( &operation,
2347 input->x, input->len,
2348 output, output_buffer_size,
2349 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002350 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002351 status = psa_cipher_finish( &operation,
2352 output + function_output_length,
2353 output_buffer_size,
2354 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002355 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002356 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002357
2358 if( expected_status == PSA_SUCCESS )
2359 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002360 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002361 ASSERT_COMPARE( expected_output->x, expected_output->len,
2362 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002363 }
2364
Gilles Peskine50e586b2018-06-08 14:28:46 +02002365exit:
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_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002374 data_t *key,
2375 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002376{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002377 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002378 psa_key_type_t key_type = key_type_arg;
2379 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002380 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002381 size_t iv_size = 16;
2382 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002383 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002384 size_t output1_size = 0;
2385 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002386 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002387 size_t output2_size = 0;
2388 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002389 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002390 psa_cipher_operation_t operation1;
2391 psa_cipher_operation_t operation2;
Jaeden Amero70261c52019-01-04 11:47:20 +00002392 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002393
Gilles Peskine8817f612018-12-18 00:18:46 +01002394 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002395
Gilles Peskine8817f612018-12-18 00:18:46 +01002396 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2397 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002398 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002399 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002400
Gilles Peskine8817f612018-12-18 00:18:46 +01002401 PSA_ASSERT( psa_import_key( handle, key_type,
2402 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002403
Gilles Peskine8817f612018-12-18 00:18:46 +01002404 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2405 handle, alg ) );
2406 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2407 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002408
Gilles Peskine8817f612018-12-18 00:18:46 +01002409 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2410 iv, iv_size,
2411 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002412 output1_size = ( (size_t) input->len +
2413 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002414 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002415
Gilles Peskine8817f612018-12-18 00:18:46 +01002416 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2417 output1, output1_size,
2418 &output1_length ) );
2419 PSA_ASSERT( psa_cipher_finish( &operation1,
2420 output1 + output1_length, output1_size,
2421 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002422
Gilles Peskine048b7f02018-06-08 14:20:49 +02002423 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002424
Gilles Peskine8817f612018-12-18 00:18:46 +01002425 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002426
2427 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002428 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002429
Gilles Peskine8817f612018-12-18 00:18:46 +01002430 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2431 iv, iv_length ) );
2432 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2433 output2, output2_size,
2434 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002435 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002436 PSA_ASSERT( psa_cipher_finish( &operation2,
2437 output2 + output2_length,
2438 output2_size,
2439 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002440
Gilles Peskine048b7f02018-06-08 14:20:49 +02002441 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002442
Gilles Peskine8817f612018-12-18 00:18:46 +01002443 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002444
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002445 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002446
2447exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002448 mbedtls_free( output1 );
2449 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002450 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002451 mbedtls_psa_crypto_free( );
2452}
2453/* END_CASE */
2454
2455/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002456void cipher_verify_output_multipart( int alg_arg,
2457 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002458 data_t *key,
2459 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002460 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002461{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002462 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002463 psa_key_type_t key_type = key_type_arg;
2464 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002465 unsigned char iv[16] = {0};
2466 size_t iv_size = 16;
2467 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002468 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002469 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002470 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002471 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002472 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002473 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002474 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002475 psa_cipher_operation_t operation1;
2476 psa_cipher_operation_t operation2;
Jaeden Amero70261c52019-01-04 11:47:20 +00002477 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002478
Gilles Peskine8817f612018-12-18 00:18:46 +01002479 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002480
Gilles Peskine8817f612018-12-18 00:18:46 +01002481 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2482 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002483 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002484 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002485
Gilles Peskine8817f612018-12-18 00:18:46 +01002486 PSA_ASSERT( psa_import_key( handle, key_type,
2487 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002488
Gilles Peskine8817f612018-12-18 00:18:46 +01002489 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2490 handle, alg ) );
2491 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2492 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002493
Gilles Peskine8817f612018-12-18 00:18:46 +01002494 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2495 iv, iv_size,
2496 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002497 output1_buffer_size = ( (size_t) input->len +
2498 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002499 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002500
Gilles Peskine4abf7412018-06-18 16:35:34 +02002501 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002502
Gilles Peskine8817f612018-12-18 00:18:46 +01002503 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2504 output1, output1_buffer_size,
2505 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002506 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002507
Gilles Peskine8817f612018-12-18 00:18:46 +01002508 PSA_ASSERT( psa_cipher_update( &operation1,
2509 input->x + first_part_size,
2510 input->len - first_part_size,
2511 output1, output1_buffer_size,
2512 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002513 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002514
Gilles Peskine8817f612018-12-18 00:18:46 +01002515 PSA_ASSERT( psa_cipher_finish( &operation1,
2516 output1 + output1_length,
2517 output1_buffer_size - output1_length,
2518 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002519 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002520
Gilles Peskine8817f612018-12-18 00:18:46 +01002521 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002522
Gilles Peskine048b7f02018-06-08 14:20:49 +02002523 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002524 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002525
Gilles Peskine8817f612018-12-18 00:18:46 +01002526 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2527 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002528
Gilles Peskine8817f612018-12-18 00:18:46 +01002529 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2530 output2, output2_buffer_size,
2531 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002532 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002533
Gilles Peskine8817f612018-12-18 00:18:46 +01002534 PSA_ASSERT( psa_cipher_update( &operation2,
2535 output1 + first_part_size,
2536 output1_length - first_part_size,
2537 output2, output2_buffer_size,
2538 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002539 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002540
Gilles Peskine8817f612018-12-18 00:18:46 +01002541 PSA_ASSERT( psa_cipher_finish( &operation2,
2542 output2 + output2_length,
2543 output2_buffer_size - output2_length,
2544 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002545 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002546
Gilles Peskine8817f612018-12-18 00:18:46 +01002547 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002548
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002549 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002550
2551exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002552 mbedtls_free( output1 );
2553 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002554 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002555 mbedtls_psa_crypto_free( );
2556}
2557/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002558
Gilles Peskine20035e32018-02-03 22:44:14 +01002559/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002560void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002561 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002562 data_t *nonce,
2563 data_t *additional_data,
2564 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002565 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002566{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002567 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002568 psa_key_type_t key_type = key_type_arg;
2569 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002570 unsigned char *output_data = NULL;
2571 size_t output_size = 0;
2572 size_t output_length = 0;
2573 unsigned char *output_data2 = NULL;
2574 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002575 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002576 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002577 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002578
Gilles Peskine4abf7412018-06-18 16:35:34 +02002579 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002580 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002581
Gilles Peskine8817f612018-12-18 00:18:46 +01002582 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002583
Gilles Peskine8817f612018-12-18 00:18:46 +01002584 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2585 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002586 psa_key_policy_set_usage( &policy,
2587 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2588 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002589 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002590
Gilles Peskine8817f612018-12-18 00:18:46 +01002591 PSA_ASSERT( psa_import_key( handle, key_type,
2592 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002593
Gilles Peskinefe11b722018-12-18 00:24:04 +01002594 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2595 nonce->x, nonce->len,
2596 additional_data->x,
2597 additional_data->len,
2598 input_data->x, input_data->len,
2599 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002600 &output_length ),
2601 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002602
2603 if( PSA_SUCCESS == expected_result )
2604 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002605 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002606
Gilles Peskinefe11b722018-12-18 00:24:04 +01002607 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2608 nonce->x, nonce->len,
2609 additional_data->x,
2610 additional_data->len,
2611 output_data, output_length,
2612 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002613 &output_length2 ),
2614 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002615
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002616 ASSERT_COMPARE( input_data->x, input_data->len,
2617 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002618 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002619
Gilles Peskinea1cac842018-06-11 19:33:02 +02002620exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002621 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002622 mbedtls_free( output_data );
2623 mbedtls_free( output_data2 );
2624 mbedtls_psa_crypto_free( );
2625}
2626/* END_CASE */
2627
2628/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002629void aead_encrypt( int key_type_arg, data_t *key_data,
2630 int alg_arg,
2631 data_t *nonce,
2632 data_t *additional_data,
2633 data_t *input_data,
2634 data_t *expected_result )
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;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002642 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002643 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002644
Gilles Peskine4abf7412018-06-18 16:35:34 +02002645 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002646 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002647
Gilles Peskine8817f612018-12-18 00:18:46 +01002648 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002649
Gilles Peskine8817f612018-12-18 00:18:46 +01002650 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2651 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002652 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002654
Gilles Peskine8817f612018-12-18 00:18:46 +01002655 PSA_ASSERT( psa_import_key( handle, key_type,
2656 key_data->x,
2657 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002658
Gilles Peskine8817f612018-12-18 00:18:46 +01002659 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2660 nonce->x, nonce->len,
2661 additional_data->x, additional_data->len,
2662 input_data->x, input_data->len,
2663 output_data, output_size,
2664 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002665
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002666 ASSERT_COMPARE( expected_result->x, expected_result->len,
2667 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002668
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002670 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002671 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672 mbedtls_psa_crypto_free( );
2673}
2674/* END_CASE */
2675
2676/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002677void aead_decrypt( int key_type_arg, data_t *key_data,
2678 int alg_arg,
2679 data_t *nonce,
2680 data_t *additional_data,
2681 data_t *input_data,
2682 data_t *expected_data,
2683 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002684{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002685 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002686 psa_key_type_t key_type = key_type_arg;
2687 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002688 unsigned char *output_data = NULL;
2689 size_t output_size = 0;
2690 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002691 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002692 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002693 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002694
Gilles Peskine4abf7412018-06-18 16:35:34 +02002695 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002696 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002697
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002699
Gilles Peskine8817f612018-12-18 00:18:46 +01002700 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2701 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002702 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002704
Gilles Peskine8817f612018-12-18 00:18:46 +01002705 PSA_ASSERT( psa_import_key( handle, key_type,
2706 key_data->x,
2707 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002708
Gilles Peskinefe11b722018-12-18 00:24:04 +01002709 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2710 nonce->x, nonce->len,
2711 additional_data->x,
2712 additional_data->len,
2713 input_data->x, input_data->len,
2714 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002715 &output_length ),
2716 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002717
Gilles Peskine2d277862018-06-18 15:41:12 +02002718 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002719 ASSERT_COMPARE( expected_data->x, expected_data->len,
2720 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002721
Gilles Peskinea1cac842018-06-11 19:33:02 +02002722exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002723 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002724 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002725 mbedtls_psa_crypto_free( );
2726}
2727/* END_CASE */
2728
2729/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002730void signature_size( int type_arg,
2731 int bits,
2732 int alg_arg,
2733 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002734{
2735 psa_key_type_t type = type_arg;
2736 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002737 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002738 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002739exit:
2740 ;
2741}
2742/* END_CASE */
2743
2744/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002745void sign_deterministic( int key_type_arg, data_t *key_data,
2746 int alg_arg, data_t *input_data,
2747 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002748{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002749 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002750 psa_key_type_t key_type = key_type_arg;
2751 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002752 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002753 unsigned char *signature = NULL;
2754 size_t signature_size;
2755 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002756 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002757
Gilles Peskine8817f612018-12-18 00:18:46 +01002758 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002759
Gilles Peskine8817f612018-12-18 00:18:46 +01002760 PSA_ASSERT( psa_allocate_key( key_type,
2761 KEY_BITS_FROM_DATA( key_type, key_data ),
2762 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002763 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002764 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002765
Gilles Peskine8817f612018-12-18 00:18:46 +01002766 PSA_ASSERT( psa_import_key( handle, key_type,
2767 key_data->x,
2768 key_data->len ) );
2769 PSA_ASSERT( psa_get_key_information( handle,
2770 NULL,
2771 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002772
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002773 /* Allocate a buffer which has the size advertized by the
2774 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002775 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2776 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002777 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002778 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002779 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002780
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002781 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2783 input_data->x, input_data->len,
2784 signature, signature_size,
2785 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002786 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002787 ASSERT_COMPARE( output_data->x, output_data->len,
2788 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002789
2790exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002791 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002792 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002793 mbedtls_psa_crypto_free( );
2794}
2795/* END_CASE */
2796
2797/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002798void sign_fail( int key_type_arg, data_t *key_data,
2799 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002800 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002801{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002802 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002803 psa_key_type_t key_type = key_type_arg;
2804 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002805 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002806 psa_status_t actual_status;
2807 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002808 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002809 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002810 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002811
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002812 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002813
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002815
Gilles Peskine8817f612018-12-18 00:18:46 +01002816 PSA_ASSERT( psa_allocate_key( key_type,
2817 KEY_BITS_FROM_DATA( key_type, key_data ),
2818 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002819 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002820 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002821
Gilles Peskine8817f612018-12-18 00:18:46 +01002822 PSA_ASSERT( psa_import_key( handle, key_type,
2823 key_data->x,
2824 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002825
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002826 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002827 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002828 signature, signature_size,
2829 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002830 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002831 /* The value of *signature_length is unspecified on error, but
2832 * whatever it is, it should be less than signature_size, so that
2833 * if the caller tries to read *signature_length bytes without
2834 * checking the error code then they don't overflow a buffer. */
2835 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002836
2837exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002838 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002839 mbedtls_free( signature );
2840 mbedtls_psa_crypto_free( );
2841}
2842/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002843
2844/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002845void sign_verify( int key_type_arg, data_t *key_data,
2846 int alg_arg, data_t *input_data )
2847{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002848 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002849 psa_key_type_t key_type = key_type_arg;
2850 psa_algorithm_t alg = alg_arg;
2851 size_t key_bits;
2852 unsigned char *signature = NULL;
2853 size_t signature_size;
2854 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002855 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002856
Gilles Peskine8817f612018-12-18 00:18:46 +01002857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002858
Gilles Peskine8817f612018-12-18 00:18:46 +01002859 PSA_ASSERT( psa_allocate_key( key_type,
2860 KEY_BITS_FROM_DATA( key_type, key_data ),
2861 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002862 psa_key_policy_set_usage( &policy,
2863 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2864 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002866
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_import_key( handle, key_type,
2868 key_data->x,
2869 key_data->len ) );
2870 PSA_ASSERT( psa_get_key_information( handle,
2871 NULL,
2872 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002873
2874 /* Allocate a buffer which has the size advertized by the
2875 * library. */
2876 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2877 key_bits, alg );
2878 TEST_ASSERT( signature_size != 0 );
2879 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002880 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002881
2882 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2884 input_data->x, input_data->len,
2885 signature, signature_size,
2886 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002887 /* Check that the signature length looks sensible. */
2888 TEST_ASSERT( signature_length <= signature_size );
2889 TEST_ASSERT( signature_length > 0 );
2890
2891 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_asymmetric_verify(
2893 handle, alg,
2894 input_data->x, input_data->len,
2895 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002896
2897 if( input_data->len != 0 )
2898 {
2899 /* Flip a bit in the input and verify that the signature is now
2900 * detected as invalid. Flip a bit at the beginning, not at the end,
2901 * because ECDSA may ignore the last few bits of the input. */
2902 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002903 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2904 input_data->x, input_data->len,
2905 signature, signature_length ),
2906 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002907 }
2908
2909exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002910 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002911 mbedtls_free( signature );
2912 mbedtls_psa_crypto_free( );
2913}
2914/* END_CASE */
2915
2916/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002917void asymmetric_verify( int key_type_arg, data_t *key_data,
2918 int alg_arg, data_t *hash_data,
2919 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002920{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002921 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002922 psa_key_type_t key_type = key_type_arg;
2923 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002924 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002925
Gilles Peskine69c12672018-06-28 00:07:19 +02002926 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2927
Gilles Peskine8817f612018-12-18 00:18:46 +01002928 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002929
Gilles Peskine8817f612018-12-18 00:18:46 +01002930 PSA_ASSERT( psa_allocate_key( key_type,
2931 KEY_BITS_FROM_DATA( key_type, key_data ),
2932 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002933 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002934 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002935
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 PSA_ASSERT( psa_import_key( handle, key_type,
2937 key_data->x,
2938 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002939
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2941 hash_data->x, hash_data->len,
2942 signature_data->x,
2943 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002944exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002945 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002946 mbedtls_psa_crypto_free( );
2947}
2948/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002949
2950/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002951void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2952 int alg_arg, data_t *hash_data,
2953 data_t *signature_data,
2954 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002956 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002957 psa_key_type_t key_type = key_type_arg;
2958 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002959 psa_status_t actual_status;
2960 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002961 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962
Gilles Peskine8817f612018-12-18 00:18:46 +01002963 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964
Gilles Peskine8817f612018-12-18 00:18:46 +01002965 PSA_ASSERT( psa_allocate_key( key_type,
2966 KEY_BITS_FROM_DATA( key_type, key_data ),
2967 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002968 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002970
Gilles Peskine8817f612018-12-18 00:18:46 +01002971 PSA_ASSERT( psa_import_key( handle, key_type,
2972 key_data->x,
2973 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002974
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002975 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002976 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002977 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002978 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979
Gilles Peskinefe11b722018-12-18 00:24:04 +01002980 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002981
2982exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002983 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002984 mbedtls_psa_crypto_free( );
2985}
2986/* END_CASE */
2987
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002989void asymmetric_encrypt( int key_type_arg,
2990 data_t *key_data,
2991 int alg_arg,
2992 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002993 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002994 int expected_output_length_arg,
2995 int expected_status_arg )
2996{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002997 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02002998 psa_key_type_t key_type = key_type_arg;
2999 psa_algorithm_t alg = alg_arg;
3000 size_t expected_output_length = expected_output_length_arg;
3001 size_t key_bits;
3002 unsigned char *output = NULL;
3003 size_t output_size;
3004 size_t output_length = ~0;
3005 psa_status_t actual_status;
3006 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003007 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003008
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003010
Gilles Peskine656896e2018-06-29 19:12:28 +02003011 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003012 PSA_ASSERT( psa_allocate_key( key_type,
3013 KEY_BITS_FROM_DATA( key_type, key_data ),
3014 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003015 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003016 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3017 PSA_ASSERT( psa_import_key( handle, key_type,
3018 key_data->x,
3019 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003020
3021 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_get_key_information( handle,
3023 NULL,
3024 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003025 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003026 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003027
3028 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003029 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003030 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003031 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003032 output, output_size,
3033 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003034 TEST_EQUAL( actual_status, expected_status );
3035 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003036
Gilles Peskine68428122018-06-30 18:42:41 +02003037 /* If the label is empty, the test framework puts a non-null pointer
3038 * in label->x. Test that a null pointer works as well. */
3039 if( label->len == 0 )
3040 {
3041 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003042 if( output_size != 0 )
3043 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003044 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003045 input_data->x, input_data->len,
3046 NULL, label->len,
3047 output, output_size,
3048 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003049 TEST_EQUAL( actual_status, expected_status );
3050 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003051 }
3052
Gilles Peskine656896e2018-06-29 19:12:28 +02003053exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003054 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003055 mbedtls_free( output );
3056 mbedtls_psa_crypto_free( );
3057}
3058/* END_CASE */
3059
3060/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003061void asymmetric_encrypt_decrypt( int key_type_arg,
3062 data_t *key_data,
3063 int alg_arg,
3064 data_t *input_data,
3065 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003066{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003067 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068 psa_key_type_t key_type = key_type_arg;
3069 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003070 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003071 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003072 size_t output_size;
3073 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003074 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003075 size_t output2_size;
3076 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003077 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078
Gilles Peskine8817f612018-12-18 00:18:46 +01003079 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003080
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 Peskinec1bb6c82018-06-18 16:04:39 +02003084 psa_key_policy_set_usage( &policy,
3085 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003086 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_import_key( handle, key_type,
3090 key_data->x,
3091 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003092
3093 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 PSA_ASSERT( psa_get_key_information( handle,
3095 NULL,
3096 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003097 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003098 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003099 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003100 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003101
Gilles Peskineeebd7382018-06-08 18:11:54 +02003102 /* We test encryption by checking that encrypt-then-decrypt gives back
3103 * the original plaintext because of the non-optional random
3104 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003105 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3106 input_data->x, input_data->len,
3107 label->x, label->len,
3108 output, output_size,
3109 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003110 /* We don't know what ciphertext length to expect, but check that
3111 * it looks sensible. */
3112 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003113
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3115 output, output_length,
3116 label->x, label->len,
3117 output2, output2_size,
3118 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003119 ASSERT_COMPARE( input_data->x, input_data->len,
3120 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121
3122exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003123 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003124 mbedtls_free( output );
3125 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127}
3128/* END_CASE */
3129
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003130/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003131void asymmetric_decrypt( int key_type_arg,
3132 data_t *key_data,
3133 int alg_arg,
3134 data_t *input_data,
3135 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003136 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003138 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003139 psa_key_type_t key_type = key_type_arg;
3140 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003142 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003143 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003144 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003145
Gilles Peskine4abf7412018-06-18 16:35:34 +02003146 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003147 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003148
Gilles Peskine8817f612018-12-18 00:18:46 +01003149 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150
Gilles Peskine8817f612018-12-18 00:18:46 +01003151 PSA_ASSERT( psa_allocate_key( key_type,
3152 KEY_BITS_FROM_DATA( key_type, key_data ),
3153 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003154 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003156
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_import_key( handle, key_type,
3158 key_data->x,
3159 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003160
Gilles Peskine8817f612018-12-18 00:18:46 +01003161 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3162 input_data->x, input_data->len,
3163 label->x, label->len,
3164 output,
3165 output_size,
3166 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003167 ASSERT_COMPARE( expected_data->x, expected_data->len,
3168 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169
Gilles Peskine68428122018-06-30 18:42:41 +02003170 /* If the label is empty, the test framework puts a non-null pointer
3171 * in label->x. Test that a null pointer works as well. */
3172 if( label->len == 0 )
3173 {
3174 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003175 if( output_size != 0 )
3176 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003177 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3178 input_data->x, input_data->len,
3179 NULL, label->len,
3180 output,
3181 output_size,
3182 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003183 ASSERT_COMPARE( expected_data->x, expected_data->len,
3184 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003185 }
3186
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003188 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003189 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191}
3192/* END_CASE */
3193
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003195void asymmetric_decrypt_fail( int key_type_arg,
3196 data_t *key_data,
3197 int alg_arg,
3198 data_t *input_data,
3199 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003200 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003202 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203 psa_key_type_t key_type = key_type_arg;
3204 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003205 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003206 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003207 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208 psa_status_t actual_status;
3209 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003210 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
Gilles Peskine4abf7412018-06-18 16:35:34 +02003212 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003213 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_allocate_key( key_type,
3218 KEY_BITS_FROM_DATA( key_type, key_data ),
3219 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003220 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003221 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003222
Gilles Peskine8817f612018-12-18 00:18:46 +01003223 PSA_ASSERT( psa_import_key( handle, key_type,
3224 key_data->x,
3225 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003227 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003228 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003229 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003230 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003231 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003232 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003233 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234
Gilles Peskine68428122018-06-30 18:42:41 +02003235 /* If the label is empty, the test framework puts a non-null pointer
3236 * in label->x. Test that a null pointer works as well. */
3237 if( label->len == 0 )
3238 {
3239 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003240 if( output_size != 0 )
3241 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003242 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003243 input_data->x, input_data->len,
3244 NULL, label->len,
3245 output, output_size,
3246 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003247 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003248 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003249 }
3250
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003252 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003253 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003254 mbedtls_psa_crypto_free( );
3255}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003256/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003257
3258/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003259void derive_setup( int key_type_arg,
3260 data_t *key_data,
3261 int alg_arg,
3262 data_t *salt,
3263 data_t *label,
3264 int requested_capacity_arg,
3265 int expected_status_arg )
3266{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003267 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003268 size_t key_type = key_type_arg;
3269 psa_algorithm_t alg = alg_arg;
3270 size_t requested_capacity = requested_capacity_arg;
3271 psa_status_t expected_status = expected_status_arg;
3272 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003273 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003274
Gilles Peskine8817f612018-12-18 00:18:46 +01003275 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003276
Gilles Peskine8817f612018-12-18 00:18:46 +01003277 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3278 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003279 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_import_key( handle, key_type,
3283 key_data->x,
3284 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003285
Gilles Peskinefe11b722018-12-18 00:24:04 +01003286 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3287 salt->x, salt->len,
3288 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003289 requested_capacity ),
3290 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003291
3292exit:
3293 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003294 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003295 mbedtls_psa_crypto_free( );
3296}
3297/* END_CASE */
3298
3299/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003300void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003301{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003302 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003303 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003304 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003305 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003306 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003307 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003308 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3309 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3310 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003311 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003312
Gilles Peskine8817f612018-12-18 00:18:46 +01003313 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003314
Gilles Peskine8817f612018-12-18 00:18:46 +01003315 PSA_ASSERT( psa_allocate_key( key_type,
3316 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3317 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003318 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003319 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003320
Gilles Peskine8817f612018-12-18 00:18:46 +01003321 PSA_ASSERT( psa_import_key( handle, key_type,
3322 key_data,
3323 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003324
3325 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003326 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3327 NULL, 0,
3328 NULL, 0,
3329 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003330
3331 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003332 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3333 NULL, 0,
3334 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003335 capacity ),
3336 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003337
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003338 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003339
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003340 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3341 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003342
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003343exit:
3344 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003345 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003346 mbedtls_psa_crypto_free( );
3347}
3348/* END_CASE */
3349
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003350/* BEGIN_CASE */
3351void test_derive_invalid_generator_tests( )
3352{
3353 uint8_t output_buffer[16];
3354 size_t buffer_size = 16;
3355 size_t capacity = 0;
3356 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3357
Nir Sonnenschein50789302018-10-31 12:16:38 +02003358 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003359 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003360
3361 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003362 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003363
Gilles Peskine8817f612018-12-18 00:18:46 +01003364 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003365
Nir Sonnenschein50789302018-10-31 12:16:38 +02003366 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003367 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003368
Nir Sonnenschein50789302018-10-31 12:16:38 +02003369 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003370 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003371
3372exit:
3373 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003374}
3375/* END_CASE */
3376
3377/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003378void derive_output( int alg_arg,
3379 data_t *key_data,
3380 data_t *salt,
3381 data_t *label,
3382 int requested_capacity_arg,
3383 data_t *expected_output1,
3384 data_t *expected_output2 )
3385{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003386 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003387 psa_algorithm_t alg = alg_arg;
3388 size_t requested_capacity = requested_capacity_arg;
3389 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3390 uint8_t *expected_outputs[2] =
3391 {expected_output1->x, expected_output2->x};
3392 size_t output_sizes[2] =
3393 {expected_output1->len, expected_output2->len};
3394 size_t output_buffer_size = 0;
3395 uint8_t *output_buffer = NULL;
3396 size_t expected_capacity;
3397 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003398 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003399 psa_status_t status;
3400 unsigned i;
3401
3402 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3403 {
3404 if( output_sizes[i] > output_buffer_size )
3405 output_buffer_size = output_sizes[i];
3406 if( output_sizes[i] == 0 )
3407 expected_outputs[i] = NULL;
3408 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003409 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003411
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3413 PSA_BYTES_TO_BITS( key_data->len ),
3414 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003415 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003416 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003417
Gilles Peskine8817f612018-12-18 00:18:46 +01003418 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3419 key_data->x,
3420 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003421
3422 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003423 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3424 salt->x, salt->len,
3425 label->x, label->len,
3426 requested_capacity ) );
3427 PSA_ASSERT( psa_get_generator_capacity( &generator,
3428 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003429 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003430 expected_capacity = requested_capacity;
3431
3432 /* Expansion phase. */
3433 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3434 {
3435 /* Read some bytes. */
3436 status = psa_generator_read( &generator,
3437 output_buffer, output_sizes[i] );
3438 if( expected_capacity == 0 && output_sizes[i] == 0 )
3439 {
3440 /* Reading 0 bytes when 0 bytes are available can go either way. */
3441 TEST_ASSERT( status == PSA_SUCCESS ||
3442 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3443 continue;
3444 }
3445 else if( expected_capacity == 0 ||
3446 output_sizes[i] > expected_capacity )
3447 {
3448 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003449 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003450 expected_capacity = 0;
3451 continue;
3452 }
3453 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003454 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003455 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003456 ASSERT_COMPARE( output_buffer, output_sizes[i],
3457 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003458 /* Check the generator status. */
3459 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003460 PSA_ASSERT( psa_get_generator_capacity( &generator,
3461 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003462 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003463 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003464 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003465
3466exit:
3467 mbedtls_free( output_buffer );
3468 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003469 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003470 mbedtls_psa_crypto_free( );
3471}
3472/* END_CASE */
3473
3474/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003475void derive_full( int alg_arg,
3476 data_t *key_data,
3477 data_t *salt,
3478 data_t *label,
3479 int requested_capacity_arg )
3480{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003481 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003482 psa_algorithm_t alg = alg_arg;
3483 size_t requested_capacity = requested_capacity_arg;
3484 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3485 unsigned char output_buffer[16];
3486 size_t expected_capacity = requested_capacity;
3487 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003488 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003489
Gilles Peskine8817f612018-12-18 00:18:46 +01003490 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003491
Gilles Peskine8817f612018-12-18 00:18:46 +01003492 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3493 PSA_BYTES_TO_BITS( key_data->len ),
3494 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003495 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003496 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003497
Gilles Peskine8817f612018-12-18 00:18:46 +01003498 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3499 key_data->x,
3500 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003501
3502 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003503 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3504 salt->x, salt->len,
3505 label->x, label->len,
3506 requested_capacity ) );
3507 PSA_ASSERT( psa_get_generator_capacity( &generator,
3508 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003509 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003510
3511 /* Expansion phase. */
3512 while( current_capacity > 0 )
3513 {
3514 size_t read_size = sizeof( output_buffer );
3515 if( read_size > current_capacity )
3516 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 PSA_ASSERT( psa_generator_read( &generator,
3518 output_buffer,
3519 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003520 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003521 PSA_ASSERT( psa_get_generator_capacity( &generator,
3522 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003523 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003524 }
3525
3526 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003527 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3528 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003529
Gilles Peskine8817f612018-12-18 00:18:46 +01003530 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003531
3532exit:
3533 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003534 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003535 mbedtls_psa_crypto_free( );
3536}
3537/* END_CASE */
3538
3539/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003540void derive_key_exercise( int alg_arg,
3541 data_t *key_data,
3542 data_t *salt,
3543 data_t *label,
3544 int derived_type_arg,
3545 int derived_bits_arg,
3546 int derived_usage_arg,
3547 int derived_alg_arg )
3548{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003549 psa_key_handle_t base_handle = 0;
3550 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003551 psa_algorithm_t alg = alg_arg;
3552 psa_key_type_t derived_type = derived_type_arg;
3553 size_t derived_bits = derived_bits_arg;
3554 psa_key_usage_t derived_usage = derived_usage_arg;
3555 psa_algorithm_t derived_alg = derived_alg_arg;
3556 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3557 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003558 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003559 psa_key_type_t got_type;
3560 size_t got_bits;
3561
Gilles Peskine8817f612018-12-18 00:18:46 +01003562 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003563
Gilles Peskine8817f612018-12-18 00:18:46 +01003564 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3565 PSA_BYTES_TO_BITS( key_data->len ),
3566 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003567 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003568 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3569 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3570 key_data->x,
3571 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003572
3573 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3575 salt->x, salt->len,
3576 label->x, label->len,
3577 capacity ) );
3578 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3579 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003580 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3582 PSA_ASSERT( psa_generator_import_key( derived_handle,
3583 derived_type,
3584 derived_bits,
3585 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003586
3587 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_get_key_information( derived_handle,
3589 &got_type,
3590 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003591 TEST_EQUAL( got_type, derived_type );
3592 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003593
3594 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003595 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003596 goto exit;
3597
3598exit:
3599 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003600 psa_destroy_key( base_handle );
3601 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003602 mbedtls_psa_crypto_free( );
3603}
3604/* END_CASE */
3605
3606/* BEGIN_CASE */
3607void derive_key_export( int alg_arg,
3608 data_t *key_data,
3609 data_t *salt,
3610 data_t *label,
3611 int bytes1_arg,
3612 int bytes2_arg )
3613{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003614 psa_key_handle_t base_handle = 0;
3615 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003616 psa_algorithm_t alg = alg_arg;
3617 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003618 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003619 size_t bytes2 = bytes2_arg;
3620 size_t capacity = bytes1 + bytes2;
3621 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003622 uint8_t *output_buffer = NULL;
3623 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003624 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003625 size_t length;
3626
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003627 ASSERT_ALLOC( output_buffer, capacity );
3628 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003630
Gilles Peskine8817f612018-12-18 00:18:46 +01003631 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3632 PSA_BYTES_TO_BITS( key_data->len ),
3633 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003634 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003635 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3636 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3637 key_data->x,
3638 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003639
3640 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003641 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3642 salt->x, salt->len,
3643 label->x, label->len,
3644 capacity ) );
3645 PSA_ASSERT( psa_generator_read( &generator,
3646 output_buffer,
3647 capacity ) );
3648 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003649
3650 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003651 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3652 salt->x, salt->len,
3653 label->x, label->len,
3654 capacity ) );
3655 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3656 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003657 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003658 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3659 PSA_ASSERT( psa_generator_import_key( derived_handle,
3660 PSA_KEY_TYPE_RAW_DATA,
3661 derived_bits,
3662 &generator ) );
3663 PSA_ASSERT( psa_export_key( derived_handle,
3664 export_buffer, bytes1,
3665 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003666 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003667 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3668 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3669 PSA_BYTES_TO_BITS( bytes2 ),
3670 &derived_handle ) );
3671 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3672 PSA_ASSERT( psa_generator_import_key( derived_handle,
3673 PSA_KEY_TYPE_RAW_DATA,
3674 PSA_BYTES_TO_BITS( bytes2 ),
3675 &generator ) );
3676 PSA_ASSERT( psa_export_key( derived_handle,
3677 export_buffer + bytes1, bytes2,
3678 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003679 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003680
3681 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003682 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3683 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003684
3685exit:
3686 mbedtls_free( output_buffer );
3687 mbedtls_free( export_buffer );
3688 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003689 psa_destroy_key( base_handle );
3690 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003691 mbedtls_psa_crypto_free( );
3692}
3693/* END_CASE */
3694
3695/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003696void key_agreement_setup( int alg_arg,
3697 int our_key_type_arg, data_t *our_key_data,
3698 data_t *peer_key_data,
3699 int expected_status_arg )
3700{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003701 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003702 psa_algorithm_t alg = alg_arg;
3703 psa_key_type_t our_key_type = our_key_type_arg;
3704 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003705 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003706
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003708
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 PSA_ASSERT( psa_allocate_key( our_key_type,
3710 KEY_BITS_FROM_DATA( our_key_type,
3711 our_key_data ),
3712 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003713 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003714 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3715 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3716 our_key_data->x,
3717 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003718
Gilles Peskinefe11b722018-12-18 00:24:04 +01003719 TEST_EQUAL( psa_key_agreement( &generator,
3720 our_key,
3721 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003722 alg ),
3723 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003724
3725exit:
3726 psa_generator_abort( &generator );
3727 psa_destroy_key( our_key );
3728 mbedtls_psa_crypto_free( );
3729}
3730/* END_CASE */
3731
3732/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003733void key_agreement_capacity( int alg_arg,
3734 int our_key_type_arg, data_t *our_key_data,
3735 data_t *peer_key_data,
3736 int expected_capacity_arg )
3737{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003738 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003739 psa_algorithm_t alg = alg_arg;
3740 psa_key_type_t our_key_type = our_key_type_arg;
3741 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003742 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003743 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003744 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003745
Gilles Peskine8817f612018-12-18 00:18:46 +01003746 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003747
Gilles Peskine8817f612018-12-18 00:18:46 +01003748 PSA_ASSERT( psa_allocate_key( our_key_type,
3749 KEY_BITS_FROM_DATA( our_key_type,
3750 our_key_data ),
3751 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003752 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003753 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3754 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3755 our_key_data->x,
3756 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003757
Gilles Peskine8817f612018-12-18 00:18:46 +01003758 PSA_ASSERT( psa_key_agreement( &generator,
3759 our_key,
3760 peer_key_data->x, peer_key_data->len,
3761 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003762
Gilles Peskinebf491972018-10-25 22:36:12 +02003763 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003764 PSA_ASSERT( psa_get_generator_capacity(
3765 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003766 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003767
Gilles Peskinebf491972018-10-25 22:36:12 +02003768 /* Test the actual capacity by reading the output. */
3769 while( actual_capacity > sizeof( output ) )
3770 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003771 PSA_ASSERT( psa_generator_read( &generator,
3772 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003773 actual_capacity -= sizeof( output );
3774 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003775 PSA_ASSERT( psa_generator_read( &generator,
3776 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003777 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3778 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003779
Gilles Peskine59685592018-09-18 12:11:34 +02003780exit:
3781 psa_generator_abort( &generator );
3782 psa_destroy_key( our_key );
3783 mbedtls_psa_crypto_free( );
3784}
3785/* END_CASE */
3786
3787/* BEGIN_CASE */
3788void key_agreement_output( int alg_arg,
3789 int our_key_type_arg, data_t *our_key_data,
3790 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003791 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003792{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003793 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003794 psa_algorithm_t alg = alg_arg;
3795 psa_key_type_t our_key_type = our_key_type_arg;
3796 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003797 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003798 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003799
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003800 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3801 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003802
Gilles Peskine8817f612018-12-18 00:18:46 +01003803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003804
Gilles Peskine8817f612018-12-18 00:18:46 +01003805 PSA_ASSERT( psa_allocate_key( our_key_type,
3806 KEY_BITS_FROM_DATA( our_key_type,
3807 our_key_data ),
3808 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003809 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003810 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3811 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3812 our_key_data->x,
3813 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003814
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 PSA_ASSERT( psa_key_agreement( &generator,
3816 our_key,
3817 peer_key_data->x, peer_key_data->len,
3818 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003819
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003820 PSA_ASSERT( psa_generator_read( &generator,
3821 actual_output,
3822 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003823 ASSERT_COMPARE( actual_output, expected_output1->len,
3824 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003825 if( expected_output2->len != 0 )
3826 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003827 PSA_ASSERT( psa_generator_read( &generator,
3828 actual_output,
3829 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003830 ASSERT_COMPARE( actual_output, expected_output2->len,
3831 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003832 }
Gilles Peskine59685592018-09-18 12:11:34 +02003833
3834exit:
3835 psa_generator_abort( &generator );
3836 psa_destroy_key( our_key );
3837 mbedtls_psa_crypto_free( );
3838 mbedtls_free( actual_output );
3839}
3840/* END_CASE */
3841
3842/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003843void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003844{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003845 size_t bytes = bytes_arg;
3846 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003847 unsigned char *output = NULL;
3848 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003849 size_t i;
3850 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003851
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003852 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3853 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003854 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003855
Gilles Peskine8817f612018-12-18 00:18:46 +01003856 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003857
Gilles Peskinea50d7392018-06-21 10:22:13 +02003858 /* Run several times, to ensure that every output byte will be
3859 * nonzero at least once with overwhelming probability
3860 * (2^(-8*number_of_runs)). */
3861 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003862 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003863 if( bytes != 0 )
3864 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003865 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003866
3867 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003868 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3869 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003870
3871 for( i = 0; i < bytes; i++ )
3872 {
3873 if( output[i] != 0 )
3874 ++changed[i];
3875 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003876 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003877
3878 /* Check that every byte was changed to nonzero at least once. This
3879 * validates that psa_generate_random is overwriting every byte of
3880 * the output buffer. */
3881 for( i = 0; i < bytes; i++ )
3882 {
3883 TEST_ASSERT( changed[i] != 0 );
3884 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003885
3886exit:
3887 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003888 mbedtls_free( output );
3889 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003890}
3891/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003892
3893/* BEGIN_CASE */
3894void generate_key( int type_arg,
3895 int bits_arg,
3896 int usage_arg,
3897 int alg_arg,
3898 int expected_status_arg )
3899{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003900 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003901 psa_key_type_t type = type_arg;
3902 psa_key_usage_t usage = usage_arg;
3903 size_t bits = bits_arg;
3904 psa_algorithm_t alg = alg_arg;
3905 psa_status_t expected_status = expected_status_arg;
3906 psa_key_type_t got_type;
3907 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003908 psa_status_t expected_info_status =
3909 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003910 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003911
Gilles Peskine8817f612018-12-18 00:18:46 +01003912 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003913
Gilles Peskine8817f612018-12-18 00:18:46 +01003914 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003915 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003916 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003917
3918 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003919 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3920 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003921
3922 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003923 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3924 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003925 if( expected_info_status != PSA_SUCCESS )
3926 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003927 TEST_EQUAL( got_type, type );
3928 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003929
Gilles Peskine818ca122018-06-20 18:16:48 +02003930 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003931 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02003932 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003933
3934exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003935 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003936 mbedtls_psa_crypto_free( );
3937}
3938/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003939
Darryl Greend49a4992018-06-18 17:27:26 +01003940/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
3941void persistent_key_load_key_from_storage( data_t *data, int type_arg,
3942 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00003943 int alg_arg, int generation_method,
3944 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01003945{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003946 psa_key_handle_t handle = 0;
3947 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01003948 psa_key_type_t type = (psa_key_type_t) type_arg;
3949 psa_key_type_t type_get;
3950 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00003951 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
3952 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003953 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
3954 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003955 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00003956 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
3957 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003958 unsigned char *first_export = NULL;
3959 unsigned char *second_export = NULL;
3960 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
3961 size_t first_exported_length;
3962 size_t second_exported_length;
3963
3964 ASSERT_ALLOC( first_export, export_size );
3965 ASSERT_ALLOC( second_export, export_size );
3966
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01003968
Gilles Peskine8817f612018-12-18 00:18:46 +01003969 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
3970 type, bits,
3971 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003972 psa_key_policy_set_usage( &policy_set, policy_usage,
3973 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003974 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003975
Darryl Green0c6575a2018-11-07 16:05:30 +00003976 switch( generation_method )
3977 {
3978 case IMPORT_KEY:
3979 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003980 PSA_ASSERT( psa_import_key( handle, type,
3981 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003982 break;
Darryl Greend49a4992018-06-18 17:27:26 +01003983
Darryl Green0c6575a2018-11-07 16:05:30 +00003984 case GENERATE_KEY:
3985 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003986 PSA_ASSERT( psa_generate_key( handle, type, bits,
3987 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003988 break;
3989
3990 case DERIVE_KEY:
3991 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003992 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3993 PSA_BYTES_TO_BITS( data->len ),
3994 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003995 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
3996 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003997 PSA_ASSERT( psa_set_key_policy(
3998 base_key, &base_policy_set ) );
3999 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4000 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004001 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004002 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4003 base_policy_alg,
4004 NULL, 0, NULL, 0,
4005 export_size ) );
4006 PSA_ASSERT( psa_generator_import_key(
4007 handle, PSA_KEY_TYPE_RAW_DATA,
4008 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004009 break;
4010 }
Darryl Greend49a4992018-06-18 17:27:26 +01004011
4012 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004013 TEST_EQUAL( psa_export_key( handle,
4014 first_export, export_size,
4015 &first_exported_length ),
4016 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004017
4018 /* Shutdown and restart */
4019 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004020 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004021
Darryl Greend49a4992018-06-18 17:27:26 +01004022 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004023 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4024 &handle ) );
4025 PSA_ASSERT( psa_get_key_information(
4026 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004027 TEST_EQUAL( type_get, type );
4028 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004029
Gilles Peskine8817f612018-12-18 00:18:46 +01004030 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004031 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4032 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004033
4034 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004035 TEST_EQUAL( psa_export_key( handle,
4036 second_export, export_size,
4037 &second_exported_length ),
4038 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004039
Darryl Green0c6575a2018-11-07 16:05:30 +00004040 if( export_status == PSA_SUCCESS )
4041 {
4042 ASSERT_COMPARE( first_export, first_exported_length,
4043 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004044
Darryl Green0c6575a2018-11-07 16:05:30 +00004045 switch( generation_method )
4046 {
4047 case IMPORT_KEY:
4048 ASSERT_COMPARE( data->x, data->len,
4049 first_export, first_exported_length );
4050 break;
4051 default:
4052 break;
4053 }
4054 }
4055
4056 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004057 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004058 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004059
4060exit:
4061 mbedtls_free( first_export );
4062 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004064 mbedtls_psa_crypto_free();
4065}
4066/* END_CASE */