blob: c1339c015afc748db4b0ce154149acee06d2bca1 [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;
905 psa_key_policy_t policy;
906 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_init( &policy );
915 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100916 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100917
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100918 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100919 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100920 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100921 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100922
923 if( expected_import1_status == PSA_SUCCESS ||
924 expected_import2_status == PSA_SUCCESS )
925 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100926 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100927 }
928
929exit:
930 mbedtls_psa_crypto_free( );
931}
932/* END_CASE */
933
934/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200935void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
936{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100937 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200938 size_t bits = bits_arg;
939 psa_status_t expected_status = expected_status_arg;
940 psa_status_t status;
941 psa_key_type_t type =
942 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
943 size_t buffer_size = /* Slight overapproximations */
944 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200945 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200946 unsigned char *p;
947 int ret;
948 size_t length;
949
Gilles Peskine8817f612018-12-18 00:18:46 +0100950 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200951 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200952
953 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
954 bits, keypair ) ) >= 0 );
955 length = ret;
956
957 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100958 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100959 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100960 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200961 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100962 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200963
964exit:
965 mbedtls_free( buffer );
966 mbedtls_psa_crypto_free( );
967}
968/* END_CASE */
969
970/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300971void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300972 int type_arg,
973 int alg_arg,
974 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 int expected_bits,
976 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200977 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978 int canonical_input )
979{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100980 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200982 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200983 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 unsigned char *exported = NULL;
986 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100988 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 size_t reexported_length;
990 psa_key_type_t got_type;
991 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200992 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993
Moran Pekercb088e72018-07-17 17:36:59 +0300994 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200995 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200997 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999
Gilles Peskine8817f612018-12-18 00:18:46 +01001000 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001001 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001002 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001003 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001004
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001005 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1006 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001007
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001009 PSA_ASSERT( psa_import_key( handle, type,
1010 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011
1012 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001013 PSA_ASSERT( psa_get_key_information( handle,
1014 &got_type,
1015 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001016 TEST_EQUAL( got_type, type );
1017 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018
1019 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001020 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001021 exported, export_size,
1022 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001023 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001024
1025 /* The exported length must be set by psa_export_key() to a value between 0
1026 * and export_size. On errors, the exported length must be 0. */
1027 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1028 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1029 TEST_ASSERT( exported_length <= export_size );
1030
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001031 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001032 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001034 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001035 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001037 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001038
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001039 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001040 goto exit;
1041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001043 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044 else
1045 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001046 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001047 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1048 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001049
Gilles Peskine8817f612018-12-18 00:18:46 +01001050 PSA_ASSERT( psa_import_key( handle2, type,
1051 exported,
1052 exported_length ) );
1053 PSA_ASSERT( psa_export_key( handle2,
1054 reexported,
1055 export_size,
1056 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001057 ASSERT_COMPARE( exported, exported_length,
1058 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001059 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001061 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062
1063destroy:
1064 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001065 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001066 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1067 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068
1069exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001070 mbedtls_free( exported );
1071 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001072 mbedtls_psa_crypto_free( );
1073}
1074/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001075
Moran Pekerf709f4a2018-06-06 17:26:04 +03001076/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001077void import_key_nonempty_slot( )
1078{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001079 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001080 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1081 psa_status_t status;
1082 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001084
Gilles Peskine8817f612018-12-18 00:18:46 +01001085 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1086 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001087
Moran Peker28a38e62018-11-07 16:18:24 +02001088 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001089 PSA_ASSERT( psa_import_key( handle, type,
1090 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001091
1092 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001093 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001094 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001095
1096exit:
1097 mbedtls_psa_crypto_free( );
1098}
1099/* END_CASE */
1100
1101/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001102void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001103{
1104 psa_status_t status;
1105 unsigned char *exported = NULL;
1106 size_t export_size = 0;
1107 size_t exported_length = INVALID_EXPORT_LENGTH;
1108 psa_status_t expected_export_status = expected_export_status_arg;
1109
Gilles Peskine8817f612018-12-18 00:18:46 +01001110 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001111
1112 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001113 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001114 exported, export_size,
1115 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001116 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001117
1118exit:
1119 mbedtls_psa_crypto_free( );
1120}
1121/* END_CASE */
1122
1123/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001124void export_with_no_key_activity( )
1125{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001126 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001127 psa_algorithm_t alg = PSA_ALG_CTR;
1128 psa_status_t status;
1129 psa_key_policy_t policy;
1130 unsigned char *exported = NULL;
1131 size_t export_size = 0;
1132 size_t exported_length = INVALID_EXPORT_LENGTH;
1133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001135
Gilles Peskine8817f612018-12-18 00:18:46 +01001136 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1137 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001138 psa_key_policy_init( &policy );
1139 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001140 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001141
1142 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001143 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001144 exported, export_size,
1145 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001146 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001147
1148exit:
1149 mbedtls_psa_crypto_free( );
1150}
1151/* END_CASE */
1152
1153/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001154void cipher_with_no_key_activity( )
1155{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001156 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001157 psa_status_t status;
1158 psa_key_policy_t policy;
1159 psa_cipher_operation_t operation;
1160 int exercise_alg = PSA_ALG_CTR;
1161
Gilles Peskine8817f612018-12-18 00:18:46 +01001162 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001163
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1165 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001166 psa_key_policy_init( &policy );
1167 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001168 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001169
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001170 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001171 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001172
1173exit:
1174 psa_cipher_abort( &operation );
1175 mbedtls_psa_crypto_free( );
1176}
1177/* END_CASE */
1178
1179/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001180void export_after_import_failure( data_t *data, int type_arg,
1181 int expected_import_status_arg )
1182{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001183 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001184 psa_key_type_t type = type_arg;
1185 psa_status_t status;
1186 unsigned char *exported = NULL;
1187 size_t export_size = 0;
1188 psa_status_t expected_import_status = expected_import_status_arg;
1189 size_t exported_length = INVALID_EXPORT_LENGTH;
1190
Gilles Peskine8817f612018-12-18 00:18:46 +01001191 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001192
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1194 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001195
Moran Peker34550092018-11-07 16:19:34 +02001196 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001197 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001198 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001199 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001200
1201 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001202 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001203 exported, export_size,
1204 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001205 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001206
1207exit:
1208 mbedtls_psa_crypto_free( );
1209}
1210/* END_CASE */
1211
1212/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001213void cipher_after_import_failure( data_t *data, int type_arg,
1214 int expected_import_status_arg )
1215{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001216 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001217 psa_cipher_operation_t operation;
1218 psa_key_type_t type = type_arg;
1219 psa_status_t status;
1220 psa_status_t expected_import_status = expected_import_status_arg;
1221 int exercise_alg = PSA_ALG_CTR;
1222
Gilles Peskine8817f612018-12-18 00:18:46 +01001223 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001224
Gilles Peskine8817f612018-12-18 00:18:46 +01001225 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1226 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001227
Moran Pekerce500072018-11-07 16:20:07 +02001228 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001230 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001231 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001232
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001233 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001234 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001235
1236exit:
1237 psa_cipher_abort( &operation );
1238 mbedtls_psa_crypto_free( );
1239}
1240/* END_CASE */
1241
1242/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001243void export_after_destroy_key( data_t *data, int type_arg )
1244{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001245 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001246 psa_key_type_t type = type_arg;
1247 psa_status_t status;
1248 psa_key_policy_t policy;
1249 psa_algorithm_t alg = PSA_ALG_CTR;
1250 unsigned char *exported = NULL;
1251 size_t export_size = 0;
1252 size_t exported_length = INVALID_EXPORT_LENGTH;
1253
Gilles Peskine8817f612018-12-18 00:18:46 +01001254 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001255
Gilles Peskine8817f612018-12-18 00:18:46 +01001256 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1257 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001258 psa_key_policy_init( &policy );
1259 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001261 export_size = (ptrdiff_t) data->len;
1262 ASSERT_ALLOC( exported, export_size );
1263
1264 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001265 PSA_ASSERT( psa_import_key( handle, type,
1266 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001267
Gilles Peskine8817f612018-12-18 00:18:46 +01001268 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1269 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001270
1271 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001272 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001273
1274 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001275 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001276 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001277 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001278
1279exit:
1280 mbedtls_free( exported );
1281 mbedtls_psa_crypto_free( );
1282}
1283/* END_CASE */
1284
1285/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001286void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001287 int type_arg,
1288 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001289 int export_size_delta,
1290 int expected_export_status_arg,
1291 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001293 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001294 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001295 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001296 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001297 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001298 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001299 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001300 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001301 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001302
Gilles Peskine8817f612018-12-18 00:18:46 +01001303 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001304
Gilles Peskine8817f612018-12-18 00:18:46 +01001305 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1306 &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001307 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001308 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001309 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001310
1311 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001312 PSA_ASSERT( psa_import_key( handle, type,
1313 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001314
Gilles Peskine49c25912018-10-29 15:15:31 +01001315 /* Export the public key */
1316 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001317 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001318 exported, export_size,
1319 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001320 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001321 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001322 {
1323 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1324 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001325 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001326 TEST_ASSERT( expected_public_key->len <=
1327 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001328 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1329 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001330 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001331
1332exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001333 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001334 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001335 mbedtls_psa_crypto_free( );
1336}
1337/* END_CASE */
1338
Gilles Peskine20035e32018-02-03 22:44:14 +01001339/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001340void import_and_exercise_key( data_t *data,
1341 int type_arg,
1342 int bits_arg,
1343 int alg_arg )
1344{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001345 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001346 psa_key_type_t type = type_arg;
1347 size_t bits = bits_arg;
1348 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001349 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001350 psa_key_policy_t policy;
1351 psa_key_type_t got_type;
1352 size_t got_bits;
1353 psa_status_t status;
1354
Gilles Peskine8817f612018-12-18 00:18:46 +01001355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001356
Gilles Peskine8817f612018-12-18 00:18:46 +01001357 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1358 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359 psa_key_policy_init( &policy );
1360 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001361 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001362
1363 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001364 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001365 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001366
1367 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001368 PSA_ASSERT( psa_get_key_information( handle,
1369 &got_type,
1370 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001371 TEST_EQUAL( got_type, type );
1372 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001373
1374 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001375 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001376 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001377
1378exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001379 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001380 mbedtls_psa_crypto_free( );
1381}
1382/* END_CASE */
1383
1384/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001385void key_policy( int usage_arg, int alg_arg )
1386{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001387 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001388 psa_algorithm_t alg = alg_arg;
1389 psa_key_usage_t usage = usage_arg;
1390 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1391 unsigned char key[32] = {0};
1392 psa_key_policy_t policy_set;
1393 psa_key_policy_t policy_get;
1394
1395 memset( key, 0x2a, sizeof( key ) );
1396
Gilles Peskine8817f612018-12-18 00:18:46 +01001397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001398
Gilles Peskine8817f612018-12-18 00:18:46 +01001399 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1400 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001401 psa_key_policy_init( &policy_set );
1402 psa_key_policy_init( &policy_get );
Gilles Peskined5b33222018-06-18 22:20:03 +02001403 psa_key_policy_set_usage( &policy_set, usage, alg );
1404
Gilles Peskinefe11b722018-12-18 00:24:04 +01001405 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1406 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001407 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001408
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( psa_import_key( handle, key_type,
1410 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001411
Gilles Peskine8817f612018-12-18 00:18:46 +01001412 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001413
Gilles Peskinefe11b722018-12-18 00:24:04 +01001414 TEST_EQUAL( policy_get.usage, policy_set.usage );
1415 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001416
1417exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001418 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001419 mbedtls_psa_crypto_free( );
1420}
1421/* END_CASE */
1422
1423/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001424void mac_key_policy( int policy_usage,
1425 int policy_alg,
1426 int key_type,
1427 data_t *key_data,
1428 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001429{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001430 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001431 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001432 psa_mac_operation_t operation;
1433 psa_status_t status;
1434 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001435
Gilles Peskine8817f612018-12-18 00:18:46 +01001436 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001437
Gilles Peskine8817f612018-12-18 00:18:46 +01001438 PSA_ASSERT( psa_allocate_key( key_type,
1439 KEY_BITS_FROM_DATA( key_type, key_data ),
1440 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001441 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001442 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001443 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001444
Gilles Peskine8817f612018-12-18 00:18:46 +01001445 PSA_ASSERT( psa_import_key( handle, key_type,
1446 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001447
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001448 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 if( policy_alg == exercise_alg &&
1450 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001451 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001452 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001453 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001454 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001455
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001456 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001457 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458 if( policy_alg == exercise_alg &&
1459 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001461 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001462 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001463
1464exit:
1465 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001466 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 mbedtls_psa_crypto_free( );
1468}
1469/* END_CASE */
1470
1471/* BEGIN_CASE */
1472void cipher_key_policy( int policy_usage,
1473 int policy_alg,
1474 int key_type,
1475 data_t *key_data,
1476 int exercise_alg )
1477{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001478 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001479 psa_key_policy_t policy;
1480 psa_cipher_operation_t operation;
1481 psa_status_t status;
1482
Gilles Peskine8817f612018-12-18 00:18:46 +01001483 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001484
Gilles Peskine8817f612018-12-18 00:18:46 +01001485 PSA_ASSERT( psa_allocate_key( key_type,
1486 KEY_BITS_FROM_DATA( key_type, key_data ),
1487 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001488 psa_key_policy_init( &policy );
1489 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001490 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001491
Gilles Peskine8817f612018-12-18 00:18:46 +01001492 PSA_ASSERT( psa_import_key( handle, key_type,
1493 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001494
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001495 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001496 if( policy_alg == exercise_alg &&
1497 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001500 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001501 psa_cipher_abort( &operation );
1502
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001503 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001504 if( policy_alg == exercise_alg &&
1505 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001506 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001507 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001508 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001509
1510exit:
1511 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001512 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513 mbedtls_psa_crypto_free( );
1514}
1515/* END_CASE */
1516
1517/* BEGIN_CASE */
1518void aead_key_policy( int policy_usage,
1519 int policy_alg,
1520 int key_type,
1521 data_t *key_data,
1522 int nonce_length_arg,
1523 int tag_length_arg,
1524 int exercise_alg )
1525{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001526 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001527 psa_key_policy_t policy;
1528 psa_status_t status;
1529 unsigned char nonce[16] = {0};
1530 size_t nonce_length = nonce_length_arg;
1531 unsigned char tag[16];
1532 size_t tag_length = tag_length_arg;
1533 size_t output_length;
1534
1535 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1536 TEST_ASSERT( tag_length <= sizeof( tag ) );
1537
Gilles Peskine8817f612018-12-18 00:18:46 +01001538 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001539
Gilles Peskine8817f612018-12-18 00:18:46 +01001540 PSA_ASSERT( psa_allocate_key( key_type,
1541 KEY_BITS_FROM_DATA( key_type, key_data ),
1542 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001543 psa_key_policy_init( &policy );
1544 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001545 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001546
Gilles Peskine8817f612018-12-18 00:18:46 +01001547 PSA_ASSERT( psa_import_key( handle, key_type,
1548 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001549
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001550 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001551 nonce, nonce_length,
1552 NULL, 0,
1553 NULL, 0,
1554 tag, tag_length,
1555 &output_length );
1556 if( policy_alg == exercise_alg &&
1557 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001559 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001560 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001561
1562 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 nonce, nonce_length,
1565 NULL, 0,
1566 tag, tag_length,
1567 NULL, 0,
1568 &output_length );
1569 if( policy_alg == exercise_alg &&
1570 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001571 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
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
1575exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001576 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 mbedtls_psa_crypto_free( );
1578}
1579/* END_CASE */
1580
1581/* BEGIN_CASE */
1582void asymmetric_encryption_key_policy( int policy_usage,
1583 int policy_alg,
1584 int key_type,
1585 data_t *key_data,
1586 int exercise_alg )
1587{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001588 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001589 psa_key_policy_t policy;
1590 psa_status_t status;
1591 size_t key_bits;
1592 size_t buffer_length;
1593 unsigned char *buffer = NULL;
1594 size_t output_length;
1595
Gilles Peskine8817f612018-12-18 00:18:46 +01001596 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001597
Gilles Peskine8817f612018-12-18 00:18:46 +01001598 PSA_ASSERT( psa_allocate_key( key_type,
1599 KEY_BITS_FROM_DATA( key_type, key_data ),
1600 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001601 psa_key_policy_init( &policy );
1602 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001603 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001604
Gilles Peskine8817f612018-12-18 00:18:46 +01001605 PSA_ASSERT( psa_import_key( handle, key_type,
1606 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001607
Gilles Peskine8817f612018-12-18 00:18:46 +01001608 PSA_ASSERT( psa_get_key_information( handle,
1609 NULL,
1610 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1612 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001613 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001615 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616 NULL, 0,
1617 NULL, 0,
1618 buffer, buffer_length,
1619 &output_length );
1620 if( policy_alg == exercise_alg &&
1621 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001622 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001624 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001626 if( buffer_length != 0 )
1627 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001628 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629 buffer, buffer_length,
1630 NULL, 0,
1631 buffer, buffer_length,
1632 &output_length );
1633 if( policy_alg == exercise_alg &&
1634 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001635 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001637 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001638
1639exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 mbedtls_psa_crypto_free( );
1642 mbedtls_free( buffer );
1643}
1644/* END_CASE */
1645
1646/* BEGIN_CASE */
1647void asymmetric_signature_key_policy( int policy_usage,
1648 int policy_alg,
1649 int key_type,
1650 data_t *key_data,
1651 int exercise_alg )
1652{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001653 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654 psa_key_policy_t policy;
1655 psa_status_t status;
1656 unsigned char payload[16] = {1};
1657 size_t payload_length = sizeof( payload );
1658 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1659 size_t signature_length;
1660
Gilles Peskine8817f612018-12-18 00:18:46 +01001661 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662
Gilles Peskine8817f612018-12-18 00:18:46 +01001663 PSA_ASSERT( psa_allocate_key( key_type,
1664 KEY_BITS_FROM_DATA( key_type, key_data ),
1665 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001666 psa_key_policy_init( &policy );
1667 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001668 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001669
Gilles Peskine8817f612018-12-18 00:18:46 +01001670 PSA_ASSERT( psa_import_key( handle, key_type,
1671 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001672
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001673 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001675 signature, sizeof( signature ),
1676 &signature_length );
1677 if( policy_alg == exercise_alg &&
1678 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001681 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001682
1683 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 status = psa_asymmetric_verify( 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 if( policy_alg == exercise_alg &&
1688 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001689 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001690 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001691 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001692
1693exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001694 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001695 mbedtls_psa_crypto_free( );
1696}
1697/* END_CASE */
1698
1699/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001700void derive_key_policy( int policy_usage,
1701 int policy_alg,
1702 int key_type,
1703 data_t *key_data,
1704 int exercise_alg )
1705{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001706 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001707 psa_key_policy_t policy;
1708 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1709 psa_status_t status;
1710
Gilles Peskine8817f612018-12-18 00:18:46 +01001711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001712
Gilles Peskine8817f612018-12-18 00:18:46 +01001713 PSA_ASSERT( psa_allocate_key( key_type,
1714 KEY_BITS_FROM_DATA( key_type, key_data ),
1715 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001716 psa_key_policy_init( &policy );
1717 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001719
Gilles Peskine8817f612018-12-18 00:18:46 +01001720 PSA_ASSERT( psa_import_key( handle, key_type,
1721 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001722
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001723 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001724 exercise_alg,
1725 NULL, 0,
1726 NULL, 0,
1727 1 );
1728 if( policy_alg == exercise_alg &&
1729 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001731 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001732 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001733
1734exit:
1735 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001736 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001737 mbedtls_psa_crypto_free( );
1738}
1739/* END_CASE */
1740
1741/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001742void agreement_key_policy( int policy_usage,
1743 int policy_alg,
1744 int key_type_arg,
1745 data_t *key_data,
1746 int exercise_alg )
1747{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001748 psa_key_handle_t handle = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001749 psa_key_policy_t policy;
1750 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001751 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1752 psa_status_t status;
1753
Gilles Peskine8817f612018-12-18 00:18:46 +01001754 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001755
Gilles Peskine8817f612018-12-18 00:18:46 +01001756 PSA_ASSERT( psa_allocate_key( key_type,
1757 KEY_BITS_FROM_DATA( key_type, key_data ),
1758 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001759 psa_key_policy_init( &policy );
1760 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001761 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001762
Gilles Peskine8817f612018-12-18 00:18:46 +01001763 PSA_ASSERT( psa_import_key( handle, key_type,
1764 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001766 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001767
Gilles Peskine01d718c2018-09-18 12:01:02 +02001768 if( policy_alg == exercise_alg &&
1769 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001771 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001772 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001773
1774exit:
1775 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001776 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001777 mbedtls_psa_crypto_free( );
1778}
1779/* END_CASE */
1780
1781/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001782void hash_setup( int alg_arg,
1783 int expected_status_arg )
1784{
1785 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001786 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001787 psa_hash_operation_t operation;
1788 psa_status_t status;
1789
Gilles Peskine8817f612018-12-18 00:18:46 +01001790 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001791
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001792 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001793 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001794 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001795
1796exit:
1797 mbedtls_psa_crypto_free( );
1798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001802void hash_bad_order( )
1803{
1804 unsigned char input[] = "";
1805 /* SHA-256 hash of an empty string */
1806 unsigned char hash[] = {
1807 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1808 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1809 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1810 size_t hash_len;
1811 psa_hash_operation_t operation;
1812
Gilles Peskine8817f612018-12-18 00:18:46 +01001813 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001814
1815 /* psa_hash_update without calling psa_hash_setup beforehand */
1816 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001817 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001818 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001819
1820 /* psa_hash_verify without calling psa_hash_setup beforehand */
1821 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001822 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001823 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001824
1825 /* psa_hash_finish without calling psa_hash_setup beforehand */
1826 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001827 TEST_EQUAL( psa_hash_finish( &operation,
1828 hash, sizeof( hash ), &hash_len ),
1829 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001830
1831exit:
1832 mbedtls_psa_crypto_free( );
1833}
1834/* END_CASE */
1835
itayzafrir27e69452018-11-01 14:26:34 +02001836/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1837void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001838{
1839 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001840 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1841 * appended to it */
1842 unsigned char hash[] = {
1843 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1844 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1845 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001846 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001847 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001848
Gilles Peskine8817f612018-12-18 00:18:46 +01001849 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001850
itayzafrir27e69452018-11-01 14:26:34 +02001851 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001852 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001853 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001854 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001855
itayzafrir27e69452018-11-01 14:26:34 +02001856 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001857 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001858 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001859 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001860
itayzafrir27e69452018-11-01 14:26:34 +02001861 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001862 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001863 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001864 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001865
itayzafrirec93d302018-10-18 18:01:10 +03001866exit:
1867 mbedtls_psa_crypto_free( );
1868}
1869/* END_CASE */
1870
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001871/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1872void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001873{
1874 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001875 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001876 size_t expected_size = PSA_HASH_SIZE( alg );
1877 psa_hash_operation_t operation;
1878 size_t hash_len;
1879
Gilles Peskine8817f612018-12-18 00:18:46 +01001880 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001881
itayzafrir58028322018-10-25 10:22:01 +03001882 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001883 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001884 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001885 hash, expected_size - 1, &hash_len ),
1886 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001887
1888exit:
1889 mbedtls_psa_crypto_free( );
1890}
1891/* END_CASE */
1892
1893/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001894void mac_setup( int key_type_arg,
1895 data_t *key,
1896 int alg_arg,
1897 int expected_status_arg )
1898{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001899 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001900 psa_key_type_t key_type = key_type_arg;
1901 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001902 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001903 psa_mac_operation_t operation;
1904 psa_key_policy_t policy;
1905 psa_status_t status;
1906
Gilles Peskine8817f612018-12-18 00:18:46 +01001907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908
Gilles Peskine8817f612018-12-18 00:18:46 +01001909 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1910 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001911 psa_key_policy_init( &policy );
1912 psa_key_policy_set_usage( &policy,
1913 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1914 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001915 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001916
Gilles Peskine8817f612018-12-18 00:18:46 +01001917 PSA_ASSERT( psa_import_key( handle, key_type,
1918 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001919
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001920 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001921 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001922 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001923
1924exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001925 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001926 mbedtls_psa_crypto_free( );
1927}
1928/* END_CASE */
1929
1930/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001931void mac_sign( int key_type_arg,
1932 data_t *key,
1933 int alg_arg,
1934 data_t *input,
1935 data_t *expected_mac )
1936{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001937 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001938 psa_key_type_t key_type = key_type_arg;
1939 psa_algorithm_t alg = alg_arg;
1940 psa_mac_operation_t operation;
1941 psa_key_policy_t policy;
1942 /* Leave a little extra room in the output buffer. At the end of the
1943 * test, we'll check that the implementation didn't overwrite onto
1944 * this extra room. */
1945 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1946 size_t mac_buffer_size =
1947 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1948 size_t mac_length = 0;
1949
1950 memset( actual_mac, '+', sizeof( actual_mac ) );
1951 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1952 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1953
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001955
Gilles Peskine8817f612018-12-18 00:18:46 +01001956 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1957 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001958 psa_key_policy_init( &policy );
1959 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001960 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001961
Gilles Peskine8817f612018-12-18 00:18:46 +01001962 PSA_ASSERT( psa_import_key( handle, key_type,
1963 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001964
1965 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01001966 PSA_ASSERT( psa_mac_sign_setup( &operation,
1967 handle, alg ) );
1968 PSA_ASSERT( psa_mac_update( &operation,
1969 input->x, input->len ) );
1970 PSA_ASSERT( psa_mac_sign_finish( &operation,
1971 actual_mac, mac_buffer_size,
1972 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001973
1974 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01001975 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
1976 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001977
1978 /* Verify that the end of the buffer is untouched. */
1979 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1980 sizeof( actual_mac ) - mac_length ) );
1981
1982exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001983 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001984 mbedtls_psa_crypto_free( );
1985}
1986/* END_CASE */
1987
1988/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001989void mac_verify( int key_type_arg,
1990 data_t *key,
1991 int alg_arg,
1992 data_t *input,
1993 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001995 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001996 psa_key_type_t key_type = key_type_arg;
1997 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001998 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001999 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002000
Gilles Peskine69c12672018-06-28 00:07:19 +02002001 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2002
Gilles Peskine8817f612018-12-18 00:18:46 +01002003 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002004
Gilles Peskine8817f612018-12-18 00:18:46 +01002005 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2006 &handle ) );
mohammad16036df908f2018-04-02 08:34:15 -07002007 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002008 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002009 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002010
Gilles Peskine8817f612018-12-18 00:18:46 +01002011 PSA_ASSERT( psa_import_key( handle, key_type,
2012 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002013
Gilles Peskine8817f612018-12-18 00:18:46 +01002014 PSA_ASSERT( psa_mac_verify_setup( &operation,
2015 handle, alg ) );
2016 PSA_ASSERT( psa_destroy_key( handle ) );
2017 PSA_ASSERT( psa_mac_update( &operation,
2018 input->x, input->len ) );
2019 PSA_ASSERT( psa_mac_verify_finish( &operation,
2020 expected_mac->x,
2021 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022
2023exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002024 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025 mbedtls_psa_crypto_free( );
2026}
2027/* END_CASE */
2028
2029/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002030void cipher_setup( int key_type_arg,
2031 data_t *key,
2032 int alg_arg,
2033 int expected_status_arg )
2034{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002035 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002036 psa_key_type_t key_type = key_type_arg;
2037 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002038 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002039 psa_cipher_operation_t operation;
2040 psa_key_policy_t policy;
2041 psa_status_t status;
2042
Gilles Peskine8817f612018-12-18 00:18:46 +01002043 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002044
Gilles Peskine8817f612018-12-18 00:18:46 +01002045 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2046 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002047 psa_key_policy_init( &policy );
2048 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002049 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002050
Gilles Peskine8817f612018-12-18 00:18:46 +01002051 PSA_ASSERT( psa_import_key( handle, key_type,
2052 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002053
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002054 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002055 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002056 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002057
2058exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002059 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002060 mbedtls_psa_crypto_free( );
2061}
2062/* END_CASE */
2063
2064/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002065void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002066 data_t *key,
2067 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002068 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002069{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002070 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002071 psa_status_t status;
2072 psa_key_type_t key_type = key_type_arg;
2073 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002074 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002075 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002076 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002077 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002078 size_t output_buffer_size = 0;
2079 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002080 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002081 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002082 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002083
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002084 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2085 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002086
Gilles Peskine8817f612018-12-18 00:18:46 +01002087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002088
Gilles Peskine8817f612018-12-18 00:18:46 +01002089 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2090 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002091 psa_key_policy_init( &policy );
2092 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002093 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002094
Gilles Peskine8817f612018-12-18 00:18:46 +01002095 PSA_ASSERT( psa_import_key( handle, key_type,
2096 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002097
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2099 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002100
Gilles Peskine8817f612018-12-18 00:18:46 +01002101 PSA_ASSERT( psa_cipher_set_iv( &operation,
2102 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002103 output_buffer_size = ( (size_t) input->len +
2104 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002105 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002106
Gilles Peskine8817f612018-12-18 00:18:46 +01002107 PSA_ASSERT( psa_cipher_update( &operation,
2108 input->x, input->len,
2109 output, output_buffer_size,
2110 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002111 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002112 status = psa_cipher_finish( &operation,
2113 output + function_output_length,
2114 output_buffer_size,
2115 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002116 total_output_length += function_output_length;
2117
Gilles Peskinefe11b722018-12-18 00:24:04 +01002118 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002119 if( expected_status == PSA_SUCCESS )
2120 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002121 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002122 ASSERT_COMPARE( expected_output->x, expected_output->len,
2123 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002124 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002125
Gilles Peskine50e586b2018-06-08 14:28:46 +02002126exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002127 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002128 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002129 mbedtls_psa_crypto_free( );
2130}
2131/* END_CASE */
2132
2133/* BEGIN_CASE */
2134void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002135 data_t *key,
2136 data_t *input,
2137 int first_part_size,
2138 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002139{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002140 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002141 psa_key_type_t key_type = key_type_arg;
2142 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002143 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002144 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002145 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146 size_t output_buffer_size = 0;
2147 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002148 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002149 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002150 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002152 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2153 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002154
Gilles Peskine8817f612018-12-18 00:18:46 +01002155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002156
Gilles Peskine8817f612018-12-18 00:18:46 +01002157 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2158 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002159 psa_key_policy_init( &policy );
2160 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002161 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002162
Gilles Peskine8817f612018-12-18 00:18:46 +01002163 PSA_ASSERT( psa_import_key( handle, key_type,
2164 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002165
Gilles Peskine8817f612018-12-18 00:18:46 +01002166 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2167 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002168
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( psa_cipher_set_iv( &operation,
2170 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002171 output_buffer_size = ( (size_t) input->len +
2172 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002173 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002174
Gilles Peskine4abf7412018-06-18 16:35:34 +02002175 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002176 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2177 output, output_buffer_size,
2178 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002179 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002180 PSA_ASSERT( psa_cipher_update( &operation,
2181 input->x + first_part_size,
2182 input->len - first_part_size,
2183 output, output_buffer_size,
2184 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002185 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( psa_cipher_finish( &operation,
2187 output + function_output_length,
2188 output_buffer_size,
2189 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002190 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002192
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002193 ASSERT_COMPARE( expected_output->x, expected_output->len,
2194 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195
2196exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002197 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002198 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002199 mbedtls_psa_crypto_free( );
2200}
2201/* END_CASE */
2202
2203/* BEGIN_CASE */
2204void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002205 data_t *key,
2206 data_t *input,
2207 int first_part_size,
2208 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002209{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002210 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002211
2212 psa_key_type_t key_type = key_type_arg;
2213 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002214 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002215 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002216 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002217 size_t output_buffer_size = 0;
2218 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002219 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002220 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002221 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002222
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002223 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2224 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002225
Gilles Peskine8817f612018-12-18 00:18:46 +01002226 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227
Gilles Peskine8817f612018-12-18 00:18:46 +01002228 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2229 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002230 psa_key_policy_init( &policy );
2231 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002232 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002233
Gilles Peskine8817f612018-12-18 00:18:46 +01002234 PSA_ASSERT( psa_import_key( handle, key_type,
2235 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002236
Gilles Peskine8817f612018-12-18 00:18:46 +01002237 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2238 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002239
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( psa_cipher_set_iv( &operation,
2241 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002242
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002243 output_buffer_size = ( (size_t) input->len +
2244 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002245 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002246
Gilles Peskine4abf7412018-06-18 16:35:34 +02002247 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002248 PSA_ASSERT( psa_cipher_update( &operation,
2249 input->x, first_part_size,
2250 output, output_buffer_size,
2251 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002252 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002253 PSA_ASSERT( psa_cipher_update( &operation,
2254 input->x + first_part_size,
2255 input->len - first_part_size,
2256 output, output_buffer_size,
2257 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002258 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002259 PSA_ASSERT( psa_cipher_finish( &operation,
2260 output + function_output_length,
2261 output_buffer_size,
2262 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002263 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002264 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002265
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002266 ASSERT_COMPARE( expected_output->x, expected_output->len,
2267 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002268
2269exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002270 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002271 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002272 mbedtls_psa_crypto_free( );
2273}
2274/* END_CASE */
2275
Gilles Peskine50e586b2018-06-08 14:28:46 +02002276/* BEGIN_CASE */
2277void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002278 data_t *key,
2279 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002280 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002281{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002282 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002283 psa_status_t status;
2284 psa_key_type_t key_type = key_type_arg;
2285 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002286 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002287 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002288 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002289 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002290 size_t output_buffer_size = 0;
2291 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002292 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002293 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002294 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002295
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002296 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2297 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002298
Gilles Peskine8817f612018-12-18 00:18:46 +01002299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002300
Gilles Peskine8817f612018-12-18 00:18:46 +01002301 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2302 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002303 psa_key_policy_init( &policy );
2304 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002305 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002306
Gilles Peskine8817f612018-12-18 00:18:46 +01002307 PSA_ASSERT( psa_import_key( handle, key_type,
2308 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002309
Gilles Peskine8817f612018-12-18 00:18:46 +01002310 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2311 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002312
Gilles Peskine8817f612018-12-18 00:18:46 +01002313 PSA_ASSERT( psa_cipher_set_iv( &operation,
2314 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002315
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002316 output_buffer_size = ( (size_t) input->len +
2317 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002318 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319
Gilles Peskine8817f612018-12-18 00:18:46 +01002320 PSA_ASSERT( psa_cipher_update( &operation,
2321 input->x, input->len,
2322 output, output_buffer_size,
2323 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002324 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002325 status = psa_cipher_finish( &operation,
2326 output + function_output_length,
2327 output_buffer_size,
2328 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002329 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002330 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002331
2332 if( expected_status == PSA_SUCCESS )
2333 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002334 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002335 ASSERT_COMPARE( expected_output->x, expected_output->len,
2336 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337 }
2338
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002340 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002341 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342 mbedtls_psa_crypto_free( );
2343}
2344/* END_CASE */
2345
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346/* BEGIN_CASE */
2347void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002348 data_t *key,
2349 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002350{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002351 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002352 psa_key_type_t key_type = key_type_arg;
2353 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002354 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002355 size_t iv_size = 16;
2356 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002357 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002358 size_t output1_size = 0;
2359 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002360 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002361 size_t output2_size = 0;
2362 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002363 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002364 psa_cipher_operation_t operation1;
2365 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002366 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002367
Gilles Peskine8817f612018-12-18 00:18:46 +01002368 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002369
Gilles Peskine8817f612018-12-18 00:18:46 +01002370 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2371 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002372 psa_key_policy_init( &policy );
2373 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;
Moran Pekered346952018-07-05 15:22:45 +03002452 psa_key_policy_t policy;
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_init( &policy );
2459 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002460 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002461
Gilles Peskine8817f612018-12-18 00:18:46 +01002462 PSA_ASSERT( psa_import_key( handle, key_type,
2463 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002464
Gilles Peskine8817f612018-12-18 00:18:46 +01002465 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2466 handle, alg ) );
2467 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2468 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002469
Gilles Peskine8817f612018-12-18 00:18:46 +01002470 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2471 iv, iv_size,
2472 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002473 output1_buffer_size = ( (size_t) input->len +
2474 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002475 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002476
Gilles Peskine4abf7412018-06-18 16:35:34 +02002477 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002478
Gilles Peskine8817f612018-12-18 00:18:46 +01002479 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2480 output1, output1_buffer_size,
2481 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002482 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002483
Gilles Peskine8817f612018-12-18 00:18:46 +01002484 PSA_ASSERT( psa_cipher_update( &operation1,
2485 input->x + first_part_size,
2486 input->len - first_part_size,
2487 output1, output1_buffer_size,
2488 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002489 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002490
Gilles Peskine8817f612018-12-18 00:18:46 +01002491 PSA_ASSERT( psa_cipher_finish( &operation1,
2492 output1 + output1_length,
2493 output1_buffer_size - output1_length,
2494 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002495 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002496
Gilles Peskine8817f612018-12-18 00:18:46 +01002497 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002498
Gilles Peskine048b7f02018-06-08 14:20:49 +02002499 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002500 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002501
Gilles Peskine8817f612018-12-18 00:18:46 +01002502 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2503 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002504
Gilles Peskine8817f612018-12-18 00:18:46 +01002505 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2506 output2, output2_buffer_size,
2507 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002508 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002509
Gilles Peskine8817f612018-12-18 00:18:46 +01002510 PSA_ASSERT( psa_cipher_update( &operation2,
2511 output1 + first_part_size,
2512 output1_length - first_part_size,
2513 output2, output2_buffer_size,
2514 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002515 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002516
Gilles Peskine8817f612018-12-18 00:18:46 +01002517 PSA_ASSERT( psa_cipher_finish( &operation2,
2518 output2 + output2_length,
2519 output2_buffer_size - output2_length,
2520 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002521 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002522
Gilles Peskine8817f612018-12-18 00:18:46 +01002523 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002524
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002525 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002526
2527exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002528 mbedtls_free( output1 );
2529 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002530 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002531 mbedtls_psa_crypto_free( );
2532}
2533/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002534
Gilles Peskine20035e32018-02-03 22:44:14 +01002535/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002536void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002537 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002538 data_t *nonce,
2539 data_t *additional_data,
2540 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002541 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002542{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002543 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002544 psa_key_type_t key_type = key_type_arg;
2545 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002546 unsigned char *output_data = NULL;
2547 size_t output_size = 0;
2548 size_t output_length = 0;
2549 unsigned char *output_data2 = NULL;
2550 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002551 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002552 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002553 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002554
Gilles Peskine4abf7412018-06-18 16:35:34 +02002555 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002556 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002557
Gilles Peskine8817f612018-12-18 00:18:46 +01002558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002559
Gilles Peskine8817f612018-12-18 00:18:46 +01002560 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2561 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002562 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002563 psa_key_policy_set_usage( &policy,
2564 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2565 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002567
Gilles Peskine8817f612018-12-18 00:18:46 +01002568 PSA_ASSERT( psa_import_key( handle, key_type,
2569 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002570
Gilles Peskinefe11b722018-12-18 00:24:04 +01002571 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2572 nonce->x, nonce->len,
2573 additional_data->x,
2574 additional_data->len,
2575 input_data->x, input_data->len,
2576 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002577 &output_length ),
2578 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002579
2580 if( PSA_SUCCESS == expected_result )
2581 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002582 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002583
Gilles Peskinefe11b722018-12-18 00:24:04 +01002584 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2585 nonce->x, nonce->len,
2586 additional_data->x,
2587 additional_data->len,
2588 output_data, output_length,
2589 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002590 &output_length2 ),
2591 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002592
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002593 ASSERT_COMPARE( input_data->x, input_data->len,
2594 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002595 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002596
Gilles Peskinea1cac842018-06-11 19:33:02 +02002597exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002598 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002599 mbedtls_free( output_data );
2600 mbedtls_free( output_data2 );
2601 mbedtls_psa_crypto_free( );
2602}
2603/* END_CASE */
2604
2605/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002606void aead_encrypt( int key_type_arg, data_t *key_data,
2607 int alg_arg,
2608 data_t *nonce,
2609 data_t *additional_data,
2610 data_t *input_data,
2611 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002612{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002613 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002614 psa_key_type_t key_type = key_type_arg;
2615 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002616 unsigned char *output_data = NULL;
2617 size_t output_size = 0;
2618 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002619 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002620 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002621
Gilles Peskine4abf7412018-06-18 16:35:34 +02002622 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002623 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002626
Gilles Peskine8817f612018-12-18 00:18:46 +01002627 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2628 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002630 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002631 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002632
Gilles Peskine8817f612018-12-18 00:18:46 +01002633 PSA_ASSERT( psa_import_key( handle, key_type,
2634 key_data->x,
2635 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002636
Gilles Peskine8817f612018-12-18 00:18:46 +01002637 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2638 nonce->x, nonce->len,
2639 additional_data->x, additional_data->len,
2640 input_data->x, input_data->len,
2641 output_data, output_size,
2642 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002643
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002644 ASSERT_COMPARE( expected_result->x, expected_result->len,
2645 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002646
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002648 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002649 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002650 mbedtls_psa_crypto_free( );
2651}
2652/* END_CASE */
2653
2654/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002655void aead_decrypt( int key_type_arg, data_t *key_data,
2656 int alg_arg,
2657 data_t *nonce,
2658 data_t *additional_data,
2659 data_t *input_data,
2660 data_t *expected_data,
2661 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002662{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002663 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002664 psa_key_type_t key_type = key_type_arg;
2665 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002666 unsigned char *output_data = NULL;
2667 size_t output_size = 0;
2668 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002670 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002671 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672
Gilles Peskine4abf7412018-06-18 16:35:34 +02002673 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002674 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002675
Gilles Peskine8817f612018-12-18 00:18:46 +01002676 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002677
Gilles Peskine8817f612018-12-18 00:18:46 +01002678 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2679 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002680 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002681 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002682 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002683
Gilles Peskine8817f612018-12-18 00:18:46 +01002684 PSA_ASSERT( psa_import_key( handle, key_type,
2685 key_data->x,
2686 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002687
Gilles Peskinefe11b722018-12-18 00:24:04 +01002688 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2689 nonce->x, nonce->len,
2690 additional_data->x,
2691 additional_data->len,
2692 input_data->x, input_data->len,
2693 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002694 &output_length ),
2695 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696
Gilles Peskine2d277862018-06-18 15:41:12 +02002697 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002698 ASSERT_COMPARE( expected_data->x, expected_data->len,
2699 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002700
Gilles Peskinea1cac842018-06-11 19:33:02 +02002701exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002702 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002703 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002704 mbedtls_psa_crypto_free( );
2705}
2706/* END_CASE */
2707
2708/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002709void signature_size( int type_arg,
2710 int bits,
2711 int alg_arg,
2712 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002713{
2714 psa_key_type_t type = type_arg;
2715 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002716 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002717 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002718exit:
2719 ;
2720}
2721/* END_CASE */
2722
2723/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002724void sign_deterministic( int key_type_arg, data_t *key_data,
2725 int alg_arg, data_t *input_data,
2726 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002727{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002728 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002729 psa_key_type_t key_type = key_type_arg;
2730 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002731 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002732 unsigned char *signature = NULL;
2733 size_t signature_size;
2734 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002735 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002736
Gilles Peskine8817f612018-12-18 00:18:46 +01002737 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002738
Gilles Peskine8817f612018-12-18 00:18:46 +01002739 PSA_ASSERT( psa_allocate_key( key_type,
2740 KEY_BITS_FROM_DATA( key_type, key_data ),
2741 &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002742 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002743 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002744 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002745
Gilles Peskine8817f612018-12-18 00:18:46 +01002746 PSA_ASSERT( psa_import_key( handle, key_type,
2747 key_data->x,
2748 key_data->len ) );
2749 PSA_ASSERT( psa_get_key_information( handle,
2750 NULL,
2751 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002752
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002753 /* Allocate a buffer which has the size advertized by the
2754 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002755 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2756 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002757 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002758 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002759 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002760
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002761 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002762 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2763 input_data->x, input_data->len,
2764 signature, signature_size,
2765 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002766 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002767 ASSERT_COMPARE( output_data->x, output_data->len,
2768 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002769
2770exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002771 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002772 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002773 mbedtls_psa_crypto_free( );
2774}
2775/* END_CASE */
2776
2777/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002778void sign_fail( int key_type_arg, data_t *key_data,
2779 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002780 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002781{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002782 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002783 psa_key_type_t key_type = key_type_arg;
2784 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002785 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002786 psa_status_t actual_status;
2787 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002788 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002789 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002790 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002791
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002792 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002793
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002795
Gilles Peskine8817f612018-12-18 00:18:46 +01002796 PSA_ASSERT( psa_allocate_key( key_type,
2797 KEY_BITS_FROM_DATA( key_type, key_data ),
2798 &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002799 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002800 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002801 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002802
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_import_key( handle, key_type,
2804 key_data->x,
2805 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002806
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002807 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002808 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002809 signature, signature_size,
2810 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002811 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002812 /* The value of *signature_length is unspecified on error, but
2813 * whatever it is, it should be less than signature_size, so that
2814 * if the caller tries to read *signature_length bytes without
2815 * checking the error code then they don't overflow a buffer. */
2816 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002817
2818exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002819 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002820 mbedtls_free( signature );
2821 mbedtls_psa_crypto_free( );
2822}
2823/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002824
2825/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002826void sign_verify( int key_type_arg, data_t *key_data,
2827 int alg_arg, data_t *input_data )
2828{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002829 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002830 psa_key_type_t key_type = key_type_arg;
2831 psa_algorithm_t alg = alg_arg;
2832 size_t key_bits;
2833 unsigned char *signature = NULL;
2834 size_t signature_size;
2835 size_t signature_length = 0xdeadbeef;
2836 psa_key_policy_t policy;
2837
Gilles Peskine8817f612018-12-18 00:18:46 +01002838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002839
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 PSA_ASSERT( psa_allocate_key( key_type,
2841 KEY_BITS_FROM_DATA( key_type, key_data ),
2842 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002843 psa_key_policy_init( &policy );
2844 psa_key_policy_set_usage( &policy,
2845 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2846 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002847 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002848
Gilles Peskine8817f612018-12-18 00:18:46 +01002849 PSA_ASSERT( psa_import_key( handle, key_type,
2850 key_data->x,
2851 key_data->len ) );
2852 PSA_ASSERT( psa_get_key_information( handle,
2853 NULL,
2854 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002855
2856 /* Allocate a buffer which has the size advertized by the
2857 * library. */
2858 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2859 key_bits, alg );
2860 TEST_ASSERT( signature_size != 0 );
2861 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002862 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002863
2864 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2866 input_data->x, input_data->len,
2867 signature, signature_size,
2868 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002869 /* Check that the signature length looks sensible. */
2870 TEST_ASSERT( signature_length <= signature_size );
2871 TEST_ASSERT( signature_length > 0 );
2872
2873 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 PSA_ASSERT( psa_asymmetric_verify(
2875 handle, alg,
2876 input_data->x, input_data->len,
2877 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002878
2879 if( input_data->len != 0 )
2880 {
2881 /* Flip a bit in the input and verify that the signature is now
2882 * detected as invalid. Flip a bit at the beginning, not at the end,
2883 * because ECDSA may ignore the last few bits of the input. */
2884 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002885 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2886 input_data->x, input_data->len,
2887 signature, signature_length ),
2888 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002889 }
2890
2891exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002892 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002893 mbedtls_free( signature );
2894 mbedtls_psa_crypto_free( );
2895}
2896/* END_CASE */
2897
2898/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002899void asymmetric_verify( int key_type_arg, data_t *key_data,
2900 int alg_arg, data_t *hash_data,
2901 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002902{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002903 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002904 psa_key_type_t key_type = key_type_arg;
2905 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002906 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002907
Gilles Peskine69c12672018-06-28 00:07:19 +02002908 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2909
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002911
Gilles Peskine8817f612018-12-18 00:18:46 +01002912 PSA_ASSERT( psa_allocate_key( key_type,
2913 KEY_BITS_FROM_DATA( key_type, key_data ),
2914 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03002915 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002916 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002917 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002918
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_import_key( handle, key_type,
2920 key_data->x,
2921 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002922
Gilles Peskine8817f612018-12-18 00:18:46 +01002923 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2924 hash_data->x, hash_data->len,
2925 signature_data->x,
2926 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002927exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002928 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002929 mbedtls_psa_crypto_free( );
2930}
2931/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002932
2933/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002934void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2935 int alg_arg, data_t *hash_data,
2936 data_t *signature_data,
2937 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002938{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002939 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002940 psa_key_type_t key_type = key_type_arg;
2941 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002942 psa_status_t actual_status;
2943 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002944 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002945
Gilles Peskine8817f612018-12-18 00:18:46 +01002946 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947
Gilles Peskine8817f612018-12-18 00:18:46 +01002948 PSA_ASSERT( psa_allocate_key( key_type,
2949 KEY_BITS_FROM_DATA( key_type, key_data ),
2950 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002951 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002952 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002954
Gilles Peskine8817f612018-12-18 00:18:46 +01002955 PSA_ASSERT( psa_import_key( handle, key_type,
2956 key_data->x,
2957 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002958
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002959 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002960 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002961 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002962 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963
Gilles Peskinefe11b722018-12-18 00:24:04 +01002964 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965
2966exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002967 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002968 mbedtls_psa_crypto_free( );
2969}
2970/* END_CASE */
2971
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002972/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002973void asymmetric_encrypt( int key_type_arg,
2974 data_t *key_data,
2975 int alg_arg,
2976 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002977 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002978 int expected_output_length_arg,
2979 int expected_status_arg )
2980{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002981 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02002982 psa_key_type_t key_type = key_type_arg;
2983 psa_algorithm_t alg = alg_arg;
2984 size_t expected_output_length = expected_output_length_arg;
2985 size_t key_bits;
2986 unsigned char *output = NULL;
2987 size_t output_size;
2988 size_t output_length = ~0;
2989 psa_status_t actual_status;
2990 psa_status_t expected_status = expected_status_arg;
2991 psa_key_policy_t policy;
2992
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002994
Gilles Peskine656896e2018-06-29 19:12:28 +02002995 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_allocate_key( key_type,
2997 KEY_BITS_FROM_DATA( key_type, key_data ),
2998 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02002999 psa_key_policy_init( &policy );
3000 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003001 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3002 PSA_ASSERT( psa_import_key( handle, key_type,
3003 key_data->x,
3004 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003005
3006 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003007 PSA_ASSERT( psa_get_key_information( handle,
3008 NULL,
3009 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003010 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003011 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003012
3013 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003014 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003015 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003016 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003017 output, output_size,
3018 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003019 TEST_EQUAL( actual_status, expected_status );
3020 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003021
Gilles Peskine68428122018-06-30 18:42:41 +02003022 /* If the label is empty, the test framework puts a non-null pointer
3023 * in label->x. Test that a null pointer works as well. */
3024 if( label->len == 0 )
3025 {
3026 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003027 if( output_size != 0 )
3028 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003029 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003030 input_data->x, input_data->len,
3031 NULL, label->len,
3032 output, output_size,
3033 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003034 TEST_EQUAL( actual_status, expected_status );
3035 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003036 }
3037
Gilles Peskine656896e2018-06-29 19:12:28 +02003038exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003039 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003040 mbedtls_free( output );
3041 mbedtls_psa_crypto_free( );
3042}
3043/* END_CASE */
3044
3045/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003046void asymmetric_encrypt_decrypt( int key_type_arg,
3047 data_t *key_data,
3048 int alg_arg,
3049 data_t *input_data,
3050 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003051{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003052 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003053 psa_key_type_t key_type = key_type_arg;
3054 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003055 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003056 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003057 size_t output_size;
3058 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003059 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003060 size_t output2_size;
3061 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003062 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003063
Gilles Peskine8817f612018-12-18 00:18:46 +01003064 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003065
Gilles Peskine8817f612018-12-18 00:18:46 +01003066 PSA_ASSERT( psa_allocate_key( key_type,
3067 KEY_BITS_FROM_DATA( key_type, key_data ),
3068 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003069 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003070 psa_key_policy_set_usage( &policy,
3071 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003072 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003073 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003074
Gilles Peskine8817f612018-12-18 00:18:46 +01003075 PSA_ASSERT( psa_import_key( handle, key_type,
3076 key_data->x,
3077 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003078
3079 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003080 PSA_ASSERT( psa_get_key_information( handle,
3081 NULL,
3082 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003083 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003084 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003085 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003086 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003087
Gilles Peskineeebd7382018-06-08 18:11:54 +02003088 /* We test encryption by checking that encrypt-then-decrypt gives back
3089 * the original plaintext because of the non-optional random
3090 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003091 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3092 input_data->x, input_data->len,
3093 label->x, label->len,
3094 output, output_size,
3095 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003096 /* We don't know what ciphertext length to expect, but check that
3097 * it looks sensible. */
3098 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003099
Gilles Peskine8817f612018-12-18 00:18:46 +01003100 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3101 output, output_length,
3102 label->x, label->len,
3103 output2, output2_size,
3104 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003105 ASSERT_COMPARE( input_data->x, input_data->len,
3106 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107
3108exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003109 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003110 mbedtls_free( output );
3111 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113}
3114/* END_CASE */
3115
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003116/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003117void asymmetric_decrypt( int key_type_arg,
3118 data_t *key_data,
3119 int alg_arg,
3120 data_t *input_data,
3121 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003122 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003123{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003124 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125 psa_key_type_t key_type = key_type_arg;
3126 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003128 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003129 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003130 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003131
Gilles Peskine4abf7412018-06-18 16:35:34 +02003132 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003133 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003134
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_allocate_key( key_type,
3138 KEY_BITS_FROM_DATA( key_type, key_data ),
3139 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003140 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003141 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003142 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003143
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_import_key( handle, key_type,
3145 key_data->x,
3146 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147
Gilles Peskine8817f612018-12-18 00:18:46 +01003148 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3149 input_data->x, input_data->len,
3150 label->x, label->len,
3151 output,
3152 output_size,
3153 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003154 ASSERT_COMPARE( expected_data->x, expected_data->len,
3155 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156
Gilles Peskine68428122018-06-30 18:42:41 +02003157 /* If the label is empty, the test framework puts a non-null pointer
3158 * in label->x. Test that a null pointer works as well. */
3159 if( label->len == 0 )
3160 {
3161 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003162 if( output_size != 0 )
3163 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3165 input_data->x, input_data->len,
3166 NULL, label->len,
3167 output,
3168 output_size,
3169 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003170 ASSERT_COMPARE( expected_data->x, expected_data->len,
3171 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003172 }
3173
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003175 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003176 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178}
3179/* END_CASE */
3180
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003182void asymmetric_decrypt_fail( int key_type_arg,
3183 data_t *key_data,
3184 int alg_arg,
3185 data_t *input_data,
3186 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003187 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003189 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190 psa_key_type_t key_type = key_type_arg;
3191 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003193 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003194 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195 psa_status_t actual_status;
3196 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003197 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003198
Gilles Peskine4abf7412018-06-18 16:35:34 +02003199 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003200 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_allocate_key( key_type,
3205 KEY_BITS_FROM_DATA( key_type, key_data ),
3206 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003207 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003208 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_import_key( handle, key_type,
3212 key_data->x,
3213 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003215 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003216 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003217 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003218 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003219 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003220 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003221 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222
Gilles Peskine68428122018-06-30 18:42:41 +02003223 /* If the label is empty, the test framework puts a non-null pointer
3224 * in label->x. Test that a null pointer works as well. */
3225 if( label->len == 0 )
3226 {
3227 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003228 if( output_size != 0 )
3229 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003230 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003231 input_data->x, input_data->len,
3232 NULL, label->len,
3233 output, output_size,
3234 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003235 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003236 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003237 }
3238
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003240 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003241 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 mbedtls_psa_crypto_free( );
3243}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003244/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003245
3246/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003247void derive_setup( int key_type_arg,
3248 data_t *key_data,
3249 int alg_arg,
3250 data_t *salt,
3251 data_t *label,
3252 int requested_capacity_arg,
3253 int expected_status_arg )
3254{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003255 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003256 size_t key_type = key_type_arg;
3257 psa_algorithm_t alg = alg_arg;
3258 size_t requested_capacity = requested_capacity_arg;
3259 psa_status_t expected_status = expected_status_arg;
3260 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3261 psa_key_policy_t policy;
3262
Gilles Peskine8817f612018-12-18 00:18:46 +01003263 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003264
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3266 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003267 psa_key_policy_init( &policy );
3268 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003270
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_import_key( handle, key_type,
3272 key_data->x,
3273 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003274
Gilles Peskinefe11b722018-12-18 00:24:04 +01003275 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3276 salt->x, salt->len,
3277 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003278 requested_capacity ),
3279 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003280
3281exit:
3282 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003283 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003284 mbedtls_psa_crypto_free( );
3285}
3286/* END_CASE */
3287
3288/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003289void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003290{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003291 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003292 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003293 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003294 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003295 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003296 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003297 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3298 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3299 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003300 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003303
Gilles Peskine8817f612018-12-18 00:18:46 +01003304 PSA_ASSERT( psa_allocate_key( key_type,
3305 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3306 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003307 psa_key_policy_init( &policy );
3308 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003309 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003310
Gilles Peskine8817f612018-12-18 00:18:46 +01003311 PSA_ASSERT( psa_import_key( handle, key_type,
3312 key_data,
3313 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003314
3315 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3317 NULL, 0,
3318 NULL, 0,
3319 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003320
3321 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003322 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3323 NULL, 0,
3324 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003325 capacity ),
3326 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003327
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003328 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003329
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003330 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3331 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003332
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003333exit:
3334 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003335 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003336 mbedtls_psa_crypto_free( );
3337}
3338/* END_CASE */
3339
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003340/* BEGIN_CASE */
3341void test_derive_invalid_generator_tests( )
3342{
3343 uint8_t output_buffer[16];
3344 size_t buffer_size = 16;
3345 size_t capacity = 0;
3346 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3347
Nir Sonnenschein50789302018-10-31 12:16:38 +02003348 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003349 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003350
3351 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003352 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003353
Gilles Peskine8817f612018-12-18 00:18:46 +01003354 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003355
Nir Sonnenschein50789302018-10-31 12:16:38 +02003356 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003357 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003358
Nir Sonnenschein50789302018-10-31 12:16:38 +02003359 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003360 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003361
3362exit:
3363 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003364}
3365/* END_CASE */
3366
3367/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003368void derive_output( int alg_arg,
3369 data_t *key_data,
3370 data_t *salt,
3371 data_t *label,
3372 int requested_capacity_arg,
3373 data_t *expected_output1,
3374 data_t *expected_output2 )
3375{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003376 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003377 psa_algorithm_t alg = alg_arg;
3378 size_t requested_capacity = requested_capacity_arg;
3379 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3380 uint8_t *expected_outputs[2] =
3381 {expected_output1->x, expected_output2->x};
3382 size_t output_sizes[2] =
3383 {expected_output1->len, expected_output2->len};
3384 size_t output_buffer_size = 0;
3385 uint8_t *output_buffer = NULL;
3386 size_t expected_capacity;
3387 size_t current_capacity;
3388 psa_key_policy_t policy;
3389 psa_status_t status;
3390 unsigned i;
3391
3392 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3393 {
3394 if( output_sizes[i] > output_buffer_size )
3395 output_buffer_size = output_sizes[i];
3396 if( output_sizes[i] == 0 )
3397 expected_outputs[i] = NULL;
3398 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003399 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003401
Gilles Peskine8817f612018-12-18 00:18:46 +01003402 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3403 PSA_BYTES_TO_BITS( key_data->len ),
3404 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003405 psa_key_policy_init( &policy );
3406 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003407 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003408
Gilles Peskine8817f612018-12-18 00:18:46 +01003409 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3410 key_data->x,
3411 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003412
3413 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3415 salt->x, salt->len,
3416 label->x, label->len,
3417 requested_capacity ) );
3418 PSA_ASSERT( psa_get_generator_capacity( &generator,
3419 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003420 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003421 expected_capacity = requested_capacity;
3422
3423 /* Expansion phase. */
3424 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3425 {
3426 /* Read some bytes. */
3427 status = psa_generator_read( &generator,
3428 output_buffer, output_sizes[i] );
3429 if( expected_capacity == 0 && output_sizes[i] == 0 )
3430 {
3431 /* Reading 0 bytes when 0 bytes are available can go either way. */
3432 TEST_ASSERT( status == PSA_SUCCESS ||
3433 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3434 continue;
3435 }
3436 else if( expected_capacity == 0 ||
3437 output_sizes[i] > expected_capacity )
3438 {
3439 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003440 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003441 expected_capacity = 0;
3442 continue;
3443 }
3444 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003445 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003446 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003447 ASSERT_COMPARE( output_buffer, output_sizes[i],
3448 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003449 /* Check the generator status. */
3450 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003451 PSA_ASSERT( psa_get_generator_capacity( &generator,
3452 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003453 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003454 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003455 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003456
3457exit:
3458 mbedtls_free( output_buffer );
3459 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003460 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003461 mbedtls_psa_crypto_free( );
3462}
3463/* END_CASE */
3464
3465/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003466void derive_full( int alg_arg,
3467 data_t *key_data,
3468 data_t *salt,
3469 data_t *label,
3470 int requested_capacity_arg )
3471{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003472 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003473 psa_algorithm_t alg = alg_arg;
3474 size_t requested_capacity = requested_capacity_arg;
3475 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3476 unsigned char output_buffer[16];
3477 size_t expected_capacity = requested_capacity;
3478 size_t current_capacity;
3479 psa_key_policy_t policy;
3480
Gilles Peskine8817f612018-12-18 00:18:46 +01003481 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3484 PSA_BYTES_TO_BITS( key_data->len ),
3485 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003486 psa_key_policy_init( &policy );
3487 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003488 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003489
Gilles Peskine8817f612018-12-18 00:18:46 +01003490 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3491 key_data->x,
3492 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003493
3494 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003495 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3496 salt->x, salt->len,
3497 label->x, label->len,
3498 requested_capacity ) );
3499 PSA_ASSERT( psa_get_generator_capacity( &generator,
3500 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003501 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003502
3503 /* Expansion phase. */
3504 while( current_capacity > 0 )
3505 {
3506 size_t read_size = sizeof( output_buffer );
3507 if( read_size > current_capacity )
3508 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 PSA_ASSERT( psa_generator_read( &generator,
3510 output_buffer,
3511 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003512 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 PSA_ASSERT( psa_get_generator_capacity( &generator,
3514 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003515 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003516 }
3517
3518 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003519 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3520 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003521
Gilles Peskine8817f612018-12-18 00:18:46 +01003522 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003523
3524exit:
3525 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003526 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003527 mbedtls_psa_crypto_free( );
3528}
3529/* END_CASE */
3530
3531/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003532void derive_key_exercise( int alg_arg,
3533 data_t *key_data,
3534 data_t *salt,
3535 data_t *label,
3536 int derived_type_arg,
3537 int derived_bits_arg,
3538 int derived_usage_arg,
3539 int derived_alg_arg )
3540{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003541 psa_key_handle_t base_handle = 0;
3542 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003543 psa_algorithm_t alg = alg_arg;
3544 psa_key_type_t derived_type = derived_type_arg;
3545 size_t derived_bits = derived_bits_arg;
3546 psa_key_usage_t derived_usage = derived_usage_arg;
3547 psa_algorithm_t derived_alg = derived_alg_arg;
3548 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3549 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3550 psa_key_policy_t policy;
3551 psa_key_type_t got_type;
3552 size_t got_bits;
3553
Gilles Peskine8817f612018-12-18 00:18:46 +01003554 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003555
Gilles Peskine8817f612018-12-18 00:18:46 +01003556 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3557 PSA_BYTES_TO_BITS( key_data->len ),
3558 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003559 psa_key_policy_init( &policy );
3560 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003561 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3562 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3563 key_data->x,
3564 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003565
3566 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003567 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3568 salt->x, salt->len,
3569 label->x, label->len,
3570 capacity ) );
3571 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3572 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003573 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3575 PSA_ASSERT( psa_generator_import_key( derived_handle,
3576 derived_type,
3577 derived_bits,
3578 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003579
3580 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_get_key_information( derived_handle,
3582 &got_type,
3583 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003584 TEST_EQUAL( got_type, derived_type );
3585 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003586
3587 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003588 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003589 goto exit;
3590
3591exit:
3592 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003593 psa_destroy_key( base_handle );
3594 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003595 mbedtls_psa_crypto_free( );
3596}
3597/* END_CASE */
3598
3599/* BEGIN_CASE */
3600void derive_key_export( int alg_arg,
3601 data_t *key_data,
3602 data_t *salt,
3603 data_t *label,
3604 int bytes1_arg,
3605 int bytes2_arg )
3606{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003607 psa_key_handle_t base_handle = 0;
3608 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003609 psa_algorithm_t alg = alg_arg;
3610 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003611 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003612 size_t bytes2 = bytes2_arg;
3613 size_t capacity = bytes1 + bytes2;
3614 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003615 uint8_t *output_buffer = NULL;
3616 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003617 psa_key_policy_t policy;
3618 size_t length;
3619
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003620 ASSERT_ALLOC( output_buffer, capacity );
3621 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003622 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003623
Gilles Peskine8817f612018-12-18 00:18:46 +01003624 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3625 PSA_BYTES_TO_BITS( key_data->len ),
3626 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003627 psa_key_policy_init( &policy );
3628 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003629 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3630 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3631 key_data->x,
3632 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003633
3634 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003635 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3636 salt->x, salt->len,
3637 label->x, label->len,
3638 capacity ) );
3639 PSA_ASSERT( psa_generator_read( &generator,
3640 output_buffer,
3641 capacity ) );
3642 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003643
3644 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003645 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3646 salt->x, salt->len,
3647 label->x, label->len,
3648 capacity ) );
3649 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3650 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003651 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3653 PSA_ASSERT( psa_generator_import_key( derived_handle,
3654 PSA_KEY_TYPE_RAW_DATA,
3655 derived_bits,
3656 &generator ) );
3657 PSA_ASSERT( psa_export_key( derived_handle,
3658 export_buffer, bytes1,
3659 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003660 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003661 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3662 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3663 PSA_BYTES_TO_BITS( bytes2 ),
3664 &derived_handle ) );
3665 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3666 PSA_ASSERT( psa_generator_import_key( derived_handle,
3667 PSA_KEY_TYPE_RAW_DATA,
3668 PSA_BYTES_TO_BITS( bytes2 ),
3669 &generator ) );
3670 PSA_ASSERT( psa_export_key( derived_handle,
3671 export_buffer + bytes1, bytes2,
3672 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003673 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003674
3675 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003676 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3677 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003678
3679exit:
3680 mbedtls_free( output_buffer );
3681 mbedtls_free( export_buffer );
3682 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003683 psa_destroy_key( base_handle );
3684 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003685 mbedtls_psa_crypto_free( );
3686}
3687/* END_CASE */
3688
3689/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003690void key_agreement_setup( int alg_arg,
3691 int our_key_type_arg, data_t *our_key_data,
3692 data_t *peer_key_data,
3693 int expected_status_arg )
3694{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003695 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003696 psa_algorithm_t alg = alg_arg;
3697 psa_key_type_t our_key_type = our_key_type_arg;
3698 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3699 psa_key_policy_t policy;
3700
Gilles Peskine8817f612018-12-18 00:18:46 +01003701 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003702
Gilles Peskine8817f612018-12-18 00:18:46 +01003703 PSA_ASSERT( psa_allocate_key( our_key_type,
3704 KEY_BITS_FROM_DATA( our_key_type,
3705 our_key_data ),
3706 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003707 psa_key_policy_init( &policy );
3708 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3710 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3711 our_key_data->x,
3712 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003713
Gilles Peskinefe11b722018-12-18 00:24:04 +01003714 TEST_EQUAL( psa_key_agreement( &generator,
3715 our_key,
3716 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003717 alg ),
3718 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003719
3720exit:
3721 psa_generator_abort( &generator );
3722 psa_destroy_key( our_key );
3723 mbedtls_psa_crypto_free( );
3724}
3725/* END_CASE */
3726
3727/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003728void key_agreement_capacity( int alg_arg,
3729 int our_key_type_arg, data_t *our_key_data,
3730 data_t *peer_key_data,
3731 int expected_capacity_arg )
3732{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003733 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003734 psa_algorithm_t alg = alg_arg;
3735 psa_key_type_t our_key_type = our_key_type_arg;
3736 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3737 psa_key_policy_t policy;
3738 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003739 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003740
Gilles Peskine8817f612018-12-18 00:18:46 +01003741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003742
Gilles Peskine8817f612018-12-18 00:18:46 +01003743 PSA_ASSERT( psa_allocate_key( our_key_type,
3744 KEY_BITS_FROM_DATA( our_key_type,
3745 our_key_data ),
3746 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003747 psa_key_policy_init( &policy );
3748 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3750 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3751 our_key_data->x,
3752 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003753
Gilles Peskine8817f612018-12-18 00:18:46 +01003754 PSA_ASSERT( psa_key_agreement( &generator,
3755 our_key,
3756 peer_key_data->x, peer_key_data->len,
3757 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003758
Gilles Peskinebf491972018-10-25 22:36:12 +02003759 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003760 PSA_ASSERT( psa_get_generator_capacity(
3761 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003762 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003763
Gilles Peskinebf491972018-10-25 22:36:12 +02003764 /* Test the actual capacity by reading the output. */
3765 while( actual_capacity > sizeof( output ) )
3766 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003767 PSA_ASSERT( psa_generator_read( &generator,
3768 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003769 actual_capacity -= sizeof( output );
3770 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003771 PSA_ASSERT( psa_generator_read( &generator,
3772 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003773 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3774 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003775
Gilles Peskine59685592018-09-18 12:11:34 +02003776exit:
3777 psa_generator_abort( &generator );
3778 psa_destroy_key( our_key );
3779 mbedtls_psa_crypto_free( );
3780}
3781/* END_CASE */
3782
3783/* BEGIN_CASE */
3784void key_agreement_output( int alg_arg,
3785 int our_key_type_arg, data_t *our_key_data,
3786 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003787 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003788{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003789 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003790 psa_algorithm_t alg = alg_arg;
3791 psa_key_type_t our_key_type = our_key_type_arg;
3792 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3793 psa_key_policy_t policy;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003794 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003795
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003796 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3797 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003798
Gilles Peskine8817f612018-12-18 00:18:46 +01003799 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003800
Gilles Peskine8817f612018-12-18 00:18:46 +01003801 PSA_ASSERT( psa_allocate_key( our_key_type,
3802 KEY_BITS_FROM_DATA( our_key_type,
3803 our_key_data ),
3804 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003805 psa_key_policy_init( &policy );
3806 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003807 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3808 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3809 our_key_data->x,
3810 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003811
Gilles Peskine8817f612018-12-18 00:18:46 +01003812 PSA_ASSERT( psa_key_agreement( &generator,
3813 our_key,
3814 peer_key_data->x, peer_key_data->len,
3815 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003816
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003817 PSA_ASSERT( psa_generator_read( &generator,
3818 actual_output,
3819 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003820 ASSERT_COMPARE( actual_output, expected_output1->len,
3821 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003822 if( expected_output2->len != 0 )
3823 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003824 PSA_ASSERT( psa_generator_read( &generator,
3825 actual_output,
3826 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003827 ASSERT_COMPARE( actual_output, expected_output2->len,
3828 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003829 }
Gilles Peskine59685592018-09-18 12:11:34 +02003830
3831exit:
3832 psa_generator_abort( &generator );
3833 psa_destroy_key( our_key );
3834 mbedtls_psa_crypto_free( );
3835 mbedtls_free( actual_output );
3836}
3837/* END_CASE */
3838
3839/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003840void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003841{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003842 size_t bytes = bytes_arg;
3843 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003844 unsigned char *output = NULL;
3845 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003846 size_t i;
3847 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003848
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003849 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3850 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003851 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003852
Gilles Peskine8817f612018-12-18 00:18:46 +01003853 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003854
Gilles Peskinea50d7392018-06-21 10:22:13 +02003855 /* Run several times, to ensure that every output byte will be
3856 * nonzero at least once with overwhelming probability
3857 * (2^(-8*number_of_runs)). */
3858 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003859 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003860 if( bytes != 0 )
3861 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003862 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003863
3864 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003865 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3866 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003867
3868 for( i = 0; i < bytes; i++ )
3869 {
3870 if( output[i] != 0 )
3871 ++changed[i];
3872 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003873 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003874
3875 /* Check that every byte was changed to nonzero at least once. This
3876 * validates that psa_generate_random is overwriting every byte of
3877 * the output buffer. */
3878 for( i = 0; i < bytes; i++ )
3879 {
3880 TEST_ASSERT( changed[i] != 0 );
3881 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003882
3883exit:
3884 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003885 mbedtls_free( output );
3886 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003887}
3888/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003889
3890/* BEGIN_CASE */
3891void generate_key( int type_arg,
3892 int bits_arg,
3893 int usage_arg,
3894 int alg_arg,
3895 int expected_status_arg )
3896{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003897 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003898 psa_key_type_t type = type_arg;
3899 psa_key_usage_t usage = usage_arg;
3900 size_t bits = bits_arg;
3901 psa_algorithm_t alg = alg_arg;
3902 psa_status_t expected_status = expected_status_arg;
3903 psa_key_type_t got_type;
3904 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003905 psa_status_t expected_info_status =
3906 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3907 psa_key_policy_t policy;
3908
Gilles Peskine8817f612018-12-18 00:18:46 +01003909 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003910
Gilles Peskine8817f612018-12-18 00:18:46 +01003911 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003912 psa_key_policy_init( &policy );
3913 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003914 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003915
3916 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003917 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3918 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003919
3920 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003921 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3922 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003923 if( expected_info_status != PSA_SUCCESS )
3924 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003925 TEST_EQUAL( got_type, type );
3926 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003927
Gilles Peskine818ca122018-06-20 18:16:48 +02003928 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003929 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02003930 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003931
3932exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003933 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003934 mbedtls_psa_crypto_free( );
3935}
3936/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003937
Darryl Greend49a4992018-06-18 17:27:26 +01003938/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
3939void persistent_key_load_key_from_storage( data_t *data, int type_arg,
3940 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00003941 int alg_arg, int generation_method,
3942 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01003943{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003944 psa_key_handle_t handle = 0;
3945 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01003946 psa_key_type_t type = (psa_key_type_t) type_arg;
3947 psa_key_type_t type_get;
3948 size_t bits_get;
3949 psa_key_policy_t policy_set;
3950 psa_key_policy_t policy_get;
3951 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
3952 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00003953 psa_key_policy_t base_policy_set;
3954 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
3955 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003956 unsigned char *first_export = NULL;
3957 unsigned char *second_export = NULL;
3958 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
3959 size_t first_exported_length;
3960 size_t second_exported_length;
3961
3962 ASSERT_ALLOC( first_export, export_size );
3963 ASSERT_ALLOC( second_export, export_size );
3964
Gilles Peskine8817f612018-12-18 00:18:46 +01003965 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01003966
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
3968 type, bits,
3969 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003970 psa_key_policy_init( &policy_set );
Darryl Greend49a4992018-06-18 17:27:26 +01003971 psa_key_policy_set_usage( &policy_set, policy_usage,
3972 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003973 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003974
Darryl Green0c6575a2018-11-07 16:05:30 +00003975 switch( generation_method )
3976 {
3977 case IMPORT_KEY:
3978 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003979 PSA_ASSERT( psa_import_key( handle, type,
3980 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003981 break;
Darryl Greend49a4992018-06-18 17:27:26 +01003982
Darryl Green0c6575a2018-11-07 16:05:30 +00003983 case GENERATE_KEY:
3984 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003985 PSA_ASSERT( psa_generate_key( handle, type, bits,
3986 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003987 break;
3988
3989 case DERIVE_KEY:
3990 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003991 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3992 PSA_BYTES_TO_BITS( data->len ),
3993 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003994 psa_key_policy_init( &base_policy_set );
Darryl Green0c6575a2018-11-07 16:05:30 +00003995 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
3996 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003997 PSA_ASSERT( psa_set_key_policy(
3998 base_key, &base_policy_set ) );
3999 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4000 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004001 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004002 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4003 base_policy_alg,
4004 NULL, 0, NULL, 0,
4005 export_size ) );
4006 PSA_ASSERT( psa_generator_import_key(
4007 handle, PSA_KEY_TYPE_RAW_DATA,
4008 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004009 break;
4010 }
Darryl Greend49a4992018-06-18 17:27:26 +01004011
4012 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004013 TEST_EQUAL( psa_export_key( handle,
4014 first_export, export_size,
4015 &first_exported_length ),
4016 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004017
4018 /* Shutdown and restart */
4019 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004020 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004021
Darryl Greend49a4992018-06-18 17:27:26 +01004022 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004023 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4024 &handle ) );
4025 PSA_ASSERT( psa_get_key_information(
4026 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004027 TEST_EQUAL( type_get, type );
4028 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004029
Gilles Peskine8817f612018-12-18 00:18:46 +01004030 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004031 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4032 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004033
4034 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004035 TEST_EQUAL( psa_export_key( handle,
4036 second_export, export_size,
4037 &second_exported_length ),
4038 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004039
Darryl Green0c6575a2018-11-07 16:05:30 +00004040 if( export_status == PSA_SUCCESS )
4041 {
4042 ASSERT_COMPARE( first_export, first_exported_length,
4043 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004044
Darryl Green0c6575a2018-11-07 16:05:30 +00004045 switch( generation_method )
4046 {
4047 case IMPORT_KEY:
4048 ASSERT_COMPARE( data->x, data->len,
4049 first_export, first_exported_length );
4050 break;
4051 default:
4052 break;
4053 }
4054 }
4055
4056 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004057 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004058 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004059
4060exit:
4061 mbedtls_free( first_export );
4062 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004064 mbedtls_psa_crypto_free();
4065}
4066/* END_CASE */