blob: 5358799641a35e16ecdfdc6715024de76e1c61cf [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinea7aa4422018-08-14 15:17:54 +020017/** Test if a buffer contains a constant byte value.
18 *
19 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020 *
21 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020028static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine0b352bc2018-06-28 00:16:11 +020039/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
40static int asn1_write_10x( unsigned char **p,
41 unsigned char *start,
42 size_t bits,
43 unsigned char x )
44{
45 int ret;
46 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020047 if( bits == 0 )
48 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
49 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020050 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030051 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
53 *p -= len;
54 ( *p )[len-1] = x;
55 if( bits % 8 == 0 )
56 ( *p )[1] |= 1;
57 else
58 ( *p )[0] |= 1 << ( bits % 8 );
59 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
60 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
61 MBEDTLS_ASN1_INTEGER ) );
62 return( len );
63}
64
65static int construct_fake_rsa_key( unsigned char *buffer,
66 size_t buffer_size,
67 unsigned char **p,
68 size_t bits,
69 int keypair )
70{
71 size_t half_bits = ( bits + 1 ) / 2;
72 int ret;
73 int len = 0;
74 /* Construct something that looks like a DER encoding of
75 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
76 * RSAPrivateKey ::= SEQUENCE {
77 * version Version,
78 * modulus INTEGER, -- n
79 * publicExponent INTEGER, -- e
80 * privateExponent INTEGER, -- d
81 * prime1 INTEGER, -- p
82 * prime2 INTEGER, -- q
83 * exponent1 INTEGER, -- d mod (p-1)
84 * exponent2 INTEGER, -- d mod (q-1)
85 * coefficient INTEGER, -- (inverse of q) mod p
86 * otherPrimeInfos OtherPrimeInfos OPTIONAL
87 * }
88 * Or, for a public key, the same structure with only
89 * version, modulus and publicExponent.
90 */
91 *p = buffer + buffer_size;
92 if( keypair )
93 {
94 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
95 asn1_write_10x( p, buffer, half_bits, 1 ) );
96 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
97 asn1_write_10x( p, buffer, half_bits, 1 ) );
98 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
99 asn1_write_10x( p, buffer, half_bits, 1 ) );
100 MBEDTLS_ASN1_CHK_ADD( len, /* q */
101 asn1_write_10x( p, buffer, half_bits, 1 ) );
102 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
103 asn1_write_10x( p, buffer, half_bits, 3 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* d */
105 asn1_write_10x( p, buffer, bits, 1 ) );
106 }
107 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
108 asn1_write_10x( p, buffer, 17, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* n */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 if( keypair )
112 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
113 mbedtls_asn1_write_int( p, buffer, 0 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
115 {
116 const unsigned char tag =
117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
118 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
119 }
120 return( len );
121}
122
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100123static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200124 psa_key_usage_t usage,
125 psa_algorithm_t alg )
126{
127 psa_mac_operation_t operation;
128 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200129 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200130 size_t mac_length = sizeof( mac );
131
132 if( usage & PSA_KEY_USAGE_SIGN )
133 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100134 PSA_ASSERT( psa_mac_sign_setup( &operation,
135 handle, alg ) );
136 PSA_ASSERT( psa_mac_update( &operation,
137 input, sizeof( input ) ) );
138 PSA_ASSERT( psa_mac_sign_finish( &operation,
139 mac, sizeof( mac ),
140 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 }
142
143 if( usage & PSA_KEY_USAGE_VERIFY )
144 {
145 psa_status_t verify_status =
146 ( usage & PSA_KEY_USAGE_SIGN ?
147 PSA_SUCCESS :
148 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100149 PSA_ASSERT( psa_mac_verify_setup( &operation,
150 handle, alg ) );
151 PSA_ASSERT( psa_mac_update( &operation,
152 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100153 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
154 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200155 }
156
157 return( 1 );
158
159exit:
160 psa_mac_abort( &operation );
161 return( 0 );
162}
163
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100164static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 psa_key_usage_t usage,
166 psa_algorithm_t alg )
167{
168 psa_cipher_operation_t operation;
169 unsigned char iv[16] = {0};
170 size_t iv_length = sizeof( iv );
171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
177 if( usage & PSA_KEY_USAGE_ENCRYPT )
178 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100179 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
180 handle, alg ) );
181 PSA_ASSERT( psa_cipher_generate_iv( &operation,
182 iv, sizeof( iv ),
183 &iv_length ) );
184 PSA_ASSERT( psa_cipher_update( &operation,
185 plaintext, sizeof( plaintext ),
186 ciphertext, sizeof( ciphertext ),
187 &ciphertext_length ) );
188 PSA_ASSERT( psa_cipher_finish( &operation,
189 ciphertext + ciphertext_length,
190 sizeof( ciphertext ) - ciphertext_length,
191 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 ciphertext_length += part_length;
193 }
194
195 if( usage & PSA_KEY_USAGE_DECRYPT )
196 {
197 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700198 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
200 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100202 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200203 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
204 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100205 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
206 handle, alg ) );
207 PSA_ASSERT( psa_cipher_set_iv( &operation,
208 iv, iv_length ) );
209 PSA_ASSERT( psa_cipher_update( &operation,
210 ciphertext, ciphertext_length,
211 decrypted, sizeof( decrypted ),
212 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200213 status = psa_cipher_finish( &operation,
214 decrypted + part_length,
215 sizeof( decrypted ) - part_length,
216 &part_length );
217 /* For a stream cipher, all inputs are valid. For a block cipher,
218 * if the input is some aribtrary data rather than an actual
219 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700220 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700221 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100222 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200223 else
224 TEST_ASSERT( status == PSA_SUCCESS ||
225 status == PSA_ERROR_INVALID_PADDING );
226 }
227
228 return( 1 );
229
230exit:
231 psa_cipher_abort( &operation );
232 return( 0 );
233}
234
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100235static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200236 psa_key_usage_t usage,
237 psa_algorithm_t alg )
238{
239 unsigned char nonce[16] = {0};
240 size_t nonce_length = sizeof( nonce );
241 unsigned char plaintext[16] = "Hello, world...";
242 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
243 size_t ciphertext_length = sizeof( ciphertext );
244 size_t plaintext_length = sizeof( ciphertext );
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100248 PSA_ASSERT( psa_aead_encrypt( handle, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100262 TEST_EQUAL( psa_aead_decrypt( handle, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100267 &plaintext_length ),
268 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100277static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200283 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 size_t signature_length = sizeof( signature );
285
286 if( usage & PSA_KEY_USAGE_SIGN )
287 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200288 /* Some algorithms require the payload to have the size of
289 * the hash encoded in the algorithm. Use this input size
290 * even for algorithms that allow other input sizes. */
291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
292 if( hash_alg != 0 )
293 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100294 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
295 payload, payload_length,
296 signature, sizeof( signature ),
297 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100306 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
307 payload, payload_length,
308 signature, signature_length ),
309 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200310 }
311
312 return( 1 );
313
314exit:
315 return( 0 );
316}
317
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100318static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200319 psa_key_usage_t usage,
320 psa_algorithm_t alg )
321{
322 unsigned char plaintext[256] = "Hello, world...";
323 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 size_t plaintext_length = 16;
326
327 if( usage & PSA_KEY_USAGE_ENCRYPT )
328 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100329 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100339 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100356static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361 unsigned char label[16] = "This is a label.";
362 size_t label_length = sizeof( label );
363 unsigned char seed[16] = "abcdefghijklmnop";
364 size_t seed_length = sizeof( seed );
365 unsigned char output[1];
366
367 if( usage & PSA_KEY_USAGE_DERIVE )
368 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100369 PSA_ASSERT( psa_key_derivation( &generator,
370 handle, alg,
371 label, label_length,
372 seed, seed_length,
373 sizeof( output ) ) );
374 PSA_ASSERT( psa_generator_read( &generator,
375 output,
376 sizeof( output ) ) );
377 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200378 }
379
380 return( 1 );
381
382exit:
383 return( 0 );
384}
385
Gilles Peskinec7998b72018-11-07 18:45:02 +0100386/* We need two keys to exercise key agreement. Exercise the
387 * private key against its own public key. */
388static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100390 psa_algorithm_t alg )
391{
392 psa_key_type_t private_key_type;
393 psa_key_type_t public_key_type;
394 size_t key_bits;
395 uint8_t *public_key = NULL;
396 size_t public_key_length;
397 /* Return UNKNOWN_ERROR if something other than the final call to
398 * psa_key_agreement fails. This isn't fully satisfactory, but it's
399 * good enough: callers will report it as a failed test anyway. */
400 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
401
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_get_key_information( handle,
403 &private_key_type,
404 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
406 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
407 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100408 PSA_ASSERT( psa_export_public_key( handle,
409 public_key, public_key_length,
410 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100412 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key, public_key_length,
414 alg );
415exit:
416 mbedtls_free( public_key );
417 return( status );
418}
419
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 psa_key_usage_t usage,
422 psa_algorithm_t alg )
423{
424 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200425 unsigned char output[1];
426 int ok = 0;
427
428 if( usage & PSA_KEY_USAGE_DERIVE )
429 {
430 /* We need two keys to exercise key agreement. Exercise the
431 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100432 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
433 PSA_ASSERT( psa_generator_read( &generator,
434 output,
435 sizeof( output ) ) );
436 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 }
438 ok = 1;
439
440exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200441 return( ok );
442}
443
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200444static int is_oid_of_key_type( psa_key_type_t type,
445 const uint8_t *oid, size_t oid_length )
446{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200447 const uint8_t *expected_oid = NULL;
448 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200449#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200450 if( PSA_KEY_TYPE_IS_RSA( type ) )
451 {
452 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
453 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
454 }
455 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200456#endif /* MBEDTLS_RSA_C */
457#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 if( PSA_KEY_TYPE_IS_ECC( type ) )
459 {
460 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
461 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
462 }
463 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200464#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465 {
466 char message[40];
467 mbedtls_snprintf( message, sizeof( message ),
468 "OID not known for key type=0x%08lx",
469 (unsigned long) type );
470 test_fail( message, __LINE__, __FILE__ );
471 return( 0 );
472 }
473
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200474 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
481static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
482 size_t min_bits, size_t max_bits,
483 int must_be_odd )
484{
485 size_t len;
486 size_t actual_bits;
487 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100488 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100489 MBEDTLS_ASN1_INTEGER ),
490 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 /* Tolerate a slight departure from DER encoding:
492 * - 0 may be represented by an empty string or a 1-byte string.
493 * - The sign bit may be used as a value bit. */
494 if( ( len == 1 && ( *p )[0] == 0 ) ||
495 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
496 {
497 ++( *p );
498 --len;
499 }
500 if( min_bits == 0 && len == 0 )
501 return( 1 );
502 msb = ( *p )[0];
503 TEST_ASSERT( msb != 0 );
504 actual_bits = 8 * ( len - 1 );
505 while( msb != 0 )
506 {
507 msb >>= 1;
508 ++actual_bits;
509 }
510 TEST_ASSERT( actual_bits >= min_bits );
511 TEST_ASSERT( actual_bits <= max_bits );
512 if( must_be_odd )
513 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
514 *p += len;
515 return( 1 );
516exit:
517 return( 0 );
518}
519
520static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
521 size_t *len,
522 unsigned char n, unsigned char tag )
523{
524 int ret;
525 ret = mbedtls_asn1_get_tag( p, end, len,
526 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
528 if( ret != 0 )
529 return( ret );
530 end = *p + *len;
531 ret = mbedtls_asn1_get_tag( p, end, len, tag );
532 if( ret != 0 )
533 return( ret );
534 if( *p + *len != end )
535 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
536 return( 0 );
537}
538
539static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
540 uint8_t *exported, size_t exported_length )
541{
542 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100543 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200544 else
545 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200546
547#if defined(MBEDTLS_DES_C)
548 if( type == PSA_KEY_TYPE_DES )
549 {
550 /* Check the parity bits. */
551 unsigned i;
552 for( i = 0; i < bits / 8; i++ )
553 {
554 unsigned bit_count = 0;
555 unsigned m;
556 for( m = 1; m <= 0x100; m <<= 1 )
557 {
558 if( exported[i] & m )
559 ++bit_count;
560 }
561 TEST_ASSERT( bit_count % 2 != 0 );
562 }
563 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200565#endif
566
567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
568 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
569 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 uint8_t *p = exported;
571 uint8_t *end = exported + exported_length;
572 size_t len;
573 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200574 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 * modulus INTEGER, -- n
576 * publicExponent INTEGER, -- e
577 * privateExponent INTEGER, -- d
578 * prime1 INTEGER, -- p
579 * prime2 INTEGER, -- q
580 * exponent1 INTEGER, -- d mod (p-1)
581 * exponent2 INTEGER, -- d mod (q-1)
582 * coefficient INTEGER, -- (inverse of q) mod p
583 * }
584 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100585 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
586 MBEDTLS_ASN1_SEQUENCE |
587 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
588 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
590 goto exit;
591 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
592 goto exit;
593 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
594 goto exit;
595 /* Require d to be at least half the size of n. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
597 goto exit;
598 /* Require p and q to be at most half the size of n, rounded up. */
599 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
606 goto exit;
607 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100609 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100610 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200611 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200612#endif /* MBEDTLS_RSA_C */
613
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
616 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100617 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_ECP_C */
622
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
624 {
625 uint8_t *p = exported;
626 uint8_t *end = exported + exported_length;
627 size_t len;
628 mbedtls_asn1_buf alg;
629 mbedtls_asn1_buf params;
630 mbedtls_asn1_bitstring bitstring;
631 /* SubjectPublicKeyInfo ::= SEQUENCE {
632 * algorithm AlgorithmIdentifier,
633 * subjectPublicKey BIT STRING }
634 * AlgorithmIdentifier ::= SEQUENCE {
635 * algorithm OBJECT IDENTIFIER,
636 * parameters ANY DEFINED BY algorithm OPTIONAL }
637 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100638 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
639 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100640 MBEDTLS_ASN1_CONSTRUCTED ),
641 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100642 TEST_EQUAL( p + len, end );
643 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200644 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
645 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100646 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
647 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200648 p = bitstring.p;
649#if defined(MBEDTLS_RSA_C)
650 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
651 {
652 /* RSAPublicKey ::= SEQUENCE {
653 * modulus INTEGER, -- n
654 * publicExponent INTEGER } -- e
655 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100656 TEST_EQUAL( bitstring.unused_bits, 0 );
657 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
658 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100659 MBEDTLS_ASN1_CONSTRUCTED ),
660 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100661 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200662 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
663 goto exit;
664 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
665 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100666 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200667 }
668 else
669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
671 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
672 {
673 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200674 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200675 * -- then x_P as an n-bit string, big endian;
676 * -- then y_P as a n-bit string, big endian,
677 * -- where n is the order of the curve.
678 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100679 TEST_EQUAL( bitstring.unused_bits, 0 );
680 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
681 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 }
683 else
684#endif /* MBEDTLS_ECP_C */
685 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100686 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200687 mbedtls_snprintf( message, sizeof( message ),
688 "No sanity check for public key type=0x%08lx",
689 (unsigned long) type );
690 test_fail( message, __LINE__, __FILE__ );
691 return( 0 );
692 }
693 }
694 else
695
696 {
697 /* No sanity checks for other types */
698 }
699
700 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200701
702exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200703 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200704}
705
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100706static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 psa_key_usage_t usage )
708{
709 psa_key_type_t type;
710 size_t bits;
711 uint8_t *exported = NULL;
712 size_t exported_size = 0;
713 size_t exported_length = 0;
714 int ok = 0;
715
Gilles Peskine8817f612018-12-18 00:18:46 +0100716 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200717
718 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
719 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200720 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100721 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
722 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723 return( 1 );
724 }
725
Gilles Peskined14664a2018-08-10 19:07:32 +0200726 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200727 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200728
Gilles Peskine8817f612018-12-18 00:18:46 +0100729 PSA_ASSERT( psa_export_key( handle,
730 exported, exported_size,
731 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200732 ok = exported_key_sanity_check( type, bits, exported, exported_length );
733
734exit:
735 mbedtls_free( exported );
736 return( ok );
737}
738
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100739static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200740{
741 psa_key_type_t type;
742 psa_key_type_t public_type;
743 size_t bits;
744 uint8_t *exported = NULL;
745 size_t exported_size = 0;
746 size_t exported_length = 0;
747 int ok = 0;
748
Gilles Peskine8817f612018-12-18 00:18:46 +0100749 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200750 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
751 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100752 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100753 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200754 return( 1 );
755 }
756
757 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
758 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200759 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200760
Gilles Peskine8817f612018-12-18 00:18:46 +0100761 PSA_ASSERT( psa_export_public_key( handle,
762 exported, exported_size,
763 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200764 ok = exported_key_sanity_check( public_type, bits,
765 exported, exported_length );
766
767exit:
768 mbedtls_free( exported );
769 return( ok );
770}
771
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100772static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 psa_key_usage_t usage,
774 psa_algorithm_t alg )
775{
776 int ok;
777 if( alg == 0 )
778 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
779 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200781 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100782 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100784 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200785 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100786 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200787 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100788 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200789 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200791 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100792 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200793 else
794 {
795 char message[40];
796 mbedtls_snprintf( message, sizeof( message ),
797 "No code to exercise alg=0x%08lx",
798 (unsigned long) alg );
799 test_fail( message, __LINE__, __FILE__ );
800 ok = 0;
801 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200802
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100803 ok = ok && exercise_export_key( handle, usage );
804 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200805
Gilles Peskine02b75072018-07-01 22:31:34 +0200806 return( ok );
807}
808
Gilles Peskine10df3412018-10-25 22:35:43 +0200809static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
810 psa_algorithm_t alg )
811{
812 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
813 {
814 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
815 PSA_KEY_USAGE_VERIFY :
816 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
817 }
818 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
819 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
820 {
821 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
822 PSA_KEY_USAGE_ENCRYPT :
823 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
824 }
825 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
826 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
827 {
828 return( PSA_KEY_USAGE_DERIVE );
829 }
830 else
831 {
832 return( 0 );
833 }
834
835}
Darryl Green0c6575a2018-11-07 16:05:30 +0000836
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100837/* An overapproximation of the amount of storage needed for a key of the
838 * given type and with the given content. The API doesn't make it easy
839 * to find a good value for the size. The current implementation doesn't
840 * care about the value anyway. */
841#define KEY_BITS_FROM_DATA( type, data ) \
842 ( data )->len
843
Darryl Green0c6575a2018-11-07 16:05:30 +0000844typedef enum {
845 IMPORT_KEY = 0,
846 GENERATE_KEY = 1,
847 DERIVE_KEY = 2
848} generate_method;
849
Gilles Peskinee59236f2018-01-27 23:32:46 +0100850/* END_HEADER */
851
852/* BEGIN_DEPENDENCIES
853 * depends_on:MBEDTLS_PSA_CRYPTO_C
854 * END_DEPENDENCIES
855 */
856
857/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200858void static_checks( )
859{
860 size_t max_truncated_mac_size =
861 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
862
863 /* Check that the length for a truncated MAC always fits in the algorithm
864 * encoding. The shifted mask is the maximum truncated value. The
865 * untruncated algorithm may be one byte larger. */
866 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
867}
868/* END_CASE */
869
870/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100873 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200874 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
Gilles Peskine8817f612018-12-18 00:18:46 +0100877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
880 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100881 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100882 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100884 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885
886exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 mbedtls_psa_crypto_free( );
888}
889/* END_CASE */
890
891/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100892void import_twice( int alg_arg, int usage_arg,
893 int type1_arg, data_t *data1,
894 int expected_import1_status_arg,
895 int type2_arg, data_t *data2,
896 int expected_import2_status_arg )
897{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100898 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100899 psa_algorithm_t alg = alg_arg;
900 psa_key_usage_t usage = usage_arg;
901 psa_key_type_t type1 = type1_arg;
902 psa_status_t expected_import1_status = expected_import1_status_arg;
903 psa_key_type_t type2 = type2_arg;
904 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000905 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100906 psa_status_t status;
907
Gilles Peskine8817f612018-12-18 00:18:46 +0100908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100909
Gilles Peskine8817f612018-12-18 00:18:46 +0100910 PSA_ASSERT( psa_allocate_key( type1,
911 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
912 KEY_BITS_FROM_DATA( type2, data2 ) ),
913 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100914 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100915 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100916
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100917 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100918 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100919 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100921
922 if( expected_import1_status == PSA_SUCCESS ||
923 expected_import2_status == PSA_SUCCESS )
924 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100926 }
927
928exit:
929 mbedtls_psa_crypto_free( );
930}
931/* END_CASE */
932
933/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200934void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
935{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100936 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200937 size_t bits = bits_arg;
938 psa_status_t expected_status = expected_status_arg;
939 psa_status_t status;
940 psa_key_type_t type =
941 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
942 size_t buffer_size = /* Slight overapproximations */
943 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200944 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200945 unsigned char *p;
946 int ret;
947 size_t length;
948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200950 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200951
952 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
953 bits, keypair ) ) >= 0 );
954 length = ret;
955
956 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100957 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100958 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200960 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100961 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200962
963exit:
964 mbedtls_free( buffer );
965 mbedtls_psa_crypto_free( );
966}
967/* END_CASE */
968
969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300970void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300971 int type_arg,
972 int alg_arg,
973 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 int expected_bits,
975 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200976 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 int canonical_input )
978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100979 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200981 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200982 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 unsigned char *exported = NULL;
985 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100987 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 size_t reexported_length;
989 psa_key_type_t got_type;
990 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000991 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
Moran Pekercb088e72018-07-17 17:36:59 +0300993 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200994 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200996 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998
Gilles Peskine8817f612018-12-18 00:18:46 +0100999 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001000 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001001 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001002
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001003 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1004 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001005
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001007 PSA_ASSERT( psa_import_key( handle, type,
1008 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009
1010 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001011 PSA_ASSERT( psa_get_key_information( handle,
1012 &got_type,
1013 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001014 TEST_EQUAL( got_type, type );
1015 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016
1017 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001018 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019 exported, export_size,
1020 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001022
1023 /* The exported length must be set by psa_export_key() to a value between 0
1024 * and export_size. On errors, the exported length must be 0. */
1025 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1026 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1027 TEST_ASSERT( exported_length <= export_size );
1028
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001029 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001030 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001032 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001035 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001038 goto exit;
1039
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001041 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 else
1043 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001045 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1046 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001047
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 PSA_ASSERT( psa_import_key( handle2, type,
1049 exported,
1050 exported_length ) );
1051 PSA_ASSERT( psa_export_key( handle2,
1052 reexported,
1053 export_size,
1054 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001055 ASSERT_COMPARE( exported, exported_length,
1056 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001057 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001059 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060
1061destroy:
1062 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001063 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001064 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1065 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066
1067exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001068 mbedtls_free( exported );
1069 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070 mbedtls_psa_crypto_free( );
1071}
1072/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001073
Moran Pekerf709f4a2018-06-06 17:26:04 +03001074/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001075void import_key_nonempty_slot( )
1076{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001077 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001078 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1079 psa_status_t status;
1080 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001081 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001082
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1084 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001085
Moran Peker28a38e62018-11-07 16:18:24 +02001086 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001087 PSA_ASSERT( psa_import_key( handle, type,
1088 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001089
1090 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001091 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001092 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001093
1094exit:
1095 mbedtls_psa_crypto_free( );
1096}
1097/* END_CASE */
1098
1099/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001101{
1102 psa_status_t status;
1103 unsigned char *exported = NULL;
1104 size_t export_size = 0;
1105 size_t exported_length = INVALID_EXPORT_LENGTH;
1106 psa_status_t expected_export_status = expected_export_status_arg;
1107
Gilles Peskine8817f612018-12-18 00:18:46 +01001108 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001109
1110 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001111 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001112 exported, export_size,
1113 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001114 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001115
1116exit:
1117 mbedtls_psa_crypto_free( );
1118}
1119/* END_CASE */
1120
1121/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001122void export_with_no_key_activity( )
1123{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001125 psa_algorithm_t alg = PSA_ALG_CTR;
1126 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001127 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001128 unsigned char *exported = NULL;
1129 size_t export_size = 0;
1130 size_t exported_length = INVALID_EXPORT_LENGTH;
1131
Gilles Peskine8817f612018-12-18 00:18:46 +01001132 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1135 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001136 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001137 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001138
1139 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001140 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001141 exported, export_size,
1142 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001143 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001144
1145exit:
1146 mbedtls_psa_crypto_free( );
1147}
1148/* END_CASE */
1149
1150/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001151void cipher_with_no_key_activity( )
1152{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001153 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001154 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001155 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001156 psa_cipher_operation_t operation;
1157 int exercise_alg = PSA_ALG_CTR;
1158
Gilles Peskine8817f612018-12-18 00:18:46 +01001159 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001160
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1162 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001163 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001165
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001166 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001167 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001168
1169exit:
1170 psa_cipher_abort( &operation );
1171 mbedtls_psa_crypto_free( );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001176void export_after_import_failure( data_t *data, int type_arg,
1177 int expected_import_status_arg )
1178{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001179 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001180 psa_key_type_t type = type_arg;
1181 psa_status_t status;
1182 unsigned char *exported = NULL;
1183 size_t export_size = 0;
1184 psa_status_t expected_import_status = expected_import_status_arg;
1185 size_t exported_length = INVALID_EXPORT_LENGTH;
1186
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001188
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1190 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001191
Moran Peker34550092018-11-07 16:19:34 +02001192 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001193 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001194 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001195 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001196
1197 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001198 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001199 exported, export_size,
1200 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001201 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001202
1203exit:
1204 mbedtls_psa_crypto_free( );
1205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001209void cipher_after_import_failure( data_t *data, int type_arg,
1210 int expected_import_status_arg )
1211{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001212 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001213 psa_cipher_operation_t operation;
1214 psa_key_type_t type = type_arg;
1215 psa_status_t status;
1216 psa_status_t expected_import_status = expected_import_status_arg;
1217 int exercise_alg = PSA_ALG_CTR;
1218
Gilles Peskine8817f612018-12-18 00:18:46 +01001219 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001220
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1222 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001223
Moran Pekerce500072018-11-07 16:20:07 +02001224 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001225 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001226 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001227 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001228
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001231
1232exit:
1233 psa_cipher_abort( &operation );
1234 mbedtls_psa_crypto_free( );
1235}
1236/* END_CASE */
1237
1238/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001239void export_after_destroy_key( data_t *data, int type_arg )
1240{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001241 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001242 psa_key_type_t type = type_arg;
1243 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001244 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001245 psa_algorithm_t alg = PSA_ALG_CTR;
1246 unsigned char *exported = NULL;
1247 size_t export_size = 0;
1248 size_t exported_length = INVALID_EXPORT_LENGTH;
1249
Gilles Peskine8817f612018-12-18 00:18:46 +01001250 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1253 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001254 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001256 export_size = (ptrdiff_t) data->len;
1257 ASSERT_ALLOC( exported, export_size );
1258
1259 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_import_key( handle, type,
1261 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001262
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1264 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001265
1266 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001267 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001268
1269 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001271 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001272 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001273
1274exit:
1275 mbedtls_free( exported );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001281void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001282 int type_arg,
1283 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001284 int export_size_delta,
1285 int expected_export_status_arg,
1286 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001288 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001289 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001290 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001291 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001293 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001294 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001295 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001296 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001297
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001299
Gilles Peskine8817f612018-12-18 00:18:46 +01001300 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1301 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001302 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001303 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001304
1305 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001306 PSA_ASSERT( psa_import_key( handle, type,
1307 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001308
Gilles Peskine49c25912018-10-29 15:15:31 +01001309 /* Export the public key */
1310 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001311 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001312 exported, export_size,
1313 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001314 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001315 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001316 {
1317 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1318 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001320 TEST_ASSERT( expected_public_key->len <=
1321 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001322 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1323 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001324 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001325
1326exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001327 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001328 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001329 mbedtls_psa_crypto_free( );
1330}
1331/* END_CASE */
1332
Gilles Peskine20035e32018-02-03 22:44:14 +01001333/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001334void import_and_exercise_key( data_t *data,
1335 int type_arg,
1336 int bits_arg,
1337 int alg_arg )
1338{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001339 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001340 psa_key_type_t type = type_arg;
1341 size_t bits = bits_arg;
1342 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001343 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001344 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001345 psa_key_type_t got_type;
1346 size_t got_bits;
1347 psa_status_t status;
1348
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001350
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1352 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001354 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001355
1356 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001358 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359
1360 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001361 PSA_ASSERT( psa_get_key_information( handle,
1362 &got_type,
1363 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001364 TEST_EQUAL( got_type, type );
1365 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001366
1367 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001368 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001369 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001370
1371exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001372 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001373 mbedtls_psa_crypto_free( );
1374}
1375/* END_CASE */
1376
1377/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001378void key_policy( int usage_arg, int alg_arg )
1379{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001380 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001381 psa_algorithm_t alg = alg_arg;
1382 psa_key_usage_t usage = usage_arg;
1383 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1384 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001385 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1386 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001387
1388 memset( key, 0x2a, sizeof( key ) );
1389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001391
Gilles Peskine8817f612018-12-18 00:18:46 +01001392 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1393 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001394 psa_key_policy_set_usage( &policy_set, usage, alg );
1395
Gilles Peskinefe11b722018-12-18 00:24:04 +01001396 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1397 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001398 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001399
Gilles Peskine8817f612018-12-18 00:18:46 +01001400 PSA_ASSERT( psa_import_key( handle, key_type,
1401 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001402
Gilles Peskine8817f612018-12-18 00:18:46 +01001403 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001404
Gilles Peskinefe11b722018-12-18 00:24:04 +01001405 TEST_EQUAL( policy_get.usage, policy_set.usage );
1406 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001407
1408exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001409 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001410 mbedtls_psa_crypto_free( );
1411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001415void key_policy_init( )
1416{
1417 /* Test each valid way of initializing the object, except for `= {0}`, as
1418 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1419 * though it's OK by the C standard. We could test for this, but we'd need
1420 * to supress the Clang warning for the test. */
1421 psa_key_policy_t func = psa_key_policy_init( );
1422 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1423 psa_key_policy_t zero;
1424
1425 memset( &zero, 0, sizeof( zero ) );
1426
1427 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1428 * specification, we test that all valid ways of initializing the object
1429 * have the same bit pattern. This is a stronger requirement that may not
1430 * be valid on all platforms or PSA Crypto implementations, but implies the
1431 * weaker actual requirement is met: that a freshly initialized object, no
1432 * matter how it was initialized, acts the same as any other valid
1433 * initialization. */
1434 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1435 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440void mac_key_policy( int policy_usage,
1441 int policy_alg,
1442 int key_type,
1443 data_t *key_data,
1444 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001447 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448 psa_mac_operation_t operation;
1449 psa_status_t status;
1450 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001451
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001453
Gilles Peskine8817f612018-12-18 00:18:46 +01001454 PSA_ASSERT( psa_allocate_key( key_type,
1455 KEY_BITS_FROM_DATA( key_type, key_data ),
1456 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001457 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001458 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001459
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_import_key( handle, key_type,
1461 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001463 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464 if( policy_alg == exercise_alg &&
1465 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001468 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001470
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001472 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473 if( policy_alg == exercise_alg &&
1474 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001477 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478
1479exit:
1480 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001481 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 mbedtls_psa_crypto_free( );
1483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
1487void cipher_key_policy( int policy_usage,
1488 int policy_alg,
1489 int key_type,
1490 data_t *key_data,
1491 int exercise_alg )
1492{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001493 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001494 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495 psa_cipher_operation_t operation;
1496 psa_status_t status;
1497
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499
Gilles Peskine8817f612018-12-18 00:18:46 +01001500 PSA_ASSERT( psa_allocate_key( key_type,
1501 KEY_BITS_FROM_DATA( key_type, key_data ),
1502 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001503 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001504 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001505
Gilles Peskine8817f612018-12-18 00:18:46 +01001506 PSA_ASSERT( psa_import_key( handle, key_type,
1507 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001508
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001509 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001510 if( policy_alg == exercise_alg &&
1511 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001512 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001514 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001515 psa_cipher_abort( &operation );
1516
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001517 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001518 if( policy_alg == exercise_alg &&
1519 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001520 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001521 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001522 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001523
1524exit:
1525 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001526 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001527 mbedtls_psa_crypto_free( );
1528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
1532void aead_key_policy( int policy_usage,
1533 int policy_alg,
1534 int key_type,
1535 data_t *key_data,
1536 int nonce_length_arg,
1537 int tag_length_arg,
1538 int exercise_alg )
1539{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001540 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001541 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542 psa_status_t status;
1543 unsigned char nonce[16] = {0};
1544 size_t nonce_length = nonce_length_arg;
1545 unsigned char tag[16];
1546 size_t tag_length = tag_length_arg;
1547 size_t output_length;
1548
1549 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1550 TEST_ASSERT( tag_length <= sizeof( tag ) );
1551
Gilles Peskine8817f612018-12-18 00:18:46 +01001552 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_allocate_key( key_type,
1555 KEY_BITS_FROM_DATA( key_type, key_data ),
1556 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001557 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_import_key( handle, key_type,
1561 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 nonce, nonce_length,
1565 NULL, 0,
1566 NULL, 0,
1567 tag, tag_length,
1568 &output_length );
1569 if( policy_alg == exercise_alg &&
1570 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001573 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
1575 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001576 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 nonce, nonce_length,
1578 NULL, 0,
1579 tag, tag_length,
1580 NULL, 0,
1581 &output_length );
1582 if( policy_alg == exercise_alg &&
1583 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001584 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001585 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001586 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001587
1588exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001590 mbedtls_psa_crypto_free( );
1591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
1595void asymmetric_encryption_key_policy( int policy_usage,
1596 int policy_alg,
1597 int key_type,
1598 data_t *key_data,
1599 int exercise_alg )
1600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001602 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 psa_status_t status;
1604 size_t key_bits;
1605 size_t buffer_length;
1606 unsigned char *buffer = NULL;
1607 size_t output_length;
1608
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( psa_allocate_key( key_type,
1612 KEY_BITS_FROM_DATA( key_type, key_data ),
1613 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616
Gilles Peskine8817f612018-12-18 00:18:46 +01001617 PSA_ASSERT( psa_import_key( handle, key_type,
1618 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_get_key_information( handle,
1621 NULL,
1622 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1624 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001625 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 NULL, 0,
1629 NULL, 0,
1630 buffer, buffer_length,
1631 &output_length );
1632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001638 if( buffer_length != 0 )
1639 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 buffer, buffer_length,
1642 NULL, 0,
1643 buffer, buffer_length,
1644 &output_length );
1645 if( policy_alg == exercise_alg &&
1646 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001647 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650
1651exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001652 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 mbedtls_psa_crypto_free( );
1654 mbedtls_free( buffer );
1655}
1656/* END_CASE */
1657
1658/* BEGIN_CASE */
1659void asymmetric_signature_key_policy( int policy_usage,
1660 int policy_alg,
1661 int key_type,
1662 data_t *key_data,
1663 int exercise_alg )
1664{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001665 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001666 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 psa_status_t status;
1668 unsigned char payload[16] = {1};
1669 size_t payload_length = sizeof( payload );
1670 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1671 size_t signature_length;
1672
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674
Gilles Peskine8817f612018-12-18 00:18:46 +01001675 PSA_ASSERT( psa_allocate_key( key_type,
1676 KEY_BITS_FROM_DATA( key_type, key_data ),
1677 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_import_key( handle, key_type,
1682 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 signature, sizeof( signature ),
1687 &signature_length );
1688 if( policy_alg == exercise_alg &&
1689 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693
1694 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001695 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001697 signature, sizeof( signature ) );
1698 if( policy_alg == exercise_alg &&
1699 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001700 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001701 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001703
1704exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001705 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001706 mbedtls_psa_crypto_free( );
1707}
1708/* END_CASE */
1709
1710/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001711void derive_key_policy( int policy_usage,
1712 int policy_alg,
1713 int key_type,
1714 data_t *key_data,
1715 int exercise_alg )
1716{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001717 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001718 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001719 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1720 psa_status_t status;
1721
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001723
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_allocate_key( key_type,
1725 KEY_BITS_FROM_DATA( key_type, key_data ),
1726 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001727 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001728 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001729
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 PSA_ASSERT( psa_import_key( handle, key_type,
1731 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001732
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001734 exercise_alg,
1735 NULL, 0,
1736 NULL, 0,
1737 1 );
1738 if( policy_alg == exercise_alg &&
1739 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001740 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001741 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001742 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001743
1744exit:
1745 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001746 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001747 mbedtls_psa_crypto_free( );
1748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001752void agreement_key_policy( int policy_usage,
1753 int policy_alg,
1754 int key_type_arg,
1755 data_t *key_data,
1756 int exercise_alg )
1757{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001758 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001759 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001760 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001761 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1762 psa_status_t status;
1763
Gilles Peskine8817f612018-12-18 00:18:46 +01001764 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765
Gilles Peskine8817f612018-12-18 00:18:46 +01001766 PSA_ASSERT( psa_allocate_key( key_type,
1767 KEY_BITS_FROM_DATA( key_type, key_data ),
1768 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001769 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001771
Gilles Peskine8817f612018-12-18 00:18:46 +01001772 PSA_ASSERT( psa_import_key( handle, key_type,
1773 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001774
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001775 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001776
Gilles Peskine01d718c2018-09-18 12:01:02 +02001777 if( policy_alg == exercise_alg &&
1778 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001781 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001782
1783exit:
1784 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001785 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001786 mbedtls_psa_crypto_free( );
1787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001791void hash_setup( int alg_arg,
1792 int expected_status_arg )
1793{
1794 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001795 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001796 psa_hash_operation_t operation;
1797 psa_status_t status;
1798
Gilles Peskine8817f612018-12-18 00:18:46 +01001799 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001800
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001801 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001803 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001804
1805exit:
1806 mbedtls_psa_crypto_free( );
1807}
1808/* END_CASE */
1809
1810/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001811void hash_bad_order( )
1812{
1813 unsigned char input[] = "";
1814 /* SHA-256 hash of an empty string */
1815 unsigned char hash[] = {
1816 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1817 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1818 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1819 size_t hash_len;
1820 psa_hash_operation_t operation;
1821
Gilles Peskine8817f612018-12-18 00:18:46 +01001822 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001823
1824 /* psa_hash_update without calling psa_hash_setup beforehand */
1825 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001826 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001827 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001828
1829 /* psa_hash_verify without calling psa_hash_setup beforehand */
1830 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001831 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001832 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001833
1834 /* psa_hash_finish without calling psa_hash_setup beforehand */
1835 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001836 TEST_EQUAL( psa_hash_finish( &operation,
1837 hash, sizeof( hash ), &hash_len ),
1838 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001839
1840exit:
1841 mbedtls_psa_crypto_free( );
1842}
1843/* END_CASE */
1844
itayzafrir27e69452018-11-01 14:26:34 +02001845/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1846void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001847{
1848 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001849 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1850 * appended to it */
1851 unsigned char hash[] = {
1852 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1853 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1854 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001855 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001856 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001857
Gilles Peskine8817f612018-12-18 00:18:46 +01001858 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001859
itayzafrir27e69452018-11-01 14:26:34 +02001860 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001861 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001862 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001863 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001864
itayzafrir27e69452018-11-01 14:26:34 +02001865 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001866 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001867 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001868 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001869
itayzafrir27e69452018-11-01 14:26:34 +02001870 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001871 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001872 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001873 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001874
itayzafrirec93d302018-10-18 18:01:10 +03001875exit:
1876 mbedtls_psa_crypto_free( );
1877}
1878/* END_CASE */
1879
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001880/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1881void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001882{
1883 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001884 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001885 size_t expected_size = PSA_HASH_SIZE( alg );
1886 psa_hash_operation_t operation;
1887 size_t hash_len;
1888
Gilles Peskine8817f612018-12-18 00:18:46 +01001889 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001890
itayzafrir58028322018-10-25 10:22:01 +03001891 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001892 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001894 hash, expected_size - 1, &hash_len ),
1895 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001896
1897exit:
1898 mbedtls_psa_crypto_free( );
1899}
1900/* END_CASE */
1901
1902/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001903void mac_setup( int key_type_arg,
1904 data_t *key,
1905 int alg_arg,
1906 int expected_status_arg )
1907{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001908 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001909 psa_key_type_t key_type = key_type_arg;
1910 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001911 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001912 psa_mac_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00001913 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001914 psa_status_t status;
1915
Gilles Peskine8817f612018-12-18 00:18:46 +01001916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001917
Gilles Peskine8817f612018-12-18 00:18:46 +01001918 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1919 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001920 psa_key_policy_set_usage( &policy,
1921 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1922 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001923 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001924
Gilles Peskine8817f612018-12-18 00:18:46 +01001925 PSA_ASSERT( psa_import_key( handle, key_type,
1926 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001927
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001928 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001929 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001930 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001931
1932exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001933 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001934 mbedtls_psa_crypto_free( );
1935}
1936/* END_CASE */
1937
1938/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001939void mac_sign( int key_type_arg,
1940 data_t *key,
1941 int alg_arg,
1942 data_t *input,
1943 data_t *expected_mac )
1944{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001945 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001946 psa_key_type_t key_type = key_type_arg;
1947 psa_algorithm_t alg = alg_arg;
1948 psa_mac_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00001949 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001950 /* Leave a little extra room in the output buffer. At the end of the
1951 * test, we'll check that the implementation didn't overwrite onto
1952 * this extra room. */
1953 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1954 size_t mac_buffer_size =
1955 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1956 size_t mac_length = 0;
1957
1958 memset( actual_mac, '+', sizeof( actual_mac ) );
1959 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1960 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1961
Gilles Peskine8817f612018-12-18 00:18:46 +01001962 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001963
Gilles Peskine8817f612018-12-18 00:18:46 +01001964 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1965 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001966 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001968
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_import_key( handle, key_type,
1970 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001971
1972 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01001973 PSA_ASSERT( psa_mac_sign_setup( &operation,
1974 handle, alg ) );
1975 PSA_ASSERT( psa_mac_update( &operation,
1976 input->x, input->len ) );
1977 PSA_ASSERT( psa_mac_sign_finish( &operation,
1978 actual_mac, mac_buffer_size,
1979 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001980
1981 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01001982 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
1983 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001984
1985 /* Verify that the end of the buffer is untouched. */
1986 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1987 sizeof( actual_mac ) - mac_length ) );
1988
1989exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001990 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001991 mbedtls_psa_crypto_free( );
1992}
1993/* END_CASE */
1994
1995/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001996void mac_verify( int key_type_arg,
1997 data_t *key,
1998 int alg_arg,
1999 data_t *input,
2000 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002001{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002002 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002003 psa_key_type_t key_type = key_type_arg;
2004 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002005 psa_mac_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002006 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007
Gilles Peskine69c12672018-06-28 00:07:19 +02002008 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2009
Gilles Peskine8817f612018-12-18 00:18:46 +01002010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002011
Gilles Peskine8817f612018-12-18 00:18:46 +01002012 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2013 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002014 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002015 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002016
Gilles Peskine8817f612018-12-18 00:18:46 +01002017 PSA_ASSERT( psa_import_key( handle, key_type,
2018 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002019
Gilles Peskine8817f612018-12-18 00:18:46 +01002020 PSA_ASSERT( psa_mac_verify_setup( &operation,
2021 handle, alg ) );
2022 PSA_ASSERT( psa_destroy_key( handle ) );
2023 PSA_ASSERT( psa_mac_update( &operation,
2024 input->x, input->len ) );
2025 PSA_ASSERT( psa_mac_verify_finish( &operation,
2026 expected_mac->x,
2027 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028
2029exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002030 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031 mbedtls_psa_crypto_free( );
2032}
2033/* END_CASE */
2034
2035/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002036void cipher_setup( int key_type_arg,
2037 data_t *key,
2038 int alg_arg,
2039 int expected_status_arg )
2040{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002041 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002042 psa_key_type_t key_type = key_type_arg;
2043 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002044 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002045 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002046 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002047 psa_status_t status;
2048
Gilles Peskine8817f612018-12-18 00:18:46 +01002049 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002050
Gilles Peskine8817f612018-12-18 00:18:46 +01002051 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2052 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002053 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002054 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002055
Gilles Peskine8817f612018-12-18 00:18:46 +01002056 PSA_ASSERT( psa_import_key( handle, key_type,
2057 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002058
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002059 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002060 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002061 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002062
2063exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002064 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002065 mbedtls_psa_crypto_free( );
2066}
2067/* END_CASE */
2068
2069/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002070void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002071 data_t *key,
2072 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002073 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002074{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002075 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002076 psa_status_t status;
2077 psa_key_type_t key_type = key_type_arg;
2078 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002079 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002080 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002081 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002082 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002083 size_t output_buffer_size = 0;
2084 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002085 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002086 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002087 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002088
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002089 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2090 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002091
Gilles Peskine8817f612018-12-18 00:18:46 +01002092 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002093
Gilles Peskine8817f612018-12-18 00:18:46 +01002094 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2095 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002096 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002097 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002098
Gilles Peskine8817f612018-12-18 00:18:46 +01002099 PSA_ASSERT( psa_import_key( handle, key_type,
2100 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002101
Gilles Peskine8817f612018-12-18 00:18:46 +01002102 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2103 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002104
Gilles Peskine8817f612018-12-18 00:18:46 +01002105 PSA_ASSERT( psa_cipher_set_iv( &operation,
2106 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002107 output_buffer_size = ( (size_t) input->len +
2108 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002109 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002110
Gilles Peskine8817f612018-12-18 00:18:46 +01002111 PSA_ASSERT( psa_cipher_update( &operation,
2112 input->x, input->len,
2113 output, output_buffer_size,
2114 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002115 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002116 status = psa_cipher_finish( &operation,
2117 output + function_output_length,
2118 output_buffer_size,
2119 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002120 total_output_length += function_output_length;
2121
Gilles Peskinefe11b722018-12-18 00:24:04 +01002122 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002123 if( expected_status == PSA_SUCCESS )
2124 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002125 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002126 ASSERT_COMPARE( expected_output->x, expected_output->len,
2127 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002128 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002129
Gilles Peskine50e586b2018-06-08 14:28:46 +02002130exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002131 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002132 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002133 mbedtls_psa_crypto_free( );
2134}
2135/* END_CASE */
2136
2137/* BEGIN_CASE */
2138void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002139 data_t *key,
2140 data_t *input,
2141 int first_part_size,
2142 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002143{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002144 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002145 psa_key_type_t key_type = key_type_arg;
2146 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002147 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002148 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002149 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002150 size_t output_buffer_size = 0;
2151 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002152 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002154 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002156 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2157 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002158
Gilles Peskine8817f612018-12-18 00:18:46 +01002159 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002160
Gilles Peskine8817f612018-12-18 00:18:46 +01002161 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2162 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002163 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002164 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002165
Gilles Peskine8817f612018-12-18 00:18:46 +01002166 PSA_ASSERT( psa_import_key( handle, key_type,
2167 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002168
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2170 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002171
Gilles Peskine8817f612018-12-18 00:18:46 +01002172 PSA_ASSERT( psa_cipher_set_iv( &operation,
2173 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002174 output_buffer_size = ( (size_t) input->len +
2175 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002176 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002177
Gilles Peskine4abf7412018-06-18 16:35:34 +02002178 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002179 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2180 output, output_buffer_size,
2181 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002182 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002183 PSA_ASSERT( psa_cipher_update( &operation,
2184 input->x + first_part_size,
2185 input->len - first_part_size,
2186 output, output_buffer_size,
2187 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002188 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002189 PSA_ASSERT( psa_cipher_finish( &operation,
2190 output + function_output_length,
2191 output_buffer_size,
2192 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002193 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002194 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002196 ASSERT_COMPARE( expected_output->x, expected_output->len,
2197 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002198
2199exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002200 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002201 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202 mbedtls_psa_crypto_free( );
2203}
2204/* END_CASE */
2205
2206/* BEGIN_CASE */
2207void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002208 data_t *key,
2209 data_t *input,
2210 int first_part_size,
2211 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002212{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002213 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002214
2215 psa_key_type_t key_type = key_type_arg;
2216 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002217 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002218 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002219 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002220 size_t output_buffer_size = 0;
2221 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002222 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002223 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002224 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002225
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002226 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2227 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002228
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002230
Gilles Peskine8817f612018-12-18 00:18:46 +01002231 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2232 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002233 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002234 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002235
Gilles Peskine8817f612018-12-18 00:18:46 +01002236 PSA_ASSERT( psa_import_key( handle, key_type,
2237 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002238
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2240 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002241
Gilles Peskine8817f612018-12-18 00:18:46 +01002242 PSA_ASSERT( psa_cipher_set_iv( &operation,
2243 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002244
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002245 output_buffer_size = ( (size_t) input->len +
2246 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002247 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002248
Gilles Peskine4abf7412018-06-18 16:35:34 +02002249 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002250 PSA_ASSERT( psa_cipher_update( &operation,
2251 input->x, first_part_size,
2252 output, output_buffer_size,
2253 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002254 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002255 PSA_ASSERT( psa_cipher_update( &operation,
2256 input->x + first_part_size,
2257 input->len - first_part_size,
2258 output, output_buffer_size,
2259 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002260 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002261 PSA_ASSERT( psa_cipher_finish( &operation,
2262 output + function_output_length,
2263 output_buffer_size,
2264 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002265 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002266 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002267
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002268 ASSERT_COMPARE( expected_output->x, expected_output->len,
2269 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002270
2271exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002272 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002273 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274 mbedtls_psa_crypto_free( );
2275}
2276/* END_CASE */
2277
Gilles Peskine50e586b2018-06-08 14:28:46 +02002278/* BEGIN_CASE */
2279void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002280 data_t *key,
2281 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002282 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002283{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002284 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002285 psa_status_t status;
2286 psa_key_type_t key_type = key_type_arg;
2287 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002288 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002290 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002291 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002292 size_t output_buffer_size = 0;
2293 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002294 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002295 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002296 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002297
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002298 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2299 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002300
Gilles Peskine8817f612018-12-18 00:18:46 +01002301 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002302
Gilles Peskine8817f612018-12-18 00:18:46 +01002303 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2304 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002305 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002306 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002307
Gilles Peskine8817f612018-12-18 00:18:46 +01002308 PSA_ASSERT( psa_import_key( handle, key_type,
2309 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002310
Gilles Peskine8817f612018-12-18 00:18:46 +01002311 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2312 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002313
Gilles Peskine8817f612018-12-18 00:18:46 +01002314 PSA_ASSERT( psa_cipher_set_iv( &operation,
2315 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002316
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002317 output_buffer_size = ( (size_t) input->len +
2318 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002319 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002320
Gilles Peskine8817f612018-12-18 00:18:46 +01002321 PSA_ASSERT( psa_cipher_update( &operation,
2322 input->x, input->len,
2323 output, output_buffer_size,
2324 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002325 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002326 status = psa_cipher_finish( &operation,
2327 output + function_output_length,
2328 output_buffer_size,
2329 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002330 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002331 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002332
2333 if( expected_status == PSA_SUCCESS )
2334 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002335 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002336 ASSERT_COMPARE( expected_output->x, expected_output->len,
2337 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338 }
2339
Gilles Peskine50e586b2018-06-08 14:28:46 +02002340exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002341 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002342 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002343 mbedtls_psa_crypto_free( );
2344}
2345/* END_CASE */
2346
Gilles Peskine50e586b2018-06-08 14:28:46 +02002347/* BEGIN_CASE */
2348void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002349 data_t *key,
2350 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002351{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002352 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002353 psa_key_type_t key_type = key_type_arg;
2354 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002355 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002356 size_t iv_size = 16;
2357 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002358 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002359 size_t output1_size = 0;
2360 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002361 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002362 size_t output2_size = 0;
2363 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002364 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002365 psa_cipher_operation_t operation1;
2366 psa_cipher_operation_t operation2;
Jaeden Amero70261c52019-01-04 11:47:20 +00002367 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002368
Gilles Peskine8817f612018-12-18 00:18:46 +01002369 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002370
Gilles Peskine8817f612018-12-18 00:18:46 +01002371 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2372 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002373 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002374 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002375
Gilles Peskine8817f612018-12-18 00:18:46 +01002376 PSA_ASSERT( psa_import_key( handle, key_type,
2377 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002378
Gilles Peskine8817f612018-12-18 00:18:46 +01002379 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2380 handle, alg ) );
2381 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2382 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002383
Gilles Peskine8817f612018-12-18 00:18:46 +01002384 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2385 iv, iv_size,
2386 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002387 output1_size = ( (size_t) input->len +
2388 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002389 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002390
Gilles Peskine8817f612018-12-18 00:18:46 +01002391 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2392 output1, output1_size,
2393 &output1_length ) );
2394 PSA_ASSERT( psa_cipher_finish( &operation1,
2395 output1 + output1_length, output1_size,
2396 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002397
Gilles Peskine048b7f02018-06-08 14:20:49 +02002398 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002399
Gilles Peskine8817f612018-12-18 00:18:46 +01002400 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002401
2402 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002403 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002404
Gilles Peskine8817f612018-12-18 00:18:46 +01002405 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2406 iv, iv_length ) );
2407 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2408 output2, output2_size,
2409 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002410 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002411 PSA_ASSERT( psa_cipher_finish( &operation2,
2412 output2 + output2_length,
2413 output2_size,
2414 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002415
Gilles Peskine048b7f02018-06-08 14:20:49 +02002416 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002417
Gilles Peskine8817f612018-12-18 00:18:46 +01002418 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002419
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002420 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002421
2422exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002423 mbedtls_free( output1 );
2424 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002425 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002426 mbedtls_psa_crypto_free( );
2427}
2428/* END_CASE */
2429
2430/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002431void cipher_verify_output_multipart( int alg_arg,
2432 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002433 data_t *key,
2434 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002436{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002437 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002438 psa_key_type_t key_type = key_type_arg;
2439 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002440 unsigned char iv[16] = {0};
2441 size_t iv_size = 16;
2442 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002443 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002444 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002445 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002446 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002447 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002448 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002449 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002450 psa_cipher_operation_t operation1;
2451 psa_cipher_operation_t operation2;
Jaeden Amero70261c52019-01-04 11:47:20 +00002452 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002453
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002455
Gilles Peskine8817f612018-12-18 00:18:46 +01002456 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2457 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002458 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002459 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002460
Gilles Peskine8817f612018-12-18 00:18:46 +01002461 PSA_ASSERT( psa_import_key( handle, key_type,
2462 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002463
Gilles Peskine8817f612018-12-18 00:18:46 +01002464 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2465 handle, alg ) );
2466 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2467 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002468
Gilles Peskine8817f612018-12-18 00:18:46 +01002469 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2470 iv, iv_size,
2471 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002472 output1_buffer_size = ( (size_t) input->len +
2473 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002474 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002475
Gilles Peskine4abf7412018-06-18 16:35:34 +02002476 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002477
Gilles Peskine8817f612018-12-18 00:18:46 +01002478 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2479 output1, output1_buffer_size,
2480 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002481 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002482
Gilles Peskine8817f612018-12-18 00:18:46 +01002483 PSA_ASSERT( psa_cipher_update( &operation1,
2484 input->x + first_part_size,
2485 input->len - first_part_size,
2486 output1, output1_buffer_size,
2487 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002488 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002489
Gilles Peskine8817f612018-12-18 00:18:46 +01002490 PSA_ASSERT( psa_cipher_finish( &operation1,
2491 output1 + output1_length,
2492 output1_buffer_size - output1_length,
2493 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002494 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002495
Gilles Peskine8817f612018-12-18 00:18:46 +01002496 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002497
Gilles Peskine048b7f02018-06-08 14:20:49 +02002498 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002499 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002500
Gilles Peskine8817f612018-12-18 00:18:46 +01002501 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2502 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002503
Gilles Peskine8817f612018-12-18 00:18:46 +01002504 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2505 output2, output2_buffer_size,
2506 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002507 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002508
Gilles Peskine8817f612018-12-18 00:18:46 +01002509 PSA_ASSERT( psa_cipher_update( &operation2,
2510 output1 + first_part_size,
2511 output1_length - first_part_size,
2512 output2, output2_buffer_size,
2513 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002514 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002515
Gilles Peskine8817f612018-12-18 00:18:46 +01002516 PSA_ASSERT( psa_cipher_finish( &operation2,
2517 output2 + output2_length,
2518 output2_buffer_size - output2_length,
2519 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002520 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002521
Gilles Peskine8817f612018-12-18 00:18:46 +01002522 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002523
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002524 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002525
2526exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002527 mbedtls_free( output1 );
2528 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002529 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002530 mbedtls_psa_crypto_free( );
2531}
2532/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002533
Gilles Peskine20035e32018-02-03 22:44:14 +01002534/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002535void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002536 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002537 data_t *nonce,
2538 data_t *additional_data,
2539 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002540 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002541{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002542 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002543 psa_key_type_t key_type = key_type_arg;
2544 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002545 unsigned char *output_data = NULL;
2546 size_t output_size = 0;
2547 size_t output_length = 0;
2548 unsigned char *output_data2 = NULL;
2549 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002550 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002551 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002552 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002553
Gilles Peskine4abf7412018-06-18 16:35:34 +02002554 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002555 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002556
Gilles Peskine8817f612018-12-18 00:18:46 +01002557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002558
Gilles Peskine8817f612018-12-18 00:18:46 +01002559 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2560 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002561 psa_key_policy_set_usage( &policy,
2562 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2563 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002564 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002565
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_import_key( handle, key_type,
2567 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002568
Gilles Peskinefe11b722018-12-18 00:24:04 +01002569 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2570 nonce->x, nonce->len,
2571 additional_data->x,
2572 additional_data->len,
2573 input_data->x, input_data->len,
2574 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002575 &output_length ),
2576 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002577
2578 if( PSA_SUCCESS == expected_result )
2579 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002580 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002581
Gilles Peskinefe11b722018-12-18 00:24:04 +01002582 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2583 nonce->x, nonce->len,
2584 additional_data->x,
2585 additional_data->len,
2586 output_data, output_length,
2587 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002588 &output_length2 ),
2589 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002590
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002591 ASSERT_COMPARE( input_data->x, input_data->len,
2592 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002593 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002594
Gilles Peskinea1cac842018-06-11 19:33:02 +02002595exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002596 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002597 mbedtls_free( output_data );
2598 mbedtls_free( output_data2 );
2599 mbedtls_psa_crypto_free( );
2600}
2601/* END_CASE */
2602
2603/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002604void aead_encrypt( int key_type_arg, data_t *key_data,
2605 int alg_arg,
2606 data_t *nonce,
2607 data_t *additional_data,
2608 data_t *input_data,
2609 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002610{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002611 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002612 psa_key_type_t key_type = key_type_arg;
2613 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002614 unsigned char *output_data = NULL;
2615 size_t output_size = 0;
2616 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002617 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002618 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002619
Gilles Peskine4abf7412018-06-18 16:35:34 +02002620 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002621 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002622
Gilles Peskine8817f612018-12-18 00:18:46 +01002623 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2626 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002627 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002628 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629
Gilles Peskine8817f612018-12-18 00:18:46 +01002630 PSA_ASSERT( psa_import_key( handle, key_type,
2631 key_data->x,
2632 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002633
Gilles Peskine8817f612018-12-18 00:18:46 +01002634 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2635 nonce->x, nonce->len,
2636 additional_data->x, additional_data->len,
2637 input_data->x, input_data->len,
2638 output_data, output_size,
2639 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002640
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002641 ASSERT_COMPARE( expected_result->x, expected_result->len,
2642 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002643
Gilles Peskinea1cac842018-06-11 19:33:02 +02002644exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002645 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002646 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647 mbedtls_psa_crypto_free( );
2648}
2649/* END_CASE */
2650
2651/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002652void aead_decrypt( int key_type_arg, data_t *key_data,
2653 int alg_arg,
2654 data_t *nonce,
2655 data_t *additional_data,
2656 data_t *input_data,
2657 data_t *expected_data,
2658 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002659{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002660 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002661 psa_key_type_t key_type = key_type_arg;
2662 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002663 unsigned char *output_data = NULL;
2664 size_t output_size = 0;
2665 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002666 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002667 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002668 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669
Gilles Peskine4abf7412018-06-18 16:35:34 +02002670 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002671 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002672
Gilles Peskine8817f612018-12-18 00:18:46 +01002673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002674
Gilles Peskine8817f612018-12-18 00:18:46 +01002675 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2676 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002677 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002678 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002679
Gilles Peskine8817f612018-12-18 00:18:46 +01002680 PSA_ASSERT( psa_import_key( handle, key_type,
2681 key_data->x,
2682 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002683
Gilles Peskinefe11b722018-12-18 00:24:04 +01002684 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2685 nonce->x, nonce->len,
2686 additional_data->x,
2687 additional_data->len,
2688 input_data->x, input_data->len,
2689 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002690 &output_length ),
2691 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002692
Gilles Peskine2d277862018-06-18 15:41:12 +02002693 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002694 ASSERT_COMPARE( expected_data->x, expected_data->len,
2695 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696
Gilles Peskinea1cac842018-06-11 19:33:02 +02002697exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002698 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002699 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002700 mbedtls_psa_crypto_free( );
2701}
2702/* END_CASE */
2703
2704/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002705void signature_size( int type_arg,
2706 int bits,
2707 int alg_arg,
2708 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002709{
2710 psa_key_type_t type = type_arg;
2711 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002712 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002713 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002714exit:
2715 ;
2716}
2717/* END_CASE */
2718
2719/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002720void sign_deterministic( int key_type_arg, data_t *key_data,
2721 int alg_arg, data_t *input_data,
2722 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002723{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002724 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002725 psa_key_type_t key_type = key_type_arg;
2726 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002727 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002728 unsigned char *signature = NULL;
2729 size_t signature_size;
2730 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002731 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002732
Gilles Peskine8817f612018-12-18 00:18:46 +01002733 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002734
Gilles Peskine8817f612018-12-18 00:18:46 +01002735 PSA_ASSERT( psa_allocate_key( key_type,
2736 KEY_BITS_FROM_DATA( key_type, key_data ),
2737 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002738 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002739 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002740
Gilles Peskine8817f612018-12-18 00:18:46 +01002741 PSA_ASSERT( psa_import_key( handle, key_type,
2742 key_data->x,
2743 key_data->len ) );
2744 PSA_ASSERT( psa_get_key_information( handle,
2745 NULL,
2746 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002747
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002748 /* Allocate a buffer which has the size advertized by the
2749 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002750 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2751 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002752 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002753 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002754 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002755
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002756 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002757 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2758 input_data->x, input_data->len,
2759 signature, signature_size,
2760 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002761 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002762 ASSERT_COMPARE( output_data->x, output_data->len,
2763 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002764
2765exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002766 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002767 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002768 mbedtls_psa_crypto_free( );
2769}
2770/* END_CASE */
2771
2772/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002773void sign_fail( int key_type_arg, data_t *key_data,
2774 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002775 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002776{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002777 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002778 psa_key_type_t key_type = key_type_arg;
2779 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002780 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002781 psa_status_t actual_status;
2782 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002783 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002784 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002785 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002786
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002787 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002788
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002790
Gilles Peskine8817f612018-12-18 00:18:46 +01002791 PSA_ASSERT( psa_allocate_key( key_type,
2792 KEY_BITS_FROM_DATA( key_type, key_data ),
2793 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002794 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002796
Gilles Peskine8817f612018-12-18 00:18:46 +01002797 PSA_ASSERT( psa_import_key( handle, key_type,
2798 key_data->x,
2799 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002800
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002801 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002802 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002803 signature, signature_size,
2804 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002805 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002806 /* The value of *signature_length is unspecified on error, but
2807 * whatever it is, it should be less than signature_size, so that
2808 * if the caller tries to read *signature_length bytes without
2809 * checking the error code then they don't overflow a buffer. */
2810 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002811
2812exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002813 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002814 mbedtls_free( signature );
2815 mbedtls_psa_crypto_free( );
2816}
2817/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002818
2819/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002820void sign_verify( int key_type_arg, data_t *key_data,
2821 int alg_arg, data_t *input_data )
2822{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002823 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002824 psa_key_type_t key_type = key_type_arg;
2825 psa_algorithm_t alg = alg_arg;
2826 size_t key_bits;
2827 unsigned char *signature = NULL;
2828 size_t signature_size;
2829 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002830 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002831
Gilles Peskine8817f612018-12-18 00:18:46 +01002832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002833
Gilles Peskine8817f612018-12-18 00:18:46 +01002834 PSA_ASSERT( psa_allocate_key( key_type,
2835 KEY_BITS_FROM_DATA( key_type, key_data ),
2836 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002837 psa_key_policy_set_usage( &policy,
2838 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2839 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002841
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_import_key( handle, key_type,
2843 key_data->x,
2844 key_data->len ) );
2845 PSA_ASSERT( psa_get_key_information( handle,
2846 NULL,
2847 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002848
2849 /* Allocate a buffer which has the size advertized by the
2850 * library. */
2851 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2852 key_bits, alg );
2853 TEST_ASSERT( signature_size != 0 );
2854 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002855 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002856
2857 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002858 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2859 input_data->x, input_data->len,
2860 signature, signature_size,
2861 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002862 /* Check that the signature length looks sensible. */
2863 TEST_ASSERT( signature_length <= signature_size );
2864 TEST_ASSERT( signature_length > 0 );
2865
2866 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_asymmetric_verify(
2868 handle, alg,
2869 input_data->x, input_data->len,
2870 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002871
2872 if( input_data->len != 0 )
2873 {
2874 /* Flip a bit in the input and verify that the signature is now
2875 * detected as invalid. Flip a bit at the beginning, not at the end,
2876 * because ECDSA may ignore the last few bits of the input. */
2877 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002878 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2879 input_data->x, input_data->len,
2880 signature, signature_length ),
2881 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002882 }
2883
2884exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002885 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002886 mbedtls_free( signature );
2887 mbedtls_psa_crypto_free( );
2888}
2889/* END_CASE */
2890
2891/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002892void asymmetric_verify( int key_type_arg, data_t *key_data,
2893 int alg_arg, data_t *hash_data,
2894 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002895{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002896 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002897 psa_key_type_t key_type = key_type_arg;
2898 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002899 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002900
Gilles Peskine69c12672018-06-28 00:07:19 +02002901 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2902
Gilles Peskine8817f612018-12-18 00:18:46 +01002903 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002904
Gilles Peskine8817f612018-12-18 00:18:46 +01002905 PSA_ASSERT( psa_allocate_key( key_type,
2906 KEY_BITS_FROM_DATA( key_type, key_data ),
2907 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002908 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002909 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002910
Gilles Peskine8817f612018-12-18 00:18:46 +01002911 PSA_ASSERT( psa_import_key( handle, key_type,
2912 key_data->x,
2913 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002914
Gilles Peskine8817f612018-12-18 00:18:46 +01002915 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2916 hash_data->x, hash_data->len,
2917 signature_data->x,
2918 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002919exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002920 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002921 mbedtls_psa_crypto_free( );
2922}
2923/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002924
2925/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002926void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2927 int alg_arg, data_t *hash_data,
2928 data_t *signature_data,
2929 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002930{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002931 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002932 psa_key_type_t key_type = key_type_arg;
2933 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002934 psa_status_t actual_status;
2935 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002936 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002937
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002939
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_allocate_key( key_type,
2941 KEY_BITS_FROM_DATA( key_type, key_data ),
2942 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002943 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002944 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002945
Gilles Peskine8817f612018-12-18 00:18:46 +01002946 PSA_ASSERT( psa_import_key( handle, key_type,
2947 key_data->x,
2948 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002949
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002950 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002951 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002952 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002953 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002954
Gilles Peskinefe11b722018-12-18 00:24:04 +01002955 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002956
2957exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002958 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002959 mbedtls_psa_crypto_free( );
2960}
2961/* END_CASE */
2962
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002964void asymmetric_encrypt( int key_type_arg,
2965 data_t *key_data,
2966 int alg_arg,
2967 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002968 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002969 int expected_output_length_arg,
2970 int expected_status_arg )
2971{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002972 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02002973 psa_key_type_t key_type = key_type_arg;
2974 psa_algorithm_t alg = alg_arg;
2975 size_t expected_output_length = expected_output_length_arg;
2976 size_t key_bits;
2977 unsigned char *output = NULL;
2978 size_t output_size;
2979 size_t output_length = ~0;
2980 psa_status_t actual_status;
2981 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002982 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02002983
Gilles Peskine8817f612018-12-18 00:18:46 +01002984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002985
Gilles Peskine656896e2018-06-29 19:12:28 +02002986 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01002987 PSA_ASSERT( psa_allocate_key( key_type,
2988 KEY_BITS_FROM_DATA( key_type, key_data ),
2989 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02002990 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002991 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2992 PSA_ASSERT( psa_import_key( handle, key_type,
2993 key_data->x,
2994 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02002995
2996 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01002997 PSA_ASSERT( psa_get_key_information( handle,
2998 NULL,
2999 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003000 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003001 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003002
3003 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003004 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003005 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003006 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003007 output, output_size,
3008 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003009 TEST_EQUAL( actual_status, expected_status );
3010 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003011
Gilles Peskine68428122018-06-30 18:42:41 +02003012 /* If the label is empty, the test framework puts a non-null pointer
3013 * in label->x. Test that a null pointer works as well. */
3014 if( label->len == 0 )
3015 {
3016 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003017 if( output_size != 0 )
3018 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003019 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003020 input_data->x, input_data->len,
3021 NULL, label->len,
3022 output, output_size,
3023 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003024 TEST_EQUAL( actual_status, expected_status );
3025 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003026 }
3027
Gilles Peskine656896e2018-06-29 19:12:28 +02003028exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003029 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003030 mbedtls_free( output );
3031 mbedtls_psa_crypto_free( );
3032}
3033/* END_CASE */
3034
3035/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003036void asymmetric_encrypt_decrypt( int key_type_arg,
3037 data_t *key_data,
3038 int alg_arg,
3039 data_t *input_data,
3040 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003041{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003042 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003043 psa_key_type_t key_type = key_type_arg;
3044 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003045 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003047 size_t output_size;
3048 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003049 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003050 size_t output2_size;
3051 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003052 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003053
Gilles Peskine8817f612018-12-18 00:18:46 +01003054 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003055
Gilles Peskine8817f612018-12-18 00:18:46 +01003056 PSA_ASSERT( psa_allocate_key( key_type,
3057 KEY_BITS_FROM_DATA( key_type, key_data ),
3058 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003059 psa_key_policy_set_usage( &policy,
3060 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003061 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003063
Gilles Peskine8817f612018-12-18 00:18:46 +01003064 PSA_ASSERT( psa_import_key( handle, key_type,
3065 key_data->x,
3066 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003067
3068 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003069 PSA_ASSERT( psa_get_key_information( handle,
3070 NULL,
3071 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003072 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003073 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003074 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003075 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003076
Gilles Peskineeebd7382018-06-08 18:11:54 +02003077 /* We test encryption by checking that encrypt-then-decrypt gives back
3078 * the original plaintext because of the non-optional random
3079 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003080 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3081 input_data->x, input_data->len,
3082 label->x, label->len,
3083 output, output_size,
3084 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003085 /* We don't know what ciphertext length to expect, but check that
3086 * it looks sensible. */
3087 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3090 output, output_length,
3091 label->x, label->len,
3092 output2, output2_size,
3093 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003094 ASSERT_COMPARE( input_data->x, input_data->len,
3095 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003096
3097exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003098 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003099 mbedtls_free( output );
3100 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003101 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102}
3103/* END_CASE */
3104
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003105/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003106void asymmetric_decrypt( int key_type_arg,
3107 data_t *key_data,
3108 int alg_arg,
3109 data_t *input_data,
3110 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003111 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003113 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003114 psa_key_type_t key_type = key_type_arg;
3115 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003116 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003117 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003118 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003119 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003120
Gilles Peskine4abf7412018-06-18 16:35:34 +02003121 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003122 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003123
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125
Gilles Peskine8817f612018-12-18 00:18:46 +01003126 PSA_ASSERT( psa_allocate_key( key_type,
3127 KEY_BITS_FROM_DATA( key_type, key_data ),
3128 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003129 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003130 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003131
Gilles Peskine8817f612018-12-18 00:18:46 +01003132 PSA_ASSERT( psa_import_key( handle, key_type,
3133 key_data->x,
3134 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3137 input_data->x, input_data->len,
3138 label->x, label->len,
3139 output,
3140 output_size,
3141 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003142 ASSERT_COMPARE( expected_data->x, expected_data->len,
3143 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003144
Gilles Peskine68428122018-06-30 18:42:41 +02003145 /* If the label is empty, the test framework puts a non-null pointer
3146 * in label->x. Test that a null pointer works as well. */
3147 if( label->len == 0 )
3148 {
3149 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003150 if( output_size != 0 )
3151 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3153 input_data->x, input_data->len,
3154 NULL, label->len,
3155 output,
3156 output_size,
3157 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003158 ASSERT_COMPARE( expected_data->x, expected_data->len,
3159 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003160 }
3161
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003163 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003164 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003165 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166}
3167/* END_CASE */
3168
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003170void asymmetric_decrypt_fail( int key_type_arg,
3171 data_t *key_data,
3172 int alg_arg,
3173 data_t *input_data,
3174 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003175 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003176{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003177 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178 psa_key_type_t key_type = key_type_arg;
3179 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003181 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003182 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003183 psa_status_t actual_status;
3184 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003185 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003186
Gilles Peskine4abf7412018-06-18 16:35:34 +02003187 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003188 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003189
Gilles Peskine8817f612018-12-18 00:18:46 +01003190 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_allocate_key( key_type,
3193 KEY_BITS_FROM_DATA( key_type, key_data ),
3194 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003195 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003197
Gilles Peskine8817f612018-12-18 00:18:46 +01003198 PSA_ASSERT( psa_import_key( handle, key_type,
3199 key_data->x,
3200 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003202 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003203 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003204 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003205 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003206 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003207 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003208 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209
Gilles Peskine68428122018-06-30 18:42:41 +02003210 /* If the label is empty, the test framework puts a non-null pointer
3211 * in label->x. Test that a null pointer works as well. */
3212 if( label->len == 0 )
3213 {
3214 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003215 if( output_size != 0 )
3216 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003217 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003218 input_data->x, input_data->len,
3219 NULL, label->len,
3220 output, output_size,
3221 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003222 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003223 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003224 }
3225
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003227 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003228 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229 mbedtls_psa_crypto_free( );
3230}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003231/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003232
3233/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003234void derive_setup( int key_type_arg,
3235 data_t *key_data,
3236 int alg_arg,
3237 data_t *salt,
3238 data_t *label,
3239 int requested_capacity_arg,
3240 int expected_status_arg )
3241{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003242 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003243 size_t key_type = key_type_arg;
3244 psa_algorithm_t alg = alg_arg;
3245 size_t requested_capacity = requested_capacity_arg;
3246 psa_status_t expected_status = expected_status_arg;
3247 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003248 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003251
Gilles Peskine8817f612018-12-18 00:18:46 +01003252 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3253 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003254 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003256
Gilles Peskine8817f612018-12-18 00:18:46 +01003257 PSA_ASSERT( psa_import_key( handle, key_type,
3258 key_data->x,
3259 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003260
Gilles Peskinefe11b722018-12-18 00:24:04 +01003261 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3262 salt->x, salt->len,
3263 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003264 requested_capacity ),
3265 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003266
3267exit:
3268 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003269 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003270 mbedtls_psa_crypto_free( );
3271}
3272/* END_CASE */
3273
3274/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003275void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003276{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003277 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003278 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003279 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003280 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003281 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003282 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003283 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3284 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3285 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003286 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003287
Gilles Peskine8817f612018-12-18 00:18:46 +01003288 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003289
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 PSA_ASSERT( psa_allocate_key( key_type,
3291 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3292 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003293 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003294 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003295
Gilles Peskine8817f612018-12-18 00:18:46 +01003296 PSA_ASSERT( psa_import_key( handle, key_type,
3297 key_data,
3298 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003299
3300 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003301 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3302 NULL, 0,
3303 NULL, 0,
3304 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003305
3306 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003307 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3308 NULL, 0,
3309 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003310 capacity ),
3311 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003312
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003313 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003314
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003315 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3316 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003317
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003318exit:
3319 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003320 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003321 mbedtls_psa_crypto_free( );
3322}
3323/* END_CASE */
3324
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003325/* BEGIN_CASE */
3326void test_derive_invalid_generator_tests( )
3327{
3328 uint8_t output_buffer[16];
3329 size_t buffer_size = 16;
3330 size_t capacity = 0;
3331 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3332
Nir Sonnenschein50789302018-10-31 12:16:38 +02003333 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003334 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003335
3336 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003337 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003338
Gilles Peskine8817f612018-12-18 00:18:46 +01003339 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003340
Nir Sonnenschein50789302018-10-31 12:16:38 +02003341 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003342 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003343
Nir Sonnenschein50789302018-10-31 12:16:38 +02003344 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003345 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003346
3347exit:
3348 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003349}
3350/* END_CASE */
3351
3352/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003353void derive_output( int alg_arg,
3354 data_t *key_data,
3355 data_t *salt,
3356 data_t *label,
3357 int requested_capacity_arg,
3358 data_t *expected_output1,
3359 data_t *expected_output2 )
3360{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003361 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003362 psa_algorithm_t alg = alg_arg;
3363 size_t requested_capacity = requested_capacity_arg;
3364 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3365 uint8_t *expected_outputs[2] =
3366 {expected_output1->x, expected_output2->x};
3367 size_t output_sizes[2] =
3368 {expected_output1->len, expected_output2->len};
3369 size_t output_buffer_size = 0;
3370 uint8_t *output_buffer = NULL;
3371 size_t expected_capacity;
3372 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003373 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003374 psa_status_t status;
3375 unsigned i;
3376
3377 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3378 {
3379 if( output_sizes[i] > output_buffer_size )
3380 output_buffer_size = output_sizes[i];
3381 if( output_sizes[i] == 0 )
3382 expected_outputs[i] = NULL;
3383 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003384 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003385 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003386
Gilles Peskine8817f612018-12-18 00:18:46 +01003387 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3388 PSA_BYTES_TO_BITS( key_data->len ),
3389 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003390 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003392
Gilles Peskine8817f612018-12-18 00:18:46 +01003393 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3394 key_data->x,
3395 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003396
3397 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003398 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3399 salt->x, salt->len,
3400 label->x, label->len,
3401 requested_capacity ) );
3402 PSA_ASSERT( psa_get_generator_capacity( &generator,
3403 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003404 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003405 expected_capacity = requested_capacity;
3406
3407 /* Expansion phase. */
3408 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3409 {
3410 /* Read some bytes. */
3411 status = psa_generator_read( &generator,
3412 output_buffer, output_sizes[i] );
3413 if( expected_capacity == 0 && output_sizes[i] == 0 )
3414 {
3415 /* Reading 0 bytes when 0 bytes are available can go either way. */
3416 TEST_ASSERT( status == PSA_SUCCESS ||
3417 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3418 continue;
3419 }
3420 else if( expected_capacity == 0 ||
3421 output_sizes[i] > expected_capacity )
3422 {
3423 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003424 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003425 expected_capacity = 0;
3426 continue;
3427 }
3428 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003429 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003430 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003431 ASSERT_COMPARE( output_buffer, output_sizes[i],
3432 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003433 /* Check the generator status. */
3434 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003435 PSA_ASSERT( psa_get_generator_capacity( &generator,
3436 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003437 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003438 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003439 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003440
3441exit:
3442 mbedtls_free( output_buffer );
3443 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003444 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003445 mbedtls_psa_crypto_free( );
3446}
3447/* END_CASE */
3448
3449/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003450void derive_full( int alg_arg,
3451 data_t *key_data,
3452 data_t *salt,
3453 data_t *label,
3454 int requested_capacity_arg )
3455{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003456 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003457 psa_algorithm_t alg = alg_arg;
3458 size_t requested_capacity = requested_capacity_arg;
3459 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3460 unsigned char output_buffer[16];
3461 size_t expected_capacity = requested_capacity;
3462 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003463 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003464
Gilles Peskine8817f612018-12-18 00:18:46 +01003465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003466
Gilles Peskine8817f612018-12-18 00:18:46 +01003467 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3468 PSA_BYTES_TO_BITS( key_data->len ),
3469 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003470 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003472
Gilles Peskine8817f612018-12-18 00:18:46 +01003473 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3474 key_data->x,
3475 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003476
3477 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003478 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3479 salt->x, salt->len,
3480 label->x, label->len,
3481 requested_capacity ) );
3482 PSA_ASSERT( psa_get_generator_capacity( &generator,
3483 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003484 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003485
3486 /* Expansion phase. */
3487 while( current_capacity > 0 )
3488 {
3489 size_t read_size = sizeof( output_buffer );
3490 if( read_size > current_capacity )
3491 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003492 PSA_ASSERT( psa_generator_read( &generator,
3493 output_buffer,
3494 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003495 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003496 PSA_ASSERT( psa_get_generator_capacity( &generator,
3497 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003498 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003499 }
3500
3501 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003502 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3503 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003504
Gilles Peskine8817f612018-12-18 00:18:46 +01003505 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003506
3507exit:
3508 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003509 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003510 mbedtls_psa_crypto_free( );
3511}
3512/* END_CASE */
3513
3514/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003515void derive_key_exercise( int alg_arg,
3516 data_t *key_data,
3517 data_t *salt,
3518 data_t *label,
3519 int derived_type_arg,
3520 int derived_bits_arg,
3521 int derived_usage_arg,
3522 int derived_alg_arg )
3523{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003524 psa_key_handle_t base_handle = 0;
3525 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003526 psa_algorithm_t alg = alg_arg;
3527 psa_key_type_t derived_type = derived_type_arg;
3528 size_t derived_bits = derived_bits_arg;
3529 psa_key_usage_t derived_usage = derived_usage_arg;
3530 psa_algorithm_t derived_alg = derived_alg_arg;
3531 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3532 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003533 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003534 psa_key_type_t got_type;
3535 size_t got_bits;
3536
Gilles Peskine8817f612018-12-18 00:18:46 +01003537 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003538
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3540 PSA_BYTES_TO_BITS( key_data->len ),
3541 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003542 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3544 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3545 key_data->x,
3546 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003547
3548 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003549 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3550 salt->x, salt->len,
3551 label->x, label->len,
3552 capacity ) );
3553 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3554 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003555 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003556 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3557 PSA_ASSERT( psa_generator_import_key( derived_handle,
3558 derived_type,
3559 derived_bits,
3560 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003561
3562 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003563 PSA_ASSERT( psa_get_key_information( derived_handle,
3564 &got_type,
3565 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003566 TEST_EQUAL( got_type, derived_type );
3567 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003568
3569 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003570 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003571 goto exit;
3572
3573exit:
3574 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003575 psa_destroy_key( base_handle );
3576 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003577 mbedtls_psa_crypto_free( );
3578}
3579/* END_CASE */
3580
3581/* BEGIN_CASE */
3582void derive_key_export( int alg_arg,
3583 data_t *key_data,
3584 data_t *salt,
3585 data_t *label,
3586 int bytes1_arg,
3587 int bytes2_arg )
3588{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003589 psa_key_handle_t base_handle = 0;
3590 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003591 psa_algorithm_t alg = alg_arg;
3592 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003593 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003594 size_t bytes2 = bytes2_arg;
3595 size_t capacity = bytes1 + bytes2;
3596 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003597 uint8_t *output_buffer = NULL;
3598 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003599 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003600 size_t length;
3601
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003602 ASSERT_ALLOC( output_buffer, capacity );
3603 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003605
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3607 PSA_BYTES_TO_BITS( key_data->len ),
3608 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003609 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3611 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3612 key_data->x,
3613 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003614
3615 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003616 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3617 salt->x, salt->len,
3618 label->x, label->len,
3619 capacity ) );
3620 PSA_ASSERT( psa_generator_read( &generator,
3621 output_buffer,
3622 capacity ) );
3623 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003624
3625 /* Derive the same output again, but this time store it in key objects. */
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( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3631 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003632 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
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 PSA_KEY_TYPE_RAW_DATA,
3636 derived_bits,
3637 &generator ) );
3638 PSA_ASSERT( psa_export_key( derived_handle,
3639 export_buffer, bytes1,
3640 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003641 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003642 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3643 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3644 PSA_BYTES_TO_BITS( bytes2 ),
3645 &derived_handle ) );
3646 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3647 PSA_ASSERT( psa_generator_import_key( derived_handle,
3648 PSA_KEY_TYPE_RAW_DATA,
3649 PSA_BYTES_TO_BITS( bytes2 ),
3650 &generator ) );
3651 PSA_ASSERT( psa_export_key( derived_handle,
3652 export_buffer + bytes1, bytes2,
3653 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003654 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003655
3656 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003657 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3658 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003659
3660exit:
3661 mbedtls_free( output_buffer );
3662 mbedtls_free( export_buffer );
3663 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003664 psa_destroy_key( base_handle );
3665 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003666 mbedtls_psa_crypto_free( );
3667}
3668/* END_CASE */
3669
3670/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003671void key_agreement_setup( int alg_arg,
3672 int our_key_type_arg, data_t *our_key_data,
3673 data_t *peer_key_data,
3674 int expected_status_arg )
3675{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003676 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003677 psa_algorithm_t alg = alg_arg;
3678 psa_key_type_t our_key_type = our_key_type_arg;
3679 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003680 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003681
Gilles Peskine8817f612018-12-18 00:18:46 +01003682 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003683
Gilles Peskine8817f612018-12-18 00:18:46 +01003684 PSA_ASSERT( psa_allocate_key( our_key_type,
3685 KEY_BITS_FROM_DATA( our_key_type,
3686 our_key_data ),
3687 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003688 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003689 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3690 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3691 our_key_data->x,
3692 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003693
Gilles Peskinefe11b722018-12-18 00:24:04 +01003694 TEST_EQUAL( psa_key_agreement( &generator,
3695 our_key,
3696 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003697 alg ),
3698 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003699
3700exit:
3701 psa_generator_abort( &generator );
3702 psa_destroy_key( our_key );
3703 mbedtls_psa_crypto_free( );
3704}
3705/* END_CASE */
3706
3707/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003708void key_agreement_capacity( int alg_arg,
3709 int our_key_type_arg, data_t *our_key_data,
3710 data_t *peer_key_data,
3711 int expected_capacity_arg )
3712{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003713 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003714 psa_algorithm_t alg = alg_arg;
3715 psa_key_type_t our_key_type = our_key_type_arg;
3716 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003717 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003718 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003719 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003720
Gilles Peskine8817f612018-12-18 00:18:46 +01003721 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003722
Gilles Peskine8817f612018-12-18 00:18:46 +01003723 PSA_ASSERT( psa_allocate_key( our_key_type,
3724 KEY_BITS_FROM_DATA( our_key_type,
3725 our_key_data ),
3726 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003727 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003728 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3729 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3730 our_key_data->x,
3731 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003732
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 PSA_ASSERT( psa_key_agreement( &generator,
3734 our_key,
3735 peer_key_data->x, peer_key_data->len,
3736 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003737
Gilles Peskinebf491972018-10-25 22:36:12 +02003738 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003739 PSA_ASSERT( psa_get_generator_capacity(
3740 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003741 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003742
Gilles Peskinebf491972018-10-25 22:36:12 +02003743 /* Test the actual capacity by reading the output. */
3744 while( actual_capacity > sizeof( output ) )
3745 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003746 PSA_ASSERT( psa_generator_read( &generator,
3747 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003748 actual_capacity -= sizeof( output );
3749 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003750 PSA_ASSERT( psa_generator_read( &generator,
3751 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003752 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3753 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003754
Gilles Peskine59685592018-09-18 12:11:34 +02003755exit:
3756 psa_generator_abort( &generator );
3757 psa_destroy_key( our_key );
3758 mbedtls_psa_crypto_free( );
3759}
3760/* END_CASE */
3761
3762/* BEGIN_CASE */
3763void key_agreement_output( int alg_arg,
3764 int our_key_type_arg, data_t *our_key_data,
3765 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003766 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003767{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003768 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003769 psa_algorithm_t alg = alg_arg;
3770 psa_key_type_t our_key_type = our_key_type_arg;
3771 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003772 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003773 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003774
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003775 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3776 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003777
Gilles Peskine8817f612018-12-18 00:18:46 +01003778 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003779
Gilles Peskine8817f612018-12-18 00:18:46 +01003780 PSA_ASSERT( psa_allocate_key( our_key_type,
3781 KEY_BITS_FROM_DATA( our_key_type,
3782 our_key_data ),
3783 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003784 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003785 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3786 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3787 our_key_data->x,
3788 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003789
Gilles Peskine8817f612018-12-18 00:18:46 +01003790 PSA_ASSERT( psa_key_agreement( &generator,
3791 our_key,
3792 peer_key_data->x, peer_key_data->len,
3793 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003794
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003795 PSA_ASSERT( psa_generator_read( &generator,
3796 actual_output,
3797 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003798 ASSERT_COMPARE( actual_output, expected_output1->len,
3799 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003800 if( expected_output2->len != 0 )
3801 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003802 PSA_ASSERT( psa_generator_read( &generator,
3803 actual_output,
3804 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003805 ASSERT_COMPARE( actual_output, expected_output2->len,
3806 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003807 }
Gilles Peskine59685592018-09-18 12:11:34 +02003808
3809exit:
3810 psa_generator_abort( &generator );
3811 psa_destroy_key( our_key );
3812 mbedtls_psa_crypto_free( );
3813 mbedtls_free( actual_output );
3814}
3815/* END_CASE */
3816
3817/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003818void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003819{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003820 size_t bytes = bytes_arg;
3821 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003822 unsigned char *output = NULL;
3823 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003824 size_t i;
3825 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003826
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003827 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3828 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003829 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003830
Gilles Peskine8817f612018-12-18 00:18:46 +01003831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003832
Gilles Peskinea50d7392018-06-21 10:22:13 +02003833 /* Run several times, to ensure that every output byte will be
3834 * nonzero at least once with overwhelming probability
3835 * (2^(-8*number_of_runs)). */
3836 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003837 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003838 if( bytes != 0 )
3839 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003841
3842 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003843 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3844 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003845
3846 for( i = 0; i < bytes; i++ )
3847 {
3848 if( output[i] != 0 )
3849 ++changed[i];
3850 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003851 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003852
3853 /* Check that every byte was changed to nonzero at least once. This
3854 * validates that psa_generate_random is overwriting every byte of
3855 * the output buffer. */
3856 for( i = 0; i < bytes; i++ )
3857 {
3858 TEST_ASSERT( changed[i] != 0 );
3859 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003860
3861exit:
3862 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003863 mbedtls_free( output );
3864 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003865}
3866/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003867
3868/* BEGIN_CASE */
3869void generate_key( int type_arg,
3870 int bits_arg,
3871 int usage_arg,
3872 int alg_arg,
3873 int expected_status_arg )
3874{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003875 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003876 psa_key_type_t type = type_arg;
3877 psa_key_usage_t usage = usage_arg;
3878 size_t bits = bits_arg;
3879 psa_algorithm_t alg = alg_arg;
3880 psa_status_t expected_status = expected_status_arg;
3881 psa_key_type_t got_type;
3882 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003883 psa_status_t expected_info_status =
3884 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003885 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003886
Gilles Peskine8817f612018-12-18 00:18:46 +01003887 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003888
Gilles Peskine8817f612018-12-18 00:18:46 +01003889 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003890 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003891 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003892
3893 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003894 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3895 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003896
3897 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003898 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3899 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003900 if( expected_info_status != PSA_SUCCESS )
3901 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003902 TEST_EQUAL( got_type, type );
3903 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003904
Gilles Peskine818ca122018-06-20 18:16:48 +02003905 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003906 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02003907 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003908
3909exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003910 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003911 mbedtls_psa_crypto_free( );
3912}
3913/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003914
Darryl Greend49a4992018-06-18 17:27:26 +01003915/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
3916void persistent_key_load_key_from_storage( data_t *data, int type_arg,
3917 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00003918 int alg_arg, int generation_method,
3919 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01003920{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003921 psa_key_handle_t handle = 0;
3922 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01003923 psa_key_type_t type = (psa_key_type_t) type_arg;
3924 psa_key_type_t type_get;
3925 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00003926 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
3927 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003928 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
3929 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003930 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00003931 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
3932 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003933 unsigned char *first_export = NULL;
3934 unsigned char *second_export = NULL;
3935 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
3936 size_t first_exported_length;
3937 size_t second_exported_length;
3938
3939 ASSERT_ALLOC( first_export, export_size );
3940 ASSERT_ALLOC( second_export, export_size );
3941
Gilles Peskine8817f612018-12-18 00:18:46 +01003942 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01003943
Gilles Peskine8817f612018-12-18 00:18:46 +01003944 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
3945 type, bits,
3946 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003947 psa_key_policy_set_usage( &policy_set, policy_usage,
3948 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003949 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003950
Darryl Green0c6575a2018-11-07 16:05:30 +00003951 switch( generation_method )
3952 {
3953 case IMPORT_KEY:
3954 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003955 PSA_ASSERT( psa_import_key( handle, type,
3956 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003957 break;
Darryl Greend49a4992018-06-18 17:27:26 +01003958
Darryl Green0c6575a2018-11-07 16:05:30 +00003959 case GENERATE_KEY:
3960 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003961 PSA_ASSERT( psa_generate_key( handle, type, bits,
3962 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003963 break;
3964
3965 case DERIVE_KEY:
3966 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3968 PSA_BYTES_TO_BITS( data->len ),
3969 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003970 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
3971 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003972 PSA_ASSERT( psa_set_key_policy(
3973 base_key, &base_policy_set ) );
3974 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3975 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003976 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003977 PSA_ASSERT( psa_key_derivation( &generator, base_key,
3978 base_policy_alg,
3979 NULL, 0, NULL, 0,
3980 export_size ) );
3981 PSA_ASSERT( psa_generator_import_key(
3982 handle, PSA_KEY_TYPE_RAW_DATA,
3983 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003984 break;
3985 }
Darryl Greend49a4992018-06-18 17:27:26 +01003986
3987 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003988 TEST_EQUAL( psa_export_key( handle,
3989 first_export, export_size,
3990 &first_exported_length ),
3991 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01003992
3993 /* Shutdown and restart */
3994 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01003995 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01003996
Darryl Greend49a4992018-06-18 17:27:26 +01003997 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01003998 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
3999 &handle ) );
4000 PSA_ASSERT( psa_get_key_information(
4001 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004002 TEST_EQUAL( type_get, type );
4003 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004004
Gilles Peskine8817f612018-12-18 00:18:46 +01004005 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004006 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4007 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004008
4009 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004010 TEST_EQUAL( psa_export_key( handle,
4011 second_export, export_size,
4012 &second_exported_length ),
4013 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004014
Darryl Green0c6575a2018-11-07 16:05:30 +00004015 if( export_status == PSA_SUCCESS )
4016 {
4017 ASSERT_COMPARE( first_export, first_exported_length,
4018 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004019
Darryl Green0c6575a2018-11-07 16:05:30 +00004020 switch( generation_method )
4021 {
4022 case IMPORT_KEY:
4023 ASSERT_COMPARE( data->x, data->len,
4024 first_export, first_exported_length );
4025 break;
4026 default:
4027 break;
4028 }
4029 }
4030
4031 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004032 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004033 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004034
4035exit:
4036 mbedtls_free( first_export );
4037 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004038 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004039 mbedtls_psa_crypto_free();
4040}
4041/* END_CASE */