blob: ea1e545f404b7e676e214441cb7f28cb6bec3909 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinea7aa4422018-08-14 15:17:54 +020017/** Test if a buffer contains a constant byte value.
18 *
19 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020 *
21 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020028static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine0b352bc2018-06-28 00:16:11 +020039/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
40static int asn1_write_10x( unsigned char **p,
41 unsigned char *start,
42 size_t bits,
43 unsigned char x )
44{
45 int ret;
46 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020047 if( bits == 0 )
48 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
49 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020050 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030051 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
53 *p -= len;
54 ( *p )[len-1] = x;
55 if( bits % 8 == 0 )
56 ( *p )[1] |= 1;
57 else
58 ( *p )[0] |= 1 << ( bits % 8 );
59 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
60 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
61 MBEDTLS_ASN1_INTEGER ) );
62 return( len );
63}
64
65static int construct_fake_rsa_key( unsigned char *buffer,
66 size_t buffer_size,
67 unsigned char **p,
68 size_t bits,
69 int keypair )
70{
71 size_t half_bits = ( bits + 1 ) / 2;
72 int ret;
73 int len = 0;
74 /* Construct something that looks like a DER encoding of
75 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
76 * RSAPrivateKey ::= SEQUENCE {
77 * version Version,
78 * modulus INTEGER, -- n
79 * publicExponent INTEGER, -- e
80 * privateExponent INTEGER, -- d
81 * prime1 INTEGER, -- p
82 * prime2 INTEGER, -- q
83 * exponent1 INTEGER, -- d mod (p-1)
84 * exponent2 INTEGER, -- d mod (q-1)
85 * coefficient INTEGER, -- (inverse of q) mod p
86 * otherPrimeInfos OtherPrimeInfos OPTIONAL
87 * }
88 * Or, for a public key, the same structure with only
89 * version, modulus and publicExponent.
90 */
91 *p = buffer + buffer_size;
92 if( keypair )
93 {
94 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
95 asn1_write_10x( p, buffer, half_bits, 1 ) );
96 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
97 asn1_write_10x( p, buffer, half_bits, 1 ) );
98 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
99 asn1_write_10x( p, buffer, half_bits, 1 ) );
100 MBEDTLS_ASN1_CHK_ADD( len, /* q */
101 asn1_write_10x( p, buffer, half_bits, 1 ) );
102 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
103 asn1_write_10x( p, buffer, half_bits, 3 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* d */
105 asn1_write_10x( p, buffer, bits, 1 ) );
106 }
107 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
108 asn1_write_10x( p, buffer, 17, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* n */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 if( keypair )
112 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
113 mbedtls_asn1_write_int( p, buffer, 0 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
115 {
116 const unsigned char tag =
117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
118 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
119 }
120 return( len );
121}
122
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100123static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200124 psa_key_usage_t usage,
125 psa_algorithm_t alg )
126{
Jaeden Amero769ce272019-01-04 11:48:03 +0000127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200128 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200129 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200130 size_t mac_length = sizeof( mac );
131
132 if( usage & PSA_KEY_USAGE_SIGN )
133 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100134 PSA_ASSERT( psa_mac_sign_setup( &operation,
135 handle, alg ) );
136 PSA_ASSERT( psa_mac_update( &operation,
137 input, sizeof( input ) ) );
138 PSA_ASSERT( psa_mac_sign_finish( &operation,
139 mac, sizeof( mac ),
140 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 }
142
143 if( usage & PSA_KEY_USAGE_VERIFY )
144 {
145 psa_status_t verify_status =
146 ( usage & PSA_KEY_USAGE_SIGN ?
147 PSA_SUCCESS :
148 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100149 PSA_ASSERT( psa_mac_verify_setup( &operation,
150 handle, alg ) );
151 PSA_ASSERT( psa_mac_update( &operation,
152 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100153 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
154 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200155 }
156
157 return( 1 );
158
159exit:
160 psa_mac_abort( &operation );
161 return( 0 );
162}
163
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100164static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 psa_key_usage_t usage,
166 psa_algorithm_t alg )
167{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000168 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200169 unsigned char iv[16] = {0};
170 size_t iv_length = sizeof( iv );
171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
177 if( usage & PSA_KEY_USAGE_ENCRYPT )
178 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100179 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
180 handle, alg ) );
181 PSA_ASSERT( psa_cipher_generate_iv( &operation,
182 iv, sizeof( iv ),
183 &iv_length ) );
184 PSA_ASSERT( psa_cipher_update( &operation,
185 plaintext, sizeof( plaintext ),
186 ciphertext, sizeof( ciphertext ),
187 &ciphertext_length ) );
188 PSA_ASSERT( psa_cipher_finish( &operation,
189 ciphertext + ciphertext_length,
190 sizeof( ciphertext ) - ciphertext_length,
191 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 ciphertext_length += part_length;
193 }
194
195 if( usage & PSA_KEY_USAGE_DECRYPT )
196 {
197 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700198 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
200 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100202 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200203 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
204 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100205 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
206 handle, alg ) );
207 PSA_ASSERT( psa_cipher_set_iv( &operation,
208 iv, iv_length ) );
209 PSA_ASSERT( psa_cipher_update( &operation,
210 ciphertext, ciphertext_length,
211 decrypted, sizeof( decrypted ),
212 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200213 status = psa_cipher_finish( &operation,
214 decrypted + part_length,
215 sizeof( decrypted ) - part_length,
216 &part_length );
217 /* For a stream cipher, all inputs are valid. For a block cipher,
218 * if the input is some aribtrary data rather than an actual
219 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700220 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700221 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100222 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200223 else
224 TEST_ASSERT( status == PSA_SUCCESS ||
225 status == PSA_ERROR_INVALID_PADDING );
226 }
227
228 return( 1 );
229
230exit:
231 psa_cipher_abort( &operation );
232 return( 0 );
233}
234
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100235static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200236 psa_key_usage_t usage,
237 psa_algorithm_t alg )
238{
239 unsigned char nonce[16] = {0};
240 size_t nonce_length = sizeof( nonce );
241 unsigned char plaintext[16] = "Hello, world...";
242 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
243 size_t ciphertext_length = sizeof( ciphertext );
244 size_t plaintext_length = sizeof( ciphertext );
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100248 PSA_ASSERT( psa_aead_encrypt( handle, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100262 TEST_EQUAL( psa_aead_decrypt( handle, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100267 &plaintext_length ),
268 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100277static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200283 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 size_t signature_length = sizeof( signature );
285
286 if( usage & PSA_KEY_USAGE_SIGN )
287 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200288 /* Some algorithms require the payload to have the size of
289 * the hash encoded in the algorithm. Use this input size
290 * even for algorithms that allow other input sizes. */
291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
292 if( hash_alg != 0 )
293 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100294 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
295 payload, payload_length,
296 signature, sizeof( signature ),
297 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100306 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
307 payload, payload_length,
308 signature, signature_length ),
309 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200310 }
311
312 return( 1 );
313
314exit:
315 return( 0 );
316}
317
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100318static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200319 psa_key_usage_t usage,
320 psa_algorithm_t alg )
321{
322 unsigned char plaintext[256] = "Hello, world...";
323 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 size_t plaintext_length = 16;
326
327 if( usage & PSA_KEY_USAGE_ENCRYPT )
328 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100329 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100339 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100356static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361 unsigned char label[16] = "This is a label.";
362 size_t label_length = sizeof( label );
363 unsigned char seed[16] = "abcdefghijklmnop";
364 size_t seed_length = sizeof( seed );
365 unsigned char output[1];
366
367 if( usage & PSA_KEY_USAGE_DERIVE )
368 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100369 PSA_ASSERT( psa_key_derivation( &generator,
370 handle, alg,
371 label, label_length,
372 seed, seed_length,
373 sizeof( output ) ) );
374 PSA_ASSERT( psa_generator_read( &generator,
375 output,
376 sizeof( output ) ) );
377 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200378 }
379
380 return( 1 );
381
382exit:
383 return( 0 );
384}
385
Gilles Peskinec7998b72018-11-07 18:45:02 +0100386/* We need two keys to exercise key agreement. Exercise the
387 * private key against its own public key. */
388static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100390 psa_algorithm_t alg )
391{
392 psa_key_type_t private_key_type;
393 psa_key_type_t public_key_type;
394 size_t key_bits;
395 uint8_t *public_key = NULL;
396 size_t public_key_length;
397 /* Return UNKNOWN_ERROR if something other than the final call to
398 * psa_key_agreement fails. This isn't fully satisfactory, but it's
399 * good enough: callers will report it as a failed test anyway. */
400 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
401
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_get_key_information( handle,
403 &private_key_type,
404 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
406 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
407 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100408 PSA_ASSERT( psa_export_public_key( handle,
409 public_key, public_key_length,
410 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100412 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key, public_key_length,
414 alg );
415exit:
416 mbedtls_free( public_key );
417 return( status );
418}
419
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 psa_key_usage_t usage,
422 psa_algorithm_t alg )
423{
424 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200425 unsigned char output[1];
426 int ok = 0;
427
428 if( usage & PSA_KEY_USAGE_DERIVE )
429 {
430 /* We need two keys to exercise key agreement. Exercise the
431 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100432 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
433 PSA_ASSERT( psa_generator_read( &generator,
434 output,
435 sizeof( output ) ) );
436 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 }
438 ok = 1;
439
440exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200441 return( ok );
442}
443
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200444static int is_oid_of_key_type( psa_key_type_t type,
445 const uint8_t *oid, size_t oid_length )
446{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200447 const uint8_t *expected_oid = NULL;
448 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200449#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200450 if( PSA_KEY_TYPE_IS_RSA( type ) )
451 {
452 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
453 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
454 }
455 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200456#endif /* MBEDTLS_RSA_C */
457#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 if( PSA_KEY_TYPE_IS_ECC( type ) )
459 {
460 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
461 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
462 }
463 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200464#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465 {
466 char message[40];
467 mbedtls_snprintf( message, sizeof( message ),
468 "OID not known for key type=0x%08lx",
469 (unsigned long) type );
470 test_fail( message, __LINE__, __FILE__ );
471 return( 0 );
472 }
473
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200474 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
481static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
482 size_t min_bits, size_t max_bits,
483 int must_be_odd )
484{
485 size_t len;
486 size_t actual_bits;
487 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100488 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100489 MBEDTLS_ASN1_INTEGER ),
490 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 /* Tolerate a slight departure from DER encoding:
492 * - 0 may be represented by an empty string or a 1-byte string.
493 * - The sign bit may be used as a value bit. */
494 if( ( len == 1 && ( *p )[0] == 0 ) ||
495 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
496 {
497 ++( *p );
498 --len;
499 }
500 if( min_bits == 0 && len == 0 )
501 return( 1 );
502 msb = ( *p )[0];
503 TEST_ASSERT( msb != 0 );
504 actual_bits = 8 * ( len - 1 );
505 while( msb != 0 )
506 {
507 msb >>= 1;
508 ++actual_bits;
509 }
510 TEST_ASSERT( actual_bits >= min_bits );
511 TEST_ASSERT( actual_bits <= max_bits );
512 if( must_be_odd )
513 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
514 *p += len;
515 return( 1 );
516exit:
517 return( 0 );
518}
519
520static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
521 size_t *len,
522 unsigned char n, unsigned char tag )
523{
524 int ret;
525 ret = mbedtls_asn1_get_tag( p, end, len,
526 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
528 if( ret != 0 )
529 return( ret );
530 end = *p + *len;
531 ret = mbedtls_asn1_get_tag( p, end, len, tag );
532 if( ret != 0 )
533 return( ret );
534 if( *p + *len != end )
535 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
536 return( 0 );
537}
538
539static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
540 uint8_t *exported, size_t exported_length )
541{
542 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100543 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200544 else
545 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200546
547#if defined(MBEDTLS_DES_C)
548 if( type == PSA_KEY_TYPE_DES )
549 {
550 /* Check the parity bits. */
551 unsigned i;
552 for( i = 0; i < bits / 8; i++ )
553 {
554 unsigned bit_count = 0;
555 unsigned m;
556 for( m = 1; m <= 0x100; m <<= 1 )
557 {
558 if( exported[i] & m )
559 ++bit_count;
560 }
561 TEST_ASSERT( bit_count % 2 != 0 );
562 }
563 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200565#endif
566
567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
568 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
569 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 uint8_t *p = exported;
571 uint8_t *end = exported + exported_length;
572 size_t len;
573 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200574 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 * modulus INTEGER, -- n
576 * publicExponent INTEGER, -- e
577 * privateExponent INTEGER, -- d
578 * prime1 INTEGER, -- p
579 * prime2 INTEGER, -- q
580 * exponent1 INTEGER, -- d mod (p-1)
581 * exponent2 INTEGER, -- d mod (q-1)
582 * coefficient INTEGER, -- (inverse of q) mod p
583 * }
584 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100585 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
586 MBEDTLS_ASN1_SEQUENCE |
587 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
588 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
590 goto exit;
591 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
592 goto exit;
593 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
594 goto exit;
595 /* Require d to be at least half the size of n. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
597 goto exit;
598 /* Require p and q to be at most half the size of n, rounded up. */
599 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
606 goto exit;
607 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100609 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100610 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200611 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200612#endif /* MBEDTLS_RSA_C */
613
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
616 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100617 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_ECP_C */
622
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
624 {
625 uint8_t *p = exported;
626 uint8_t *end = exported + exported_length;
627 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200628#if defined(MBEDTLS_RSA_C)
629 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
630 {
631 /* RSAPublicKey ::= SEQUENCE {
632 * modulus INTEGER, -- n
633 * publicExponent INTEGER } -- e
634 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100635 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
636 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100637 MBEDTLS_ASN1_CONSTRUCTED ),
638 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100639 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200640 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
641 goto exit;
642 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
643 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100644 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200645 }
646 else
647#endif /* MBEDTLS_RSA_C */
648#if defined(MBEDTLS_ECP_C)
649 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
650 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000651 /* The representation of an ECC public key is:
652 * - The byte 0x04;
653 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
654 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
655 * - where m is the bit size associated with the curve.
Jaeden Amero25384a22019-01-10 10:23:21 +0000656 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100657 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
658 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200659 }
660 else
661#endif /* MBEDTLS_ECP_C */
662 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100663 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200664 mbedtls_snprintf( message, sizeof( message ),
665 "No sanity check for public key type=0x%08lx",
666 (unsigned long) type );
667 test_fail( message, __LINE__, __FILE__ );
668 return( 0 );
669 }
670 }
671 else
672
673 {
674 /* No sanity checks for other types */
675 }
676
677 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200678
679exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200680 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200681}
682
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100683static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200684 psa_key_usage_t usage )
685{
686 psa_key_type_t type;
687 size_t bits;
688 uint8_t *exported = NULL;
689 size_t exported_size = 0;
690 size_t exported_length = 0;
691 int ok = 0;
692
Gilles Peskine8817f612018-12-18 00:18:46 +0100693 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200694
695 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
696 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200697 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100698 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
699 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200700 return( 1 );
701 }
702
Gilles Peskined14664a2018-08-10 19:07:32 +0200703 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200704 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200705
Gilles Peskine8817f612018-12-18 00:18:46 +0100706 PSA_ASSERT( psa_export_key( handle,
707 exported, exported_size,
708 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200709 ok = exported_key_sanity_check( type, bits, exported, exported_length );
710
711exit:
712 mbedtls_free( exported );
713 return( ok );
714}
715
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100716static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200717{
718 psa_key_type_t type;
719 psa_key_type_t public_type;
720 size_t bits;
721 uint8_t *exported = NULL;
722 size_t exported_size = 0;
723 size_t exported_length = 0;
724 int ok = 0;
725
Gilles Peskine8817f612018-12-18 00:18:46 +0100726 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200727 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
728 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100729 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200731 return( 1 );
732 }
733
734 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
735 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200736 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200737
Gilles Peskine8817f612018-12-18 00:18:46 +0100738 PSA_ASSERT( psa_export_public_key( handle,
739 exported, exported_size,
740 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200741 ok = exported_key_sanity_check( public_type, bits,
742 exported, exported_length );
743
744exit:
745 mbedtls_free( exported );
746 return( ok );
747}
748
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100749static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200750 psa_key_usage_t usage,
751 psa_algorithm_t alg )
752{
753 int ok;
754 if( alg == 0 )
755 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
756 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100757 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200758 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100759 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200760 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100761 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200762 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100763 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200764 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100765 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200766 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100767 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200768 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100769 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200770 else
771 {
772 char message[40];
773 mbedtls_snprintf( message, sizeof( message ),
774 "No code to exercise alg=0x%08lx",
775 (unsigned long) alg );
776 test_fail( message, __LINE__, __FILE__ );
777 ok = 0;
778 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200779
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780 ok = ok && exercise_export_key( handle, usage );
781 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200782
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 return( ok );
784}
785
Gilles Peskine10df3412018-10-25 22:35:43 +0200786static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
787 psa_algorithm_t alg )
788{
789 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
790 {
791 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
792 PSA_KEY_USAGE_VERIFY :
793 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
794 }
795 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
796 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
797 {
798 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
799 PSA_KEY_USAGE_ENCRYPT :
800 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
801 }
802 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
803 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
804 {
805 return( PSA_KEY_USAGE_DERIVE );
806 }
807 else
808 {
809 return( 0 );
810 }
811
812}
Darryl Green0c6575a2018-11-07 16:05:30 +0000813
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100814/* An overapproximation of the amount of storage needed for a key of the
815 * given type and with the given content. The API doesn't make it easy
816 * to find a good value for the size. The current implementation doesn't
817 * care about the value anyway. */
818#define KEY_BITS_FROM_DATA( type, data ) \
819 ( data )->len
820
Darryl Green0c6575a2018-11-07 16:05:30 +0000821typedef enum {
822 IMPORT_KEY = 0,
823 GENERATE_KEY = 1,
824 DERIVE_KEY = 2
825} generate_method;
826
Gilles Peskinee59236f2018-01-27 23:32:46 +0100827/* END_HEADER */
828
829/* BEGIN_DEPENDENCIES
830 * depends_on:MBEDTLS_PSA_CRYPTO_C
831 * END_DEPENDENCIES
832 */
833
834/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200835void static_checks( )
836{
837 size_t max_truncated_mac_size =
838 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
839
840 /* Check that the length for a truncated MAC always fits in the algorithm
841 * encoding. The shifted mask is the maximum truncated value. The
842 * untruncated algorithm may be one byte larger. */
843 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
844}
845/* END_CASE */
846
847/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200848void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100849{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100850 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200851 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100852 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853
Gilles Peskine8817f612018-12-18 00:18:46 +0100854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855
Gilles Peskine8817f612018-12-18 00:18:46 +0100856 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
857 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100858 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100859 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100860 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100861 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100862
863exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864 mbedtls_psa_crypto_free( );
865}
866/* END_CASE */
867
868/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100869void import_twice( int alg_arg, int usage_arg,
870 int type1_arg, data_t *data1,
871 int expected_import1_status_arg,
872 int type2_arg, data_t *data2,
873 int expected_import2_status_arg )
874{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100875 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100876 psa_algorithm_t alg = alg_arg;
877 psa_key_usage_t usage = usage_arg;
878 psa_key_type_t type1 = type1_arg;
879 psa_status_t expected_import1_status = expected_import1_status_arg;
880 psa_key_type_t type2 = type2_arg;
881 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000882 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100883 psa_status_t status;
884
Gilles Peskine8817f612018-12-18 00:18:46 +0100885 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100886
Gilles Peskine8817f612018-12-18 00:18:46 +0100887 PSA_ASSERT( psa_allocate_key( type1,
888 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
889 KEY_BITS_FROM_DATA( type2, data2 ) ),
890 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100891 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100892 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100893
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100894 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100895 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100896 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100897 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100898
899 if( expected_import1_status == PSA_SUCCESS ||
900 expected_import2_status == PSA_SUCCESS )
901 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100902 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100903 }
904
905exit:
906 mbedtls_psa_crypto_free( );
907}
908/* END_CASE */
909
910/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200911void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
912{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100913 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200914 size_t bits = bits_arg;
915 psa_status_t expected_status = expected_status_arg;
916 psa_status_t status;
917 psa_key_type_t type =
918 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
919 size_t buffer_size = /* Slight overapproximations */
920 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200921 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200922 unsigned char *p;
923 int ret;
924 size_t length;
925
Gilles Peskine8817f612018-12-18 00:18:46 +0100926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200927 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200928
929 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
930 bits, keypair ) ) >= 0 );
931 length = ret;
932
933 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100934 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100935 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200937 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100938 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200939
940exit:
941 mbedtls_free( buffer );
942 mbedtls_psa_crypto_free( );
943}
944/* END_CASE */
945
946/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300947void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300948 int type_arg,
949 int alg_arg,
950 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951 int expected_bits,
952 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200953 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100954 int canonical_input )
955{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100956 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200958 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200959 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100960 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961 unsigned char *exported = NULL;
962 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100963 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100964 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965 size_t reexported_length;
966 psa_key_type_t got_type;
967 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000968 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969
Moran Pekercb088e72018-07-17 17:36:59 +0300970 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200971 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200973 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100974 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975
Gilles Peskine8817f612018-12-18 00:18:46 +0100976 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200977 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100978 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100979
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100980 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
981 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700982
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100984 PSA_ASSERT( psa_import_key( handle, type,
985 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986
987 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_get_key_information( handle,
989 &got_type,
990 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100991 TEST_EQUAL( got_type, type );
992 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993
994 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100995 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 exported, export_size,
997 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100998 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100999
1000 /* The exported length must be set by psa_export_key() to a value between 0
1001 * and export_size. On errors, the exported length must be 0. */
1002 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1003 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1004 TEST_ASSERT( exported_length <= export_size );
1005
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001006 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001007 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001009 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001010 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001012 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001014 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001015 goto exit;
1016
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001018 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019 else
1020 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001021 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001022 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1023 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001024
Gilles Peskine8817f612018-12-18 00:18:46 +01001025 PSA_ASSERT( psa_import_key( handle2, type,
1026 exported,
1027 exported_length ) );
1028 PSA_ASSERT( psa_export_key( handle2,
1029 reexported,
1030 export_size,
1031 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001032 ASSERT_COMPARE( exported, exported_length,
1033 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001034 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001036 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037
1038destroy:
1039 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001040 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001041 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1042 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043
1044exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001045 mbedtls_free( exported );
1046 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001047 mbedtls_psa_crypto_free( );
1048}
1049/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001050
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001052void import_key_nonempty_slot( )
1053{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001054 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001055 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1056 psa_status_t status;
1057 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001058 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001059
Gilles Peskine8817f612018-12-18 00:18:46 +01001060 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1061 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001062
Moran Peker28a38e62018-11-07 16:18:24 +02001063 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001064 PSA_ASSERT( psa_import_key( handle, type,
1065 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001066
1067 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001068 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001069 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001070
1071exit:
1072 mbedtls_psa_crypto_free( );
1073}
1074/* END_CASE */
1075
1076/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001077void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001078{
1079 psa_status_t status;
1080 unsigned char *exported = NULL;
1081 size_t export_size = 0;
1082 size_t exported_length = INVALID_EXPORT_LENGTH;
1083 psa_status_t expected_export_status = expected_export_status_arg;
1084
Gilles Peskine8817f612018-12-18 00:18:46 +01001085 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001086
1087 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001088 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001089 exported, export_size,
1090 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001091 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001092
1093exit:
1094 mbedtls_psa_crypto_free( );
1095}
1096/* END_CASE */
1097
1098/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001099void export_with_no_key_activity( )
1100{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001101 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001102 psa_algorithm_t alg = PSA_ALG_CTR;
1103 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001104 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001105 unsigned char *exported = NULL;
1106 size_t export_size = 0;
1107 size_t exported_length = INVALID_EXPORT_LENGTH;
1108
Gilles Peskine8817f612018-12-18 00:18:46 +01001109 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001110
Gilles Peskine8817f612018-12-18 00:18:46 +01001111 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1112 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001113 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001114 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001115
1116 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001117 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001118 exported, export_size,
1119 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001120 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001121
1122exit:
1123 mbedtls_psa_crypto_free( );
1124}
1125/* END_CASE */
1126
1127/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001128void cipher_with_no_key_activity( )
1129{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001130 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001131 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001132 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001133 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001134 int exercise_alg = PSA_ALG_CTR;
1135
Gilles Peskine8817f612018-12-18 00:18:46 +01001136 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001137
Gilles Peskine8817f612018-12-18 00:18:46 +01001138 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1139 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001140 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001141 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001142
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001143 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001144 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001145
1146exit:
1147 psa_cipher_abort( &operation );
1148 mbedtls_psa_crypto_free( );
1149}
1150/* END_CASE */
1151
1152/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001153void export_after_import_failure( data_t *data, int type_arg,
1154 int expected_import_status_arg )
1155{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001156 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001157 psa_key_type_t type = type_arg;
1158 psa_status_t status;
1159 unsigned char *exported = NULL;
1160 size_t export_size = 0;
1161 psa_status_t expected_import_status = expected_import_status_arg;
1162 size_t exported_length = INVALID_EXPORT_LENGTH;
1163
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001165
Gilles Peskine8817f612018-12-18 00:18:46 +01001166 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1167 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001168
Moran Peker34550092018-11-07 16:19:34 +02001169 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001170 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001171 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001172 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001173
1174 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001175 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001176 exported, export_size,
1177 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001178 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001179
1180exit:
1181 mbedtls_psa_crypto_free( );
1182}
1183/* END_CASE */
1184
1185/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001186void cipher_after_import_failure( data_t *data, int type_arg,
1187 int expected_import_status_arg )
1188{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001189 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001190 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001191 psa_key_type_t type = type_arg;
1192 psa_status_t status;
1193 psa_status_t expected_import_status = expected_import_status_arg;
1194 int exercise_alg = PSA_ALG_CTR;
1195
Gilles Peskine8817f612018-12-18 00:18:46 +01001196 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001197
Gilles Peskine8817f612018-12-18 00:18:46 +01001198 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1199 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001200
Moran Pekerce500072018-11-07 16:20:07 +02001201 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001202 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001203 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001204 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001205
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001206 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001207 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001208
1209exit:
1210 psa_cipher_abort( &operation );
1211 mbedtls_psa_crypto_free( );
1212}
1213/* END_CASE */
1214
1215/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001216void export_after_destroy_key( data_t *data, int type_arg )
1217{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001218 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001219 psa_key_type_t type = type_arg;
1220 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001221 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001222 psa_algorithm_t alg = PSA_ALG_CTR;
1223 unsigned char *exported = NULL;
1224 size_t export_size = 0;
1225 size_t exported_length = INVALID_EXPORT_LENGTH;
1226
Gilles Peskine8817f612018-12-18 00:18:46 +01001227 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001228
Gilles Peskine8817f612018-12-18 00:18:46 +01001229 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1230 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001231 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001232 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001233 export_size = (ptrdiff_t) data->len;
1234 ASSERT_ALLOC( exported, export_size );
1235
1236 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001237 PSA_ASSERT( psa_import_key( handle, type,
1238 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001239
Gilles Peskine8817f612018-12-18 00:18:46 +01001240 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1241 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001242
1243 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001244 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001245
1246 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001247 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001248 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001249 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001250
1251exit:
1252 mbedtls_free( exported );
1253 mbedtls_psa_crypto_free( );
1254}
1255/* END_CASE */
1256
1257/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001258void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001259 int type_arg,
1260 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001261 int export_size_delta,
1262 int expected_export_status_arg,
1263 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001264{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001265 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001266 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001267 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001268 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001269 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001270 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001271 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001272 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001273 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001274
Gilles Peskine8817f612018-12-18 00:18:46 +01001275 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001276
Gilles Peskine8817f612018-12-18 00:18:46 +01001277 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1278 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001279 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001280 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001281
1282 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001283 PSA_ASSERT( psa_import_key( handle, type,
1284 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001285
Gilles Peskine49c25912018-10-29 15:15:31 +01001286 /* Export the public key */
1287 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001288 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001289 exported, export_size,
1290 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001291 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001292 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001293 {
1294 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1295 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001296 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001297 TEST_ASSERT( expected_public_key->len <=
1298 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001299 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1300 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001301 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001302
1303exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001304 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001305 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001306 mbedtls_psa_crypto_free( );
1307}
1308/* END_CASE */
1309
Gilles Peskine20035e32018-02-03 22:44:14 +01001310/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001311void import_and_exercise_key( data_t *data,
1312 int type_arg,
1313 int bits_arg,
1314 int alg_arg )
1315{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001316 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001317 psa_key_type_t type = type_arg;
1318 size_t bits = bits_arg;
1319 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001320 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001321 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001322 psa_key_type_t got_type;
1323 size_t got_bits;
1324 psa_status_t status;
1325
Gilles Peskine8817f612018-12-18 00:18:46 +01001326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001327
Gilles Peskine8817f612018-12-18 00:18:46 +01001328 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1329 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001330 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001332
1333 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001334 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001335 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001336
1337 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( psa_get_key_information( handle,
1339 &got_type,
1340 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001341 TEST_EQUAL( got_type, type );
1342 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001343
1344 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001345 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001346 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001347
1348exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001349 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001350 mbedtls_psa_crypto_free( );
1351}
1352/* END_CASE */
1353
1354/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001355void key_policy( int usage_arg, int alg_arg )
1356{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001358 psa_algorithm_t alg = alg_arg;
1359 psa_key_usage_t usage = usage_arg;
1360 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1361 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001362 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1363 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001364
1365 memset( key, 0x2a, sizeof( key ) );
1366
Gilles Peskine8817f612018-12-18 00:18:46 +01001367 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001368
Gilles Peskine8817f612018-12-18 00:18:46 +01001369 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1370 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001371 psa_key_policy_set_usage( &policy_set, usage, alg );
1372
Gilles Peskinefe11b722018-12-18 00:24:04 +01001373 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1374 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001375 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001376
Gilles Peskine8817f612018-12-18 00:18:46 +01001377 PSA_ASSERT( psa_import_key( handle, key_type,
1378 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001379
Gilles Peskine8817f612018-12-18 00:18:46 +01001380 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001381
Gilles Peskinefe11b722018-12-18 00:24:04 +01001382 TEST_EQUAL( policy_get.usage, policy_set.usage );
1383 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001384
1385exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001386 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001387 mbedtls_psa_crypto_free( );
1388}
1389/* END_CASE */
1390
1391/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001392void key_policy_init( )
1393{
1394 /* Test each valid way of initializing the object, except for `= {0}`, as
1395 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1396 * though it's OK by the C standard. We could test for this, but we'd need
1397 * to supress the Clang warning for the test. */
1398 psa_key_policy_t func = psa_key_policy_init( );
1399 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1400 psa_key_policy_t zero;
1401
1402 memset( &zero, 0, sizeof( zero ) );
1403
1404 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1405 * specification, we test that all valid ways of initializing the object
1406 * have the same bit pattern. This is a stronger requirement that may not
1407 * be valid on all platforms or PSA Crypto implementations, but implies the
1408 * weaker actual requirement is met: that a freshly initialized object, no
1409 * matter how it was initialized, acts the same as any other valid
1410 * initialization. */
1411 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1412 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1413}
1414/* END_CASE */
1415
1416/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001417void mac_key_policy( int policy_usage,
1418 int policy_alg,
1419 int key_type,
1420 data_t *key_data,
1421 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001422{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001423 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001424 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001425 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001426 psa_status_t status;
1427 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001428
Gilles Peskine8817f612018-12-18 00:18:46 +01001429 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001430
Gilles Peskine8817f612018-12-18 00:18:46 +01001431 PSA_ASSERT( psa_allocate_key( key_type,
1432 KEY_BITS_FROM_DATA( key_type, key_data ),
1433 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001434 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001435 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001436
Gilles Peskine8817f612018-12-18 00:18:46 +01001437 PSA_ASSERT( psa_import_key( handle, key_type,
1438 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001439
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001440 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001441 if( policy_alg == exercise_alg &&
1442 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001443 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001444 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001445 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001446 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001447
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001449 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001450 if( policy_alg == exercise_alg &&
1451 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001453 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001454 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001455
1456exit:
1457 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001458 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001459 mbedtls_psa_crypto_free( );
1460}
1461/* END_CASE */
1462
1463/* BEGIN_CASE */
1464void cipher_key_policy( int policy_usage,
1465 int policy_alg,
1466 int key_type,
1467 data_t *key_data,
1468 int exercise_alg )
1469{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001470 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001471 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001472 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473 psa_status_t status;
1474
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476
Gilles Peskine8817f612018-12-18 00:18:46 +01001477 PSA_ASSERT( psa_allocate_key( key_type,
1478 KEY_BITS_FROM_DATA( key_type, key_data ),
1479 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001480 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001481 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482
Gilles Peskine8817f612018-12-18 00:18:46 +01001483 PSA_ASSERT( psa_import_key( handle, key_type,
1484 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001485
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001486 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001487 if( policy_alg == exercise_alg &&
1488 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001489 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001490 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001491 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001492 psa_cipher_abort( &operation );
1493
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001494 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495 if( policy_alg == exercise_alg &&
1496 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001497 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001498 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001499 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001500
1501exit:
1502 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001503 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001504 mbedtls_psa_crypto_free( );
1505}
1506/* END_CASE */
1507
1508/* BEGIN_CASE */
1509void aead_key_policy( int policy_usage,
1510 int policy_alg,
1511 int key_type,
1512 data_t *key_data,
1513 int nonce_length_arg,
1514 int tag_length_arg,
1515 int exercise_alg )
1516{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001517 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001518 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001519 psa_status_t status;
1520 unsigned char nonce[16] = {0};
1521 size_t nonce_length = nonce_length_arg;
1522 unsigned char tag[16];
1523 size_t tag_length = tag_length_arg;
1524 size_t output_length;
1525
1526 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1527 TEST_ASSERT( tag_length <= sizeof( tag ) );
1528
Gilles Peskine8817f612018-12-18 00:18:46 +01001529 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001530
Gilles Peskine8817f612018-12-18 00:18:46 +01001531 PSA_ASSERT( psa_allocate_key( key_type,
1532 KEY_BITS_FROM_DATA( key_type, key_data ),
1533 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001534 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001535 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001536
Gilles Peskine8817f612018-12-18 00:18:46 +01001537 PSA_ASSERT( psa_import_key( handle, key_type,
1538 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001539
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001540 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001541 nonce, nonce_length,
1542 NULL, 0,
1543 NULL, 0,
1544 tag, tag_length,
1545 &output_length );
1546 if( policy_alg == exercise_alg &&
1547 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001548 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001549 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001550 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001551
1552 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001553 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001554 nonce, nonce_length,
1555 NULL, 0,
1556 tag, tag_length,
1557 NULL, 0,
1558 &output_length );
1559 if( policy_alg == exercise_alg &&
1560 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001561 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001562 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001563 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564
1565exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001566 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001567 mbedtls_psa_crypto_free( );
1568}
1569/* END_CASE */
1570
1571/* BEGIN_CASE */
1572void asymmetric_encryption_key_policy( int policy_usage,
1573 int policy_alg,
1574 int key_type,
1575 data_t *key_data,
1576 int exercise_alg )
1577{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001578 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001579 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001580 psa_status_t status;
1581 size_t key_bits;
1582 size_t buffer_length;
1583 unsigned char *buffer = NULL;
1584 size_t output_length;
1585
Gilles Peskine8817f612018-12-18 00:18:46 +01001586 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001587
Gilles Peskine8817f612018-12-18 00:18:46 +01001588 PSA_ASSERT( psa_allocate_key( key_type,
1589 KEY_BITS_FROM_DATA( key_type, key_data ),
1590 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001591 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001592 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001593
Gilles Peskine8817f612018-12-18 00:18:46 +01001594 PSA_ASSERT( psa_import_key( handle, key_type,
1595 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001596
Gilles Peskine8817f612018-12-18 00:18:46 +01001597 PSA_ASSERT( psa_get_key_information( handle,
1598 NULL,
1599 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001600 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1601 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001602 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001604 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001605 NULL, 0,
1606 NULL, 0,
1607 buffer, buffer_length,
1608 &output_length );
1609 if( policy_alg == exercise_alg &&
1610 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001612 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001613 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001615 if( buffer_length != 0 )
1616 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001617 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001618 buffer, buffer_length,
1619 NULL, 0,
1620 buffer, buffer_length,
1621 &output_length );
1622 if( policy_alg == exercise_alg &&
1623 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001624 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001626 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627
1628exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001629 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630 mbedtls_psa_crypto_free( );
1631 mbedtls_free( buffer );
1632}
1633/* END_CASE */
1634
1635/* BEGIN_CASE */
1636void asymmetric_signature_key_policy( int policy_usage,
1637 int policy_alg,
1638 int key_type,
1639 data_t *key_data,
1640 int exercise_alg )
1641{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001642 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001643 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001644 psa_status_t status;
1645 unsigned char payload[16] = {1};
1646 size_t payload_length = sizeof( payload );
1647 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1648 size_t signature_length;
1649
Gilles Peskine8817f612018-12-18 00:18:46 +01001650 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001651
Gilles Peskine8817f612018-12-18 00:18:46 +01001652 PSA_ASSERT( psa_allocate_key( key_type,
1653 KEY_BITS_FROM_DATA( key_type, key_data ),
1654 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001656 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001657
Gilles Peskine8817f612018-12-18 00:18:46 +01001658 PSA_ASSERT( psa_import_key( handle, key_type,
1659 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001661 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001663 signature, sizeof( signature ),
1664 &signature_length );
1665 if( policy_alg == exercise_alg &&
1666 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001667 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670
1671 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001672 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001673 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674 signature, sizeof( signature ) );
1675 if( policy_alg == exercise_alg &&
1676 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001677 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001679 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001680
1681exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001682 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001683 mbedtls_psa_crypto_free( );
1684}
1685/* END_CASE */
1686
1687/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001688void derive_key_policy( int policy_usage,
1689 int policy_alg,
1690 int key_type,
1691 data_t *key_data,
1692 int exercise_alg )
1693{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001694 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001695 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001696 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1697 psa_status_t status;
1698
Gilles Peskine8817f612018-12-18 00:18:46 +01001699 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001700
Gilles Peskine8817f612018-12-18 00:18:46 +01001701 PSA_ASSERT( psa_allocate_key( key_type,
1702 KEY_BITS_FROM_DATA( key_type, key_data ),
1703 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001704 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001706
Gilles Peskine8817f612018-12-18 00:18:46 +01001707 PSA_ASSERT( psa_import_key( handle, key_type,
1708 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001709
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001710 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001711 exercise_alg,
1712 NULL, 0,
1713 NULL, 0,
1714 1 );
1715 if( policy_alg == exercise_alg &&
1716 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001717 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001718 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001719 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001720
1721exit:
1722 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001723 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001724 mbedtls_psa_crypto_free( );
1725}
1726/* END_CASE */
1727
1728/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001729void agreement_key_policy( int policy_usage,
1730 int policy_alg,
1731 int key_type_arg,
1732 data_t *key_data,
1733 int exercise_alg )
1734{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001735 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001736 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001737 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001738 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1739 psa_status_t status;
1740
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001742
Gilles Peskine8817f612018-12-18 00:18:46 +01001743 PSA_ASSERT( psa_allocate_key( key_type,
1744 KEY_BITS_FROM_DATA( key_type, key_data ),
1745 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001746 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001747 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001748
Gilles Peskine8817f612018-12-18 00:18:46 +01001749 PSA_ASSERT( psa_import_key( handle, key_type,
1750 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001751
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001752 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001753
Gilles Peskine01d718c2018-09-18 12:01:02 +02001754 if( policy_alg == exercise_alg &&
1755 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001756 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001757 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001758 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001759
1760exit:
1761 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001762 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001763 mbedtls_psa_crypto_free( );
1764}
1765/* END_CASE */
1766
1767/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001768void hash_operation_init( )
1769{
1770 /* Test each valid way of initializing the object, except for `= {0}`, as
1771 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1772 * though it's OK by the C standard. We could test for this, but we'd need
1773 * to supress the Clang warning for the test. */
1774 psa_hash_operation_t func = psa_hash_operation_init( );
1775 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1776 psa_hash_operation_t zero;
1777
1778 memset( &zero, 0, sizeof( zero ) );
1779
1780 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1781 * specification, we test that all valid ways of initializing the object
1782 * have the same bit pattern. This is a stronger requirement that may not
1783 * be valid on all platforms or PSA Crypto implementations, but implies the
1784 * weaker actual requirement is met: that a freshly initialized object, no
1785 * matter how it was initialized, acts the same as any other valid
1786 * initialization. */
1787 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1788 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001793void hash_setup( int alg_arg,
1794 int expected_status_arg )
1795{
1796 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001797 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001798 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001799 psa_status_t status;
1800
Gilles Peskine8817f612018-12-18 00:18:46 +01001801 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001803 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001804 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001805 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001806
1807exit:
1808 mbedtls_psa_crypto_free( );
1809}
1810/* END_CASE */
1811
1812/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001813void hash_bad_order( )
1814{
1815 unsigned char input[] = "";
1816 /* SHA-256 hash of an empty string */
1817 unsigned char hash[] = {
1818 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1819 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1820 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1821 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001822 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001823
Gilles Peskine8817f612018-12-18 00:18:46 +01001824 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001825
1826 /* psa_hash_update without calling psa_hash_setup beforehand */
1827 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001828 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001829 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001830
1831 /* psa_hash_verify without calling psa_hash_setup beforehand */
1832 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001833 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001834 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001835
1836 /* psa_hash_finish without calling psa_hash_setup beforehand */
1837 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001838 TEST_EQUAL( psa_hash_finish( &operation,
1839 hash, sizeof( hash ), &hash_len ),
1840 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001841
1842exit:
1843 mbedtls_psa_crypto_free( );
1844}
1845/* END_CASE */
1846
itayzafrir27e69452018-11-01 14:26:34 +02001847/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1848void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001849{
1850 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001851 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1852 * appended to it */
1853 unsigned char hash[] = {
1854 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1855 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1856 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001857 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001858 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001859
Gilles Peskine8817f612018-12-18 00:18:46 +01001860 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001861
itayzafrir27e69452018-11-01 14:26:34 +02001862 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001864 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001865 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001866
itayzafrir27e69452018-11-01 14:26:34 +02001867 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001868 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001869 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001870 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001871
itayzafrir27e69452018-11-01 14:26:34 +02001872 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001873 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001874 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001875 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001876
itayzafrirec93d302018-10-18 18:01:10 +03001877exit:
1878 mbedtls_psa_crypto_free( );
1879}
1880/* END_CASE */
1881
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001882/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1883void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001884{
1885 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001886 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001887 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001888 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001889 size_t hash_len;
1890
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001892
itayzafrir58028322018-10-25 10:22:01 +03001893 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001894 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001895 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001896 hash, expected_size - 1, &hash_len ),
1897 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001898
1899exit:
1900 mbedtls_psa_crypto_free( );
1901}
1902/* END_CASE */
1903
1904/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001905void mac_operation_init( )
1906{
1907 /* Test each valid way of initializing the object, except for `= {0}`, as
1908 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1909 * though it's OK by the C standard. We could test for this, but we'd need
1910 * to supress the Clang warning for the test. */
1911 psa_mac_operation_t func = psa_mac_operation_init( );
1912 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1913 psa_mac_operation_t zero;
1914
1915 memset( &zero, 0, sizeof( zero ) );
1916
1917 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1918 * specification, we test that all valid ways of initializing the object
1919 * have the same bit pattern. This is a stronger requirement that may not
1920 * be valid on all platforms or PSA Crypto implementations, but implies the
1921 * weaker actual requirement is met: that a freshly initialized object, no
1922 * matter how it was initialized, acts the same as any other valid
1923 * initialization. */
1924 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1925 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1926}
1927/* END_CASE */
1928
1929/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001930void mac_setup( int key_type_arg,
1931 data_t *key,
1932 int alg_arg,
1933 int expected_status_arg )
1934{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001935 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001936 psa_key_type_t key_type = key_type_arg;
1937 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001938 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001939 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001940 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001941 psa_status_t status;
1942
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1946 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001947 psa_key_policy_set_usage( &policy,
1948 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1949 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001951
Gilles Peskine8817f612018-12-18 00:18:46 +01001952 PSA_ASSERT( psa_import_key( handle, key_type,
1953 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001954
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001955 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001956 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001957 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001958
1959exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001960 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001961 mbedtls_psa_crypto_free( );
1962}
1963/* END_CASE */
1964
1965/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001966void mac_sign( int key_type_arg,
1967 data_t *key,
1968 int alg_arg,
1969 data_t *input,
1970 data_t *expected_mac )
1971{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001972 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001973 psa_key_type_t key_type = key_type_arg;
1974 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001975 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001976 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001977 /* Leave a little extra room in the output buffer. At the end of the
1978 * test, we'll check that the implementation didn't overwrite onto
1979 * this extra room. */
1980 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1981 size_t mac_buffer_size =
1982 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1983 size_t mac_length = 0;
1984
1985 memset( actual_mac, '+', sizeof( actual_mac ) );
1986 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1987 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1988
Gilles Peskine8817f612018-12-18 00:18:46 +01001989 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001990
Gilles Peskine8817f612018-12-18 00:18:46 +01001991 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1992 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001993 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001994 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001995
Gilles Peskine8817f612018-12-18 00:18:46 +01001996 PSA_ASSERT( psa_import_key( handle, key_type,
1997 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001998
1999 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002000 PSA_ASSERT( psa_mac_sign_setup( &operation,
2001 handle, alg ) );
2002 PSA_ASSERT( psa_mac_update( &operation,
2003 input->x, input->len ) );
2004 PSA_ASSERT( psa_mac_sign_finish( &operation,
2005 actual_mac, mac_buffer_size,
2006 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002007
2008 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002009 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2010 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002011
2012 /* Verify that the end of the buffer is untouched. */
2013 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2014 sizeof( actual_mac ) - mac_length ) );
2015
2016exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002017 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002018 mbedtls_psa_crypto_free( );
2019}
2020/* END_CASE */
2021
2022/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002023void mac_verify( int key_type_arg,
2024 data_t *key,
2025 int alg_arg,
2026 data_t *input,
2027 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002029 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030 psa_key_type_t key_type = key_type_arg;
2031 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002032 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002033 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002034
Gilles Peskine69c12672018-06-28 00:07:19 +02002035 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2036
Gilles Peskine8817f612018-12-18 00:18:46 +01002037 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002038
Gilles Peskine8817f612018-12-18 00:18:46 +01002039 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2040 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002041 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002042 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002043
Gilles Peskine8817f612018-12-18 00:18:46 +01002044 PSA_ASSERT( psa_import_key( handle, key_type,
2045 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002046
Gilles Peskine8817f612018-12-18 00:18:46 +01002047 PSA_ASSERT( psa_mac_verify_setup( &operation,
2048 handle, alg ) );
2049 PSA_ASSERT( psa_destroy_key( handle ) );
2050 PSA_ASSERT( psa_mac_update( &operation,
2051 input->x, input->len ) );
2052 PSA_ASSERT( psa_mac_verify_finish( &operation,
2053 expected_mac->x,
2054 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002055
2056exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002057 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002058 mbedtls_psa_crypto_free( );
2059}
2060/* END_CASE */
2061
2062/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002063void cipher_operation_init( )
2064{
2065 /* Test each valid way of initializing the object, except for `= {0}`, as
2066 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2067 * though it's OK by the C standard. We could test for this, but we'd need
2068 * to supress the Clang warning for the test. */
2069 psa_cipher_operation_t func = psa_cipher_operation_init( );
2070 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2071 psa_cipher_operation_t zero;
2072
2073 memset( &zero, 0, sizeof( zero ) );
2074
2075 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2076 * specification, we test that all valid ways of initializing the object
2077 * have the same bit pattern. This is a stronger requirement that may not
2078 * be valid on all platforms or PSA Crypto implementations, but implies the
2079 * weaker actual requirement is met: that a freshly initialized object, no
2080 * matter how it was initialized, acts the same as any other valid
2081 * initialization. */
2082 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2083 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2084}
2085/* END_CASE */
2086
2087/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002088void cipher_setup( int key_type_arg,
2089 data_t *key,
2090 int alg_arg,
2091 int expected_status_arg )
2092{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002093 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002094 psa_key_type_t key_type = key_type_arg;
2095 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002096 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002097 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002098 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002099 psa_status_t status;
2100
Gilles Peskine8817f612018-12-18 00:18:46 +01002101 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102
Gilles Peskine8817f612018-12-18 00:18:46 +01002103 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2104 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002106 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002107
Gilles Peskine8817f612018-12-18 00:18:46 +01002108 PSA_ASSERT( psa_import_key( handle, key_type,
2109 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002111 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002113 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002114
2115exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002116 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002117 mbedtls_psa_crypto_free( );
2118}
2119/* END_CASE */
2120
2121/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002122void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002123 data_t *key,
2124 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002125 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002126{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002127 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002128 psa_status_t status;
2129 psa_key_type_t key_type = key_type_arg;
2130 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002131 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002132 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002133 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002134 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002135 size_t output_buffer_size = 0;
2136 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002137 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002138 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002139 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002140
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002141 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2142 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002143
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002145
Gilles Peskine8817f612018-12-18 00:18:46 +01002146 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2147 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002148 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002149 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002150
Gilles Peskine8817f612018-12-18 00:18:46 +01002151 PSA_ASSERT( psa_import_key( handle, key_type,
2152 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153
Gilles Peskine8817f612018-12-18 00:18:46 +01002154 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2155 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002156
Gilles Peskine8817f612018-12-18 00:18:46 +01002157 PSA_ASSERT( psa_cipher_set_iv( &operation,
2158 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002159 output_buffer_size = ( (size_t) input->len +
2160 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002161 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002162
Gilles Peskine8817f612018-12-18 00:18:46 +01002163 PSA_ASSERT( psa_cipher_update( &operation,
2164 input->x, input->len,
2165 output, output_buffer_size,
2166 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002167 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002168 status = psa_cipher_finish( &operation,
2169 output + function_output_length,
2170 output_buffer_size,
2171 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002172 total_output_length += function_output_length;
2173
Gilles Peskinefe11b722018-12-18 00:24:04 +01002174 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002175 if( expected_status == PSA_SUCCESS )
2176 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002177 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002178 ASSERT_COMPARE( expected_output->x, expected_output->len,
2179 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002181
Gilles Peskine50e586b2018-06-08 14:28:46 +02002182exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002183 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002184 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185 mbedtls_psa_crypto_free( );
2186}
2187/* END_CASE */
2188
2189/* BEGIN_CASE */
2190void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002191 data_t *key,
2192 data_t *input,
2193 int first_part_size,
2194 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002196 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002197 psa_key_type_t key_type = key_type_arg;
2198 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002199 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002200 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002201 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202 size_t output_buffer_size = 0;
2203 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002204 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002205 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002206 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002207
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002208 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2209 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210
Gilles Peskine8817f612018-12-18 00:18:46 +01002211 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002212
Gilles Peskine8817f612018-12-18 00:18:46 +01002213 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2214 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002215 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002216 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002217
Gilles Peskine8817f612018-12-18 00:18:46 +01002218 PSA_ASSERT( psa_import_key( handle, key_type,
2219 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002220
Gilles Peskine8817f612018-12-18 00:18:46 +01002221 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2222 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002223
Gilles Peskine8817f612018-12-18 00:18:46 +01002224 PSA_ASSERT( psa_cipher_set_iv( &operation,
2225 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002226 output_buffer_size = ( (size_t) input->len +
2227 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002228 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002229
Gilles Peskine4abf7412018-06-18 16:35:34 +02002230 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002231 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2232 output, output_buffer_size,
2233 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002234 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002235 PSA_ASSERT( psa_cipher_update( &operation,
2236 input->x + first_part_size,
2237 input->len - first_part_size,
2238 output, output_buffer_size,
2239 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002240 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002241 PSA_ASSERT( psa_cipher_finish( &operation,
2242 output + function_output_length,
2243 output_buffer_size,
2244 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002245 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002246 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002247
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002248 ASSERT_COMPARE( expected_output->x, expected_output->len,
2249 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002250
2251exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002252 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002253 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002254 mbedtls_psa_crypto_free( );
2255}
2256/* END_CASE */
2257
2258/* BEGIN_CASE */
2259void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002260 data_t *key,
2261 data_t *input,
2262 int first_part_size,
2263 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002264{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002265 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002266
2267 psa_key_type_t key_type = key_type_arg;
2268 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002269 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002270 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002271 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002272 size_t output_buffer_size = 0;
2273 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002274 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002275 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002276 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002277
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002278 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2279 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002282
Gilles Peskine8817f612018-12-18 00:18:46 +01002283 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2284 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002285 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002287
Gilles Peskine8817f612018-12-18 00:18:46 +01002288 PSA_ASSERT( psa_import_key( handle, key_type,
2289 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002290
Gilles Peskine8817f612018-12-18 00:18:46 +01002291 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2292 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002293
Gilles Peskine8817f612018-12-18 00:18:46 +01002294 PSA_ASSERT( psa_cipher_set_iv( &operation,
2295 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002296
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002297 output_buffer_size = ( (size_t) input->len +
2298 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002299 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002300
Gilles Peskine4abf7412018-06-18 16:35:34 +02002301 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002302 PSA_ASSERT( psa_cipher_update( &operation,
2303 input->x, first_part_size,
2304 output, output_buffer_size,
2305 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002306 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002307 PSA_ASSERT( psa_cipher_update( &operation,
2308 input->x + first_part_size,
2309 input->len - first_part_size,
2310 output, output_buffer_size,
2311 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002312 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002313 PSA_ASSERT( psa_cipher_finish( &operation,
2314 output + function_output_length,
2315 output_buffer_size,
2316 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002317 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002318 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002320 ASSERT_COMPARE( expected_output->x, expected_output->len,
2321 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002322
2323exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002324 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002325 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002326 mbedtls_psa_crypto_free( );
2327}
2328/* END_CASE */
2329
Gilles Peskine50e586b2018-06-08 14:28:46 +02002330/* BEGIN_CASE */
2331void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002332 data_t *key,
2333 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002334 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002335{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002336 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337 psa_status_t status;
2338 psa_key_type_t key_type = key_type_arg;
2339 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002340 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002341 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002342 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002343 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002344 size_t output_buffer_size = 0;
2345 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002346 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002347 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002348 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002349
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002350 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2351 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002352
Gilles Peskine8817f612018-12-18 00:18:46 +01002353 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002354
Gilles Peskine8817f612018-12-18 00:18:46 +01002355 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2356 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002357 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002358 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002359
Gilles Peskine8817f612018-12-18 00:18:46 +01002360 PSA_ASSERT( psa_import_key( handle, key_type,
2361 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002362
Gilles Peskine8817f612018-12-18 00:18:46 +01002363 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2364 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002365
Gilles Peskine8817f612018-12-18 00:18:46 +01002366 PSA_ASSERT( psa_cipher_set_iv( &operation,
2367 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002368
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002369 output_buffer_size = ( (size_t) input->len +
2370 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002371 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002372
Gilles Peskine8817f612018-12-18 00:18:46 +01002373 PSA_ASSERT( psa_cipher_update( &operation,
2374 input->x, input->len,
2375 output, output_buffer_size,
2376 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002377 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378 status = psa_cipher_finish( &operation,
2379 output + function_output_length,
2380 output_buffer_size,
2381 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002382 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002383 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002384
2385 if( expected_status == PSA_SUCCESS )
2386 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002387 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002388 ASSERT_COMPARE( expected_output->x, expected_output->len,
2389 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002390 }
2391
Gilles Peskine50e586b2018-06-08 14:28:46 +02002392exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002393 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002394 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395 mbedtls_psa_crypto_free( );
2396}
2397/* END_CASE */
2398
Gilles Peskine50e586b2018-06-08 14:28:46 +02002399/* BEGIN_CASE */
2400void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002401 data_t *key,
2402 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002403{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002404 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002405 psa_key_type_t key_type = key_type_arg;
2406 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002407 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002408 size_t iv_size = 16;
2409 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002410 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002411 size_t output1_size = 0;
2412 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002413 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002414 size_t output2_size = 0;
2415 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002416 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002417 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2418 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002419 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002420
Gilles Peskine8817f612018-12-18 00:18:46 +01002421 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002422
Gilles Peskine8817f612018-12-18 00:18:46 +01002423 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2424 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002425 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002426 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002427
Gilles Peskine8817f612018-12-18 00:18:46 +01002428 PSA_ASSERT( psa_import_key( handle, key_type,
2429 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002430
Gilles Peskine8817f612018-12-18 00:18:46 +01002431 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2432 handle, alg ) );
2433 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2434 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002435
Gilles Peskine8817f612018-12-18 00:18:46 +01002436 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2437 iv, iv_size,
2438 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002439 output1_size = ( (size_t) input->len +
2440 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002441 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002442
Gilles Peskine8817f612018-12-18 00:18:46 +01002443 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2444 output1, output1_size,
2445 &output1_length ) );
2446 PSA_ASSERT( psa_cipher_finish( &operation1,
2447 output1 + output1_length, output1_size,
2448 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002449
Gilles Peskine048b7f02018-06-08 14:20:49 +02002450 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002451
Gilles Peskine8817f612018-12-18 00:18:46 +01002452 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002453
2454 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002455 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002456
Gilles Peskine8817f612018-12-18 00:18:46 +01002457 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2458 iv, iv_length ) );
2459 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2460 output2, output2_size,
2461 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002462 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002463 PSA_ASSERT( psa_cipher_finish( &operation2,
2464 output2 + output2_length,
2465 output2_size,
2466 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002467
Gilles Peskine048b7f02018-06-08 14:20:49 +02002468 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002469
Gilles Peskine8817f612018-12-18 00:18:46 +01002470 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002471
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002472 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002473
2474exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002475 mbedtls_free( output1 );
2476 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002477 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002478 mbedtls_psa_crypto_free( );
2479}
2480/* END_CASE */
2481
2482/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002483void cipher_verify_output_multipart( int alg_arg,
2484 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002485 data_t *key,
2486 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002488{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002489 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002490 psa_key_type_t key_type = key_type_arg;
2491 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002492 unsigned char iv[16] = {0};
2493 size_t iv_size = 16;
2494 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002495 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002496 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002497 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002498 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002499 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002500 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002501 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002502 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2503 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002504 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002505
Gilles Peskine8817f612018-12-18 00:18:46 +01002506 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002507
Gilles Peskine8817f612018-12-18 00:18:46 +01002508 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2509 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002510 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002511 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002512
Gilles Peskine8817f612018-12-18 00:18:46 +01002513 PSA_ASSERT( psa_import_key( handle, key_type,
2514 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002515
Gilles Peskine8817f612018-12-18 00:18:46 +01002516 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2517 handle, alg ) );
2518 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2519 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002520
Gilles Peskine8817f612018-12-18 00:18:46 +01002521 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2522 iv, iv_size,
2523 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002524 output1_buffer_size = ( (size_t) input->len +
2525 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002526 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002527
Gilles Peskine4abf7412018-06-18 16:35:34 +02002528 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002529
Gilles Peskine8817f612018-12-18 00:18:46 +01002530 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2531 output1, output1_buffer_size,
2532 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002533 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002534
Gilles Peskine8817f612018-12-18 00:18:46 +01002535 PSA_ASSERT( psa_cipher_update( &operation1,
2536 input->x + first_part_size,
2537 input->len - first_part_size,
2538 output1, output1_buffer_size,
2539 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002540 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002541
Gilles Peskine8817f612018-12-18 00:18:46 +01002542 PSA_ASSERT( psa_cipher_finish( &operation1,
2543 output1 + output1_length,
2544 output1_buffer_size - output1_length,
2545 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002546 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002547
Gilles Peskine8817f612018-12-18 00:18:46 +01002548 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002549
Gilles Peskine048b7f02018-06-08 14:20:49 +02002550 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002551 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002552
Gilles Peskine8817f612018-12-18 00:18:46 +01002553 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2554 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002555
Gilles Peskine8817f612018-12-18 00:18:46 +01002556 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2557 output2, output2_buffer_size,
2558 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002559 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002560
Gilles Peskine8817f612018-12-18 00:18:46 +01002561 PSA_ASSERT( psa_cipher_update( &operation2,
2562 output1 + first_part_size,
2563 output1_length - first_part_size,
2564 output2, output2_buffer_size,
2565 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002566 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002567
Gilles Peskine8817f612018-12-18 00:18:46 +01002568 PSA_ASSERT( psa_cipher_finish( &operation2,
2569 output2 + output2_length,
2570 output2_buffer_size - output2_length,
2571 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002572 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002573
Gilles Peskine8817f612018-12-18 00:18:46 +01002574 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002575
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002576 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002577
2578exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002579 mbedtls_free( output1 );
2580 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002581 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002582 mbedtls_psa_crypto_free( );
2583}
2584/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002585
Gilles Peskine20035e32018-02-03 22:44:14 +01002586/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002587void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002588 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002589 data_t *nonce,
2590 data_t *additional_data,
2591 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002592 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002593{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002594 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002595 psa_key_type_t key_type = key_type_arg;
2596 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002597 unsigned char *output_data = NULL;
2598 size_t output_size = 0;
2599 size_t output_length = 0;
2600 unsigned char *output_data2 = NULL;
2601 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002602 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002603 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002604 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002605
Gilles Peskine4abf7412018-06-18 16:35:34 +02002606 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002607 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002608
Gilles Peskine8817f612018-12-18 00:18:46 +01002609 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002610
Gilles Peskine8817f612018-12-18 00:18:46 +01002611 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2612 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002613 psa_key_policy_set_usage( &policy,
2614 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2615 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002616 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002617
Gilles Peskine8817f612018-12-18 00:18:46 +01002618 PSA_ASSERT( psa_import_key( handle, key_type,
2619 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002620
Gilles Peskinefe11b722018-12-18 00:24:04 +01002621 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2622 nonce->x, nonce->len,
2623 additional_data->x,
2624 additional_data->len,
2625 input_data->x, input_data->len,
2626 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002627 &output_length ),
2628 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629
2630 if( PSA_SUCCESS == expected_result )
2631 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002632 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002633
Gilles Peskinefe11b722018-12-18 00:24:04 +01002634 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2635 nonce->x, nonce->len,
2636 additional_data->x,
2637 additional_data->len,
2638 output_data, output_length,
2639 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002640 &output_length2 ),
2641 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002642
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002643 ASSERT_COMPARE( input_data->x, input_data->len,
2644 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002645 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002646
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002648 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002649 mbedtls_free( output_data );
2650 mbedtls_free( output_data2 );
2651 mbedtls_psa_crypto_free( );
2652}
2653/* END_CASE */
2654
2655/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002656void aead_encrypt( int key_type_arg, data_t *key_data,
2657 int alg_arg,
2658 data_t *nonce,
2659 data_t *additional_data,
2660 data_t *input_data,
2661 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002662{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002663 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002664 psa_key_type_t key_type = key_type_arg;
2665 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002666 unsigned char *output_data = NULL;
2667 size_t output_size = 0;
2668 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002670 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002671
Gilles Peskine4abf7412018-06-18 16:35:34 +02002672 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002673 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002674
Gilles Peskine8817f612018-12-18 00:18:46 +01002675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002676
Gilles Peskine8817f612018-12-18 00:18:46 +01002677 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2678 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002679 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002680 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002681
Gilles Peskine8817f612018-12-18 00:18:46 +01002682 PSA_ASSERT( psa_import_key( handle, key_type,
2683 key_data->x,
2684 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002685
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2687 nonce->x, nonce->len,
2688 additional_data->x, additional_data->len,
2689 input_data->x, input_data->len,
2690 output_data, output_size,
2691 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002692
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002693 ASSERT_COMPARE( expected_result->x, expected_result->len,
2694 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002695
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002697 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002698 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002699 mbedtls_psa_crypto_free( );
2700}
2701/* END_CASE */
2702
2703/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002704void aead_decrypt( int key_type_arg, data_t *key_data,
2705 int alg_arg,
2706 data_t *nonce,
2707 data_t *additional_data,
2708 data_t *input_data,
2709 data_t *expected_data,
2710 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002711{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002712 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002713 psa_key_type_t key_type = key_type_arg;
2714 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002715 unsigned char *output_data = NULL;
2716 size_t output_size = 0;
2717 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002718 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002719 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002720 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002721
Gilles Peskine4abf7412018-06-18 16:35:34 +02002722 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002723 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002724
Gilles Peskine8817f612018-12-18 00:18:46 +01002725 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002726
Gilles Peskine8817f612018-12-18 00:18:46 +01002727 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2728 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002729 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002730 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002731
Gilles Peskine8817f612018-12-18 00:18:46 +01002732 PSA_ASSERT( psa_import_key( handle, key_type,
2733 key_data->x,
2734 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002735
Gilles Peskinefe11b722018-12-18 00:24:04 +01002736 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2737 nonce->x, nonce->len,
2738 additional_data->x,
2739 additional_data->len,
2740 input_data->x, input_data->len,
2741 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002742 &output_length ),
2743 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002744
Gilles Peskine2d277862018-06-18 15:41:12 +02002745 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002746 ASSERT_COMPARE( expected_data->x, expected_data->len,
2747 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002748
Gilles Peskinea1cac842018-06-11 19:33:02 +02002749exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002750 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002751 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002752 mbedtls_psa_crypto_free( );
2753}
2754/* END_CASE */
2755
2756/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002757void signature_size( int type_arg,
2758 int bits,
2759 int alg_arg,
2760 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002761{
2762 psa_key_type_t type = type_arg;
2763 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002764 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002765 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002766exit:
2767 ;
2768}
2769/* END_CASE */
2770
2771/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002772void sign_deterministic( int key_type_arg, data_t *key_data,
2773 int alg_arg, data_t *input_data,
2774 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002775{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002776 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002777 psa_key_type_t key_type = key_type_arg;
2778 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002779 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002780 unsigned char *signature = NULL;
2781 size_t signature_size;
2782 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002783 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002786
Gilles Peskine8817f612018-12-18 00:18:46 +01002787 PSA_ASSERT( psa_allocate_key( key_type,
2788 KEY_BITS_FROM_DATA( key_type, key_data ),
2789 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002790 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002791 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002792
Gilles Peskine8817f612018-12-18 00:18:46 +01002793 PSA_ASSERT( psa_import_key( handle, key_type,
2794 key_data->x,
2795 key_data->len ) );
2796 PSA_ASSERT( psa_get_key_information( handle,
2797 NULL,
2798 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002799
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002800 /* Allocate a buffer which has the size advertized by the
2801 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002802 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2803 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002804 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002805 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002806 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002807
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002808 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002809 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2810 input_data->x, input_data->len,
2811 signature, signature_size,
2812 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002813 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002814 ASSERT_COMPARE( output_data->x, output_data->len,
2815 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002816
2817exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002818 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002819 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002820 mbedtls_psa_crypto_free( );
2821}
2822/* END_CASE */
2823
2824/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002825void sign_fail( int key_type_arg, data_t *key_data,
2826 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002827 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002828{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002829 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002830 psa_key_type_t key_type = key_type_arg;
2831 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002832 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002833 psa_status_t actual_status;
2834 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002835 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002836 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002837 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002838
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002839 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002840
Gilles Peskine8817f612018-12-18 00:18:46 +01002841 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002842
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_allocate_key( key_type,
2844 KEY_BITS_FROM_DATA( key_type, key_data ),
2845 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002846 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002847 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002848
Gilles Peskine8817f612018-12-18 00:18:46 +01002849 PSA_ASSERT( psa_import_key( handle, key_type,
2850 key_data->x,
2851 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002852
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002853 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002854 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002855 signature, signature_size,
2856 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002857 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002858 /* The value of *signature_length is unspecified on error, but
2859 * whatever it is, it should be less than signature_size, so that
2860 * if the caller tries to read *signature_length bytes without
2861 * checking the error code then they don't overflow a buffer. */
2862 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002863
2864exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002865 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002866 mbedtls_free( signature );
2867 mbedtls_psa_crypto_free( );
2868}
2869/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002870
2871/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002872void sign_verify( int key_type_arg, data_t *key_data,
2873 int alg_arg, data_t *input_data )
2874{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002875 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002876 psa_key_type_t key_type = key_type_arg;
2877 psa_algorithm_t alg = alg_arg;
2878 size_t key_bits;
2879 unsigned char *signature = NULL;
2880 size_t signature_size;
2881 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002882 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002883
Gilles Peskine8817f612018-12-18 00:18:46 +01002884 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002885
Gilles Peskine8817f612018-12-18 00:18:46 +01002886 PSA_ASSERT( psa_allocate_key( key_type,
2887 KEY_BITS_FROM_DATA( key_type, key_data ),
2888 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002889 psa_key_policy_set_usage( &policy,
2890 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2891 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002893
Gilles Peskine8817f612018-12-18 00:18:46 +01002894 PSA_ASSERT( psa_import_key( handle, key_type,
2895 key_data->x,
2896 key_data->len ) );
2897 PSA_ASSERT( psa_get_key_information( handle,
2898 NULL,
2899 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002900
2901 /* Allocate a buffer which has the size advertized by the
2902 * library. */
2903 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2904 key_bits, alg );
2905 TEST_ASSERT( signature_size != 0 );
2906 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002907 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002908
2909 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2911 input_data->x, input_data->len,
2912 signature, signature_size,
2913 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002914 /* Check that the signature length looks sensible. */
2915 TEST_ASSERT( signature_length <= signature_size );
2916 TEST_ASSERT( signature_length > 0 );
2917
2918 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_asymmetric_verify(
2920 handle, alg,
2921 input_data->x, input_data->len,
2922 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002923
2924 if( input_data->len != 0 )
2925 {
2926 /* Flip a bit in the input and verify that the signature is now
2927 * detected as invalid. Flip a bit at the beginning, not at the end,
2928 * because ECDSA may ignore the last few bits of the input. */
2929 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002930 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2931 input_data->x, input_data->len,
2932 signature, signature_length ),
2933 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002934 }
2935
2936exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002937 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002938 mbedtls_free( signature );
2939 mbedtls_psa_crypto_free( );
2940}
2941/* END_CASE */
2942
2943/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002944void asymmetric_verify( int key_type_arg, data_t *key_data,
2945 int alg_arg, data_t *hash_data,
2946 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002947{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002948 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002949 psa_key_type_t key_type = key_type_arg;
2950 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002951 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002952
Gilles Peskine69c12672018-06-28 00:07:19 +02002953 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2954
Gilles Peskine8817f612018-12-18 00:18:46 +01002955 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002956
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_allocate_key( key_type,
2958 KEY_BITS_FROM_DATA( key_type, key_data ),
2959 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002960 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002962
Gilles Peskine8817f612018-12-18 00:18:46 +01002963 PSA_ASSERT( psa_import_key( handle, key_type,
2964 key_data->x,
2965 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002966
Gilles Peskine8817f612018-12-18 00:18:46 +01002967 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2968 hash_data->x, hash_data->len,
2969 signature_data->x,
2970 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002971exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002972 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002973 mbedtls_psa_crypto_free( );
2974}
2975/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002976
2977/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002978void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2979 int alg_arg, data_t *hash_data,
2980 data_t *signature_data,
2981 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002983 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002984 psa_key_type_t key_type = key_type_arg;
2985 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002986 psa_status_t actual_status;
2987 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002988 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002991
Gilles Peskine8817f612018-12-18 00:18:46 +01002992 PSA_ASSERT( psa_allocate_key( key_type,
2993 KEY_BITS_FROM_DATA( key_type, key_data ),
2994 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002995 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002997
Gilles Peskine8817f612018-12-18 00:18:46 +01002998 PSA_ASSERT( psa_import_key( handle, key_type,
2999 key_data->x,
3000 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003001
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003002 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003003 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003004 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003005 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003006
Gilles Peskinefe11b722018-12-18 00:24:04 +01003007 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003008
3009exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003010 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003011 mbedtls_psa_crypto_free( );
3012}
3013/* END_CASE */
3014
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003015/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003016void asymmetric_encrypt( int key_type_arg,
3017 data_t *key_data,
3018 int alg_arg,
3019 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003020 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003021 int expected_output_length_arg,
3022 int expected_status_arg )
3023{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003024 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003025 psa_key_type_t key_type = key_type_arg;
3026 psa_algorithm_t alg = alg_arg;
3027 size_t expected_output_length = expected_output_length_arg;
3028 size_t key_bits;
3029 unsigned char *output = NULL;
3030 size_t output_size;
3031 size_t output_length = ~0;
3032 psa_status_t actual_status;
3033 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003034 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003035
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003037
Gilles Peskine656896e2018-06-29 19:12:28 +02003038 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_allocate_key( key_type,
3040 KEY_BITS_FROM_DATA( key_type, key_data ),
3041 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003042 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3044 PSA_ASSERT( psa_import_key( handle, key_type,
3045 key_data->x,
3046 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003047
3048 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003049 PSA_ASSERT( psa_get_key_information( handle,
3050 NULL,
3051 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003052 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003053 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003054
3055 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003056 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003057 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003058 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003059 output, output_size,
3060 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003061 TEST_EQUAL( actual_status, expected_status );
3062 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003063
Gilles Peskine68428122018-06-30 18:42:41 +02003064 /* If the label is empty, the test framework puts a non-null pointer
3065 * in label->x. Test that a null pointer works as well. */
3066 if( label->len == 0 )
3067 {
3068 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003069 if( output_size != 0 )
3070 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003071 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003072 input_data->x, input_data->len,
3073 NULL, label->len,
3074 output, output_size,
3075 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003076 TEST_EQUAL( actual_status, expected_status );
3077 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003078 }
3079
Gilles Peskine656896e2018-06-29 19:12:28 +02003080exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003081 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003082 mbedtls_free( output );
3083 mbedtls_psa_crypto_free( );
3084}
3085/* END_CASE */
3086
3087/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003088void asymmetric_encrypt_decrypt( int key_type_arg,
3089 data_t *key_data,
3090 int alg_arg,
3091 data_t *input_data,
3092 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003094 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095 psa_key_type_t key_type = key_type_arg;
3096 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003097 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003098 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003099 size_t output_size;
3100 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003101 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003102 size_t output2_size;
3103 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003104 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003105
Gilles Peskine8817f612018-12-18 00:18:46 +01003106 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107
Gilles Peskine8817f612018-12-18 00:18:46 +01003108 PSA_ASSERT( psa_allocate_key( key_type,
3109 KEY_BITS_FROM_DATA( key_type, key_data ),
3110 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003111 psa_key_policy_set_usage( &policy,
3112 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003113 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003115
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_import_key( handle, key_type,
3117 key_data->x,
3118 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003119
3120 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003121 PSA_ASSERT( psa_get_key_information( handle,
3122 NULL,
3123 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003124 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003125 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003126 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003127 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003128
Gilles Peskineeebd7382018-06-08 18:11:54 +02003129 /* We test encryption by checking that encrypt-then-decrypt gives back
3130 * the original plaintext because of the non-optional random
3131 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003132 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3133 input_data->x, input_data->len,
3134 label->x, label->len,
3135 output, output_size,
3136 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003137 /* We don't know what ciphertext length to expect, but check that
3138 * it looks sensible. */
3139 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003140
Gilles Peskine8817f612018-12-18 00:18:46 +01003141 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3142 output, output_length,
3143 label->x, label->len,
3144 output2, output2_size,
3145 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003146 ASSERT_COMPARE( input_data->x, input_data->len,
3147 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148
3149exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003150 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003151 mbedtls_free( output );
3152 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003153 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003154}
3155/* END_CASE */
3156
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003157/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003158void asymmetric_decrypt( int key_type_arg,
3159 data_t *key_data,
3160 int alg_arg,
3161 data_t *input_data,
3162 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003163 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003165 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166 psa_key_type_t key_type = key_type_arg;
3167 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003169 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003170 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003171 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003172
Gilles Peskine4abf7412018-06-18 16:35:34 +02003173 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003174 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177
Gilles Peskine8817f612018-12-18 00:18:46 +01003178 PSA_ASSERT( psa_allocate_key( key_type,
3179 KEY_BITS_FROM_DATA( key_type, key_data ),
3180 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003181 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003183
Gilles Peskine8817f612018-12-18 00:18:46 +01003184 PSA_ASSERT( psa_import_key( handle, key_type,
3185 key_data->x,
3186 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3189 input_data->x, input_data->len,
3190 label->x, label->len,
3191 output,
3192 output_size,
3193 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003194 ASSERT_COMPARE( expected_data->x, expected_data->len,
3195 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196
Gilles Peskine68428122018-06-30 18:42:41 +02003197 /* If the label is empty, the test framework puts a non-null pointer
3198 * in label->x. Test that a null pointer works as well. */
3199 if( label->len == 0 )
3200 {
3201 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003202 if( output_size != 0 )
3203 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3205 input_data->x, input_data->len,
3206 NULL, label->len,
3207 output,
3208 output_size,
3209 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003210 ASSERT_COMPARE( expected_data->x, expected_data->len,
3211 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003212 }
3213
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003215 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003216 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003218}
3219/* END_CASE */
3220
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003222void asymmetric_decrypt_fail( int key_type_arg,
3223 data_t *key_data,
3224 int alg_arg,
3225 data_t *input_data,
3226 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003227 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003229 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003230 psa_key_type_t key_type = key_type_arg;
3231 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003232 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003233 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003234 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235 psa_status_t actual_status;
3236 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003237 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Gilles Peskine4abf7412018-06-18 16:35:34 +02003239 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003240 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003241
Gilles Peskine8817f612018-12-18 00:18:46 +01003242 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003243
Gilles Peskine8817f612018-12-18 00:18:46 +01003244 PSA_ASSERT( psa_allocate_key( key_type,
3245 KEY_BITS_FROM_DATA( key_type, key_data ),
3246 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003247 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003248 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_import_key( handle, key_type,
3251 key_data->x,
3252 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003254 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003255 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003256 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003257 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003258 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003259 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003260 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261
Gilles Peskine68428122018-06-30 18:42:41 +02003262 /* If the label is empty, the test framework puts a non-null pointer
3263 * in label->x. Test that a null pointer works as well. */
3264 if( label->len == 0 )
3265 {
3266 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003267 if( output_size != 0 )
3268 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003269 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003270 input_data->x, input_data->len,
3271 NULL, label->len,
3272 output, output_size,
3273 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003274 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003275 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003276 }
3277
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003279 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003280 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281 mbedtls_psa_crypto_free( );
3282}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003283/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003284
3285/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003286void crypto_generator_init( )
3287{
3288 /* Test each valid way of initializing the object, except for `= {0}`, as
3289 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3290 * though it's OK by the C standard. We could test for this, but we'd need
3291 * to supress the Clang warning for the test. */
3292 psa_crypto_generator_t func = psa_crypto_generator_init( );
3293 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3294 psa_crypto_generator_t zero;
3295
3296 memset( &zero, 0, sizeof( zero ) );
3297
3298 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3299 * specification, we test that all valid ways of initializing the object
3300 * have the same bit pattern. This is a stronger requirement that may not
3301 * be valid on all platforms or PSA Crypto implementations, but implies the
3302 * weaker actual requirement is met: that a freshly initialized object, no
3303 * matter how it was initialized, acts the same as any other valid
3304 * initialization. */
3305 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3306 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3307}
3308/* END_CASE */
3309
3310/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003311void derive_setup( int key_type_arg,
3312 data_t *key_data,
3313 int alg_arg,
3314 data_t *salt,
3315 data_t *label,
3316 int requested_capacity_arg,
3317 int expected_status_arg )
3318{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003319 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003320 size_t key_type = key_type_arg;
3321 psa_algorithm_t alg = alg_arg;
3322 size_t requested_capacity = requested_capacity_arg;
3323 psa_status_t expected_status = expected_status_arg;
3324 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003325 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003326
Gilles Peskine8817f612018-12-18 00:18:46 +01003327 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003328
Gilles Peskine8817f612018-12-18 00:18:46 +01003329 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3330 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003331 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003332 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003333
Gilles Peskine8817f612018-12-18 00:18:46 +01003334 PSA_ASSERT( psa_import_key( handle, key_type,
3335 key_data->x,
3336 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003337
Gilles Peskinefe11b722018-12-18 00:24:04 +01003338 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3339 salt->x, salt->len,
3340 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003341 requested_capacity ),
3342 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003343
3344exit:
3345 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003346 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003347 mbedtls_psa_crypto_free( );
3348}
3349/* END_CASE */
3350
3351/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003352void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003353{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003354 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003355 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003356 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003357 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003358 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003359 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003360 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3361 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3362 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003363 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003364
Gilles Peskine8817f612018-12-18 00:18:46 +01003365 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003366
Gilles Peskine8817f612018-12-18 00:18:46 +01003367 PSA_ASSERT( psa_allocate_key( key_type,
3368 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3369 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003370 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003371 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003372
Gilles Peskine8817f612018-12-18 00:18:46 +01003373 PSA_ASSERT( psa_import_key( handle, key_type,
3374 key_data,
3375 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003376
3377 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3379 NULL, 0,
3380 NULL, 0,
3381 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003382
3383 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003384 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3385 NULL, 0,
3386 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003387 capacity ),
3388 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003389
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003390 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003391
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003392 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3393 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003394
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003395exit:
3396 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003397 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003398 mbedtls_psa_crypto_free( );
3399}
3400/* END_CASE */
3401
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003402/* BEGIN_CASE */
3403void test_derive_invalid_generator_tests( )
3404{
3405 uint8_t output_buffer[16];
3406 size_t buffer_size = 16;
3407 size_t capacity = 0;
3408 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3409
Nir Sonnenschein50789302018-10-31 12:16:38 +02003410 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003411 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003412
3413 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003414 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003415
Gilles Peskine8817f612018-12-18 00:18:46 +01003416 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003417
Nir Sonnenschein50789302018-10-31 12:16:38 +02003418 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003419 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003420
Nir Sonnenschein50789302018-10-31 12:16:38 +02003421 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003422 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003423
3424exit:
3425 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003426}
3427/* END_CASE */
3428
3429/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003430void derive_output( int alg_arg,
3431 data_t *key_data,
3432 data_t *salt,
3433 data_t *label,
3434 int requested_capacity_arg,
3435 data_t *expected_output1,
3436 data_t *expected_output2 )
3437{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003438 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003439 psa_algorithm_t alg = alg_arg;
3440 size_t requested_capacity = requested_capacity_arg;
3441 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3442 uint8_t *expected_outputs[2] =
3443 {expected_output1->x, expected_output2->x};
3444 size_t output_sizes[2] =
3445 {expected_output1->len, expected_output2->len};
3446 size_t output_buffer_size = 0;
3447 uint8_t *output_buffer = NULL;
3448 size_t expected_capacity;
3449 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003450 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003451 psa_status_t status;
3452 unsigned i;
3453
3454 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3455 {
3456 if( output_sizes[i] > output_buffer_size )
3457 output_buffer_size = output_sizes[i];
3458 if( output_sizes[i] == 0 )
3459 expected_outputs[i] = NULL;
3460 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003461 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003463
Gilles Peskine8817f612018-12-18 00:18:46 +01003464 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3465 PSA_BYTES_TO_BITS( key_data->len ),
3466 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003467 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003468 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003469
Gilles Peskine8817f612018-12-18 00:18:46 +01003470 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3471 key_data->x,
3472 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003473
3474 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003475 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3476 salt->x, salt->len,
3477 label->x, label->len,
3478 requested_capacity ) );
3479 PSA_ASSERT( psa_get_generator_capacity( &generator,
3480 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003481 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003482 expected_capacity = requested_capacity;
3483
3484 /* Expansion phase. */
3485 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3486 {
3487 /* Read some bytes. */
3488 status = psa_generator_read( &generator,
3489 output_buffer, output_sizes[i] );
3490 if( expected_capacity == 0 && output_sizes[i] == 0 )
3491 {
3492 /* Reading 0 bytes when 0 bytes are available can go either way. */
3493 TEST_ASSERT( status == PSA_SUCCESS ||
3494 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3495 continue;
3496 }
3497 else if( expected_capacity == 0 ||
3498 output_sizes[i] > expected_capacity )
3499 {
3500 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003501 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003502 expected_capacity = 0;
3503 continue;
3504 }
3505 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003507 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003508 ASSERT_COMPARE( output_buffer, output_sizes[i],
3509 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003510 /* Check the generator status. */
3511 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_get_generator_capacity( &generator,
3513 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003514 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003515 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003516 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003517
3518exit:
3519 mbedtls_free( output_buffer );
3520 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003521 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003522 mbedtls_psa_crypto_free( );
3523}
3524/* END_CASE */
3525
3526/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003527void derive_full( int alg_arg,
3528 data_t *key_data,
3529 data_t *salt,
3530 data_t *label,
3531 int requested_capacity_arg )
3532{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003533 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003534 psa_algorithm_t alg = alg_arg;
3535 size_t requested_capacity = requested_capacity_arg;
3536 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3537 unsigned char output_buffer[16];
3538 size_t expected_capacity = requested_capacity;
3539 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003540 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003541
Gilles Peskine8817f612018-12-18 00:18:46 +01003542 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003543
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3545 PSA_BYTES_TO_BITS( key_data->len ),
3546 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003547 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003548 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003549
Gilles Peskine8817f612018-12-18 00:18:46 +01003550 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3551 key_data->x,
3552 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003553
3554 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3556 salt->x, salt->len,
3557 label->x, label->len,
3558 requested_capacity ) );
3559 PSA_ASSERT( psa_get_generator_capacity( &generator,
3560 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003561 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003562
3563 /* Expansion phase. */
3564 while( current_capacity > 0 )
3565 {
3566 size_t read_size = sizeof( output_buffer );
3567 if( read_size > current_capacity )
3568 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003569 PSA_ASSERT( psa_generator_read( &generator,
3570 output_buffer,
3571 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003572 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003573 PSA_ASSERT( psa_get_generator_capacity( &generator,
3574 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003575 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003576 }
3577
3578 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003579 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3580 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003581
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003583
3584exit:
3585 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003586 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003587 mbedtls_psa_crypto_free( );
3588}
3589/* END_CASE */
3590
3591/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003592void derive_key_exercise( int alg_arg,
3593 data_t *key_data,
3594 data_t *salt,
3595 data_t *label,
3596 int derived_type_arg,
3597 int derived_bits_arg,
3598 int derived_usage_arg,
3599 int derived_alg_arg )
3600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003601 psa_key_handle_t base_handle = 0;
3602 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003603 psa_algorithm_t alg = alg_arg;
3604 psa_key_type_t derived_type = derived_type_arg;
3605 size_t derived_bits = derived_bits_arg;
3606 psa_key_usage_t derived_usage = derived_usage_arg;
3607 psa_algorithm_t derived_alg = derived_alg_arg;
3608 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3609 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003610 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003611 psa_key_type_t got_type;
3612 size_t got_bits;
3613
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003615
Gilles Peskine8817f612018-12-18 00:18:46 +01003616 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3617 PSA_BYTES_TO_BITS( key_data->len ),
3618 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003619 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003620 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3621 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3622 key_data->x,
3623 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003624
3625 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003626 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3627 salt->x, salt->len,
3628 label->x, label->len,
3629 capacity ) );
3630 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3631 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003632 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003633 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3634 PSA_ASSERT( psa_generator_import_key( derived_handle,
3635 derived_type,
3636 derived_bits,
3637 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003638
3639 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_get_key_information( derived_handle,
3641 &got_type,
3642 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003643 TEST_EQUAL( got_type, derived_type );
3644 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003645
3646 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003647 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003648 goto exit;
3649
3650exit:
3651 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003652 psa_destroy_key( base_handle );
3653 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003654 mbedtls_psa_crypto_free( );
3655}
3656/* END_CASE */
3657
3658/* BEGIN_CASE */
3659void derive_key_export( int alg_arg,
3660 data_t *key_data,
3661 data_t *salt,
3662 data_t *label,
3663 int bytes1_arg,
3664 int bytes2_arg )
3665{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003666 psa_key_handle_t base_handle = 0;
3667 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003668 psa_algorithm_t alg = alg_arg;
3669 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003670 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003671 size_t bytes2 = bytes2_arg;
3672 size_t capacity = bytes1 + bytes2;
3673 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003674 uint8_t *output_buffer = NULL;
3675 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003676 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003677 size_t length;
3678
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003679 ASSERT_ALLOC( output_buffer, capacity );
3680 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003682
Gilles Peskine8817f612018-12-18 00:18:46 +01003683 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3684 PSA_BYTES_TO_BITS( key_data->len ),
3685 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003686 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003687 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3688 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3689 key_data->x,
3690 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003691
3692 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003693 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3694 salt->x, salt->len,
3695 label->x, label->len,
3696 capacity ) );
3697 PSA_ASSERT( psa_generator_read( &generator,
3698 output_buffer,
3699 capacity ) );
3700 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003701
3702 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003703 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3704 salt->x, salt->len,
3705 label->x, label->len,
3706 capacity ) );
3707 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3708 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003709 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003710 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3711 PSA_ASSERT( psa_generator_import_key( derived_handle,
3712 PSA_KEY_TYPE_RAW_DATA,
3713 derived_bits,
3714 &generator ) );
3715 PSA_ASSERT( psa_export_key( derived_handle,
3716 export_buffer, bytes1,
3717 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003718 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003719 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3720 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3721 PSA_BYTES_TO_BITS( bytes2 ),
3722 &derived_handle ) );
3723 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3724 PSA_ASSERT( psa_generator_import_key( derived_handle,
3725 PSA_KEY_TYPE_RAW_DATA,
3726 PSA_BYTES_TO_BITS( bytes2 ),
3727 &generator ) );
3728 PSA_ASSERT( psa_export_key( derived_handle,
3729 export_buffer + bytes1, bytes2,
3730 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003731 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003732
3733 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003734 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3735 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003736
3737exit:
3738 mbedtls_free( output_buffer );
3739 mbedtls_free( export_buffer );
3740 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003741 psa_destroy_key( base_handle );
3742 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003743 mbedtls_psa_crypto_free( );
3744}
3745/* END_CASE */
3746
3747/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003748void key_agreement_setup( int alg_arg,
3749 int our_key_type_arg, data_t *our_key_data,
3750 data_t *peer_key_data,
3751 int expected_status_arg )
3752{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003753 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003754 psa_algorithm_t alg = alg_arg;
3755 psa_key_type_t our_key_type = our_key_type_arg;
3756 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003757 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003758
Gilles Peskine8817f612018-12-18 00:18:46 +01003759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003760
Gilles Peskine8817f612018-12-18 00:18:46 +01003761 PSA_ASSERT( psa_allocate_key( our_key_type,
3762 KEY_BITS_FROM_DATA( our_key_type,
3763 our_key_data ),
3764 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003765 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003766 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3767 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3768 our_key_data->x,
3769 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003770
Gilles Peskinefe11b722018-12-18 00:24:04 +01003771 TEST_EQUAL( psa_key_agreement( &generator,
3772 our_key,
3773 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003774 alg ),
3775 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003776
3777exit:
3778 psa_generator_abort( &generator );
3779 psa_destroy_key( our_key );
3780 mbedtls_psa_crypto_free( );
3781}
3782/* END_CASE */
3783
3784/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003785void key_agreement_capacity( int alg_arg,
3786 int our_key_type_arg, data_t *our_key_data,
3787 data_t *peer_key_data,
3788 int expected_capacity_arg )
3789{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003790 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003791 psa_algorithm_t alg = alg_arg;
3792 psa_key_type_t our_key_type = our_key_type_arg;
3793 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003794 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003795 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003796 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003797
Gilles Peskine8817f612018-12-18 00:18:46 +01003798 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003799
Gilles Peskine8817f612018-12-18 00:18:46 +01003800 PSA_ASSERT( psa_allocate_key( our_key_type,
3801 KEY_BITS_FROM_DATA( our_key_type,
3802 our_key_data ),
3803 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003804 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003805 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3806 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3807 our_key_data->x,
3808 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003809
Gilles Peskine8817f612018-12-18 00:18:46 +01003810 PSA_ASSERT( psa_key_agreement( &generator,
3811 our_key,
3812 peer_key_data->x, peer_key_data->len,
3813 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003814
Gilles Peskinebf491972018-10-25 22:36:12 +02003815 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003816 PSA_ASSERT( psa_get_generator_capacity(
3817 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003818 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003819
Gilles Peskinebf491972018-10-25 22:36:12 +02003820 /* Test the actual capacity by reading the output. */
3821 while( actual_capacity > sizeof( output ) )
3822 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_generator_read( &generator,
3824 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003825 actual_capacity -= sizeof( output );
3826 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003827 PSA_ASSERT( psa_generator_read( &generator,
3828 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003829 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3830 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003831
Gilles Peskine59685592018-09-18 12:11:34 +02003832exit:
3833 psa_generator_abort( &generator );
3834 psa_destroy_key( our_key );
3835 mbedtls_psa_crypto_free( );
3836}
3837/* END_CASE */
3838
3839/* BEGIN_CASE */
3840void key_agreement_output( int alg_arg,
3841 int our_key_type_arg, data_t *our_key_data,
3842 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003843 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003844{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003845 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003846 psa_algorithm_t alg = alg_arg;
3847 psa_key_type_t our_key_type = our_key_type_arg;
3848 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003849 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003850 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003851
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003852 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3853 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003854
Gilles Peskine8817f612018-12-18 00:18:46 +01003855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003856
Gilles Peskine8817f612018-12-18 00:18:46 +01003857 PSA_ASSERT( psa_allocate_key( our_key_type,
3858 KEY_BITS_FROM_DATA( our_key_type,
3859 our_key_data ),
3860 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003861 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003862 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3863 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3864 our_key_data->x,
3865 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003866
Gilles Peskine8817f612018-12-18 00:18:46 +01003867 PSA_ASSERT( psa_key_agreement( &generator,
3868 our_key,
3869 peer_key_data->x, peer_key_data->len,
3870 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003871
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003872 PSA_ASSERT( psa_generator_read( &generator,
3873 actual_output,
3874 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003875 ASSERT_COMPARE( actual_output, expected_output1->len,
3876 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003877 if( expected_output2->len != 0 )
3878 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003879 PSA_ASSERT( psa_generator_read( &generator,
3880 actual_output,
3881 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003882 ASSERT_COMPARE( actual_output, expected_output2->len,
3883 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003884 }
Gilles Peskine59685592018-09-18 12:11:34 +02003885
3886exit:
3887 psa_generator_abort( &generator );
3888 psa_destroy_key( our_key );
3889 mbedtls_psa_crypto_free( );
3890 mbedtls_free( actual_output );
3891}
3892/* END_CASE */
3893
3894/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003895void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003896{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003897 size_t bytes = bytes_arg;
3898 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003899 unsigned char *output = NULL;
3900 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003901 size_t i;
3902 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003903
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003904 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3905 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003906 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003907
Gilles Peskine8817f612018-12-18 00:18:46 +01003908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003909
Gilles Peskinea50d7392018-06-21 10:22:13 +02003910 /* Run several times, to ensure that every output byte will be
3911 * nonzero at least once with overwhelming probability
3912 * (2^(-8*number_of_runs)). */
3913 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003914 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003915 if( bytes != 0 )
3916 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003917 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003918
3919 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003920 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3921 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003922
3923 for( i = 0; i < bytes; i++ )
3924 {
3925 if( output[i] != 0 )
3926 ++changed[i];
3927 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003928 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003929
3930 /* Check that every byte was changed to nonzero at least once. This
3931 * validates that psa_generate_random is overwriting every byte of
3932 * the output buffer. */
3933 for( i = 0; i < bytes; i++ )
3934 {
3935 TEST_ASSERT( changed[i] != 0 );
3936 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003937
3938exit:
3939 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003940 mbedtls_free( output );
3941 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003942}
3943/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003944
3945/* BEGIN_CASE */
3946void generate_key( int type_arg,
3947 int bits_arg,
3948 int usage_arg,
3949 int alg_arg,
3950 int expected_status_arg )
3951{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003952 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003953 psa_key_type_t type = type_arg;
3954 psa_key_usage_t usage = usage_arg;
3955 size_t bits = bits_arg;
3956 psa_algorithm_t alg = alg_arg;
3957 psa_status_t expected_status = expected_status_arg;
3958 psa_key_type_t got_type;
3959 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003960 psa_status_t expected_info_status =
3961 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003962 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003963
Gilles Peskine8817f612018-12-18 00:18:46 +01003964 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003965
Gilles Peskine8817f612018-12-18 00:18:46 +01003966 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003967 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003968 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003969
3970 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003971 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3972 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003973
3974 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003975 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3976 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003977 if( expected_info_status != PSA_SUCCESS )
3978 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003979 TEST_EQUAL( got_type, type );
3980 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003981
Gilles Peskine818ca122018-06-20 18:16:48 +02003982 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003983 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02003984 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003985
3986exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003987 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003988 mbedtls_psa_crypto_free( );
3989}
3990/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003991
Darryl Greend49a4992018-06-18 17:27:26 +01003992/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
3993void persistent_key_load_key_from_storage( data_t *data, int type_arg,
3994 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00003995 int alg_arg, int generation_method,
3996 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01003997{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003998 psa_key_handle_t handle = 0;
3999 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004000 psa_key_type_t type = (psa_key_type_t) type_arg;
4001 psa_key_type_t type_get;
4002 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004003 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4004 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004005 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4006 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004007 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004008 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4009 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004010 unsigned char *first_export = NULL;
4011 unsigned char *second_export = NULL;
4012 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4013 size_t first_exported_length;
4014 size_t second_exported_length;
4015
4016 ASSERT_ALLOC( first_export, export_size );
4017 ASSERT_ALLOC( second_export, export_size );
4018
Gilles Peskine8817f612018-12-18 00:18:46 +01004019 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004020
Gilles Peskine8817f612018-12-18 00:18:46 +01004021 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4022 type, bits,
4023 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004024 psa_key_policy_set_usage( &policy_set, policy_usage,
4025 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004026 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004027
Darryl Green0c6575a2018-11-07 16:05:30 +00004028 switch( generation_method )
4029 {
4030 case IMPORT_KEY:
4031 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004032 PSA_ASSERT( psa_import_key( handle, type,
4033 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004034 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004035
Darryl Green0c6575a2018-11-07 16:05:30 +00004036 case GENERATE_KEY:
4037 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004038 PSA_ASSERT( psa_generate_key( handle, type, bits,
4039 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004040 break;
4041
4042 case DERIVE_KEY:
4043 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4045 PSA_BYTES_TO_BITS( data->len ),
4046 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004047 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4048 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004049 PSA_ASSERT( psa_set_key_policy(
4050 base_key, &base_policy_set ) );
4051 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4052 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004053 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004054 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4055 base_policy_alg,
4056 NULL, 0, NULL, 0,
4057 export_size ) );
4058 PSA_ASSERT( psa_generator_import_key(
4059 handle, PSA_KEY_TYPE_RAW_DATA,
4060 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004061 break;
4062 }
Darryl Greend49a4992018-06-18 17:27:26 +01004063
4064 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004065 TEST_EQUAL( psa_export_key( handle,
4066 first_export, export_size,
4067 &first_exported_length ),
4068 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004069
4070 /* Shutdown and restart */
4071 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004072 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004073
Darryl Greend49a4992018-06-18 17:27:26 +01004074 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004075 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4076 &handle ) );
4077 PSA_ASSERT( psa_get_key_information(
4078 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004079 TEST_EQUAL( type_get, type );
4080 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004081
Gilles Peskine8817f612018-12-18 00:18:46 +01004082 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004083 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4084 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004085
4086 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004087 TEST_EQUAL( psa_export_key( handle,
4088 second_export, export_size,
4089 &second_exported_length ),
4090 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004091
Darryl Green0c6575a2018-11-07 16:05:30 +00004092 if( export_status == PSA_SUCCESS )
4093 {
4094 ASSERT_COMPARE( first_export, first_exported_length,
4095 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004096
Darryl Green0c6575a2018-11-07 16:05:30 +00004097 switch( generation_method )
4098 {
4099 case IMPORT_KEY:
4100 ASSERT_COMPARE( data->x, data->len,
4101 first_export, first_exported_length );
4102 break;
4103 default:
4104 break;
4105 }
4106 }
4107
4108 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004109 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004110 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004111
4112exit:
4113 mbedtls_free( first_export );
4114 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004115 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004116 mbedtls_psa_crypto_free();
4117}
4118/* END_CASE */