blob: 561136de22402ea0b36b41b6bcddebd8570a7d09 [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
Gilles Peskinefc411f12018-10-25 22:34:48 +020014#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
15
itayzafrir3e02b3b2018-06-12 17:06:52 +030016#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020017#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#else
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100131static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200132 psa_key_usage_t usage,
133 psa_algorithm_t alg )
134{
135 psa_mac_operation_t operation;
136 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200137 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200138 size_t mac_length = sizeof( mac );
139
140 if( usage & PSA_KEY_USAGE_SIGN )
141 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100142 PSA_ASSERT( psa_mac_sign_setup( &operation,
143 handle, alg ) );
144 PSA_ASSERT( psa_mac_update( &operation,
145 input, sizeof( input ) ) );
146 PSA_ASSERT( psa_mac_sign_finish( &operation,
147 mac, sizeof( mac ),
148 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200149 }
150
151 if( usage & PSA_KEY_USAGE_VERIFY )
152 {
153 psa_status_t verify_status =
154 ( usage & PSA_KEY_USAGE_SIGN ?
155 PSA_SUCCESS :
156 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100157 PSA_ASSERT( psa_mac_verify_setup( &operation,
158 handle, alg ) );
159 PSA_ASSERT( psa_mac_update( &operation,
160 input, sizeof( input ) ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100161 TEST_EQUAL( psa_mac_verify_finish( &operation,
162 mac,
163 mac_length ), verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200164 }
165
166 return( 1 );
167
168exit:
169 psa_mac_abort( &operation );
170 return( 0 );
171}
172
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100173static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200174 psa_key_usage_t usage,
175 psa_algorithm_t alg )
176{
177 psa_cipher_operation_t operation;
178 unsigned char iv[16] = {0};
179 size_t iv_length = sizeof( iv );
180 const unsigned char plaintext[16] = "Hello, world...";
181 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
182 size_t ciphertext_length = sizeof( ciphertext );
183 unsigned char decrypted[sizeof( ciphertext )];
184 size_t part_length;
185
186 if( usage & PSA_KEY_USAGE_ENCRYPT )
187 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100188 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
189 handle, alg ) );
190 PSA_ASSERT( psa_cipher_generate_iv( &operation,
191 iv, sizeof( iv ),
192 &iv_length ) );
193 PSA_ASSERT( psa_cipher_update( &operation,
194 plaintext, sizeof( plaintext ),
195 ciphertext, sizeof( ciphertext ),
196 &ciphertext_length ) );
197 PSA_ASSERT( psa_cipher_finish( &operation,
198 ciphertext + ciphertext_length,
199 sizeof( ciphertext ) - ciphertext_length,
200 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 ciphertext_length += part_length;
202 }
203
204 if( usage & PSA_KEY_USAGE_DECRYPT )
205 {
206 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700207 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200208 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
209 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100211 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200212 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
213 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100214 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
215 handle, alg ) );
216 PSA_ASSERT( psa_cipher_set_iv( &operation,
217 iv, iv_length ) );
218 PSA_ASSERT( psa_cipher_update( &operation,
219 ciphertext, ciphertext_length,
220 decrypted, sizeof( decrypted ),
221 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200222 status = psa_cipher_finish( &operation,
223 decrypted + part_length,
224 sizeof( decrypted ) - part_length,
225 &part_length );
226 /* For a stream cipher, all inputs are valid. For a block cipher,
227 * if the input is some aribtrary data rather than an actual
228 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700229 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700230 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100231 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200232 else
233 TEST_ASSERT( status == PSA_SUCCESS ||
234 status == PSA_ERROR_INVALID_PADDING );
235 }
236
237 return( 1 );
238
239exit:
240 psa_cipher_abort( &operation );
241 return( 0 );
242}
243
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100244static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200245 psa_key_usage_t usage,
246 psa_algorithm_t alg )
247{
248 unsigned char nonce[16] = {0};
249 size_t nonce_length = sizeof( nonce );
250 unsigned char plaintext[16] = "Hello, world...";
251 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
252 size_t ciphertext_length = sizeof( ciphertext );
253 size_t plaintext_length = sizeof( ciphertext );
254
255 if( usage & PSA_KEY_USAGE_ENCRYPT )
256 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100257 PSA_ASSERT( psa_aead_encrypt( handle, alg,
258 nonce, nonce_length,
259 NULL, 0,
260 plaintext, sizeof( plaintext ),
261 ciphertext, sizeof( ciphertext ),
262 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200263 }
264
265 if( usage & PSA_KEY_USAGE_DECRYPT )
266 {
267 psa_status_t verify_status =
268 ( usage & PSA_KEY_USAGE_ENCRYPT ?
269 PSA_SUCCESS :
270 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100271 TEST_EQUAL( psa_aead_decrypt( handle, alg,
272 nonce, nonce_length,
273 NULL, 0,
274 ciphertext, ciphertext_length,
275 plaintext, sizeof( plaintext ),
276 &plaintext_length ), verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200277 }
278
279 return( 1 );
280
281exit:
282 return( 0 );
283}
284
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100285static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200286 psa_key_usage_t usage,
287 psa_algorithm_t alg )
288{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200289 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
290 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200291 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 size_t signature_length = sizeof( signature );
293
294 if( usage & PSA_KEY_USAGE_SIGN )
295 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200296 /* Some algorithms require the payload to have the size of
297 * the hash encoded in the algorithm. Use this input size
298 * even for algorithms that allow other input sizes. */
299 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
300 if( hash_alg != 0 )
301 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100302 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
303 payload, payload_length,
304 signature, sizeof( signature ),
305 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 }
307
308 if( usage & PSA_KEY_USAGE_VERIFY )
309 {
310 psa_status_t verify_status =
311 ( usage & PSA_KEY_USAGE_SIGN ?
312 PSA_SUCCESS :
313 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100314 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
315 payload, payload_length,
316 signature, signature_length ),
317 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200318 }
319
320 return( 1 );
321
322exit:
323 return( 0 );
324}
325
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100326static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200327 psa_key_usage_t usage,
328 psa_algorithm_t alg )
329{
330 unsigned char plaintext[256] = "Hello, world...";
331 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
332 size_t ciphertext_length = sizeof( ciphertext );
333 size_t plaintext_length = 16;
334
335 if( usage & PSA_KEY_USAGE_ENCRYPT )
336 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100337 PSA_ASSERT(
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100338 psa_asymmetric_encrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200339 plaintext, plaintext_length,
340 NULL, 0,
341 ciphertext, sizeof( ciphertext ),
Gilles Peskine8817f612018-12-18 00:18:46 +0100342 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200343 }
344
345 if( usage & PSA_KEY_USAGE_DECRYPT )
346 {
347 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100348 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200349 ciphertext, ciphertext_length,
350 NULL, 0,
351 plaintext, sizeof( plaintext ),
352 &plaintext_length );
353 TEST_ASSERT( status == PSA_SUCCESS ||
354 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
355 ( status == PSA_ERROR_INVALID_ARGUMENT ||
356 status == PSA_ERROR_INVALID_PADDING ) ) );
357 }
358
359 return( 1 );
360
361exit:
362 return( 0 );
363}
Gilles Peskine02b75072018-07-01 22:31:34 +0200364
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100365static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200366 psa_key_usage_t usage,
367 psa_algorithm_t alg )
368{
369 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
370 unsigned char label[16] = "This is a label.";
371 size_t label_length = sizeof( label );
372 unsigned char seed[16] = "abcdefghijklmnop";
373 size_t seed_length = sizeof( seed );
374 unsigned char output[1];
375
376 if( usage & PSA_KEY_USAGE_DERIVE )
377 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100378 PSA_ASSERT( psa_key_derivation( &generator,
379 handle, alg,
380 label, label_length,
381 seed, seed_length,
382 sizeof( output ) ) );
383 PSA_ASSERT( psa_generator_read( &generator,
384 output,
385 sizeof( output ) ) );
386 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200387 }
388
389 return( 1 );
390
391exit:
392 return( 0 );
393}
394
Gilles Peskinec7998b72018-11-07 18:45:02 +0100395/* We need two keys to exercise key agreement. Exercise the
396 * private key against its own public key. */
397static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100398 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100399 psa_algorithm_t alg )
400{
401 psa_key_type_t private_key_type;
402 psa_key_type_t public_key_type;
403 size_t key_bits;
404 uint8_t *public_key = NULL;
405 size_t public_key_length;
406 /* Return UNKNOWN_ERROR if something other than the final call to
407 * psa_key_agreement fails. This isn't fully satisfactory, but it's
408 * good enough: callers will report it as a failed test anyway. */
409 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
410
Gilles Peskine8817f612018-12-18 00:18:46 +0100411 PSA_ASSERT( psa_get_key_information( handle,
412 &private_key_type,
413 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100414 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
415 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
416 ASSERT_ALLOC( public_key, public_key_length );
417 TEST_ASSERT( public_key != NULL );
Gilles Peskine8817f612018-12-18 00:18:46 +0100418 PSA_ASSERT( psa_export_public_key( handle,
419 public_key, public_key_length,
420 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100421
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100422 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100423 public_key, public_key_length,
424 alg );
425exit:
426 mbedtls_free( public_key );
427 return( status );
428}
429
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100430static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200431 psa_key_usage_t usage,
432 psa_algorithm_t alg )
433{
434 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200435 unsigned char output[1];
436 int ok = 0;
437
438 if( usage & PSA_KEY_USAGE_DERIVE )
439 {
440 /* We need two keys to exercise key agreement. Exercise the
441 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100442 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
443 PSA_ASSERT( psa_generator_read( &generator,
444 output,
445 sizeof( output ) ) );
446 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200447 }
448 ok = 1;
449
450exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200451 return( ok );
452}
453
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200454static int is_oid_of_key_type( psa_key_type_t type,
455 const uint8_t *oid, size_t oid_length )
456{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200457 const uint8_t *expected_oid = NULL;
458 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200459#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200460 if( PSA_KEY_TYPE_IS_RSA( type ) )
461 {
462 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
463 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
464 }
465 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200466#endif /* MBEDTLS_RSA_C */
467#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200468 if( PSA_KEY_TYPE_IS_ECC( type ) )
469 {
470 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
471 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
472 }
473 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200474#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 {
476 char message[40];
477 mbedtls_snprintf( message, sizeof( message ),
478 "OID not known for key type=0x%08lx",
479 (unsigned long) type );
480 test_fail( message, __LINE__, __FILE__ );
481 return( 0 );
482 }
483
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200484 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200485 return( 1 );
486
487exit:
488 return( 0 );
489}
490
491static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
492 size_t min_bits, size_t max_bits,
493 int must_be_odd )
494{
495 size_t len;
496 size_t actual_bits;
497 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100498 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
499 MBEDTLS_ASN1_INTEGER ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200500 /* Tolerate a slight departure from DER encoding:
501 * - 0 may be represented by an empty string or a 1-byte string.
502 * - The sign bit may be used as a value bit. */
503 if( ( len == 1 && ( *p )[0] == 0 ) ||
504 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
505 {
506 ++( *p );
507 --len;
508 }
509 if( min_bits == 0 && len == 0 )
510 return( 1 );
511 msb = ( *p )[0];
512 TEST_ASSERT( msb != 0 );
513 actual_bits = 8 * ( len - 1 );
514 while( msb != 0 )
515 {
516 msb >>= 1;
517 ++actual_bits;
518 }
519 TEST_ASSERT( actual_bits >= min_bits );
520 TEST_ASSERT( actual_bits <= max_bits );
521 if( must_be_odd )
522 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
523 *p += len;
524 return( 1 );
525exit:
526 return( 0 );
527}
528
529static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
530 size_t *len,
531 unsigned char n, unsigned char tag )
532{
533 int ret;
534 ret = mbedtls_asn1_get_tag( p, end, len,
535 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
536 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
537 if( ret != 0 )
538 return( ret );
539 end = *p + *len;
540 ret = mbedtls_asn1_get_tag( p, end, len, tag );
541 if( ret != 0 )
542 return( ret );
543 if( *p + *len != end )
544 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
545 return( 0 );
546}
547
548static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
549 uint8_t *exported, size_t exported_length )
550{
551 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100552 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200553 else
554 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200555
556#if defined(MBEDTLS_DES_C)
557 if( type == PSA_KEY_TYPE_DES )
558 {
559 /* Check the parity bits. */
560 unsigned i;
561 for( i = 0; i < bits / 8; i++ )
562 {
563 unsigned bit_count = 0;
564 unsigned m;
565 for( m = 1; m <= 0x100; m <<= 1 )
566 {
567 if( exported[i] & m )
568 ++bit_count;
569 }
570 TEST_ASSERT( bit_count % 2 != 0 );
571 }
572 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200573 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200574#endif
575
576#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
577 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
578 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200579 uint8_t *p = exported;
580 uint8_t *end = exported + exported_length;
581 size_t len;
582 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200583 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200584 * modulus INTEGER, -- n
585 * publicExponent INTEGER, -- e
586 * privateExponent INTEGER, -- d
587 * prime1 INTEGER, -- p
588 * prime2 INTEGER, -- q
589 * exponent1 INTEGER, -- d mod (p-1)
590 * exponent2 INTEGER, -- d mod (q-1)
591 * coefficient INTEGER, -- (inverse of q) mod p
592 * }
593 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100594 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
595 MBEDTLS_ASN1_SEQUENCE |
596 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
597 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200598 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
599 goto exit;
600 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
601 goto exit;
602 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
603 goto exit;
604 /* Require d to be at least half the size of n. */
605 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
606 goto exit;
607 /* Require p and q to be at most half the size of n, rounded up. */
608 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
609 goto exit;
610 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
611 goto exit;
612 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
613 goto exit;
614 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
615 goto exit;
616 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
617 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_RSA_C */
622
623#if defined(MBEDTLS_ECP_C)
624 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
625 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100626 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100627 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100628 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200629 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200630#endif /* MBEDTLS_ECP_C */
631
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200632 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
633 {
634 uint8_t *p = exported;
635 uint8_t *end = exported + exported_length;
636 size_t len;
637 mbedtls_asn1_buf alg;
638 mbedtls_asn1_buf params;
639 mbedtls_asn1_bitstring bitstring;
640 /* SubjectPublicKeyInfo ::= SEQUENCE {
641 * algorithm AlgorithmIdentifier,
642 * subjectPublicKey BIT STRING }
643 * AlgorithmIdentifier ::= SEQUENCE {
644 * algorithm OBJECT IDENTIFIER,
645 * parameters ANY DEFINED BY algorithm OPTIONAL }
646 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100647 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
648 MBEDTLS_ASN1_SEQUENCE |
649 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
650 TEST_EQUAL( p + len, end );
651 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200652 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
653 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100654 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
655 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200656 p = bitstring.p;
657#if defined(MBEDTLS_RSA_C)
658 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
659 {
660 /* RSAPublicKey ::= SEQUENCE {
661 * modulus INTEGER, -- n
662 * publicExponent INTEGER } -- e
663 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100664 TEST_EQUAL( bitstring.unused_bits, 0 );
665 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
666 MBEDTLS_ASN1_SEQUENCE |
667 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
668 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200669 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
670 goto exit;
671 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
672 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100673 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200674 }
675 else
676#endif /* MBEDTLS_RSA_C */
677#if defined(MBEDTLS_ECP_C)
678 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
679 {
680 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200681 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 * -- then x_P as an n-bit string, big endian;
683 * -- then y_P as a n-bit string, big endian,
684 * -- where n is the order of the curve.
685 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100686 TEST_EQUAL( bitstring.unused_bits, 0 );
687 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
688 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200689 }
690 else
691#endif /* MBEDTLS_ECP_C */
692 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100693 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200694 mbedtls_snprintf( message, sizeof( message ),
695 "No sanity check for public key type=0x%08lx",
696 (unsigned long) type );
697 test_fail( message, __LINE__, __FILE__ );
698 return( 0 );
699 }
700 }
701 else
702
703 {
704 /* No sanity checks for other types */
705 }
706
707 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200708
709exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200710 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200711}
712
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100713static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200714 psa_key_usage_t usage )
715{
716 psa_key_type_t type;
717 size_t bits;
718 uint8_t *exported = NULL;
719 size_t exported_size = 0;
720 size_t exported_length = 0;
721 int ok = 0;
722
Gilles Peskine8817f612018-12-18 00:18:46 +0100723 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200724
725 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
726 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200727 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100728 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
729 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200730 return( 1 );
731 }
732
Gilles Peskined14664a2018-08-10 19:07:32 +0200733 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200734 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200735
Gilles Peskine8817f612018-12-18 00:18:46 +0100736 PSA_ASSERT( psa_export_key( handle,
737 exported, exported_size,
738 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200739 ok = exported_key_sanity_check( type, bits, exported, exported_length );
740
741exit:
742 mbedtls_free( exported );
743 return( ok );
744}
745
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100746static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200747{
748 psa_key_type_t type;
749 psa_key_type_t public_type;
750 size_t bits;
751 uint8_t *exported = NULL;
752 size_t exported_size = 0;
753 size_t exported_length = 0;
754 int ok = 0;
755
Gilles Peskine8817f612018-12-18 00:18:46 +0100756 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200757 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
758 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100759 TEST_EQUAL( psa_export_public_key( handle,
760 NULL, 0, &exported_length ),
761 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200762 return( 1 );
763 }
764
765 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
766 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200767 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200768
Gilles Peskine8817f612018-12-18 00:18:46 +0100769 PSA_ASSERT( psa_export_public_key( handle,
770 exported, exported_size,
771 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200772 ok = exported_key_sanity_check( public_type, bits,
773 exported, exported_length );
774
775exit:
776 mbedtls_free( exported );
777 return( ok );
778}
779
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200781 psa_key_usage_t usage,
782 psa_algorithm_t alg )
783{
784 int ok;
785 if( alg == 0 )
786 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
787 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100788 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200789 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200791 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100792 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200793 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100794 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200795 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100796 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200797 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100798 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200799 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100800 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200801 else
802 {
803 char message[40];
804 mbedtls_snprintf( message, sizeof( message ),
805 "No code to exercise alg=0x%08lx",
806 (unsigned long) alg );
807 test_fail( message, __LINE__, __FILE__ );
808 ok = 0;
809 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200810
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100811 ok = ok && exercise_export_key( handle, usage );
812 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200813
Gilles Peskine02b75072018-07-01 22:31:34 +0200814 return( ok );
815}
816
Gilles Peskine10df3412018-10-25 22:35:43 +0200817static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
818 psa_algorithm_t alg )
819{
820 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
821 {
822 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
823 PSA_KEY_USAGE_VERIFY :
824 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
825 }
826 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
827 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
828 {
829 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
830 PSA_KEY_USAGE_ENCRYPT :
831 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
832 }
833 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
834 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
835 {
836 return( PSA_KEY_USAGE_DERIVE );
837 }
838 else
839 {
840 return( 0 );
841 }
842
843}
Darryl Green0c6575a2018-11-07 16:05:30 +0000844
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100845/* An overapproximation of the amount of storage needed for a key of the
846 * given type and with the given content. The API doesn't make it easy
847 * to find a good value for the size. The current implementation doesn't
848 * care about the value anyway. */
849#define KEY_BITS_FROM_DATA( type, data ) \
850 ( data )->len
851
Darryl Green0c6575a2018-11-07 16:05:30 +0000852typedef enum {
853 IMPORT_KEY = 0,
854 GENERATE_KEY = 1,
855 DERIVE_KEY = 2
856} generate_method;
857
Gilles Peskinee59236f2018-01-27 23:32:46 +0100858/* END_HEADER */
859
860/* BEGIN_DEPENDENCIES
861 * depends_on:MBEDTLS_PSA_CRYPTO_C
862 * END_DEPENDENCIES
863 */
864
865/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200866void static_checks( )
867{
868 size_t max_truncated_mac_size =
869 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
870
871 /* Check that the length for a truncated MAC always fits in the algorithm
872 * encoding. The shifted mask is the maximum truncated value. The
873 * untruncated algorithm may be one byte larger. */
874 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
875}
876/* END_CASE */
877
878/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200879void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100880{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100881 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200882 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300886 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100887 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888
Gilles Peskine8817f612018-12-18 00:18:46 +0100889 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
890 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100891 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100892 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100894 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895
896exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100897 mbedtls_psa_crypto_free( );
898}
899/* END_CASE */
900
901/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100902void import_twice( int alg_arg, int usage_arg,
903 int type1_arg, data_t *data1,
904 int expected_import1_status_arg,
905 int type2_arg, data_t *data2,
906 int expected_import2_status_arg )
907{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100908 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100909 psa_algorithm_t alg = alg_arg;
910 psa_key_usage_t usage = usage_arg;
911 psa_key_type_t type1 = type1_arg;
912 psa_status_t expected_import1_status = expected_import1_status_arg;
913 psa_key_type_t type2 = type2_arg;
914 psa_status_t expected_import2_status = expected_import2_status_arg;
915 psa_key_policy_t policy;
916 psa_status_t status;
917
Gilles Peskine8817f612018-12-18 00:18:46 +0100918 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100919
Gilles Peskine8817f612018-12-18 00:18:46 +0100920 PSA_ASSERT( psa_allocate_key( type1,
921 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
922 KEY_BITS_FROM_DATA( type2, data2 ) ),
923 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100924 psa_key_policy_init( &policy );
925 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100926 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100927
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100928 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100929 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100930 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100931 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100932
933 if( expected_import1_status == PSA_SUCCESS ||
934 expected_import2_status == PSA_SUCCESS )
935 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100936 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100937 }
938
939exit:
940 mbedtls_psa_crypto_free( );
941}
942/* END_CASE */
943
944/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200945void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
946{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100947 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200948 size_t bits = bits_arg;
949 psa_status_t expected_status = expected_status_arg;
950 psa_status_t status;
951 psa_key_type_t type =
952 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
953 size_t buffer_size = /* Slight overapproximations */
954 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200955 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200956 unsigned char *p;
957 int ret;
958 size_t length;
959
Gilles Peskine8817f612018-12-18 00:18:46 +0100960 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200961 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200962
963 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
964 bits, keypair ) ) >= 0 );
965 length = ret;
966
967 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100968 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100969 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100970 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200971 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100972 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200973
974exit:
975 mbedtls_free( buffer );
976 mbedtls_psa_crypto_free( );
977}
978/* END_CASE */
979
980/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300981void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300982 int type_arg,
983 int alg_arg,
984 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 int expected_bits,
986 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200987 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 int canonical_input )
989{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100990 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200992 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200993 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 unsigned char *exported = NULL;
996 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100998 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 size_t reexported_length;
1000 psa_key_type_t got_type;
1001 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001002 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001005 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001006 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001007 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001009 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011
Gilles Peskine8817f612018-12-18 00:18:46 +01001012 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001013 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001014 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001015 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001016
Gilles Peskinefe11b722018-12-18 00:24:04 +01001017 TEST_EQUAL( psa_get_key_information(
1018 handle, NULL, NULL ), PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001019
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001021 PSA_ASSERT( psa_import_key( handle, type,
1022 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023
1024 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001025 PSA_ASSERT( psa_get_key_information( handle,
1026 &got_type,
1027 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001028 TEST_EQUAL( got_type, type );
1029 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030
1031 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001032 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033 exported, export_size,
1034 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001035 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001036
1037 /* The exported length must be set by psa_export_key() to a value between 0
1038 * and export_size. On errors, the exported length must be 0. */
1039 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1040 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1041 TEST_ASSERT( exported_length <= export_size );
1042
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001043 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001044 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001046 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001047 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001048 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001049 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001051 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001052 goto exit;
1053
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001054 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001055 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056 else
1057 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001058 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001059 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1060 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001061
Gilles Peskine8817f612018-12-18 00:18:46 +01001062 PSA_ASSERT( psa_import_key( handle2, type,
1063 exported,
1064 exported_length ) );
1065 PSA_ASSERT( psa_export_key( handle2,
1066 reexported,
1067 export_size,
1068 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001069 ASSERT_COMPARE( exported, exported_length,
1070 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001071 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001072 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001073 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001074
1075destroy:
1076 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001077 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001078 TEST_EQUAL( psa_get_key_information(
1079 handle, NULL, NULL ), PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001080
1081exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001082 mbedtls_free( exported );
1083 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001084 mbedtls_psa_crypto_free( );
1085}
1086/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001087
Moran Pekerf709f4a2018-06-06 17:26:04 +03001088/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001089void import_key_nonempty_slot( )
1090{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001091 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001092 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1093 psa_status_t status;
1094 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001095 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001096
Gilles Peskine8817f612018-12-18 00:18:46 +01001097 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1098 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001099
Moran Peker28a38e62018-11-07 16:18:24 +02001100 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001101 PSA_ASSERT( psa_import_key( handle, type,
1102 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001103
1104 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001105 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001106 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001107
1108exit:
1109 mbedtls_psa_crypto_free( );
1110}
1111/* END_CASE */
1112
1113/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001114void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001115{
1116 psa_status_t status;
1117 unsigned char *exported = NULL;
1118 size_t export_size = 0;
1119 size_t exported_length = INVALID_EXPORT_LENGTH;
1120 psa_status_t expected_export_status = expected_export_status_arg;
1121
Gilles Peskine8817f612018-12-18 00:18:46 +01001122 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001123
1124 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001125 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001126 exported, export_size,
1127 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001128 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001129
1130exit:
1131 mbedtls_psa_crypto_free( );
1132}
1133/* END_CASE */
1134
1135/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001136void export_with_no_key_activity( )
1137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001138 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001139 psa_algorithm_t alg = PSA_ALG_CTR;
1140 psa_status_t status;
1141 psa_key_policy_t policy;
1142 unsigned char *exported = NULL;
1143 size_t export_size = 0;
1144 size_t exported_length = INVALID_EXPORT_LENGTH;
1145
Gilles Peskine8817f612018-12-18 00:18:46 +01001146 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001147
Gilles Peskine8817f612018-12-18 00:18:46 +01001148 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1149 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001150 psa_key_policy_init( &policy );
1151 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001152 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001153
1154 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001155 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001156 exported, export_size,
1157 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001158 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001159
1160exit:
1161 mbedtls_psa_crypto_free( );
1162}
1163/* END_CASE */
1164
1165/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001166void cipher_with_no_key_activity( )
1167{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001168 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001169 psa_status_t status;
1170 psa_key_policy_t policy;
1171 psa_cipher_operation_t operation;
1172 int exercise_alg = PSA_ALG_CTR;
1173
Gilles Peskine8817f612018-12-18 00:18:46 +01001174 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001175
Gilles Peskine8817f612018-12-18 00:18:46 +01001176 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1177 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001178 psa_key_policy_init( &policy );
1179 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001181
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001182 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001183 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001184
1185exit:
1186 psa_cipher_abort( &operation );
1187 mbedtls_psa_crypto_free( );
1188}
1189/* END_CASE */
1190
1191/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001192void export_after_import_failure( data_t *data, int type_arg,
1193 int expected_import_status_arg )
1194{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001195 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001196 psa_key_type_t type = type_arg;
1197 psa_status_t status;
1198 unsigned char *exported = NULL;
1199 size_t export_size = 0;
1200 psa_status_t expected_import_status = expected_import_status_arg;
1201 size_t exported_length = INVALID_EXPORT_LENGTH;
1202
Gilles Peskine8817f612018-12-18 00:18:46 +01001203 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001204
Gilles Peskine8817f612018-12-18 00:18:46 +01001205 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1206 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001207
Moran Peker34550092018-11-07 16:19:34 +02001208 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001209 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001210 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001211 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001212
1213 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001214 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001215 exported, export_size,
1216 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001217 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001218
1219exit:
1220 mbedtls_psa_crypto_free( );
1221}
1222/* END_CASE */
1223
1224/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001225void cipher_after_import_failure( data_t *data, int type_arg,
1226 int expected_import_status_arg )
1227{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001228 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001229 psa_cipher_operation_t operation;
1230 psa_key_type_t type = type_arg;
1231 psa_status_t status;
1232 psa_status_t expected_import_status = expected_import_status_arg;
1233 int exercise_alg = PSA_ALG_CTR;
1234
Gilles Peskine8817f612018-12-18 00:18:46 +01001235 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001236
Gilles Peskine8817f612018-12-18 00:18:46 +01001237 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1238 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001239
Moran Pekerce500072018-11-07 16:20:07 +02001240 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001241 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001242 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001243 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001244
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001245 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001246 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001247
1248exit:
1249 psa_cipher_abort( &operation );
1250 mbedtls_psa_crypto_free( );
1251}
1252/* END_CASE */
1253
1254/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001255void export_after_destroy_key( data_t *data, int type_arg )
1256{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001257 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001258 psa_key_type_t type = type_arg;
1259 psa_status_t status;
1260 psa_key_policy_t policy;
1261 psa_algorithm_t alg = PSA_ALG_CTR;
1262 unsigned char *exported = NULL;
1263 size_t export_size = 0;
1264 size_t exported_length = INVALID_EXPORT_LENGTH;
1265
Gilles Peskine8817f612018-12-18 00:18:46 +01001266 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001267
Gilles Peskine8817f612018-12-18 00:18:46 +01001268 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1269 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001270 psa_key_policy_init( &policy );
1271 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001272 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001273 export_size = (ptrdiff_t) data->len;
1274 ASSERT_ALLOC( exported, export_size );
1275
1276 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001277 PSA_ASSERT( psa_import_key( handle, type,
1278 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001279
Gilles Peskine8817f612018-12-18 00:18:46 +01001280 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1281 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001282
1283 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001284 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001285
1286 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001287 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001288 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001289 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001290
1291exit:
1292 mbedtls_free( exported );
1293 mbedtls_psa_crypto_free( );
1294}
1295/* END_CASE */
1296
1297/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001298void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001299 int type_arg,
1300 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001301 int export_size_delta,
1302 int expected_export_status_arg,
1303 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001304{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001305 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001306 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001307 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001308 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001309 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001310 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001311 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001312 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001313 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001314
Gilles Peskine8817f612018-12-18 00:18:46 +01001315 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001316
Gilles Peskine8817f612018-12-18 00:18:46 +01001317 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1318 &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001319 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001320 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001321 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001322
1323 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001324 PSA_ASSERT( psa_import_key( handle, type,
1325 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001326
Gilles Peskine49c25912018-10-29 15:15:31 +01001327 /* Export the public key */
1328 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001329 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001330 exported, export_size,
1331 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001332 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001333 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001334 {
1335 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1336 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001337 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001338 TEST_ASSERT( expected_public_key->len <=
1339 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001340 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1341 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001342 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001343
1344exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001345 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001346 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001347 mbedtls_psa_crypto_free( );
1348}
1349/* END_CASE */
1350
Gilles Peskine20035e32018-02-03 22:44:14 +01001351/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001352void import_and_exercise_key( data_t *data,
1353 int type_arg,
1354 int bits_arg,
1355 int alg_arg )
1356{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001358 psa_key_type_t type = type_arg;
1359 size_t bits = bits_arg;
1360 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001361 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001362 psa_key_policy_t policy;
1363 psa_key_type_t got_type;
1364 size_t got_bits;
1365 psa_status_t status;
1366
Gilles Peskine8817f612018-12-18 00:18:46 +01001367 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001368
Gilles Peskine8817f612018-12-18 00:18:46 +01001369 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1370 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001371 psa_key_policy_init( &policy );
1372 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001373 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001374
1375 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001376 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001377 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001378
1379 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001380 PSA_ASSERT( psa_get_key_information( handle,
1381 &got_type,
1382 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001383 TEST_EQUAL( got_type, type );
1384 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001385
1386 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001387 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001388 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001389
1390exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001391 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001392 mbedtls_psa_crypto_free( );
1393}
1394/* END_CASE */
1395
1396/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001397void key_policy( int usage_arg, int alg_arg )
1398{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001399 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001400 psa_algorithm_t alg = alg_arg;
1401 psa_key_usage_t usage = usage_arg;
1402 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1403 unsigned char key[32] = {0};
1404 psa_key_policy_t policy_set;
1405 psa_key_policy_t policy_get;
1406
1407 memset( key, 0x2a, sizeof( key ) );
1408
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001410
Gilles Peskine8817f612018-12-18 00:18:46 +01001411 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1412 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001413 psa_key_policy_init( &policy_set );
1414 psa_key_policy_init( &policy_get );
Gilles Peskined5b33222018-06-18 22:20:03 +02001415 psa_key_policy_set_usage( &policy_set, usage, alg );
1416
Gilles Peskinefe11b722018-12-18 00:24:04 +01001417 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1418 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001419 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001420
Gilles Peskine8817f612018-12-18 00:18:46 +01001421 PSA_ASSERT( psa_import_key( handle, key_type,
1422 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001423
Gilles Peskine8817f612018-12-18 00:18:46 +01001424 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001425
Gilles Peskinefe11b722018-12-18 00:24:04 +01001426 TEST_EQUAL( policy_get.usage, policy_set.usage );
1427 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001428
1429exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001430 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001431 mbedtls_psa_crypto_free( );
1432}
1433/* END_CASE */
1434
1435/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436void mac_key_policy( int policy_usage,
1437 int policy_alg,
1438 int key_type,
1439 data_t *key_data,
1440 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001441{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001442 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001443 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001444 psa_mac_operation_t operation;
1445 psa_status_t status;
1446 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001447
Gilles Peskine8817f612018-12-18 00:18:46 +01001448 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001449
Gilles Peskine8817f612018-12-18 00:18:46 +01001450 PSA_ASSERT( psa_allocate_key( key_type,
1451 KEY_BITS_FROM_DATA( key_type, key_data ),
1452 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001453 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001454 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001455 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001456
Gilles Peskine8817f612018-12-18 00:18:46 +01001457 PSA_ASSERT( psa_import_key( handle, key_type,
1458 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001459
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001460 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001461 if( policy_alg == exercise_alg &&
1462 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001463 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001465 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001466 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001467
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001468 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001469 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001470 if( policy_alg == exercise_alg &&
1471 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001472 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001474 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001475
1476exit:
1477 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001478 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001479 mbedtls_psa_crypto_free( );
1480}
1481/* END_CASE */
1482
1483/* BEGIN_CASE */
1484void cipher_key_policy( int policy_usage,
1485 int policy_alg,
1486 int key_type,
1487 data_t *key_data,
1488 int exercise_alg )
1489{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001490 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001491 psa_key_policy_t policy;
1492 psa_cipher_operation_t operation;
1493 psa_status_t status;
1494
Gilles Peskine8817f612018-12-18 00:18:46 +01001495 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001496
Gilles Peskine8817f612018-12-18 00:18:46 +01001497 PSA_ASSERT( psa_allocate_key( key_type,
1498 KEY_BITS_FROM_DATA( key_type, key_data ),
1499 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001500 psa_key_policy_init( &policy );
1501 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001502 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001503
Gilles Peskine8817f612018-12-18 00:18:46 +01001504 PSA_ASSERT( psa_import_key( handle, key_type,
1505 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001506
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001507 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001508 if( policy_alg == exercise_alg &&
1509 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001510 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001512 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513 psa_cipher_abort( &operation );
1514
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001515 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001516 if( policy_alg == exercise_alg &&
1517 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001518 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001519 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001520 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001521
1522exit:
1523 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001524 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001525 mbedtls_psa_crypto_free( );
1526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
1530void aead_key_policy( int policy_usage,
1531 int policy_alg,
1532 int key_type,
1533 data_t *key_data,
1534 int nonce_length_arg,
1535 int tag_length_arg,
1536 int exercise_alg )
1537{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001538 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001539 psa_key_policy_t policy;
1540 psa_status_t status;
1541 unsigned char nonce[16] = {0};
1542 size_t nonce_length = nonce_length_arg;
1543 unsigned char tag[16];
1544 size_t tag_length = tag_length_arg;
1545 size_t output_length;
1546
1547 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1548 TEST_ASSERT( tag_length <= sizeof( tag ) );
1549
Gilles Peskine8817f612018-12-18 00:18:46 +01001550 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001551
Gilles Peskine8817f612018-12-18 00:18:46 +01001552 PSA_ASSERT( psa_allocate_key( key_type,
1553 KEY_BITS_FROM_DATA( key_type, key_data ),
1554 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001555 psa_key_policy_init( &policy );
1556 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001557 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558
Gilles Peskine8817f612018-12-18 00:18:46 +01001559 PSA_ASSERT( psa_import_key( handle, key_type,
1560 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001561
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001562 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001563 nonce, nonce_length,
1564 NULL, 0,
1565 NULL, 0,
1566 tag, tag_length,
1567 &output_length );
1568 if( policy_alg == exercise_alg &&
1569 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001570 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001572 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001573
1574 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001575 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001576 nonce, nonce_length,
1577 NULL, 0,
1578 tag, tag_length,
1579 NULL, 0,
1580 &output_length );
1581 if( policy_alg == exercise_alg &&
1582 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001583 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001584 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001585 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001586
1587exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001588 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001589 mbedtls_psa_crypto_free( );
1590}
1591/* END_CASE */
1592
1593/* BEGIN_CASE */
1594void asymmetric_encryption_key_policy( int policy_usage,
1595 int policy_alg,
1596 int key_type,
1597 data_t *key_data,
1598 int exercise_alg )
1599{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001600 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001601 psa_key_policy_t policy;
1602 psa_status_t status;
1603 size_t key_bits;
1604 size_t buffer_length;
1605 unsigned char *buffer = NULL;
1606 size_t output_length;
1607
Gilles Peskine8817f612018-12-18 00:18:46 +01001608 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_allocate_key( key_type,
1611 KEY_BITS_FROM_DATA( key_type, key_data ),
1612 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001613 psa_key_policy_init( &policy );
1614 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616
Gilles Peskine8817f612018-12-18 00:18:46 +01001617 PSA_ASSERT( psa_import_key( handle, key_type,
1618 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_get_key_information( handle,
1621 NULL,
1622 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1624 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001625 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 NULL, 0,
1629 NULL, 0,
1630 buffer, buffer_length,
1631 &output_length );
1632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001638 if( buffer_length != 0 )
1639 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 buffer, buffer_length,
1642 NULL, 0,
1643 buffer, buffer_length,
1644 &output_length );
1645 if( policy_alg == exercise_alg &&
1646 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001647 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650
1651exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001652 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 mbedtls_psa_crypto_free( );
1654 mbedtls_free( buffer );
1655}
1656/* END_CASE */
1657
1658/* BEGIN_CASE */
1659void asymmetric_signature_key_policy( int policy_usage,
1660 int policy_alg,
1661 int key_type,
1662 data_t *key_data,
1663 int exercise_alg )
1664{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001665 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001666 psa_key_policy_t policy;
1667 psa_status_t status;
1668 unsigned char payload[16] = {1};
1669 size_t payload_length = sizeof( payload );
1670 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1671 size_t signature_length;
1672
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674
Gilles Peskine8817f612018-12-18 00:18:46 +01001675 PSA_ASSERT( psa_allocate_key( key_type,
1676 KEY_BITS_FROM_DATA( key_type, key_data ),
1677 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 psa_key_policy_init( &policy );
1679 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681
Gilles Peskine8817f612018-12-18 00:18:46 +01001682 PSA_ASSERT( psa_import_key( handle, key_type,
1683 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001684
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001685 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001687 signature, sizeof( signature ),
1688 &signature_length );
1689 if( policy_alg == exercise_alg &&
1690 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001691 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001693 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001694
1695 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001696 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001697 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698 signature, sizeof( signature ) );
1699 if( policy_alg == exercise_alg &&
1700 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001701 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001702 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001703 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001704
1705exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001706 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001707 mbedtls_psa_crypto_free( );
1708}
1709/* END_CASE */
1710
1711/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001712void derive_key_policy( int policy_usage,
1713 int policy_alg,
1714 int key_type,
1715 data_t *key_data,
1716 int exercise_alg )
1717{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001718 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001719 psa_key_policy_t policy;
1720 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1721 psa_status_t status;
1722
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001724
Gilles Peskine8817f612018-12-18 00:18:46 +01001725 PSA_ASSERT( psa_allocate_key( key_type,
1726 KEY_BITS_FROM_DATA( key_type, key_data ),
1727 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001728 psa_key_policy_init( &policy );
1729 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001731
Gilles Peskine8817f612018-12-18 00:18:46 +01001732 PSA_ASSERT( psa_import_key( handle, key_type,
1733 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001734
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001735 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001736 exercise_alg,
1737 NULL, 0,
1738 NULL, 0,
1739 1 );
1740 if( policy_alg == exercise_alg &&
1741 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001742 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001743 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001744 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001745
1746exit:
1747 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001748 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001749 mbedtls_psa_crypto_free( );
1750}
1751/* END_CASE */
1752
1753/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001754void agreement_key_policy( int policy_usage,
1755 int policy_alg,
1756 int key_type_arg,
1757 data_t *key_data,
1758 int exercise_alg )
1759{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001760 psa_key_handle_t handle = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001761 psa_key_policy_t policy;
1762 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001763 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1764 psa_status_t status;
1765
Gilles Peskine8817f612018-12-18 00:18:46 +01001766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001767
Gilles Peskine8817f612018-12-18 00:18:46 +01001768 PSA_ASSERT( psa_allocate_key( key_type,
1769 KEY_BITS_FROM_DATA( key_type, key_data ),
1770 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001771 psa_key_policy_init( &policy );
1772 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001774
Gilles Peskine8817f612018-12-18 00:18:46 +01001775 PSA_ASSERT( psa_import_key( handle, key_type,
1776 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001777
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001778 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001779
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780 if( policy_alg == exercise_alg &&
1781 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001782 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001783 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001784 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001785
1786exit:
1787 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001788 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001789 mbedtls_psa_crypto_free( );
1790}
1791/* END_CASE */
1792
1793/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001794void hash_setup( int alg_arg,
1795 int expected_status_arg )
1796{
1797 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001798 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001799 psa_hash_operation_t operation;
1800 psa_status_t status;
1801
Gilles Peskine8817f612018-12-18 00:18:46 +01001802 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001803
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001804 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001805 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001806 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001807
1808exit:
1809 mbedtls_psa_crypto_free( );
1810}
1811/* END_CASE */
1812
1813/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001814void hash_bad_order( )
1815{
1816 unsigned char input[] = "";
1817 /* SHA-256 hash of an empty string */
1818 unsigned char hash[] = {
1819 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1820 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1821 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1822 size_t hash_len;
1823 psa_hash_operation_t operation;
1824
Gilles Peskine8817f612018-12-18 00:18:46 +01001825 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001826
1827 /* psa_hash_update without calling psa_hash_setup beforehand */
1828 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001829 TEST_EQUAL( psa_hash_update( &operation,
1830 input, sizeof( input ) ),
1831 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001832
1833 /* psa_hash_verify without calling psa_hash_setup beforehand */
1834 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001835 TEST_EQUAL( psa_hash_verify( &operation,
1836 hash, sizeof( hash ) ),
1837 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001838
1839 /* psa_hash_finish without calling psa_hash_setup beforehand */
1840 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001841 TEST_EQUAL( psa_hash_finish( &operation,
1842 hash, sizeof( hash ), &hash_len ),
1843 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001844
1845exit:
1846 mbedtls_psa_crypto_free( );
1847}
1848/* END_CASE */
1849
itayzafrir27e69452018-11-01 14:26:34 +02001850/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1851void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001852{
1853 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001854 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1855 * appended to it */
1856 unsigned char hash[] = {
1857 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1858 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1859 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001860 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001861 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001862
Gilles Peskine8817f612018-12-18 00:18:46 +01001863 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001864
itayzafrir27e69452018-11-01 14:26:34 +02001865 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001866 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001867 TEST_EQUAL( psa_hash_verify( &operation,
1868 hash, expected_size - 1 ),
1869 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001870
itayzafrir27e69452018-11-01 14:26:34 +02001871 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001872 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001873 TEST_EQUAL( psa_hash_verify( &operation,
1874 hash + 1, expected_size ),
1875 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001876
itayzafrir27e69452018-11-01 14:26:34 +02001877 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001878 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001879 TEST_EQUAL( psa_hash_verify( &operation,
1880 hash, sizeof( hash ) ),
1881 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001882
itayzafrirec93d302018-10-18 18:01:10 +03001883exit:
1884 mbedtls_psa_crypto_free( );
1885}
1886/* END_CASE */
1887
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001888/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1889void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001890{
1891 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001892 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001893 size_t expected_size = PSA_HASH_SIZE( alg );
1894 psa_hash_operation_t operation;
1895 size_t hash_len;
1896
Gilles Peskine8817f612018-12-18 00:18:46 +01001897 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001898
itayzafrir58028322018-10-25 10:22:01 +03001899 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001900 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001901 TEST_EQUAL( psa_hash_finish( &operation,
1902 hash, expected_size - 1,
1903 &hash_len ), PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001904
1905exit:
1906 mbedtls_psa_crypto_free( );
1907}
1908/* END_CASE */
1909
1910/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001911void mac_setup( int key_type_arg,
1912 data_t *key,
1913 int alg_arg,
1914 int expected_status_arg )
1915{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001916 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001917 psa_key_type_t key_type = key_type_arg;
1918 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001919 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001920 psa_mac_operation_t operation;
1921 psa_key_policy_t policy;
1922 psa_status_t status;
1923
Gilles Peskine8817f612018-12-18 00:18:46 +01001924 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001925
Gilles Peskine8817f612018-12-18 00:18:46 +01001926 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1927 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001928 psa_key_policy_init( &policy );
1929 psa_key_policy_set_usage( &policy,
1930 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1931 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001932 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001933
Gilles Peskine8817f612018-12-18 00:18:46 +01001934 PSA_ASSERT( psa_import_key( handle, key_type,
1935 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001936
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001937 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001939 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001940
1941exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001942 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001943 mbedtls_psa_crypto_free( );
1944}
1945/* END_CASE */
1946
1947/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001948void mac_sign( int key_type_arg,
1949 data_t *key,
1950 int alg_arg,
1951 data_t *input,
1952 data_t *expected_mac )
1953{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001954 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001955 psa_key_type_t key_type = key_type_arg;
1956 psa_algorithm_t alg = alg_arg;
1957 psa_mac_operation_t operation;
1958 psa_key_policy_t policy;
1959 /* Leave a little extra room in the output buffer. At the end of the
1960 * test, we'll check that the implementation didn't overwrite onto
1961 * this extra room. */
1962 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1963 size_t mac_buffer_size =
1964 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1965 size_t mac_length = 0;
1966
1967 memset( actual_mac, '+', sizeof( actual_mac ) );
1968 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1969 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1970
Gilles Peskine8817f612018-12-18 00:18:46 +01001971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001972
Gilles Peskine8817f612018-12-18 00:18:46 +01001973 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1974 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001975 psa_key_policy_init( &policy );
1976 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001977 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001978
Gilles Peskine8817f612018-12-18 00:18:46 +01001979 PSA_ASSERT( psa_import_key( handle, key_type,
1980 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001981
1982 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01001983 PSA_ASSERT( psa_mac_sign_setup( &operation,
1984 handle, alg ) );
1985 PSA_ASSERT( psa_mac_update( &operation,
1986 input->x, input->len ) );
1987 PSA_ASSERT( psa_mac_sign_finish( &operation,
1988 actual_mac, mac_buffer_size,
1989 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001990
1991 /* Compare with the expected value. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001992 TEST_EQUAL( mac_length, expected_mac->len );
1993 TEST_EQUAL( memcmp( actual_mac, expected_mac->x, mac_length ), 0 );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001994
1995 /* Verify that the end of the buffer is untouched. */
1996 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1997 sizeof( actual_mac ) - mac_length ) );
1998
1999exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002000 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002001 mbedtls_psa_crypto_free( );
2002}
2003/* END_CASE */
2004
2005/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002006void mac_verify( int key_type_arg,
2007 data_t *key,
2008 int alg_arg,
2009 data_t *input,
2010 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002011{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002012 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013 psa_key_type_t key_type = key_type_arg;
2014 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002016 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017
Gilles Peskine69c12672018-06-28 00:07:19 +02002018 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2019
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002023 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002024 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2025 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026
Gilles Peskine8817f612018-12-18 00:18:46 +01002027 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028
Gilles Peskine8817f612018-12-18 00:18:46 +01002029 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2030 &handle ) );
mohammad16036df908f2018-04-02 08:34:15 -07002031 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002032 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002033 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002034
Gilles Peskine8817f612018-12-18 00:18:46 +01002035 PSA_ASSERT( psa_import_key( handle, key_type,
2036 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002037
Gilles Peskine8817f612018-12-18 00:18:46 +01002038 PSA_ASSERT( psa_mac_verify_setup( &operation,
2039 handle, alg ) );
2040 PSA_ASSERT( psa_destroy_key( handle ) );
2041 PSA_ASSERT( psa_mac_update( &operation,
2042 input->x, input->len ) );
2043 PSA_ASSERT( psa_mac_verify_finish( &operation,
2044 expected_mac->x,
2045 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046
2047exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002048 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002049 mbedtls_psa_crypto_free( );
2050}
2051/* END_CASE */
2052
2053/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002054void cipher_setup( int key_type_arg,
2055 data_t *key,
2056 int alg_arg,
2057 int expected_status_arg )
2058{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002059 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002060 psa_key_type_t key_type = key_type_arg;
2061 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002062 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002063 psa_cipher_operation_t operation;
2064 psa_key_policy_t policy;
2065 psa_status_t status;
2066
Gilles Peskine8817f612018-12-18 00:18:46 +01002067 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002068
Gilles Peskine8817f612018-12-18 00:18:46 +01002069 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2070 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002071 psa_key_policy_init( &policy );
2072 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002073 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002074
Gilles Peskine8817f612018-12-18 00:18:46 +01002075 PSA_ASSERT( psa_import_key( handle, key_type,
2076 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002077
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002078 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002079 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002080 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002081
2082exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002083 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002084 mbedtls_psa_crypto_free( );
2085}
2086/* END_CASE */
2087
2088/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002089void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002090 data_t *key,
2091 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002092 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002093{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002094 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002095 psa_status_t status;
2096 psa_key_type_t key_type = key_type_arg;
2097 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002098 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002099 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002100 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002101 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002102 size_t output_buffer_size = 0;
2103 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002104 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002105 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002106 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002107
Gilles Peskine50e586b2018-06-08 14:28:46 +02002108 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002109 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002110 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002111 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2112 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2113 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002114
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002115 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2116 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002117
Gilles Peskine8817f612018-12-18 00:18:46 +01002118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002119
Gilles Peskine8817f612018-12-18 00:18:46 +01002120 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2121 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002122 psa_key_policy_init( &policy );
2123 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002124 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002125
Gilles Peskine8817f612018-12-18 00:18:46 +01002126 PSA_ASSERT( psa_import_key( handle, key_type,
2127 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002128
Gilles Peskine8817f612018-12-18 00:18:46 +01002129 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2130 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002131
Gilles Peskine8817f612018-12-18 00:18:46 +01002132 PSA_ASSERT( psa_cipher_set_iv( &operation,
2133 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002134 output_buffer_size = ( (size_t) input->len +
2135 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002136 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002137
Gilles Peskine8817f612018-12-18 00:18:46 +01002138 PSA_ASSERT( psa_cipher_update( &operation,
2139 input->x, input->len,
2140 output, output_buffer_size,
2141 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002142 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002143 status = psa_cipher_finish( &operation,
2144 output + function_output_length,
2145 output_buffer_size,
2146 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002147 total_output_length += function_output_length;
2148
Gilles Peskinefe11b722018-12-18 00:24:04 +01002149 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002150 if( expected_status == PSA_SUCCESS )
2151 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002152 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002153 ASSERT_COMPARE( expected_output->x, expected_output->len,
2154 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002156
Gilles Peskine50e586b2018-06-08 14:28:46 +02002157exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002158 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002159 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002160 mbedtls_psa_crypto_free( );
2161}
2162/* END_CASE */
2163
2164/* BEGIN_CASE */
2165void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002166 data_t *key,
2167 data_t *input,
2168 int first_part_size,
2169 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002170{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002171 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002172 psa_key_type_t key_type = key_type_arg;
2173 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002174 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002175 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002176 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002177 size_t output_buffer_size = 0;
2178 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002179 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002181 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002182
Gilles Peskine50e586b2018-06-08 14:28:46 +02002183 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002184 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002186 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2187 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2188 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002189
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002190 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2191 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002192
Gilles Peskine8817f612018-12-18 00:18:46 +01002193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002194
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2196 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002197 psa_key_policy_init( &policy );
2198 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002199 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002200
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_import_key( handle, key_type,
2202 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002203
Gilles Peskine8817f612018-12-18 00:18:46 +01002204 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2205 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002206
Gilles Peskine8817f612018-12-18 00:18:46 +01002207 PSA_ASSERT( psa_cipher_set_iv( &operation,
2208 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002209 output_buffer_size = ( (size_t) input->len +
2210 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002211 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002212
Gilles Peskine4abf7412018-06-18 16:35:34 +02002213 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002214 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2215 output, output_buffer_size,
2216 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002217 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002218 PSA_ASSERT( psa_cipher_update( &operation,
2219 input->x + first_part_size,
2220 input->len - first_part_size,
2221 output, output_buffer_size,
2222 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002223 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002224 PSA_ASSERT( psa_cipher_finish( &operation,
2225 output + function_output_length,
2226 output_buffer_size,
2227 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002228 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002230
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002231 ASSERT_COMPARE( expected_output->x, expected_output->len,
2232 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233
2234exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002235 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002236 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002237 mbedtls_psa_crypto_free( );
2238}
2239/* END_CASE */
2240
2241/* BEGIN_CASE */
2242void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002243 data_t *key,
2244 data_t *input,
2245 int first_part_size,
2246 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002247{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002248 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002249
2250 psa_key_type_t key_type = key_type_arg;
2251 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002252 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002253 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002254 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002255 size_t output_buffer_size = 0;
2256 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002257 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002258 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002259 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002260
Gilles Peskine50e586b2018-06-08 14:28:46 +02002261 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002263 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002264 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2265 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2266 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002267
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002268 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2269 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002270
Gilles Peskine8817f612018-12-18 00:18:46 +01002271 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002272
Gilles Peskine8817f612018-12-18 00:18:46 +01002273 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2274 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002275 psa_key_policy_init( &policy );
2276 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002277 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002278
Gilles Peskine8817f612018-12-18 00:18:46 +01002279 PSA_ASSERT( psa_import_key( handle, key_type,
2280 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002281
Gilles Peskine8817f612018-12-18 00:18:46 +01002282 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2283 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002284
Gilles Peskine8817f612018-12-18 00:18:46 +01002285 PSA_ASSERT( psa_cipher_set_iv( &operation,
2286 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002287
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002288 output_buffer_size = ( (size_t) input->len +
2289 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002290 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002291
Gilles Peskine4abf7412018-06-18 16:35:34 +02002292 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002293 PSA_ASSERT( psa_cipher_update( &operation,
2294 input->x, first_part_size,
2295 output, output_buffer_size,
2296 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002297 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002298 PSA_ASSERT( psa_cipher_update( &operation,
2299 input->x + first_part_size,
2300 input->len - first_part_size,
2301 output, output_buffer_size,
2302 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002303 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002304 PSA_ASSERT( psa_cipher_finish( &operation,
2305 output + function_output_length,
2306 output_buffer_size,
2307 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002308 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002309 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002310
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002311 ASSERT_COMPARE( expected_output->x, expected_output->len,
2312 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002313
2314exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002315 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002316 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002317 mbedtls_psa_crypto_free( );
2318}
2319/* END_CASE */
2320
Gilles Peskine50e586b2018-06-08 14:28:46 +02002321/* BEGIN_CASE */
2322void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002323 data_t *key,
2324 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002325 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002326{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002327 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002328 psa_status_t status;
2329 psa_key_type_t key_type = key_type_arg;
2330 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002331 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002332 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002333 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002334 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002335 size_t output_buffer_size = 0;
2336 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002337 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002339 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002340
Gilles Peskine50e586b2018-06-08 14:28:46 +02002341 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002343 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002344 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2345 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2346 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002347
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002348 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2349 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002350
Gilles Peskine8817f612018-12-18 00:18:46 +01002351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002352
Gilles Peskine8817f612018-12-18 00:18:46 +01002353 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2354 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002355 psa_key_policy_init( &policy );
2356 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002357 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002358
Gilles Peskine8817f612018-12-18 00:18:46 +01002359 PSA_ASSERT( psa_import_key( handle, key_type,
2360 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002361
Gilles Peskine8817f612018-12-18 00:18:46 +01002362 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2363 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002364
Gilles Peskine8817f612018-12-18 00:18:46 +01002365 PSA_ASSERT( psa_cipher_set_iv( &operation,
2366 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002367
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002368 output_buffer_size = ( (size_t) input->len +
2369 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002370 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002371
Gilles Peskine8817f612018-12-18 00:18:46 +01002372 PSA_ASSERT( psa_cipher_update( &operation,
2373 input->x, input->len,
2374 output, output_buffer_size,
2375 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002376 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002377 status = psa_cipher_finish( &operation,
2378 output + function_output_length,
2379 output_buffer_size,
2380 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002381 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002382 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002383
2384 if( expected_status == PSA_SUCCESS )
2385 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002386 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002387 ASSERT_COMPARE( expected_output->x, expected_output->len,
2388 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002389 }
2390
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002392 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002393 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002394 mbedtls_psa_crypto_free( );
2395}
2396/* END_CASE */
2397
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398/* BEGIN_CASE */
2399void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002400 data_t *key,
2401 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002402{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002403 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002404 psa_key_type_t key_type = key_type_arg;
2405 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002406 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002407 size_t iv_size = 16;
2408 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002409 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002410 size_t output1_size = 0;
2411 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002412 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002413 size_t output2_size = 0;
2414 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002415 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002416 psa_cipher_operation_t operation1;
2417 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002418 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002419
mohammad1603d7d7ba52018-03-12 18:51:53 +02002420 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002421 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002422 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2423 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002424
Gilles Peskine8817f612018-12-18 00:18:46 +01002425 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002426
Gilles Peskine8817f612018-12-18 00:18:46 +01002427 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2428 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002429 psa_key_policy_init( &policy );
2430 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002431 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002432
Gilles Peskine8817f612018-12-18 00:18:46 +01002433 PSA_ASSERT( psa_import_key( handle, key_type,
2434 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002435
Gilles Peskine8817f612018-12-18 00:18:46 +01002436 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2437 handle, alg ) );
2438 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2439 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002440
Gilles Peskine8817f612018-12-18 00:18:46 +01002441 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2442 iv, iv_size,
2443 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002444 output1_size = ( (size_t) input->len +
2445 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002446 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002447
Gilles Peskine8817f612018-12-18 00:18:46 +01002448 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2449 output1, output1_size,
2450 &output1_length ) );
2451 PSA_ASSERT( psa_cipher_finish( &operation1,
2452 output1 + output1_length, output1_size,
2453 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002454
Gilles Peskine048b7f02018-06-08 14:20:49 +02002455 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002456
Gilles Peskine8817f612018-12-18 00:18:46 +01002457 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002458
2459 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002460 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002461
Gilles Peskine8817f612018-12-18 00:18:46 +01002462 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2463 iv, iv_length ) );
2464 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2465 output2, output2_size,
2466 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002467 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002468 PSA_ASSERT( psa_cipher_finish( &operation2,
2469 output2 + output2_length,
2470 output2_size,
2471 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002472
Gilles Peskine048b7f02018-06-08 14:20:49 +02002473 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002474
Gilles Peskine8817f612018-12-18 00:18:46 +01002475 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002476
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002477 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002478
2479exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002480 mbedtls_free( output1 );
2481 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002482 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002483 mbedtls_psa_crypto_free( );
2484}
2485/* END_CASE */
2486
2487/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002488void cipher_verify_output_multipart( int alg_arg,
2489 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002490 data_t *key,
2491 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002492 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002493{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002494 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002495 psa_key_type_t key_type = key_type_arg;
2496 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002497 unsigned char iv[16] = {0};
2498 size_t iv_size = 16;
2499 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002500 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002501 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002502 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002503 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002504 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002505 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002506 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002507 psa_cipher_operation_t operation1;
2508 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002509 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002510
Moran Pekerded84402018-06-06 16:36:50 +03002511 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002512 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002513 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2514 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002515
Gilles Peskine8817f612018-12-18 00:18:46 +01002516 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002517
Gilles Peskine8817f612018-12-18 00:18:46 +01002518 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2519 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002520 psa_key_policy_init( &policy );
2521 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002522 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002523
Gilles Peskine8817f612018-12-18 00:18:46 +01002524 PSA_ASSERT( psa_import_key( handle, key_type,
2525 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002526
Gilles Peskine8817f612018-12-18 00:18:46 +01002527 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2528 handle, alg ) );
2529 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2530 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002531
Gilles Peskine8817f612018-12-18 00:18:46 +01002532 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2533 iv, iv_size,
2534 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002535 output1_buffer_size = ( (size_t) input->len +
2536 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002537 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002538
Gilles Peskine4abf7412018-06-18 16:35:34 +02002539 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002540
Gilles Peskine8817f612018-12-18 00:18:46 +01002541 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2542 output1, output1_buffer_size,
2543 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002544 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002545
Gilles Peskine8817f612018-12-18 00:18:46 +01002546 PSA_ASSERT( psa_cipher_update( &operation1,
2547 input->x + first_part_size,
2548 input->len - first_part_size,
2549 output1, output1_buffer_size,
2550 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002551 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002552
Gilles Peskine8817f612018-12-18 00:18:46 +01002553 PSA_ASSERT( psa_cipher_finish( &operation1,
2554 output1 + output1_length,
2555 output1_buffer_size - output1_length,
2556 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002557 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002558
Gilles Peskine8817f612018-12-18 00:18:46 +01002559 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002560
Gilles Peskine048b7f02018-06-08 14:20:49 +02002561 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002562 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002563
Gilles Peskine8817f612018-12-18 00:18:46 +01002564 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2565 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002566
Gilles Peskine8817f612018-12-18 00:18:46 +01002567 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2568 output2, output2_buffer_size,
2569 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002570 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002571
Gilles Peskine8817f612018-12-18 00:18:46 +01002572 PSA_ASSERT( psa_cipher_update( &operation2,
2573 output1 + first_part_size,
2574 output1_length - first_part_size,
2575 output2, output2_buffer_size,
2576 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002577 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002578
Gilles Peskine8817f612018-12-18 00:18:46 +01002579 PSA_ASSERT( psa_cipher_finish( &operation2,
2580 output2 + output2_length,
2581 output2_buffer_size - output2_length,
2582 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002583 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002584
Gilles Peskine8817f612018-12-18 00:18:46 +01002585 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002586
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002587 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002588
2589exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002590 mbedtls_free( output1 );
2591 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002592 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002593 mbedtls_psa_crypto_free( );
2594}
2595/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002596
Gilles Peskine20035e32018-02-03 22:44:14 +01002597/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002598void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002599 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002600 data_t *nonce,
2601 data_t *additional_data,
2602 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002603 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002604{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002605 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002606 psa_key_type_t key_type = key_type_arg;
2607 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002608 unsigned char *output_data = NULL;
2609 size_t output_size = 0;
2610 size_t output_length = 0;
2611 unsigned char *output_data2 = NULL;
2612 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002613 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002614 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002615 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002616
Gilles Peskinea1cac842018-06-11 19:33:02 +02002617 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002618 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002619 TEST_ASSERT( nonce != NULL );
2620 TEST_ASSERT( additional_data != NULL );
2621 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2622 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2623 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2624 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2625
Gilles Peskine4abf7412018-06-18 16:35:34 +02002626 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002627 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002628
Gilles Peskine8817f612018-12-18 00:18:46 +01002629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002630
Gilles Peskine8817f612018-12-18 00:18:46 +01002631 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2632 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002633 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002634 psa_key_policy_set_usage( &policy,
2635 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2636 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002637 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002638
Gilles Peskine8817f612018-12-18 00:18:46 +01002639 PSA_ASSERT( psa_import_key( handle, key_type,
2640 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002641
Gilles Peskinefe11b722018-12-18 00:24:04 +01002642 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2643 nonce->x, nonce->len,
2644 additional_data->x,
2645 additional_data->len,
2646 input_data->x, input_data->len,
2647 output_data, output_size,
2648 &output_length ), expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002649
2650 if( PSA_SUCCESS == expected_result )
2651 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002652 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002653
Gilles Peskinefe11b722018-12-18 00:24:04 +01002654 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2655 nonce->x, nonce->len,
2656 additional_data->x,
2657 additional_data->len,
2658 output_data, output_length,
2659 output_data2, output_length,
2660 &output_length2 ), expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002661
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002662 ASSERT_COMPARE( input_data->x, input_data->len,
2663 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002664 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002665
Gilles Peskinea1cac842018-06-11 19:33:02 +02002666exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002667 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002668 mbedtls_free( output_data );
2669 mbedtls_free( output_data2 );
2670 mbedtls_psa_crypto_free( );
2671}
2672/* END_CASE */
2673
2674/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002675void aead_encrypt( int key_type_arg, data_t *key_data,
2676 int alg_arg,
2677 data_t *nonce,
2678 data_t *additional_data,
2679 data_t *input_data,
2680 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002681{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002682 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002683 psa_key_type_t key_type = key_type_arg;
2684 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002685 unsigned char *output_data = NULL;
2686 size_t output_size = 0;
2687 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002688 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002689 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002690
Gilles Peskinea1cac842018-06-11 19:33:02 +02002691 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002692 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002693 TEST_ASSERT( additional_data != NULL );
2694 TEST_ASSERT( nonce != NULL );
2695 TEST_ASSERT( expected_result != NULL );
2696 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2697 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2698 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2699 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2700 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2701
Gilles Peskine4abf7412018-06-18 16:35:34 +02002702 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002703 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002704
Gilles Peskine8817f612018-12-18 00:18:46 +01002705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002706
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2708 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002709 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002710 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002712
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 PSA_ASSERT( psa_import_key( handle, key_type,
2714 key_data->x,
2715 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2718 nonce->x, nonce->len,
2719 additional_data->x, additional_data->len,
2720 input_data->x, input_data->len,
2721 output_data, output_size,
2722 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002723
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002724 ASSERT_COMPARE( expected_result->x, expected_result->len,
2725 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002726
Gilles Peskinea1cac842018-06-11 19:33:02 +02002727exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002728 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002729 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002730 mbedtls_psa_crypto_free( );
2731}
2732/* END_CASE */
2733
2734/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002735void aead_decrypt( int key_type_arg, data_t *key_data,
2736 int alg_arg,
2737 data_t *nonce,
2738 data_t *additional_data,
2739 data_t *input_data,
2740 data_t *expected_data,
2741 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002743 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002744 psa_key_type_t key_type = key_type_arg;
2745 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002746 unsigned char *output_data = NULL;
2747 size_t output_size = 0;
2748 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002749 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002750 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002751 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002752
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002754 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002755 TEST_ASSERT( additional_data != NULL );
2756 TEST_ASSERT( nonce != NULL );
2757 TEST_ASSERT( expected_data != NULL );
2758 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2759 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2760 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2761 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2762 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2763
Gilles Peskine4abf7412018-06-18 16:35:34 +02002764 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002765 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002766
Gilles Peskine8817f612018-12-18 00:18:46 +01002767 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002768
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2770 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002772 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002773 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002774
Gilles Peskine8817f612018-12-18 00:18:46 +01002775 PSA_ASSERT( psa_import_key( handle, key_type,
2776 key_data->x,
2777 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002778
Gilles Peskinefe11b722018-12-18 00:24:04 +01002779 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2780 nonce->x, nonce->len,
2781 additional_data->x,
2782 additional_data->len,
2783 input_data->x, input_data->len,
2784 output_data, output_size,
2785 &output_length ), expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002786
Gilles Peskine2d277862018-06-18 15:41:12 +02002787 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002788 ASSERT_COMPARE( expected_data->x, expected_data->len,
2789 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002790
Gilles Peskinea1cac842018-06-11 19:33:02 +02002791exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002792 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002793 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002794 mbedtls_psa_crypto_free( );
2795}
2796/* END_CASE */
2797
2798/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002799void signature_size( int type_arg,
2800 int bits,
2801 int alg_arg,
2802 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002803{
2804 psa_key_type_t type = type_arg;
2805 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002806 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002807 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002808exit:
2809 ;
2810}
2811/* END_CASE */
2812
2813/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814void sign_deterministic( int key_type_arg, data_t *key_data,
2815 int alg_arg, data_t *input_data,
2816 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002817{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002818 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002819 psa_key_type_t key_type = key_type_arg;
2820 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002821 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002822 unsigned char *signature = NULL;
2823 size_t signature_size;
2824 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002825 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002826
Gilles Peskine20035e32018-02-03 22:44:14 +01002827 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002828 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002829 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002830 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2831 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2832 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002833
Gilles Peskine8817f612018-12-18 00:18:46 +01002834 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_allocate_key( key_type,
2837 KEY_BITS_FROM_DATA( key_type, key_data ),
2838 &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002839 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002840 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002841 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002842
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_import_key( handle, key_type,
2844 key_data->x,
2845 key_data->len ) );
2846 PSA_ASSERT( psa_get_key_information( handle,
2847 NULL,
2848 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002849
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002850 /* Allocate a buffer which has the size advertized by the
2851 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002852 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2853 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002854 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002855 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002856 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002857
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002858 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002859 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2860 input_data->x, input_data->len,
2861 signature, signature_size,
2862 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002863 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002864 ASSERT_COMPARE( output_data->x, output_data->len,
2865 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002866
2867exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002868 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002869 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002870 mbedtls_psa_crypto_free( );
2871}
2872/* END_CASE */
2873
2874/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002875void sign_fail( int key_type_arg, data_t *key_data,
2876 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002877 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002878{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002879 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002880 psa_key_type_t key_type = key_type_arg;
2881 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002882 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002883 psa_status_t actual_status;
2884 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002885 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002886 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002887 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002888
Gilles Peskine20035e32018-02-03 22:44:14 +01002889 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002890 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002891 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2892 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2893
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002894 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002895
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002897
Gilles Peskine8817f612018-12-18 00:18:46 +01002898 PSA_ASSERT( psa_allocate_key( key_type,
2899 KEY_BITS_FROM_DATA( key_type, key_data ),
2900 &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002901 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002902 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002903 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002904
Gilles Peskine8817f612018-12-18 00:18:46 +01002905 PSA_ASSERT( psa_import_key( handle, key_type,
2906 key_data->x,
2907 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002908
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002909 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002910 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002911 signature, signature_size,
2912 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002913 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002914 /* The value of *signature_length is unspecified on error, but
2915 * whatever it is, it should be less than signature_size, so that
2916 * if the caller tries to read *signature_length bytes without
2917 * checking the error code then they don't overflow a buffer. */
2918 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002919
2920exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002921 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002922 mbedtls_free( signature );
2923 mbedtls_psa_crypto_free( );
2924}
2925/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002926
2927/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002928void sign_verify( int key_type_arg, data_t *key_data,
2929 int alg_arg, data_t *input_data )
2930{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002931 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002932 psa_key_type_t key_type = key_type_arg;
2933 psa_algorithm_t alg = alg_arg;
2934 size_t key_bits;
2935 unsigned char *signature = NULL;
2936 size_t signature_size;
2937 size_t signature_length = 0xdeadbeef;
2938 psa_key_policy_t policy;
2939
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002941
Gilles Peskine8817f612018-12-18 00:18:46 +01002942 PSA_ASSERT( psa_allocate_key( key_type,
2943 KEY_BITS_FROM_DATA( key_type, key_data ),
2944 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002945 psa_key_policy_init( &policy );
2946 psa_key_policy_set_usage( &policy,
2947 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2948 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002950
Gilles Peskine8817f612018-12-18 00:18:46 +01002951 PSA_ASSERT( psa_import_key( handle, key_type,
2952 key_data->x,
2953 key_data->len ) );
2954 PSA_ASSERT( psa_get_key_information( handle,
2955 NULL,
2956 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002957
2958 /* Allocate a buffer which has the size advertized by the
2959 * library. */
2960 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2961 key_bits, alg );
2962 TEST_ASSERT( signature_size != 0 );
2963 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002964 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002965
2966 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002967 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2968 input_data->x, input_data->len,
2969 signature, signature_size,
2970 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002971 /* Check that the signature length looks sensible. */
2972 TEST_ASSERT( signature_length <= signature_size );
2973 TEST_ASSERT( signature_length > 0 );
2974
2975 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002976 PSA_ASSERT( psa_asymmetric_verify(
2977 handle, alg,
2978 input_data->x, input_data->len,
2979 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002980
2981 if( input_data->len != 0 )
2982 {
2983 /* Flip a bit in the input and verify that the signature is now
2984 * detected as invalid. Flip a bit at the beginning, not at the end,
2985 * because ECDSA may ignore the last few bits of the input. */
2986 input_data->x[0] ^= 1;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002987 TEST_EQUAL( psa_asymmetric_verify(
2988 handle, alg,
2989 input_data->x, input_data->len,
2990 signature,
2991 signature_length ), PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002992 }
2993
2994exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002995 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002996 mbedtls_free( signature );
2997 mbedtls_psa_crypto_free( );
2998}
2999/* END_CASE */
3000
3001/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003002void asymmetric_verify( int key_type_arg, data_t *key_data,
3003 int alg_arg, data_t *hash_data,
3004 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003005{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003006 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003007 psa_key_type_t key_type = key_type_arg;
3008 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003009 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03003010
Gilles Peskine69c12672018-06-28 00:07:19 +02003011 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3012
itayzafrir5c753392018-05-08 11:18:38 +03003013 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003014 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003015 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003016 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3017 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3018 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003019
Gilles Peskine8817f612018-12-18 00:18:46 +01003020 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_allocate_key( key_type,
3023 KEY_BITS_FROM_DATA( key_type, key_data ),
3024 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003025 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003026 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003028
Gilles Peskine8817f612018-12-18 00:18:46 +01003029 PSA_ASSERT( psa_import_key( handle, key_type,
3030 key_data->x,
3031 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003032
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3034 hash_data->x, hash_data->len,
3035 signature_data->x,
3036 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003037exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003038 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003039 mbedtls_psa_crypto_free( );
3040}
3041/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042
3043/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003044void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3045 int alg_arg, data_t *hash_data,
3046 data_t *signature_data,
3047 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003049 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050 psa_key_type_t key_type = key_type_arg;
3051 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003052 psa_status_t actual_status;
3053 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003054 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003055
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003056 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003057 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003059 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3060 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3061 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003064
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 PSA_ASSERT( psa_allocate_key( key_type,
3066 KEY_BITS_FROM_DATA( key_type, key_data ),
3067 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003068 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003069 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003071
Gilles Peskine8817f612018-12-18 00:18:46 +01003072 PSA_ASSERT( psa_import_key( handle, key_type,
3073 key_data->x,
3074 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003075
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003076 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003077 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003078 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003079 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003080
Gilles Peskinefe11b722018-12-18 00:24:04 +01003081 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003082
3083exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003084 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085 mbedtls_psa_crypto_free( );
3086}
3087/* END_CASE */
3088
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003089/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003090void asymmetric_encrypt( int key_type_arg,
3091 data_t *key_data,
3092 int alg_arg,
3093 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003094 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003095 int expected_output_length_arg,
3096 int expected_status_arg )
3097{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003098 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003099 psa_key_type_t key_type = key_type_arg;
3100 psa_algorithm_t alg = alg_arg;
3101 size_t expected_output_length = expected_output_length_arg;
3102 size_t key_bits;
3103 unsigned char *output = NULL;
3104 size_t output_size;
3105 size_t output_length = ~0;
3106 psa_status_t actual_status;
3107 psa_status_t expected_status = expected_status_arg;
3108 psa_key_policy_t policy;
3109
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003111
Gilles Peskine656896e2018-06-29 19:12:28 +02003112 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003113 PSA_ASSERT( psa_allocate_key( key_type,
3114 KEY_BITS_FROM_DATA( key_type, key_data ),
3115 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003116 psa_key_policy_init( &policy );
3117 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003118 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3119 PSA_ASSERT( psa_import_key( handle, key_type,
3120 key_data->x,
3121 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003122
3123 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_get_key_information( handle,
3125 NULL,
3126 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003127 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003128 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003129
3130 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003131 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003132 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003133 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003134 output, output_size,
3135 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003136 TEST_EQUAL( actual_status, expected_status );
3137 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003138
Gilles Peskine68428122018-06-30 18:42:41 +02003139 /* If the label is empty, the test framework puts a non-null pointer
3140 * in label->x. Test that a null pointer works as well. */
3141 if( label->len == 0 )
3142 {
3143 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003144 if( output_size != 0 )
3145 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003146 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003147 input_data->x, input_data->len,
3148 NULL, label->len,
3149 output, output_size,
3150 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003151 TEST_EQUAL( actual_status, expected_status );
3152 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003153 }
3154
Gilles Peskine656896e2018-06-29 19:12:28 +02003155exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003156 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003157 mbedtls_free( output );
3158 mbedtls_psa_crypto_free( );
3159}
3160/* END_CASE */
3161
3162/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003163void asymmetric_encrypt_decrypt( int key_type_arg,
3164 data_t *key_data,
3165 int alg_arg,
3166 data_t *input_data,
3167 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003169 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170 psa_key_type_t key_type = key_type_arg;
3171 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003172 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003174 size_t output_size;
3175 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003176 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003177 size_t output2_size;
3178 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003179 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003183 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3184 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_allocate_key( key_type,
3189 KEY_BITS_FROM_DATA( key_type, key_data ),
3190 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003191 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003192 psa_key_policy_set_usage( &policy,
3193 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003194 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003195 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_import_key( handle, key_type,
3198 key_data->x,
3199 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003200
3201 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_get_key_information( handle,
3203 NULL,
3204 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003205 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003206 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003207 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003208 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003209
Gilles Peskineeebd7382018-06-08 18:11:54 +02003210 /* We test encryption by checking that encrypt-then-decrypt gives back
3211 * the original plaintext because of the non-optional random
3212 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3214 input_data->x, input_data->len,
3215 label->x, label->len,
3216 output, output_size,
3217 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003218 /* We don't know what ciphertext length to expect, but check that
3219 * it looks sensible. */
3220 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003221
Gilles Peskine8817f612018-12-18 00:18:46 +01003222 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3223 output, output_length,
3224 label->x, label->len,
3225 output2, output2_size,
3226 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003227 ASSERT_COMPARE( input_data->x, input_data->len,
3228 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229
3230exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003231 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003232 mbedtls_free( output );
3233 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235}
3236/* END_CASE */
3237
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003239void asymmetric_decrypt( int key_type_arg,
3240 data_t *key_data,
3241 int alg_arg,
3242 data_t *input_data,
3243 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003244 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003246 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247 psa_key_type_t key_type = key_type_arg;
3248 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003250 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003251 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003252 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003254 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003256 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003257 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3258 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3259 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3260
Gilles Peskine4abf7412018-06-18 16:35:34 +02003261 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003262 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265
Gilles Peskine8817f612018-12-18 00:18:46 +01003266 PSA_ASSERT( psa_allocate_key( key_type,
3267 KEY_BITS_FROM_DATA( key_type, key_data ),
3268 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003269 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003270 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003272
Gilles Peskine8817f612018-12-18 00:18:46 +01003273 PSA_ASSERT( psa_import_key( handle, key_type,
3274 key_data->x,
3275 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276
Gilles Peskine8817f612018-12-18 00:18:46 +01003277 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3278 input_data->x, input_data->len,
3279 label->x, label->len,
3280 output,
3281 output_size,
3282 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003283 ASSERT_COMPARE( expected_data->x, expected_data->len,
3284 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285
Gilles Peskine68428122018-06-30 18:42:41 +02003286 /* If the label is empty, the test framework puts a non-null pointer
3287 * in label->x. Test that a null pointer works as well. */
3288 if( label->len == 0 )
3289 {
3290 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003291 if( output_size != 0 )
3292 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3294 input_data->x, input_data->len,
3295 NULL, label->len,
3296 output,
3297 output_size,
3298 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003299 ASSERT_COMPARE( expected_data->x, expected_data->len,
3300 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003301 }
3302
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003303exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003304 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003305 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003307}
3308/* END_CASE */
3309
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003310/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003311void asymmetric_decrypt_fail( int key_type_arg,
3312 data_t *key_data,
3313 int alg_arg,
3314 data_t *input_data,
3315 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003316 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003318 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319 psa_key_type_t key_type = key_type_arg;
3320 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003321 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003322 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003323 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003324 psa_status_t actual_status;
3325 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003326 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003328 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003330 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3331 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3332
Gilles Peskine4abf7412018-06-18 16:35:34 +02003333 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003334 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003335
Gilles Peskine8817f612018-12-18 00:18:46 +01003336 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337
Gilles Peskine8817f612018-12-18 00:18:46 +01003338 PSA_ASSERT( psa_allocate_key( key_type,
3339 KEY_BITS_FROM_DATA( key_type, key_data ),
3340 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003341 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003342 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003343 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_import_key( handle, key_type,
3346 key_data->x,
3347 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003348
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003349 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003350 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003351 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003352 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003353 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003354 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003355 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003356
Gilles Peskine68428122018-06-30 18:42:41 +02003357 /* If the label is empty, the test framework puts a non-null pointer
3358 * in label->x. Test that a null pointer works as well. */
3359 if( label->len == 0 )
3360 {
3361 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003362 if( output_size != 0 )
3363 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003364 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003365 input_data->x, input_data->len,
3366 NULL, label->len,
3367 output, output_size,
3368 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003369 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003370 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003371 }
3372
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003373exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003374 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003375 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003376 mbedtls_psa_crypto_free( );
3377}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003378/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003379
3380/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003381void derive_setup( int key_type_arg,
3382 data_t *key_data,
3383 int alg_arg,
3384 data_t *salt,
3385 data_t *label,
3386 int requested_capacity_arg,
3387 int expected_status_arg )
3388{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003389 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003390 size_t key_type = key_type_arg;
3391 psa_algorithm_t alg = alg_arg;
3392 size_t requested_capacity = requested_capacity_arg;
3393 psa_status_t expected_status = expected_status_arg;
3394 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3395 psa_key_policy_t policy;
3396
Gilles Peskine8817f612018-12-18 00:18:46 +01003397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003398
Gilles Peskine8817f612018-12-18 00:18:46 +01003399 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3400 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003401 psa_key_policy_init( &policy );
3402 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003403 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003404
Gilles Peskine8817f612018-12-18 00:18:46 +01003405 PSA_ASSERT( psa_import_key( handle, key_type,
3406 key_data->x,
3407 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003408
Gilles Peskinefe11b722018-12-18 00:24:04 +01003409 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3410 salt->x, salt->len,
3411 label->x, label->len,
3412 requested_capacity ), expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003413
3414exit:
3415 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003416 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003417 mbedtls_psa_crypto_free( );
3418}
3419/* END_CASE */
3420
3421/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003422void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003423{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003424 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003425 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003426 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003427 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003428 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003429 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003430 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3431 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3432 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003433 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003434
Gilles Peskine8817f612018-12-18 00:18:46 +01003435 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003436
Gilles Peskine8817f612018-12-18 00:18:46 +01003437 PSA_ASSERT( psa_allocate_key( key_type,
3438 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3439 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003440 psa_key_policy_init( &policy );
3441 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003442 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003443
Gilles Peskine8817f612018-12-18 00:18:46 +01003444 PSA_ASSERT( psa_import_key( handle, key_type,
3445 key_data,
3446 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003447
3448 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003449 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3450 NULL, 0,
3451 NULL, 0,
3452 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003453
3454 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003455 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3456 NULL, 0,
3457 NULL, 0,
3458 capacity ), PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003459
Gilles Peskine8817f612018-12-18 00:18:46 +01003460 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity )
3461 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003462
Gilles Peskinefe11b722018-12-18 00:24:04 +01003463 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity )
3464 , PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003465
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003466exit:
3467 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003468 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003469 mbedtls_psa_crypto_free( );
3470}
3471/* END_CASE */
3472
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003473/* BEGIN_CASE */
3474void test_derive_invalid_generator_tests( )
3475{
3476 uint8_t output_buffer[16];
3477 size_t buffer_size = 16;
3478 size_t capacity = 0;
3479 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3480
Nir Sonnenschein50789302018-10-31 12:16:38 +02003481 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003482 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003483
3484 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003485 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003486
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003488
Nir Sonnenschein50789302018-10-31 12:16:38 +02003489 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003490 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003491
Nir Sonnenschein50789302018-10-31 12:16:38 +02003492 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003493 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003494
3495exit:
3496 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003497}
3498/* END_CASE */
3499
3500/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003501void derive_output( int alg_arg,
3502 data_t *key_data,
3503 data_t *salt,
3504 data_t *label,
3505 int requested_capacity_arg,
3506 data_t *expected_output1,
3507 data_t *expected_output2 )
3508{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003509 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003510 psa_algorithm_t alg = alg_arg;
3511 size_t requested_capacity = requested_capacity_arg;
3512 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3513 uint8_t *expected_outputs[2] =
3514 {expected_output1->x, expected_output2->x};
3515 size_t output_sizes[2] =
3516 {expected_output1->len, expected_output2->len};
3517 size_t output_buffer_size = 0;
3518 uint8_t *output_buffer = NULL;
3519 size_t expected_capacity;
3520 size_t current_capacity;
3521 psa_key_policy_t policy;
3522 psa_status_t status;
3523 unsigned i;
3524
3525 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3526 {
3527 if( output_sizes[i] > output_buffer_size )
3528 output_buffer_size = output_sizes[i];
3529 if( output_sizes[i] == 0 )
3530 expected_outputs[i] = NULL;
3531 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003532 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003534
Gilles Peskine8817f612018-12-18 00:18:46 +01003535 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3536 PSA_BYTES_TO_BITS( key_data->len ),
3537 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003538 psa_key_policy_init( &policy );
3539 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003541
Gilles Peskine8817f612018-12-18 00:18:46 +01003542 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3543 key_data->x,
3544 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003545
3546 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003547 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3548 salt->x, salt->len,
3549 label->x, label->len,
3550 requested_capacity ) );
3551 PSA_ASSERT( psa_get_generator_capacity( &generator,
3552 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003553 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003554 expected_capacity = requested_capacity;
3555
3556 /* Expansion phase. */
3557 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3558 {
3559 /* Read some bytes. */
3560 status = psa_generator_read( &generator,
3561 output_buffer, output_sizes[i] );
3562 if( expected_capacity == 0 && output_sizes[i] == 0 )
3563 {
3564 /* Reading 0 bytes when 0 bytes are available can go either way. */
3565 TEST_ASSERT( status == PSA_SUCCESS ||
3566 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3567 continue;
3568 }
3569 else if( expected_capacity == 0 ||
3570 output_sizes[i] > expected_capacity )
3571 {
3572 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003573 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003574 expected_capacity = 0;
3575 continue;
3576 }
3577 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003579 if( output_sizes[i] != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01003580 TEST_EQUAL( memcmp( output_buffer, expected_outputs[i],
3581 output_sizes[i] ), 0 );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003582 /* Check the generator status. */
3583 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003584 PSA_ASSERT( psa_get_generator_capacity( &generator,
3585 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003586 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003587 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003589
3590exit:
3591 mbedtls_free( output_buffer );
3592 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003593 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003594 mbedtls_psa_crypto_free( );
3595}
3596/* END_CASE */
3597
3598/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003599void derive_full( int alg_arg,
3600 data_t *key_data,
3601 data_t *salt,
3602 data_t *label,
3603 int requested_capacity_arg )
3604{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003605 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003606 psa_algorithm_t alg = alg_arg;
3607 size_t requested_capacity = requested_capacity_arg;
3608 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3609 unsigned char output_buffer[16];
3610 size_t expected_capacity = requested_capacity;
3611 size_t current_capacity;
3612 psa_key_policy_t policy;
3613
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003615
Gilles Peskine8817f612018-12-18 00:18:46 +01003616 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3617 PSA_BYTES_TO_BITS( key_data->len ),
3618 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003619 psa_key_policy_init( &policy );
3620 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003621 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003622
Gilles Peskine8817f612018-12-18 00:18:46 +01003623 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3624 key_data->x,
3625 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003626
3627 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003628 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3629 salt->x, salt->len,
3630 label->x, label->len,
3631 requested_capacity ) );
3632 PSA_ASSERT( psa_get_generator_capacity( &generator,
3633 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003634 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003635
3636 /* Expansion phase. */
3637 while( current_capacity > 0 )
3638 {
3639 size_t read_size = sizeof( output_buffer );
3640 if( read_size > current_capacity )
3641 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003642 PSA_ASSERT( psa_generator_read( &generator,
3643 output_buffer,
3644 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003645 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003646 PSA_ASSERT( psa_get_generator_capacity( &generator,
3647 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003648 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003649 }
3650
3651 /* Check that the generator refuses to go over capacity. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003652 TEST_EQUAL( psa_generator_read( &generator,
3653 output_buffer,
3654 1 ), PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003655
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003657
3658exit:
3659 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003660 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003661 mbedtls_psa_crypto_free( );
3662}
3663/* END_CASE */
3664
3665/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003666void derive_key_exercise( int alg_arg,
3667 data_t *key_data,
3668 data_t *salt,
3669 data_t *label,
3670 int derived_type_arg,
3671 int derived_bits_arg,
3672 int derived_usage_arg,
3673 int derived_alg_arg )
3674{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003675 psa_key_handle_t base_handle = 0;
3676 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003677 psa_algorithm_t alg = alg_arg;
3678 psa_key_type_t derived_type = derived_type_arg;
3679 size_t derived_bits = derived_bits_arg;
3680 psa_key_usage_t derived_usage = derived_usage_arg;
3681 psa_algorithm_t derived_alg = derived_alg_arg;
3682 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3683 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3684 psa_key_policy_t policy;
3685 psa_key_type_t got_type;
3686 size_t got_bits;
3687
Gilles Peskine8817f612018-12-18 00:18:46 +01003688 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003689
Gilles Peskine8817f612018-12-18 00:18:46 +01003690 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3691 PSA_BYTES_TO_BITS( key_data->len ),
3692 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003693 psa_key_policy_init( &policy );
3694 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003695 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3696 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3697 key_data->x,
3698 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003699
3700 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003701 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3702 salt->x, salt->len,
3703 label->x, label->len,
3704 capacity ) );
3705 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3706 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003707 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003708 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3709 PSA_ASSERT( psa_generator_import_key( derived_handle,
3710 derived_type,
3711 derived_bits,
3712 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003713
3714 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003715 PSA_ASSERT( psa_get_key_information( derived_handle,
3716 &got_type,
3717 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003718 TEST_EQUAL( got_type, derived_type );
3719 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003720
3721 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003722 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003723 goto exit;
3724
3725exit:
3726 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003727 psa_destroy_key( base_handle );
3728 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003729 mbedtls_psa_crypto_free( );
3730}
3731/* END_CASE */
3732
3733/* BEGIN_CASE */
3734void derive_key_export( int alg_arg,
3735 data_t *key_data,
3736 data_t *salt,
3737 data_t *label,
3738 int bytes1_arg,
3739 int bytes2_arg )
3740{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003741 psa_key_handle_t base_handle = 0;
3742 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003743 psa_algorithm_t alg = alg_arg;
3744 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003745 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003746 size_t bytes2 = bytes2_arg;
3747 size_t capacity = bytes1 + bytes2;
3748 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003749 uint8_t *output_buffer = NULL;
3750 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003751 psa_key_policy_t policy;
3752 size_t length;
3753
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003754 ASSERT_ALLOC( output_buffer, capacity );
3755 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003756 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003757
Gilles Peskine8817f612018-12-18 00:18:46 +01003758 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3759 PSA_BYTES_TO_BITS( key_data->len ),
3760 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003761 psa_key_policy_init( &policy );
3762 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003763 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3764 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3765 key_data->x,
3766 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003767
3768 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003769 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3770 salt->x, salt->len,
3771 label->x, label->len,
3772 capacity ) );
3773 PSA_ASSERT( psa_generator_read( &generator,
3774 output_buffer,
3775 capacity ) );
3776 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003777
3778 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003779 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3780 salt->x, salt->len,
3781 label->x, label->len,
3782 capacity ) );
3783 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3784 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003785 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003786 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3787 PSA_ASSERT( psa_generator_import_key( derived_handle,
3788 PSA_KEY_TYPE_RAW_DATA,
3789 derived_bits,
3790 &generator ) );
3791 PSA_ASSERT( psa_export_key( derived_handle,
3792 export_buffer, bytes1,
3793 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003794 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003795 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3796 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3797 PSA_BYTES_TO_BITS( bytes2 ),
3798 &derived_handle ) );
3799 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3800 PSA_ASSERT( psa_generator_import_key( derived_handle,
3801 PSA_KEY_TYPE_RAW_DATA,
3802 PSA_BYTES_TO_BITS( bytes2 ),
3803 &generator ) );
3804 PSA_ASSERT( psa_export_key( derived_handle,
3805 export_buffer + bytes1, bytes2,
3806 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003807 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003808
3809 /* Compare the outputs from the two runs. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003810 TEST_EQUAL( memcmp( output_buffer, export_buffer, capacity ), 0 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003811
3812exit:
3813 mbedtls_free( output_buffer );
3814 mbedtls_free( export_buffer );
3815 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003816 psa_destroy_key( base_handle );
3817 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003818 mbedtls_psa_crypto_free( );
3819}
3820/* END_CASE */
3821
3822/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003823void key_agreement_setup( int alg_arg,
3824 int our_key_type_arg, data_t *our_key_data,
3825 data_t *peer_key_data,
3826 int expected_status_arg )
3827{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003828 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003829 psa_algorithm_t alg = alg_arg;
3830 psa_key_type_t our_key_type = our_key_type_arg;
3831 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3832 psa_key_policy_t policy;
3833
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003835
Gilles Peskine8817f612018-12-18 00:18:46 +01003836 PSA_ASSERT( psa_allocate_key( our_key_type,
3837 KEY_BITS_FROM_DATA( our_key_type,
3838 our_key_data ),
3839 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003840 psa_key_policy_init( &policy );
3841 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003842 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3843 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3844 our_key_data->x,
3845 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003846
Gilles Peskinefe11b722018-12-18 00:24:04 +01003847 TEST_EQUAL( psa_key_agreement( &generator,
3848 our_key,
3849 peer_key_data->x, peer_key_data->len,
3850 alg ), expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003851
3852exit:
3853 psa_generator_abort( &generator );
3854 psa_destroy_key( our_key );
3855 mbedtls_psa_crypto_free( );
3856}
3857/* END_CASE */
3858
3859/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003860void key_agreement_capacity( int alg_arg,
3861 int our_key_type_arg, data_t *our_key_data,
3862 data_t *peer_key_data,
3863 int expected_capacity_arg )
3864{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003865 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003866 psa_algorithm_t alg = alg_arg;
3867 psa_key_type_t our_key_type = our_key_type_arg;
3868 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3869 psa_key_policy_t policy;
3870 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003871 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003872
Gilles Peskine8817f612018-12-18 00:18:46 +01003873 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003874
Gilles Peskine8817f612018-12-18 00:18:46 +01003875 PSA_ASSERT( psa_allocate_key( our_key_type,
3876 KEY_BITS_FROM_DATA( our_key_type,
3877 our_key_data ),
3878 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003879 psa_key_policy_init( &policy );
3880 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003881 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3882 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3883 our_key_data->x,
3884 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003885
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_key_agreement( &generator,
3887 our_key,
3888 peer_key_data->x, peer_key_data->len,
3889 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003890
Gilles Peskinebf491972018-10-25 22:36:12 +02003891 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003892 PSA_ASSERT( psa_get_generator_capacity(
3893 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003894 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003895
Gilles Peskinebf491972018-10-25 22:36:12 +02003896 /* Test the actual capacity by reading the output. */
3897 while( actual_capacity > sizeof( output ) )
3898 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003899 PSA_ASSERT( psa_generator_read( &generator,
3900 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003901 actual_capacity -= sizeof( output );
3902 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003903 PSA_ASSERT( psa_generator_read( &generator,
3904 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003905 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3906 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003907
Gilles Peskine59685592018-09-18 12:11:34 +02003908exit:
3909 psa_generator_abort( &generator );
3910 psa_destroy_key( our_key );
3911 mbedtls_psa_crypto_free( );
3912}
3913/* END_CASE */
3914
3915/* BEGIN_CASE */
3916void key_agreement_output( int alg_arg,
3917 int our_key_type_arg, data_t *our_key_data,
3918 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003919 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003920{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003921 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003922 psa_algorithm_t alg = alg_arg;
3923 psa_key_type_t our_key_type = our_key_type_arg;
3924 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3925 psa_key_policy_t policy;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003926 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003927
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003928 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3929 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003930
Gilles Peskine8817f612018-12-18 00:18:46 +01003931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003932
Gilles Peskine8817f612018-12-18 00:18:46 +01003933 PSA_ASSERT( psa_allocate_key( our_key_type,
3934 KEY_BITS_FROM_DATA( our_key_type,
3935 our_key_data ),
3936 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003937 psa_key_policy_init( &policy );
3938 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003939 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3940 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3941 our_key_data->x,
3942 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003943
Gilles Peskine8817f612018-12-18 00:18:46 +01003944 PSA_ASSERT( psa_key_agreement( &generator,
3945 our_key,
3946 peer_key_data->x, peer_key_data->len,
3947 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003948
Gilles Peskine8817f612018-12-18 00:18:46 +01003949 PSA_ASSERT(
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003950 psa_generator_read( &generator,
3951 actual_output,
Gilles Peskine8817f612018-12-18 00:18:46 +01003952 expected_output1->len ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003953 TEST_EQUAL( memcmp( actual_output, expected_output1->x,
3954 expected_output1->len ), 0 );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003955 if( expected_output2->len != 0 )
3956 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003957 PSA_ASSERT(
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003958 psa_generator_read( &generator,
3959 actual_output,
Gilles Peskine8817f612018-12-18 00:18:46 +01003960 expected_output2->len ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003961 TEST_EQUAL( memcmp( actual_output, expected_output2->x,
3962 expected_output2->len ), 0 );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003963 }
Gilles Peskine59685592018-09-18 12:11:34 +02003964
3965exit:
3966 psa_generator_abort( &generator );
3967 psa_destroy_key( our_key );
3968 mbedtls_psa_crypto_free( );
3969 mbedtls_free( actual_output );
3970}
3971/* END_CASE */
3972
3973/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003974void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003975{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003976 size_t bytes = bytes_arg;
3977 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003978 unsigned char *output = NULL;
3979 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003980 size_t i;
3981 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003982
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003983 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3984 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003985 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003986
Gilles Peskine8817f612018-12-18 00:18:46 +01003987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003988
Gilles Peskinea50d7392018-06-21 10:22:13 +02003989 /* Run several times, to ensure that every output byte will be
3990 * nonzero at least once with overwhelming probability
3991 * (2^(-8*number_of_runs)). */
3992 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003993 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003994 if( bytes != 0 )
3995 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003996 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003997
3998 /* Check that no more than bytes have been overwritten */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003999 TEST_EQUAL( memcmp( output + bytes, trail, sizeof( trail ) ), 0 );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004000
4001 for( i = 0; i < bytes; i++ )
4002 {
4003 if( output[i] != 0 )
4004 ++changed[i];
4005 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004006 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004007
4008 /* Check that every byte was changed to nonzero at least once. This
4009 * validates that psa_generate_random is overwriting every byte of
4010 * the output buffer. */
4011 for( i = 0; i < bytes; i++ )
4012 {
4013 TEST_ASSERT( changed[i] != 0 );
4014 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004015
4016exit:
4017 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004018 mbedtls_free( output );
4019 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004020}
4021/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004022
4023/* BEGIN_CASE */
4024void generate_key( int type_arg,
4025 int bits_arg,
4026 int usage_arg,
4027 int alg_arg,
4028 int expected_status_arg )
4029{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004030 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004031 psa_key_type_t type = type_arg;
4032 psa_key_usage_t usage = usage_arg;
4033 size_t bits = bits_arg;
4034 psa_algorithm_t alg = alg_arg;
4035 psa_status_t expected_status = expected_status_arg;
4036 psa_key_type_t got_type;
4037 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004038 psa_status_t expected_info_status =
4039 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
4040 psa_key_policy_t policy;
4041
Gilles Peskine8817f612018-12-18 00:18:46 +01004042 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004043
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004045 psa_key_policy_init( &policy );
4046 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004047 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004048
4049 /* Generate a key */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004050 TEST_EQUAL( psa_generate_key( handle, type, bits,
4051 NULL, 0 ), expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004052
4053 /* Test the key information */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004054 TEST_EQUAL( psa_get_key_information( handle,
4055 &got_type,
4056 &got_bits ), expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004057 if( expected_info_status != PSA_SUCCESS )
4058 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004059 TEST_EQUAL( got_type, type );
4060 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004061
Gilles Peskine818ca122018-06-20 18:16:48 +02004062 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004064 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004065
4066exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004067 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004068 mbedtls_psa_crypto_free( );
4069}
4070/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004071
Darryl Greend49a4992018-06-18 17:27:26 +01004072/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4073void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4074 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004075 int alg_arg, int generation_method,
4076 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004077{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004078 psa_key_handle_t handle = 0;
4079 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004080 psa_key_type_t type = (psa_key_type_t) type_arg;
4081 psa_key_type_t type_get;
4082 size_t bits_get;
4083 psa_key_policy_t policy_set;
4084 psa_key_policy_t policy_get;
4085 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4086 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004087 psa_key_policy_t base_policy_set;
4088 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4089 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004090 unsigned char *first_export = NULL;
4091 unsigned char *second_export = NULL;
4092 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4093 size_t first_exported_length;
4094 size_t second_exported_length;
4095
4096 ASSERT_ALLOC( first_export, export_size );
4097 ASSERT_ALLOC( second_export, export_size );
4098
Gilles Peskine8817f612018-12-18 00:18:46 +01004099 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004100
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4102 type, bits,
4103 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004104 psa_key_policy_init( &policy_set );
Darryl Greend49a4992018-06-18 17:27:26 +01004105 psa_key_policy_set_usage( &policy_set, policy_usage,
4106 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004107 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004108
Darryl Green0c6575a2018-11-07 16:05:30 +00004109 switch( generation_method )
4110 {
4111 case IMPORT_KEY:
4112 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004113 PSA_ASSERT( psa_import_key( handle, type,
4114 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004115 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004116
Darryl Green0c6575a2018-11-07 16:05:30 +00004117 case GENERATE_KEY:
4118 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004119 PSA_ASSERT( psa_generate_key( handle, type, bits,
4120 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004121 break;
4122
4123 case DERIVE_KEY:
4124 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004125 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4126 PSA_BYTES_TO_BITS( data->len ),
4127 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004128 psa_key_policy_init( &base_policy_set );
Darryl Green0c6575a2018-11-07 16:05:30 +00004129 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4130 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004131 PSA_ASSERT( psa_set_key_policy(
4132 base_key, &base_policy_set ) );
4133 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4134 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004135 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004136 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4137 base_policy_alg,
4138 NULL, 0, NULL, 0,
4139 export_size ) );
4140 PSA_ASSERT( psa_generator_import_key(
4141 handle, PSA_KEY_TYPE_RAW_DATA,
4142 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004143 break;
4144 }
Darryl Greend49a4992018-06-18 17:27:26 +01004145
4146 /* Export the key */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004147 TEST_EQUAL( psa_export_key( handle, first_export, export_size,
4148 &first_exported_length ), export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004149
4150 /* Shutdown and restart */
4151 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004152 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004153
Darryl Greend49a4992018-06-18 17:27:26 +01004154 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004155 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4156 &handle ) );
4157 PSA_ASSERT( psa_get_key_information(
4158 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004159 TEST_EQUAL( type_get, type );
4160 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004161
Gilles Peskine8817f612018-12-18 00:18:46 +01004162 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004163 TEST_EQUAL( psa_key_policy_get_usage(
4164 &policy_get ), policy_usage );
4165 TEST_EQUAL( psa_key_policy_get_algorithm(
4166 &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004167
4168 /* Export the key again */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004169 TEST_EQUAL( psa_export_key( handle, second_export, export_size,
4170 &second_exported_length ), export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004171
Darryl Green0c6575a2018-11-07 16:05:30 +00004172 if( export_status == PSA_SUCCESS )
4173 {
4174 ASSERT_COMPARE( first_export, first_exported_length,
4175 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004176
Darryl Green0c6575a2018-11-07 16:05:30 +00004177 switch( generation_method )
4178 {
4179 case IMPORT_KEY:
4180 ASSERT_COMPARE( data->x, data->len,
4181 first_export, first_exported_length );
4182 break;
4183 default:
4184 break;
4185 }
4186 }
4187
4188 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004189 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004190 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004191
4192exit:
4193 mbedtls_free( first_export );
4194 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004195 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004196 mbedtls_psa_crypto_free();
4197}
4198/* END_CASE */