blob: b7b7c4c5d1d4fa1137c5987ed8e064842ca32809 [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 Peskine96ee5c72018-07-12 17:24:54 +020014#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
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 Peskine818ca122018-06-20 18:16:48 +0200131static int exercise_mac_key( psa_key_slot_t key,
132 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 Peskine89167cb2018-07-08 20:12:23 +0200142 TEST_ASSERT( psa_mac_sign_setup( &operation,
143 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200144 TEST_ASSERT( psa_mac_update( &operation,
145 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200146 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200147 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 &mac_length ) == PSA_SUCCESS );
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 Peskine89167cb2018-07-08 20:12:23 +0200157 TEST_ASSERT( psa_mac_verify_setup( &operation,
158 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200159 TEST_ASSERT( psa_mac_update( &operation,
160 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200161 TEST_ASSERT( 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
173static int exercise_cipher_key( psa_key_slot_t key,
174 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 Peskinefe119512018-07-08 21:39:34 +0200188 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
189 key, alg ) == PSA_SUCCESS );
190 TEST_ASSERT( psa_cipher_generate_iv( &operation,
191 iv, sizeof( iv ),
192 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200193 TEST_ASSERT( psa_cipher_update( &operation,
194 plaintext, sizeof( plaintext ),
195 ciphertext, sizeof( ciphertext ),
196 &ciphertext_length ) == PSA_SUCCESS );
197 TEST_ASSERT( psa_cipher_finish( &operation,
198 ciphertext + ciphertext_length,
199 sizeof( ciphertext ) - ciphertext_length,
200 &part_length ) == PSA_SUCCESS );
201 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;
211 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
212 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
213 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200214 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
215 key, alg ) == PSA_SUCCESS );
216 TEST_ASSERT( psa_cipher_set_iv( &operation,
217 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200218 TEST_ASSERT( psa_cipher_update( &operation,
219 ciphertext, ciphertext_length,
220 decrypted, sizeof( decrypted ),
221 &part_length ) == PSA_SUCCESS );
222 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 Peskine818ca122018-06-20 18:16:48 +0200231 TEST_ASSERT( status == PSA_SUCCESS );
232 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
244static int exercise_aead_key( psa_key_slot_t key,
245 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 {
257 TEST_ASSERT( psa_aead_encrypt( key, alg,
258 nonce, nonce_length,
259 NULL, 0,
260 plaintext, sizeof( plaintext ),
261 ciphertext, sizeof( ciphertext ),
262 &ciphertext_length ) == PSA_SUCCESS );
263 }
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 );
271 TEST_ASSERT( psa_aead_decrypt( key, alg,
272 nonce, nonce_length,
273 NULL, 0,
274 ciphertext, ciphertext_length,
275 plaintext, sizeof( plaintext ),
276 &plaintext_length ) == verify_status );
277 }
278
279 return( 1 );
280
281exit:
282 return( 0 );
283}
284
285static int exercise_signature_key( psa_key_slot_t key,
286 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 Peskine818ca122018-06-20 18:16:48 +0200302 TEST_ASSERT( psa_asymmetric_sign( key, alg,
303 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 signature, sizeof( signature ),
305 &signature_length ) == PSA_SUCCESS );
306 }
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 );
314 TEST_ASSERT( psa_asymmetric_verify( key, alg,
315 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 signature, signature_length ) ==
317 verify_status );
318 }
319
320 return( 1 );
321
322exit:
323 return( 0 );
324}
325
326static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
327 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 {
337 TEST_ASSERT(
338 psa_asymmetric_encrypt( key, alg,
339 plaintext, plaintext_length,
340 NULL, 0,
341 ciphertext, sizeof( ciphertext ),
342 &ciphertext_length ) == PSA_SUCCESS );
343 }
344
345 if( usage & PSA_KEY_USAGE_DECRYPT )
346 {
347 psa_status_t status =
348 psa_asymmetric_decrypt( key, alg,
349 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 Peskineea0fb492018-07-12 17:17:20 +0200365static int exercise_key_derivation_key( psa_key_slot_t key,
366 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 {
378 TEST_ASSERT( psa_key_derivation( &generator,
379 key, alg,
380 label, label_length,
381 seed, seed_length,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_read( &generator,
384 output,
385 sizeof( output ) ) == PSA_SUCCESS );
386 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
387 }
388
389 return( 1 );
390
391exit:
392 return( 0 );
393}
394
Gilles Peskine01d718c2018-09-18 12:01:02 +0200395static int exercise_key_agreement_key( psa_key_slot_t key,
396 psa_key_usage_t usage,
397 psa_algorithm_t alg )
398{
399 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
400 psa_key_type_t key_type;
401 psa_key_type_t public_key_type;
402 size_t key_bits;
403 uint8_t *public_key = NULL;
404 size_t public_key_length;
405 unsigned char output[1];
406 int ok = 0;
407
408 if( usage & PSA_KEY_USAGE_DERIVE )
409 {
410 /* We need two keys to exercise key agreement. Exercise the
411 * private key against its own public key. */
412 TEST_ASSERT( psa_get_key_information( key,
413 &key_type,
414 &key_bits ) == PSA_SUCCESS );
415 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( key_type );
416 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type,
417 key_bits );
418 public_key = mbedtls_calloc( 1, public_key_length );
419 TEST_ASSERT( public_key != NULL );
420 TEST_ASSERT(
421 psa_export_public_key( key,
422 public_key, public_key_length,
423 &public_key_length ) == PSA_SUCCESS );
424 TEST_ASSERT( psa_key_agreement( &generator,
425 key,
426 public_key, public_key_length,
427 alg ) == PSA_SUCCESS );
428 TEST_ASSERT( psa_generator_read( &generator,
429 output,
430 sizeof( output ) ) == PSA_SUCCESS );
431 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
432 }
433 ok = 1;
434
435exit:
436 mbedtls_free( public_key );
437 return( ok );
438}
439
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200440static int is_oid_of_key_type( psa_key_type_t type,
441 const uint8_t *oid, size_t oid_length )
442{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200443 const uint8_t *expected_oid = NULL;
444 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200445#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200446 if( PSA_KEY_TYPE_IS_RSA( type ) )
447 {
448 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
449 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
450 }
451 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200452#endif /* MBEDTLS_RSA_C */
453#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200454 if( PSA_KEY_TYPE_IS_ECC( type ) )
455 {
456 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
457 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
458 }
459 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200460#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200461 {
462 char message[40];
463 mbedtls_snprintf( message, sizeof( message ),
464 "OID not known for key type=0x%08lx",
465 (unsigned long) type );
466 test_fail( message, __LINE__, __FILE__ );
467 return( 0 );
468 }
469
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200470 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200471 return( 1 );
472
473exit:
474 return( 0 );
475}
476
477static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
478 size_t min_bits, size_t max_bits,
479 int must_be_odd )
480{
481 size_t len;
482 size_t actual_bits;
483 unsigned char msb;
484 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
485 MBEDTLS_ASN1_INTEGER ) == 0 );
486 /* Tolerate a slight departure from DER encoding:
487 * - 0 may be represented by an empty string or a 1-byte string.
488 * - The sign bit may be used as a value bit. */
489 if( ( len == 1 && ( *p )[0] == 0 ) ||
490 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
491 {
492 ++( *p );
493 --len;
494 }
495 if( min_bits == 0 && len == 0 )
496 return( 1 );
497 msb = ( *p )[0];
498 TEST_ASSERT( msb != 0 );
499 actual_bits = 8 * ( len - 1 );
500 while( msb != 0 )
501 {
502 msb >>= 1;
503 ++actual_bits;
504 }
505 TEST_ASSERT( actual_bits >= min_bits );
506 TEST_ASSERT( actual_bits <= max_bits );
507 if( must_be_odd )
508 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
509 *p += len;
510 return( 1 );
511exit:
512 return( 0 );
513}
514
515static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
516 size_t *len,
517 unsigned char n, unsigned char tag )
518{
519 int ret;
520 ret = mbedtls_asn1_get_tag( p, end, len,
521 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
522 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
523 if( ret != 0 )
524 return( ret );
525 end = *p + *len;
526 ret = mbedtls_asn1_get_tag( p, end, len, tag );
527 if( ret != 0 )
528 return( ret );
529 if( *p + *len != end )
530 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
531 return( 0 );
532}
533
534static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
535 uint8_t *exported, size_t exported_length )
536{
537 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200538 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200539 else
540 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200541
542#if defined(MBEDTLS_DES_C)
543 if( type == PSA_KEY_TYPE_DES )
544 {
545 /* Check the parity bits. */
546 unsigned i;
547 for( i = 0; i < bits / 8; i++ )
548 {
549 unsigned bit_count = 0;
550 unsigned m;
551 for( m = 1; m <= 0x100; m <<= 1 )
552 {
553 if( exported[i] & m )
554 ++bit_count;
555 }
556 TEST_ASSERT( bit_count % 2 != 0 );
557 }
558 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200559 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200560#endif
561
562#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
563 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
564 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200565 uint8_t *p = exported;
566 uint8_t *end = exported + exported_length;
567 size_t len;
568 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200569 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 * modulus INTEGER, -- n
571 * publicExponent INTEGER, -- e
572 * privateExponent INTEGER, -- d
573 * prime1 INTEGER, -- p
574 * prime2 INTEGER, -- q
575 * exponent1 INTEGER, -- d mod (p-1)
576 * exponent2 INTEGER, -- d mod (q-1)
577 * coefficient INTEGER, -- (inverse of q) mod p
578 * }
579 */
580 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
581 MBEDTLS_ASN1_SEQUENCE |
582 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
583 TEST_ASSERT( p + len == end );
584 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
585 goto exit;
586 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
587 goto exit;
588 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
589 goto exit;
590 /* Require d to be at least half the size of n. */
591 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
592 goto exit;
593 /* Require p and q to be at most half the size of n, rounded up. */
594 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
595 goto exit;
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
597 goto exit;
598 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
599 goto exit;
600 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
601 goto exit;
602 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
603 goto exit;
604 TEST_ASSERT( p == end );
605 }
606 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200607#endif /* MBEDTLS_RSA_C */
608
609#if defined(MBEDTLS_ECP_C)
610 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
611 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100612 /* Just the secret value */
613 TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) );
614 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200615 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200616#endif /* MBEDTLS_ECP_C */
617
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200618 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
619 {
620 uint8_t *p = exported;
621 uint8_t *end = exported + exported_length;
622 size_t len;
623 mbedtls_asn1_buf alg;
624 mbedtls_asn1_buf params;
625 mbedtls_asn1_bitstring bitstring;
626 /* SubjectPublicKeyInfo ::= SEQUENCE {
627 * algorithm AlgorithmIdentifier,
628 * subjectPublicKey BIT STRING }
629 * AlgorithmIdentifier ::= SEQUENCE {
630 * algorithm OBJECT IDENTIFIER,
631 * parameters ANY DEFINED BY algorithm OPTIONAL }
632 */
633 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
634 MBEDTLS_ASN1_SEQUENCE |
635 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
636 TEST_ASSERT( p + len == end );
637 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
638 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
639 goto exit;
640 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
641 TEST_ASSERT( p == end );
642 p = bitstring.p;
643#if defined(MBEDTLS_RSA_C)
644 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
645 {
646 /* RSAPublicKey ::= SEQUENCE {
647 * modulus INTEGER, -- n
648 * publicExponent INTEGER } -- e
649 */
650 TEST_ASSERT( bitstring.unused_bits == 0 );
651 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
652 MBEDTLS_ASN1_SEQUENCE |
653 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
654 TEST_ASSERT( p + len == end );
655 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
656 goto exit;
657 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
658 goto exit;
659 TEST_ASSERT( p == end );
660 }
661 else
662#endif /* MBEDTLS_RSA_C */
663#if defined(MBEDTLS_ECP_C)
664 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
665 {
666 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200667 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200668 * -- then x_P as an n-bit string, big endian;
669 * -- then y_P as a n-bit string, big endian,
670 * -- where n is the order of the curve.
671 */
672 TEST_ASSERT( bitstring.unused_bits == 0 );
673 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
674 TEST_ASSERT( p[0] == 4 );
675 }
676 else
677#endif /* MBEDTLS_ECP_C */
678 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100679 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200680 mbedtls_snprintf( message, sizeof( message ),
681 "No sanity check for public key type=0x%08lx",
682 (unsigned long) type );
683 test_fail( message, __LINE__, __FILE__ );
684 return( 0 );
685 }
686 }
687 else
688
689 {
690 /* No sanity checks for other types */
691 }
692
693 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200694
695exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200696 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200697}
698
699static int exercise_export_key( psa_key_slot_t slot,
700 psa_key_usage_t usage )
701{
702 psa_key_type_t type;
703 size_t bits;
704 uint8_t *exported = NULL;
705 size_t exported_size = 0;
706 size_t exported_length = 0;
707 int ok = 0;
708
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200709 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
710
711 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
712 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200713 {
714 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
715 PSA_ERROR_NOT_PERMITTED );
716 return( 1 );
717 }
718
Gilles Peskined14664a2018-08-10 19:07:32 +0200719 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200720 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200721
722 TEST_ASSERT( psa_export_key( slot,
723 exported, exported_size,
724 &exported_length ) == PSA_SUCCESS );
725 ok = exported_key_sanity_check( type, bits, exported, exported_length );
726
727exit:
728 mbedtls_free( exported );
729 return( ok );
730}
731
732static int exercise_export_public_key( psa_key_slot_t slot )
733{
734 psa_key_type_t type;
735 psa_key_type_t public_type;
736 size_t bits;
737 uint8_t *exported = NULL;
738 size_t exported_size = 0;
739 size_t exported_length = 0;
740 int ok = 0;
741
742 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
743 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
744 {
745 TEST_ASSERT( psa_export_public_key( slot,
746 NULL, 0, &exported_length ) ==
747 PSA_ERROR_INVALID_ARGUMENT );
748 return( 1 );
749 }
750
751 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
752 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200753 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200754
755 TEST_ASSERT( psa_export_public_key( slot,
756 exported, exported_size,
757 &exported_length ) == PSA_SUCCESS );
758 ok = exported_key_sanity_check( public_type, bits,
759 exported, exported_length );
760
761exit:
762 mbedtls_free( exported );
763 return( ok );
764}
765
Gilles Peskine02b75072018-07-01 22:31:34 +0200766static int exercise_key( psa_key_slot_t slot,
767 psa_key_usage_t usage,
768 psa_algorithm_t alg )
769{
770 int ok;
771 if( alg == 0 )
772 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
773 else if( PSA_ALG_IS_MAC( alg ) )
774 ok = exercise_mac_key( slot, usage, alg );
775 else if( PSA_ALG_IS_CIPHER( alg ) )
776 ok = exercise_cipher_key( slot, usage, alg );
777 else if( PSA_ALG_IS_AEAD( alg ) )
778 ok = exercise_aead_key( slot, usage, alg );
779 else if( PSA_ALG_IS_SIGN( alg ) )
780 ok = exercise_signature_key( slot, usage, alg );
781 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
782 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200783 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
784 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200785 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
786 ok = exercise_key_agreement_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200787 else
788 {
789 char message[40];
790 mbedtls_snprintf( message, sizeof( message ),
791 "No code to exercise alg=0x%08lx",
792 (unsigned long) alg );
793 test_fail( message, __LINE__, __FILE__ );
794 ok = 0;
795 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200796
797 ok = ok && exercise_export_key( slot, usage );
798 ok = ok && exercise_export_public_key( slot );
799
Gilles Peskine02b75072018-07-01 22:31:34 +0200800 return( ok );
801}
802
Gilles Peskinee59236f2018-01-27 23:32:46 +0100803/* END_HEADER */
804
805/* BEGIN_DEPENDENCIES
806 * depends_on:MBEDTLS_PSA_CRYPTO_C
807 * END_DEPENDENCIES
808 */
809
810/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200811void static_checks( )
812{
813 size_t max_truncated_mac_size =
814 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
815
816 /* Check that the length for a truncated MAC always fits in the algorithm
817 * encoding. The shifted mask is the maximum truncated value. The
818 * untruncated algorithm may be one byte larger. */
819 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
820}
821/* END_CASE */
822
823/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200824void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100825{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100827 int i;
828 for( i = 0; i <= 1; i++ )
829 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100830 status = psa_crypto_init( );
831 TEST_ASSERT( status == PSA_SUCCESS );
832 status = psa_crypto_init( );
833 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100834 mbedtls_psa_crypto_free( );
835 }
836}
837/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100838
839/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200840void fill_slots( int max_arg )
841{
842 /* Fill all the slots until we run out of memory or out of slots,
843 * or until some limit specified in the test data for the sake of
844 * implementations with an essentially unlimited number of slots.
845 * This test assumes that available slots are numbered from 1. */
846
847 psa_key_slot_t slot;
848 psa_key_slot_t max = 0;
849 psa_key_policy_t policy;
850 uint8_t exported[sizeof( max )];
851 size_t exported_size;
852 psa_status_t status;
853
854 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
855
856 psa_key_policy_init( &policy );
857 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
858
859 for( max = 1; max <= (size_t) max_arg; max++ )
860 {
861 status = psa_set_key_policy( max, &policy );
862 /* Stop filling slots if we run out of memory or out of
863 * available slots. */
864 TEST_ASSERT( status == PSA_SUCCESS ||
865 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
866 status == PSA_ERROR_INVALID_ARGUMENT );
867 if( status != PSA_SUCCESS )
868 break;
869 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
870 (uint8_t*) &max, sizeof( max ) );
871 /* Since psa_set_key_policy succeeded, we know that the slot
872 * number is valid. But we may legitimately run out of memory. */
873 TEST_ASSERT( status == PSA_SUCCESS ||
874 status == PSA_ERROR_INSUFFICIENT_MEMORY );
875 if( status != PSA_SUCCESS )
876 break;
877 }
878 /* `max` is now the first slot number that wasn't filled. */
879 max -= 1;
880
881 for( slot = 1; slot <= max; slot++ )
882 {
883 TEST_ASSERT( psa_export_key( slot,
884 exported, sizeof( exported ),
885 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200886 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200887 }
888
889exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200890 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200891 mbedtls_psa_crypto_free( );
892}
893/* END_CASE */
894
895/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200896void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100897{
898 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200899 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100900 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100901
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300903 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100904 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
905
Gilles Peskine4abf7412018-06-18 16:35:34 +0200906 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200907 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100908 if( status == PSA_SUCCESS )
909 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
910
911exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100912 mbedtls_psa_crypto_free( );
913}
914/* END_CASE */
915
916/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200917void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
918{
919 int slot = 1;
920 size_t bits = bits_arg;
921 psa_status_t expected_status = expected_status_arg;
922 psa_status_t status;
923 psa_key_type_t type =
924 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
925 size_t buffer_size = /* Slight overapproximations */
926 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200927 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200928 unsigned char *p;
929 int ret;
930 size_t length;
931
932 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200933 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200934
935 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
936 bits, keypair ) ) >= 0 );
937 length = ret;
938
939 /* Try importing the key */
940 status = psa_import_key( slot, type, p, length );
941 TEST_ASSERT( status == expected_status );
942 if( status == PSA_SUCCESS )
943 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
944
945exit:
946 mbedtls_free( buffer );
947 mbedtls_psa_crypto_free( );
948}
949/* END_CASE */
950
951/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300952void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300953 int type_arg,
954 int alg_arg,
955 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 int expected_bits,
957 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200958 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959 int canonical_input )
960{
961 int slot = 1;
962 int slot2 = slot + 1;
963 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200964 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200965 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100967 unsigned char *exported = NULL;
968 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100970 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971 size_t reexported_length;
972 psa_key_type_t got_type;
973 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200974 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100976 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300977 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300978 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200979 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200981 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
983
mohammad1603a97cb8c2018-03-28 03:46:26 -0700984 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200985 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700986 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
987
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 /* Import the key */
989 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200990 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991
992 /* Test the key information */
993 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200994 &got_type,
995 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 TEST_ASSERT( got_type == type );
997 TEST_ASSERT( got_bits == (size_t) expected_bits );
998
999 /* Export the key */
1000 status = psa_export_key( slot,
1001 exported, export_size,
1002 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001003 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001004
1005 /* The exported length must be set by psa_export_key() to a value between 0
1006 * and export_size. On errors, the exported length must be 0. */
1007 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1008 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1009 TEST_ASSERT( exported_length <= export_size );
1010
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001011 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001012 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001014 {
1015 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001017 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018
Gilles Peskine8f609232018-08-11 01:24:55 +02001019 if( ! exercise_export_key( slot, usage_arg ) )
1020 goto exit;
1021
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001023 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 else
1025 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001026 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1027
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001028 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001029 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001030 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001032 reexported,
1033 export_size,
1034 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001035 ASSERT_COMPARE( exported, exported_length,
1036 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001038 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001039
1040destroy:
1041 /* Destroy the key */
1042 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1043 TEST_ASSERT( psa_get_key_information(
1044 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1045
1046exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001047 mbedtls_free( exported );
1048 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 mbedtls_psa_crypto_free( );
1050}
1051/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001052
Moran Pekerf709f4a2018-06-06 17:26:04 +03001053/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001054void import_key_nonempty_slot( )
1055{
1056 int slot = 1;
1057 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1058 psa_status_t status;
1059 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1060 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1061
1062 /* Import the key */
1063 TEST_ASSERT( psa_import_key( slot, type,
1064 data, sizeof( data ) ) == PSA_SUCCESS );
1065
1066 /* Import the key again */
1067 status = psa_import_key( slot, type, data, sizeof( data ) );
1068 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1069
1070exit:
1071 mbedtls_psa_crypto_free( );
1072}
1073/* END_CASE */
1074
1075/* BEGIN_CASE */
1076void export_invalid_slot( int slot, int expected_export_status_arg )
1077{
1078 psa_status_t status;
1079 unsigned char *exported = NULL;
1080 size_t export_size = 0;
1081 size_t exported_length = INVALID_EXPORT_LENGTH;
1082 psa_status_t expected_export_status = expected_export_status_arg;
1083
1084 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1085
1086 /* Export the key */
1087 status = psa_export_key( slot,
1088 exported, export_size,
1089 &exported_length );
1090 TEST_ASSERT( status == expected_export_status );
1091
1092exit:
1093 mbedtls_psa_crypto_free( );
1094}
1095/* END_CASE */
1096
1097/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001098void export_with_no_key_activity( )
1099{
1100 int slot = 1;
1101 psa_algorithm_t alg = PSA_ALG_CTR;
1102 psa_status_t status;
1103 psa_key_policy_t policy;
1104 unsigned char *exported = NULL;
1105 size_t export_size = 0;
1106 size_t exported_length = INVALID_EXPORT_LENGTH;
1107
1108 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1109
1110 psa_key_policy_init( &policy );
1111 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1112 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1113
1114 /* Export the key */
1115 status = psa_export_key( slot,
1116 exported, export_size,
1117 &exported_length );
1118 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1119
1120exit:
1121 mbedtls_psa_crypto_free( );
1122}
1123/* END_CASE */
1124
1125/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001126void cipher_with_no_key_activity( )
1127{
1128 int slot = 1;
1129 psa_status_t status;
1130 psa_key_policy_t policy;
1131 psa_cipher_operation_t operation;
1132 int exercise_alg = PSA_ALG_CTR;
1133
1134 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1135
1136 psa_key_policy_init( &policy );
1137 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
1138 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1139
1140 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1141 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1142
1143exit:
1144 psa_cipher_abort( &operation );
1145 mbedtls_psa_crypto_free( );
1146}
1147/* END_CASE */
1148
1149/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001150void export_after_import_failure( data_t *data, int type_arg,
1151 int expected_import_status_arg )
1152{
1153 int slot = 1;
1154 psa_key_type_t type = type_arg;
1155 psa_status_t status;
1156 unsigned char *exported = NULL;
1157 size_t export_size = 0;
1158 psa_status_t expected_import_status = expected_import_status_arg;
1159 size_t exported_length = INVALID_EXPORT_LENGTH;
1160
1161 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1162
1163 /* Import the key - expect failure */
1164 status = psa_import_key( slot, type,
1165 data->x, data->len );
1166 TEST_ASSERT( status == expected_import_status );
1167
1168 /* Export the key */
1169 status = psa_export_key( slot,
1170 exported, export_size,
1171 &exported_length );
1172 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1173
1174exit:
1175 mbedtls_psa_crypto_free( );
1176}
1177/* END_CASE */
1178
1179/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001180void cipher_after_import_failure( data_t *data, int type_arg,
1181 int expected_import_status_arg )
1182{
1183 int slot = 1;
1184 psa_cipher_operation_t operation;
1185 psa_key_type_t type = type_arg;
1186 psa_status_t status;
1187 psa_status_t expected_import_status = expected_import_status_arg;
1188 int exercise_alg = PSA_ALG_CTR;
1189
1190 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1191
1192 /* Import the key - expect failure */
1193 status = psa_import_key( slot, type,
1194 data->x, data->len );
1195 TEST_ASSERT( status == expected_import_status );
1196
1197 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1198 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1199
1200exit:
1201 psa_cipher_abort( &operation );
1202 mbedtls_psa_crypto_free( );
1203}
1204/* END_CASE */
1205
1206/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001207void export_after_destroy_key( data_t *data, int type_arg )
1208{
1209 int slot = 1;
1210 psa_key_type_t type = type_arg;
1211 psa_status_t status;
1212 psa_key_policy_t policy;
1213 psa_algorithm_t alg = PSA_ALG_CTR;
1214 unsigned char *exported = NULL;
1215 size_t export_size = 0;
1216 size_t exported_length = INVALID_EXPORT_LENGTH;
1217
1218 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1219
1220 psa_key_policy_init( &policy );
1221 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1222 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1223 export_size = (ptrdiff_t) data->len;
1224 ASSERT_ALLOC( exported, export_size );
1225
1226 /* Import the key */
1227 TEST_ASSERT( psa_import_key( slot, type,
1228 data->x, data->len ) == PSA_SUCCESS );
1229
1230 TEST_ASSERT( psa_export_key( slot, exported, export_size,
1231 &exported_length ) == PSA_SUCCESS );
1232
1233 /* Destroy the key */
1234 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1235
1236 /* Export the key */
1237 status = psa_export_key( slot, exported, export_size,
1238 &exported_length );
1239 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1240
1241exit:
1242 mbedtls_free( exported );
1243 mbedtls_psa_crypto_free( );
1244}
1245/* END_CASE */
1246
1247/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001248void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001249 int type_arg,
1250 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001251 int export_size_delta,
1252 int expected_export_status_arg,
1253 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001254{
1255 int slot = 1;
1256 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001257 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001258 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001259 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001260 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001261 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001262 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001263 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001264
Moran Pekerf709f4a2018-06-06 17:26:04 +03001265 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1266
1267 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001268 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001269 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1270
1271 /* Import the key */
1272 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001273 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001274
Gilles Peskine49c25912018-10-29 15:15:31 +01001275 /* Export the public key */
1276 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001277 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001278 exported, export_size,
1279 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001280 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001281 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001282 {
1283 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1284 size_t bits;
1285 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1286 PSA_SUCCESS );
1287 TEST_ASSERT( expected_public_key->len <=
1288 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001289 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1290 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001291 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292
1293exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001294 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001295 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001296 mbedtls_psa_crypto_free( );
1297}
1298/* END_CASE */
1299
Gilles Peskine20035e32018-02-03 22:44:14 +01001300/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001301void import_and_exercise_key( data_t *data,
1302 int type_arg,
1303 int bits_arg,
1304 int alg_arg )
1305{
1306 int slot = 1;
1307 psa_key_type_t type = type_arg;
1308 size_t bits = bits_arg;
1309 psa_algorithm_t alg = alg_arg;
1310 psa_key_usage_t usage =
1311 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1312 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1313 PSA_KEY_USAGE_VERIFY :
1314 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1315 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1316 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1317 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1318 PSA_KEY_USAGE_ENCRYPT :
1319 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001320 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskine01d718c2018-09-18 12:01:02 +02001321 PSA_ALG_IS_KEY_AGREEMENT( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001322 0 );
1323 psa_key_policy_t policy;
1324 psa_key_type_t got_type;
1325 size_t got_bits;
1326 psa_status_t status;
1327
1328 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1329
1330 psa_key_policy_init( &policy );
1331 psa_key_policy_set_usage( &policy, usage, alg );
1332 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1333
1334 /* Import the key */
1335 status = psa_import_key( slot, type, data->x, data->len );
1336 TEST_ASSERT( status == PSA_SUCCESS );
1337
1338 /* Test the key information */
1339 TEST_ASSERT( psa_get_key_information( slot,
1340 &got_type,
1341 &got_bits ) == PSA_SUCCESS );
1342 TEST_ASSERT( got_type == type );
1343 TEST_ASSERT( got_bits == bits );
1344
1345 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001346 if( ! exercise_key( slot, usage, alg ) )
1347 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001348
1349exit:
1350 psa_destroy_key( slot );
1351 mbedtls_psa_crypto_free( );
1352}
1353/* END_CASE */
1354
1355/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001356void key_policy( int usage_arg, int alg_arg )
1357{
1358 int key_slot = 1;
1359 psa_algorithm_t alg = alg_arg;
1360 psa_key_usage_t usage = usage_arg;
1361 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1362 unsigned char key[32] = {0};
1363 psa_key_policy_t policy_set;
1364 psa_key_policy_t policy_get;
1365
1366 memset( key, 0x2a, sizeof( key ) );
1367
1368 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1369
1370 psa_key_policy_init( &policy_set );
1371 psa_key_policy_init( &policy_get );
1372
1373 psa_key_policy_set_usage( &policy_set, usage, alg );
1374
1375 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1376 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1377 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1378
1379 TEST_ASSERT( psa_import_key( key_slot, key_type,
1380 key, sizeof( key ) ) == PSA_SUCCESS );
1381
1382 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1383
1384 TEST_ASSERT( policy_get.usage == policy_set.usage );
1385 TEST_ASSERT( policy_get.alg == policy_set.alg );
1386
1387exit:
1388 psa_destroy_key( key_slot );
1389 mbedtls_psa_crypto_free( );
1390}
1391/* END_CASE */
1392
1393/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001394void mac_key_policy( int policy_usage,
1395 int policy_alg,
1396 int key_type,
1397 data_t *key_data,
1398 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001399{
1400 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001401 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001402 psa_mac_operation_t operation;
1403 psa_status_t status;
1404 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001405
1406 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1407
1408 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001409 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001410 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1411
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001412 TEST_ASSERT( psa_import_key( key_slot, key_type,
1413 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001414
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001416 if( policy_alg == exercise_alg &&
1417 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1418 TEST_ASSERT( status == PSA_SUCCESS );
1419 else
1420 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1421 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001422
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001423 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001425 if( policy_alg == exercise_alg &&
1426 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001427 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001428 else
1429 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1430
1431exit:
1432 psa_mac_abort( &operation );
1433 psa_destroy_key( key_slot );
1434 mbedtls_psa_crypto_free( );
1435}
1436/* END_CASE */
1437
1438/* BEGIN_CASE */
1439void cipher_key_policy( int policy_usage,
1440 int policy_alg,
1441 int key_type,
1442 data_t *key_data,
1443 int exercise_alg )
1444{
1445 int key_slot = 1;
1446 psa_key_policy_t policy;
1447 psa_cipher_operation_t operation;
1448 psa_status_t status;
1449
1450 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1451
1452 psa_key_policy_init( &policy );
1453 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1454 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1455
1456 TEST_ASSERT( psa_import_key( key_slot, key_type,
1457 key_data->x, key_data->len ) == PSA_SUCCESS );
1458
Gilles Peskinefe119512018-07-08 21:39:34 +02001459 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001460 if( policy_alg == exercise_alg &&
1461 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1462 TEST_ASSERT( status == PSA_SUCCESS );
1463 else
1464 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1465 psa_cipher_abort( &operation );
1466
Gilles Peskinefe119512018-07-08 21:39:34 +02001467 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001468 if( policy_alg == exercise_alg &&
1469 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1470 TEST_ASSERT( status == PSA_SUCCESS );
1471 else
1472 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1473
1474exit:
1475 psa_cipher_abort( &operation );
1476 psa_destroy_key( key_slot );
1477 mbedtls_psa_crypto_free( );
1478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
1482void aead_key_policy( int policy_usage,
1483 int policy_alg,
1484 int key_type,
1485 data_t *key_data,
1486 int nonce_length_arg,
1487 int tag_length_arg,
1488 int exercise_alg )
1489{
1490 int key_slot = 1;
1491 psa_key_policy_t policy;
1492 psa_status_t status;
1493 unsigned char nonce[16] = {0};
1494 size_t nonce_length = nonce_length_arg;
1495 unsigned char tag[16];
1496 size_t tag_length = tag_length_arg;
1497 size_t output_length;
1498
1499 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1500 TEST_ASSERT( tag_length <= sizeof( tag ) );
1501
1502 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1503
1504 psa_key_policy_init( &policy );
1505 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1506 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1507
1508 TEST_ASSERT( psa_import_key( key_slot, key_type,
1509 key_data->x, key_data->len ) == PSA_SUCCESS );
1510
1511 status = psa_aead_encrypt( key_slot, exercise_alg,
1512 nonce, nonce_length,
1513 NULL, 0,
1514 NULL, 0,
1515 tag, tag_length,
1516 &output_length );
1517 if( policy_alg == exercise_alg &&
1518 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1519 TEST_ASSERT( status == PSA_SUCCESS );
1520 else
1521 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1522
1523 memset( tag, 0, sizeof( tag ) );
1524 status = psa_aead_decrypt( key_slot, exercise_alg,
1525 nonce, nonce_length,
1526 NULL, 0,
1527 tag, tag_length,
1528 NULL, 0,
1529 &output_length );
1530 if( policy_alg == exercise_alg &&
1531 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1532 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1533 else
1534 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1535
1536exit:
1537 psa_destroy_key( key_slot );
1538 mbedtls_psa_crypto_free( );
1539}
1540/* END_CASE */
1541
1542/* BEGIN_CASE */
1543void asymmetric_encryption_key_policy( int policy_usage,
1544 int policy_alg,
1545 int key_type,
1546 data_t *key_data,
1547 int exercise_alg )
1548{
1549 int key_slot = 1;
1550 psa_key_policy_t policy;
1551 psa_status_t status;
1552 size_t key_bits;
1553 size_t buffer_length;
1554 unsigned char *buffer = NULL;
1555 size_t output_length;
1556
1557 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1558
1559 psa_key_policy_init( &policy );
1560 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1561 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1562
1563 TEST_ASSERT( psa_import_key( key_slot, key_type,
1564 key_data->x, key_data->len ) == PSA_SUCCESS );
1565
1566 TEST_ASSERT( psa_get_key_information( key_slot,
1567 NULL,
1568 &key_bits ) == PSA_SUCCESS );
1569 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1570 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001571 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572
1573 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1574 NULL, 0,
1575 NULL, 0,
1576 buffer, buffer_length,
1577 &output_length );
1578 if( policy_alg == exercise_alg &&
1579 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1580 TEST_ASSERT( status == PSA_SUCCESS );
1581 else
1582 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1583
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001584 if( buffer_length != 0 )
1585 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001586 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1587 buffer, buffer_length,
1588 NULL, 0,
1589 buffer, buffer_length,
1590 &output_length );
1591 if( policy_alg == exercise_alg &&
1592 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1593 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1594 else
1595 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1596
1597exit:
1598 psa_destroy_key( key_slot );
1599 mbedtls_psa_crypto_free( );
1600 mbedtls_free( buffer );
1601}
1602/* END_CASE */
1603
1604/* BEGIN_CASE */
1605void asymmetric_signature_key_policy( int policy_usage,
1606 int policy_alg,
1607 int key_type,
1608 data_t *key_data,
1609 int exercise_alg )
1610{
1611 int key_slot = 1;
1612 psa_key_policy_t policy;
1613 psa_status_t status;
1614 unsigned char payload[16] = {1};
1615 size_t payload_length = sizeof( payload );
1616 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1617 size_t signature_length;
1618
1619 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1620
1621 psa_key_policy_init( &policy );
1622 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1623 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1624
1625 TEST_ASSERT( psa_import_key( key_slot, key_type,
1626 key_data->x, key_data->len ) == PSA_SUCCESS );
1627
1628 status = psa_asymmetric_sign( key_slot, exercise_alg,
1629 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630 signature, sizeof( signature ),
1631 &signature_length );
1632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1634 TEST_ASSERT( status == PSA_SUCCESS );
1635 else
1636 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1637
1638 memset( signature, 0, sizeof( signature ) );
1639 status = psa_asymmetric_verify( key_slot, exercise_alg,
1640 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 signature, sizeof( signature ) );
1642 if( policy_alg == exercise_alg &&
1643 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1644 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1645 else
1646 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001647
1648exit:
1649 psa_destroy_key( key_slot );
1650 mbedtls_psa_crypto_free( );
1651}
1652/* END_CASE */
1653
1654/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001655void derive_key_policy( int policy_usage,
1656 int policy_alg,
1657 int key_type,
1658 data_t *key_data,
1659 int exercise_alg )
1660{
1661 int key_slot = 1;
1662 psa_key_policy_t policy;
1663 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1664 psa_status_t status;
1665
1666 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1667
1668 psa_key_policy_init( &policy );
1669 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1670 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1671
1672 TEST_ASSERT( psa_import_key( key_slot, key_type,
1673 key_data->x, key_data->len ) == PSA_SUCCESS );
1674
1675 status = psa_key_derivation( &generator, key_slot,
1676 exercise_alg,
1677 NULL, 0,
1678 NULL, 0,
1679 1 );
1680 if( policy_alg == exercise_alg &&
1681 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1682 TEST_ASSERT( status == PSA_SUCCESS );
1683 else
1684 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1685
1686exit:
1687 psa_generator_abort( &generator );
1688 psa_destroy_key( key_slot );
1689 mbedtls_psa_crypto_free( );
1690}
1691/* END_CASE */
1692
1693/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001694void agreement_key_policy( int policy_usage,
1695 int policy_alg,
1696 int key_type_arg,
1697 data_t *key_data,
1698 int exercise_alg )
1699{
1700 int key_slot = 1;
1701 psa_key_policy_t policy;
1702 psa_key_type_t key_type = key_type_arg;
1703 psa_key_type_t public_key_type;
1704 size_t key_bits;
1705 uint8_t *public_key = NULL;
1706 size_t public_key_length;
1707 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1708 psa_status_t status;
1709
1710 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1711
1712 psa_key_policy_init( &policy );
1713 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1714 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1715
1716 TEST_ASSERT( psa_import_key( key_slot, key_type,
1717 key_data->x, key_data->len ) == PSA_SUCCESS );
1718
1719 /* We need two keys to exercise key agreement. Exercise the
1720 * private key against its own public key. */
1721 TEST_ASSERT( psa_get_key_information( key_slot,
1722 &key_type,
1723 &key_bits ) == PSA_SUCCESS );
1724 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( key_type );
1725 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
1726 public_key = mbedtls_calloc( 1, public_key_length );
1727 TEST_ASSERT( public_key != NULL );
1728 TEST_ASSERT( psa_export_public_key( key_slot,
1729 public_key, public_key_length,
1730 &public_key_length ) == PSA_SUCCESS );
1731
1732 status = psa_key_agreement( &generator, key_slot,
1733 public_key, public_key_length,
1734 exercise_alg );
1735 if( policy_alg == exercise_alg &&
1736 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1737 TEST_ASSERT( status == PSA_SUCCESS );
1738 else
1739 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1740
1741exit:
1742 psa_generator_abort( &generator );
1743 psa_destroy_key( key_slot );
1744 mbedtls_psa_crypto_free( );
1745}
1746/* END_CASE */
1747
1748/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001749void key_lifetime( int lifetime_arg )
1750{
1751 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001752 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001753 unsigned char key[32] = {0};
1754 psa_key_lifetime_t lifetime_set = lifetime_arg;
1755 psa_key_lifetime_t lifetime_get;
1756
1757 memset( key, 0x2a, sizeof( key ) );
1758
1759 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1760
1761 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1762 lifetime_set ) == PSA_SUCCESS );
1763
1764 TEST_ASSERT( psa_import_key( key_slot, key_type,
1765 key, sizeof( key ) ) == PSA_SUCCESS );
1766
1767 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1768 &lifetime_get ) == PSA_SUCCESS );
1769
1770 TEST_ASSERT( lifetime_get == lifetime_set );
1771
1772exit:
1773 psa_destroy_key( key_slot );
1774 mbedtls_psa_crypto_free( );
1775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
1779void key_lifetime_set_fail( int key_slot_arg,
1780 int lifetime_arg,
1781 int expected_status_arg )
1782{
1783 psa_key_slot_t key_slot = key_slot_arg;
1784 psa_key_lifetime_t lifetime_set = lifetime_arg;
1785 psa_status_t actual_status;
1786 psa_status_t expected_status = expected_status_arg;
1787
1788 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1789
1790 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1791
1792 if( actual_status == PSA_SUCCESS )
1793 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1794
1795 TEST_ASSERT( expected_status == actual_status );
1796
1797exit:
1798 psa_destroy_key( key_slot );
1799 mbedtls_psa_crypto_free( );
1800}
1801/* END_CASE */
1802
1803/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001804void hash_setup( int alg_arg,
1805 int expected_status_arg )
1806{
1807 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001808 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001809 psa_hash_operation_t operation;
1810 psa_status_t status;
1811
1812 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1813
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001814 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001815 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001816 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001817
1818exit:
1819 mbedtls_psa_crypto_free( );
1820}
1821/* END_CASE */
1822
1823/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001824void hash_bad_order( )
1825{
1826 unsigned char input[] = "";
1827 /* SHA-256 hash of an empty string */
1828 unsigned char hash[] = {
1829 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1830 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1831 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1832 size_t hash_len;
1833 psa_hash_operation_t operation;
1834
1835 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1836
1837 /* psa_hash_update without calling psa_hash_setup beforehand */
1838 memset( &operation, 0, sizeof( operation ) );
1839 TEST_ASSERT( psa_hash_update( &operation,
1840 input, sizeof( input ) ) ==
1841 PSA_ERROR_INVALID_ARGUMENT );
1842
1843 /* psa_hash_verify without calling psa_hash_setup beforehand */
1844 memset( &operation, 0, sizeof( operation ) );
1845 TEST_ASSERT( psa_hash_verify( &operation,
1846 hash, sizeof( hash ) ) ==
1847 PSA_ERROR_INVALID_ARGUMENT );
1848
1849 /* psa_hash_finish without calling psa_hash_setup beforehand */
1850 memset( &operation, 0, sizeof( operation ) );
1851 TEST_ASSERT( psa_hash_finish( &operation,
1852 hash, sizeof( hash ), &hash_len ) ==
1853 PSA_ERROR_INVALID_ARGUMENT );
1854
1855exit:
1856 mbedtls_psa_crypto_free( );
1857}
1858/* END_CASE */
1859
itayzafrir27e69452018-11-01 14:26:34 +02001860/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1861void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001862{
1863 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001864 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1865 * appended to it */
1866 unsigned char hash[] = {
1867 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1868 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1869 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001870 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001871 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001872
1873 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1874
itayzafrir27e69452018-11-01 14:26:34 +02001875 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001876 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1877 TEST_ASSERT( psa_hash_verify( &operation,
1878 hash, expected_size - 1 ) ==
1879 PSA_ERROR_INVALID_SIGNATURE );
1880
itayzafrir27e69452018-11-01 14:26:34 +02001881 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001882 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001883 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001884 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001885 PSA_ERROR_INVALID_SIGNATURE );
1886
itayzafrir27e69452018-11-01 14:26:34 +02001887 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001888 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001889 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001890 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001891 PSA_ERROR_INVALID_SIGNATURE );
1892
itayzafrirec93d302018-10-18 18:01:10 +03001893exit:
1894 mbedtls_psa_crypto_free( );
1895}
1896/* END_CASE */
1897
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001898/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1899void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001900{
1901 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001902 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001903 size_t expected_size = PSA_HASH_SIZE( alg );
1904 psa_hash_operation_t operation;
1905 size_t hash_len;
1906
1907 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1908
itayzafrir58028322018-10-25 10:22:01 +03001909 /* psa_hash_finish with a smaller hash buffer than expected */
1910 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1911 TEST_ASSERT( psa_hash_finish( &operation,
1912 hash, expected_size - 1,
1913 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1914
1915exit:
1916 mbedtls_psa_crypto_free( );
1917}
1918/* END_CASE */
1919
1920/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001921void mac_setup( int key_type_arg,
1922 data_t *key,
1923 int alg_arg,
1924 int expected_status_arg )
1925{
1926 int key_slot = 1;
1927 psa_key_type_t key_type = key_type_arg;
1928 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001929 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001930 psa_mac_operation_t operation;
1931 psa_key_policy_t policy;
1932 psa_status_t status;
1933
1934 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1935
1936 psa_key_policy_init( &policy );
1937 psa_key_policy_set_usage( &policy,
1938 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1939 alg );
1940 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1941
1942 TEST_ASSERT( psa_import_key( key_slot, key_type,
1943 key->x, key->len ) == PSA_SUCCESS );
1944
Gilles Peskine89167cb2018-07-08 20:12:23 +02001945 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001946 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001947 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948
1949exit:
1950 psa_destroy_key( key_slot );
1951 mbedtls_psa_crypto_free( );
1952}
1953/* END_CASE */
1954
1955/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001956void mac_sign( int key_type_arg,
1957 data_t *key,
1958 int alg_arg,
1959 data_t *input,
1960 data_t *expected_mac )
1961{
1962 int key_slot = 1;
1963 psa_key_type_t key_type = key_type_arg;
1964 psa_algorithm_t alg = alg_arg;
1965 psa_mac_operation_t operation;
1966 psa_key_policy_t policy;
1967 /* Leave a little extra room in the output buffer. At the end of the
1968 * test, we'll check that the implementation didn't overwrite onto
1969 * this extra room. */
1970 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1971 size_t mac_buffer_size =
1972 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1973 size_t mac_length = 0;
1974
1975 memset( actual_mac, '+', sizeof( actual_mac ) );
1976 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1977 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1978
1979 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1980
1981 psa_key_policy_init( &policy );
1982 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1983 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1984
1985 TEST_ASSERT( psa_import_key( key_slot, key_type,
1986 key->x, key->len ) == PSA_SUCCESS );
1987
1988 /* Calculate the MAC. */
1989 TEST_ASSERT( psa_mac_sign_setup( &operation,
1990 key_slot, alg ) == PSA_SUCCESS );
1991 TEST_ASSERT( psa_mac_update( &operation,
1992 input->x, input->len ) == PSA_SUCCESS );
1993 TEST_ASSERT( psa_mac_sign_finish( &operation,
1994 actual_mac, mac_buffer_size,
1995 &mac_length ) == PSA_SUCCESS );
1996
1997 /* Compare with the expected value. */
1998 TEST_ASSERT( mac_length == expected_mac->len );
1999 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
2000
2001 /* Verify that the end of the buffer is untouched. */
2002 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2003 sizeof( actual_mac ) - mac_length ) );
2004
2005exit:
2006 psa_destroy_key( key_slot );
2007 mbedtls_psa_crypto_free( );
2008}
2009/* END_CASE */
2010
2011/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002012void mac_verify( int key_type_arg,
2013 data_t *key,
2014 int alg_arg,
2015 data_t *input,
2016 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017{
2018 int key_slot = 1;
2019 psa_key_type_t key_type = key_type_arg;
2020 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002022 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002023
Gilles Peskine69c12672018-06-28 00:07:19 +02002024 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2025
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002029 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002030 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2031 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002032
2033 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2034
mohammad16036df908f2018-04-02 08:34:15 -07002035 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002036 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07002037 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2038
Gilles Peskine8c9def32018-02-08 10:02:12 +01002039 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002040 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002041
Gilles Peskine89167cb2018-07-08 20:12:23 +02002042 TEST_ASSERT( psa_mac_verify_setup( &operation,
2043 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002044 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
2045 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002046 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02002047 TEST_ASSERT( psa_mac_verify_finish( &operation,
2048 expected_mac->x,
2049 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002050
2051exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002052 psa_destroy_key( key_slot );
2053 mbedtls_psa_crypto_free( );
2054}
2055/* END_CASE */
2056
2057/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002058void cipher_setup( int key_type_arg,
2059 data_t *key,
2060 int alg_arg,
2061 int expected_status_arg )
2062{
2063 int key_slot = 1;
2064 psa_key_type_t key_type = key_type_arg;
2065 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002066 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002067 psa_cipher_operation_t operation;
2068 psa_key_policy_t policy;
2069 psa_status_t status;
2070
2071 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2072
2073 psa_key_policy_init( &policy );
2074 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2075 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2076
2077 TEST_ASSERT( psa_import_key( key_slot, key_type,
2078 key->x, key->len ) == PSA_SUCCESS );
2079
Gilles Peskinefe119512018-07-08 21:39:34 +02002080 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002081 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002082 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002083
2084exit:
2085 psa_destroy_key( key_slot );
2086 mbedtls_psa_crypto_free( );
2087}
2088/* END_CASE */
2089
2090/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002091void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002092 data_t *key,
2093 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002094 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002095{
2096 int key_slot = 1;
2097 psa_status_t status;
2098 psa_key_type_t key_type = key_type_arg;
2099 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002100 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002101 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002102 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002103 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002104 size_t output_buffer_size = 0;
2105 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002106 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002107 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002108 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002109
Gilles Peskine50e586b2018-06-08 14:28:46 +02002110 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002111 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002112 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002113 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2114 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2115 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002116
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002117 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2118 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002119
2120 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2121
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 );
2124 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2125
Gilles Peskine50e586b2018-06-08 14:28:46 +02002126 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002127 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002128
Gilles Peskinefe119512018-07-08 21:39:34 +02002129 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2130 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002131
Gilles Peskinefe119512018-07-08 21:39:34 +02002132 TEST_ASSERT( psa_cipher_set_iv( &operation,
2133 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002134 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 Peskine4abf7412018-06-18 16:35:34 +02002138 TEST_ASSERT( psa_cipher_update( &operation,
2139 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002140 output, output_buffer_size,
2141 &function_output_length ) == PSA_SUCCESS );
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 Peskineb866e2b2018-06-21 09:25:10 +02002149 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002150 if( expected_status == PSA_SUCCESS )
2151 {
2152 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
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 Peskine50e586b2018-06-08 14:28:46 +02002159 psa_destroy_key( key_slot );
2160 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{
2171 int key_slot = 1;
2172 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
2193 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2194
Moran Pekered346952018-07-05 15:22:45 +03002195 psa_key_policy_init( &policy );
2196 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2197 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2198
Gilles Peskine50e586b2018-06-08 14:28:46 +02002199 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002200 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002201
Gilles Peskinefe119512018-07-08 21:39:34 +02002202 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2203 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002204
Gilles Peskinefe119512018-07-08 21:39:34 +02002205 TEST_ASSERT( psa_cipher_set_iv( &operation,
2206 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002207 output_buffer_size = (size_t) input->len +
2208 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002209 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210
Gilles Peskine4abf7412018-06-18 16:35:34 +02002211 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002212 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002213 output, output_buffer_size,
2214 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002215 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002216 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002217 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002218 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002219 output, output_buffer_size,
2220 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002221 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002222 TEST_ASSERT( psa_cipher_finish( &operation,
2223 output + function_output_length,
2224 output_buffer_size,
2225 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002226 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2228
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002229 ASSERT_COMPARE( expected_output->x, expected_output->len,
2230 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002231
2232exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002233 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002234 psa_destroy_key( key_slot );
2235 mbedtls_psa_crypto_free( );
2236}
2237/* END_CASE */
2238
2239/* BEGIN_CASE */
2240void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002241 data_t *key,
2242 data_t *input,
2243 int first_part_size,
2244 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245{
2246 int key_slot = 1;
2247
2248 psa_key_type_t key_type = key_type_arg;
2249 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002250 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002251 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002252 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002253 size_t output_buffer_size = 0;
2254 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002255 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002256 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002257 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002258
Gilles Peskine50e586b2018-06-08 14:28:46 +02002259 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002260 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002261 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002262 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2263 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2264 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002265
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002266 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2267 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002268
2269 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2270
Moran Pekered346952018-07-05 15:22:45 +03002271 psa_key_policy_init( &policy );
2272 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2273 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2274
Gilles Peskine50e586b2018-06-08 14:28:46 +02002275 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002276 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002277
Gilles Peskinefe119512018-07-08 21:39:34 +02002278 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2279 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002280
Gilles Peskinefe119512018-07-08 21:39:34 +02002281 TEST_ASSERT( psa_cipher_set_iv( &operation,
2282 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002283
mohammad16033d91abe2018-07-03 13:15:54 +03002284 output_buffer_size = (size_t) input->len +
2285 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002286 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002287
Gilles Peskine4abf7412018-06-18 16:35:34 +02002288 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2289 TEST_ASSERT( psa_cipher_update( &operation,
2290 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002291 output, output_buffer_size,
2292 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002293 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002294 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002295 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002296 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002297 output, output_buffer_size,
2298 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002299 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002300 TEST_ASSERT( psa_cipher_finish( &operation,
2301 output + function_output_length,
2302 output_buffer_size,
2303 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002304 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002305 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2306
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002307 ASSERT_COMPARE( expected_output->x, expected_output->len,
2308 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002309
2310exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002311 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002312 psa_destroy_key( key_slot );
2313 mbedtls_psa_crypto_free( );
2314}
2315/* END_CASE */
2316
Gilles Peskine50e586b2018-06-08 14:28:46 +02002317/* BEGIN_CASE */
2318void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002319 data_t *key,
2320 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002321 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002322{
2323 int key_slot = 1;
2324 psa_status_t status;
2325 psa_key_type_t key_type = key_type_arg;
2326 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002327 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002328 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002329 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002330 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002331 size_t output_buffer_size = 0;
2332 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002333 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002334 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002335 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002336
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002340 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2341 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002343
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002344 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2345 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346
2347 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2348
Moran Pekered346952018-07-05 15:22:45 +03002349 psa_key_policy_init( &policy );
2350 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2351 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2352
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002354 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002355
Gilles Peskinefe119512018-07-08 21:39:34 +02002356 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2357 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002358
Gilles Peskinefe119512018-07-08 21:39:34 +02002359 TEST_ASSERT( psa_cipher_set_iv( &operation,
2360 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002361
mohammad16033d91abe2018-07-03 13:15:54 +03002362 output_buffer_size = (size_t) input->len +
2363 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002364 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002365
Gilles Peskine4abf7412018-06-18 16:35:34 +02002366 TEST_ASSERT( psa_cipher_update( &operation,
2367 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002368 output, output_buffer_size,
2369 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002370 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002371 status = psa_cipher_finish( &operation,
2372 output + function_output_length,
2373 output_buffer_size,
2374 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002375 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002376 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002377
2378 if( expected_status == PSA_SUCCESS )
2379 {
2380 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002381 ASSERT_COMPARE( expected_output->x, expected_output->len,
2382 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002383 }
2384
Gilles Peskine50e586b2018-06-08 14:28:46 +02002385exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002386 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387 psa_destroy_key( key_slot );
2388 mbedtls_psa_crypto_free( );
2389}
2390/* END_CASE */
2391
Gilles Peskine50e586b2018-06-08 14:28:46 +02002392/* BEGIN_CASE */
2393void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002394 data_t *key,
2395 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002396{
2397 int key_slot = 1;
2398 psa_key_type_t key_type = key_type_arg;
2399 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002400 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002401 size_t iv_size = 16;
2402 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002403 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002404 size_t output1_size = 0;
2405 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002406 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002407 size_t output2_size = 0;
2408 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002409 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002410 psa_cipher_operation_t operation1;
2411 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002412 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002413
mohammad1603d7d7ba52018-03-12 18:51:53 +02002414 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002415 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002416 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002418
mohammad1603d7d7ba52018-03-12 18:51:53 +02002419 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2420
Moran Pekered346952018-07-05 15:22:45 +03002421 psa_key_policy_init( &policy );
2422 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2423 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2424
mohammad1603d7d7ba52018-03-12 18:51:53 +02002425 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002426 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002427
Gilles Peskinefe119512018-07-08 21:39:34 +02002428 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2429 key_slot, alg ) == PSA_SUCCESS );
2430 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2431 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002432
Gilles Peskinefe119512018-07-08 21:39:34 +02002433 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2434 iv, iv_size,
2435 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002436 output1_size = (size_t) input->len +
2437 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002438 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002439
Gilles Peskine4abf7412018-06-18 16:35:34 +02002440 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002441 output1, output1_size,
2442 &output1_length ) == PSA_SUCCESS );
2443 TEST_ASSERT( psa_cipher_finish( &operation1,
2444 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002445 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002446
Gilles Peskine048b7f02018-06-08 14:20:49 +02002447 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002448
2449 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2450
2451 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002452 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002453
Gilles Peskinefe119512018-07-08 21:39:34 +02002454 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2455 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002456 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2457 output2, output2_size,
2458 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002459 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002460 TEST_ASSERT( psa_cipher_finish( &operation2,
2461 output2 + output2_length,
2462 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002463 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002464
Gilles Peskine048b7f02018-06-08 14:20:49 +02002465 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002466
Janos Follath25c4fa82018-07-06 16:23:25 +01002467 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002468
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002469 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002470
2471exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002472 mbedtls_free( output1 );
2473 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002474 psa_destroy_key( key_slot );
2475 mbedtls_psa_crypto_free( );
2476}
2477/* END_CASE */
2478
2479/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002480void cipher_verify_output_multipart( int alg_arg,
2481 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002482 data_t *key,
2483 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002484 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002485{
2486 int key_slot = 1;
2487 psa_key_type_t key_type = key_type_arg;
2488 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002489 unsigned char iv[16] = {0};
2490 size_t iv_size = 16;
2491 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002492 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002493 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002494 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002495 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002496 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002497 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002498 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002499 psa_cipher_operation_t operation1;
2500 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002501 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002502
Moran Pekerded84402018-06-06 16:36:50 +03002503 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002504 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002505 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2506 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002507
Moran Pekerded84402018-06-06 16:36:50 +03002508 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2509
Moran Pekered346952018-07-05 15:22:45 +03002510 psa_key_policy_init( &policy );
2511 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2512 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2513
Moran Pekerded84402018-06-06 16:36:50 +03002514 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002515 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002516
Gilles Peskinefe119512018-07-08 21:39:34 +02002517 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2518 key_slot, alg ) == PSA_SUCCESS );
2519 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2520 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002521
Gilles Peskinefe119512018-07-08 21:39:34 +02002522 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2523 iv, iv_size,
2524 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002525 output1_buffer_size = (size_t) input->len +
2526 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002527 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002528
Gilles Peskine4abf7412018-06-18 16:35:34 +02002529 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002530
itayzafrir3e02b3b2018-06-12 17:06:52 +03002531 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002532 output1, output1_buffer_size,
2533 &function_output_length ) == PSA_SUCCESS );
2534 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002535
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002536 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002537 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002538 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002539 output1, output1_buffer_size,
2540 &function_output_length ) == PSA_SUCCESS );
2541 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002542
Gilles Peskine048b7f02018-06-08 14:20:49 +02002543 TEST_ASSERT( psa_cipher_finish( &operation1,
2544 output1 + output1_length,
2545 output1_buffer_size - output1_length,
2546 &function_output_length ) == PSA_SUCCESS );
2547 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002548
2549 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2550
Gilles Peskine048b7f02018-06-08 14:20:49 +02002551 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002552 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002553
Gilles Peskinefe119512018-07-08 21:39:34 +02002554 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2555 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002556
2557 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002558 output2, output2_buffer_size,
2559 &function_output_length ) == PSA_SUCCESS );
2560 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002561
Gilles Peskine048b7f02018-06-08 14:20:49 +02002562 TEST_ASSERT( psa_cipher_update( &operation2,
2563 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002564 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002565 output2, output2_buffer_size,
2566 &function_output_length ) == PSA_SUCCESS );
2567 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002568
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002569 TEST_ASSERT( psa_cipher_finish( &operation2,
2570 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002571 output2_buffer_size - output2_length,
2572 &function_output_length ) == PSA_SUCCESS );
2573 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002574
Janos Follath25c4fa82018-07-06 16:23:25 +01002575 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002576
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002577 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002578
2579exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002580 mbedtls_free( output1 );
2581 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002582 psa_destroy_key( key_slot );
2583 mbedtls_psa_crypto_free( );
2584}
2585/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002586
Gilles Peskine20035e32018-02-03 22:44:14 +01002587/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002588void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002589 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002590 data_t *nonce,
2591 data_t *additional_data,
2592 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002593 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002594{
2595 int slot = 1;
2596 psa_key_type_t key_type = key_type_arg;
2597 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002598 unsigned char *output_data = NULL;
2599 size_t output_size = 0;
2600 size_t output_length = 0;
2601 unsigned char *output_data2 = NULL;
2602 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002603 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002604 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002605 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002606
Gilles Peskinea1cac842018-06-11 19:33:02 +02002607 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002608 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002609 TEST_ASSERT( nonce != NULL );
2610 TEST_ASSERT( additional_data != NULL );
2611 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2612 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2613 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2614 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2615
Gilles Peskine4abf7412018-06-18 16:35:34 +02002616 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002617 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002618
2619 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2620
2621 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002622 psa_key_policy_set_usage( &policy,
2623 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2624 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002625 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2626
2627 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002628 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629
2630 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002631 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002632 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002633 additional_data->len,
2634 input_data->x, input_data->len,
2635 output_data, output_size,
2636 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002637
2638 if( PSA_SUCCESS == expected_result )
2639 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002640 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002641
2642 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002643 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002644 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002645 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002646 output_data, output_length,
2647 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002648 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002649
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002650 ASSERT_COMPARE( input_data->x, input_data->len,
2651 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002652 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002653
Gilles Peskinea1cac842018-06-11 19:33:02 +02002654exit:
2655 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002656 mbedtls_free( output_data );
2657 mbedtls_free( output_data2 );
2658 mbedtls_psa_crypto_free( );
2659}
2660/* END_CASE */
2661
2662/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002663void aead_encrypt( int key_type_arg, data_t *key_data,
2664 int alg_arg,
2665 data_t *nonce,
2666 data_t *additional_data,
2667 data_t *input_data,
2668 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669{
2670 int slot = 1;
2671 psa_key_type_t key_type = key_type_arg;
2672 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002673 unsigned char *output_data = NULL;
2674 size_t output_size = 0;
2675 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002676 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002677 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002678
Gilles Peskinea1cac842018-06-11 19:33:02 +02002679 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002680 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002681 TEST_ASSERT( additional_data != NULL );
2682 TEST_ASSERT( nonce != NULL );
2683 TEST_ASSERT( expected_result != NULL );
2684 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2685 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2686 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2687 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2688 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2689
Gilles Peskine4abf7412018-06-18 16:35:34 +02002690 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002691 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002692
Gilles Peskinea1cac842018-06-11 19:33:02 +02002693 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2694
2695 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002697 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2698
2699 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002700 key_data->x,
2701 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002702
2703 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002704 nonce->x, nonce->len,
2705 additional_data->x, additional_data->len,
2706 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002707 output_data, output_size,
2708 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002709
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002710 ASSERT_COMPARE( expected_result->x, expected_result->len,
2711 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002712
Gilles Peskinea1cac842018-06-11 19:33:02 +02002713exit:
2714 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002715 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716 mbedtls_psa_crypto_free( );
2717}
2718/* END_CASE */
2719
2720/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002721void aead_decrypt( int key_type_arg, data_t *key_data,
2722 int alg_arg,
2723 data_t *nonce,
2724 data_t *additional_data,
2725 data_t *input_data,
2726 data_t *expected_data,
2727 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002728{
2729 int slot = 1;
2730 psa_key_type_t key_type = key_type_arg;
2731 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002732 unsigned char *output_data = NULL;
2733 size_t output_size = 0;
2734 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002735 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002736 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002737 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002738
Gilles Peskinea1cac842018-06-11 19:33:02 +02002739 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002740 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002741 TEST_ASSERT( additional_data != NULL );
2742 TEST_ASSERT( nonce != NULL );
2743 TEST_ASSERT( expected_data != NULL );
2744 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2745 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2746 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2747 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2748 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2749
Gilles Peskine4abf7412018-06-18 16:35:34 +02002750 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002751 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002752
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2754
2755 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002756 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002757 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2758
2759 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002760 key_data->x,
2761 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002762
2763 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002764 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002765 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002766 additional_data->len,
2767 input_data->x, input_data->len,
2768 output_data, output_size,
2769 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002770
Gilles Peskine2d277862018-06-18 15:41:12 +02002771 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002772 ASSERT_COMPARE( expected_data->x, expected_data->len,
2773 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002774
Gilles Peskinea1cac842018-06-11 19:33:02 +02002775exit:
2776 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002777 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002778 mbedtls_psa_crypto_free( );
2779}
2780/* END_CASE */
2781
2782/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002783void signature_size( int type_arg,
2784 int bits,
2785 int alg_arg,
2786 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002787{
2788 psa_key_type_t type = type_arg;
2789 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002790 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002791 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2792exit:
2793 ;
2794}
2795/* END_CASE */
2796
2797/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002798void sign_deterministic( int key_type_arg, data_t *key_data,
2799 int alg_arg, data_t *input_data,
2800 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002801{
2802 int slot = 1;
2803 psa_key_type_t key_type = key_type_arg;
2804 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002805 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002806 unsigned char *signature = NULL;
2807 size_t signature_size;
2808 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002809 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002810
Gilles Peskine20035e32018-02-03 22:44:14 +01002811 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002812 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002813 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2815 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2816 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002817
2818 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2819
mohammad1603a97cb8c2018-03-28 03:46:26 -07002820 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002821 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002822 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2823
Gilles Peskine20035e32018-02-03 22:44:14 +01002824 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002825 key_data->x,
2826 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002827 TEST_ASSERT( psa_get_key_information( slot,
2828 NULL,
2829 &key_bits ) == PSA_SUCCESS );
2830
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002831 /* Allocate a buffer which has the size advertized by the
2832 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002833 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2834 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002835 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002836 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002837 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002838
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002839 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002840 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002841 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002842 signature, signature_size,
2843 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002844 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002845 ASSERT_COMPARE( output_data->x, output_data->len,
2846 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002847
2848exit:
2849 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002850 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002851 mbedtls_psa_crypto_free( );
2852}
2853/* END_CASE */
2854
2855/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002856void sign_fail( int key_type_arg, data_t *key_data,
2857 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002858 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002859{
2860 int slot = 1;
2861 psa_key_type_t key_type = key_type_arg;
2862 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002863 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002864 psa_status_t actual_status;
2865 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002866 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002867 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002868 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002869
Gilles Peskine20035e32018-02-03 22:44:14 +01002870 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002871 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002872 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2873 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2874
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002875 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002876
2877 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2878
mohammad1603a97cb8c2018-03-28 03:46:26 -07002879 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002880 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002881 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2882
Gilles Peskine20035e32018-02-03 22:44:14 +01002883 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002884 key_data->x,
2885 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002886
2887 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002888 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002889 signature, signature_size,
2890 &signature_length );
2891 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002892 /* The value of *signature_length is unspecified on error, but
2893 * whatever it is, it should be less than signature_size, so that
2894 * if the caller tries to read *signature_length bytes without
2895 * checking the error code then they don't overflow a buffer. */
2896 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002897
2898exit:
2899 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002900 mbedtls_free( signature );
2901 mbedtls_psa_crypto_free( );
2902}
2903/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002904
2905/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002906void sign_verify( int key_type_arg, data_t *key_data,
2907 int alg_arg, data_t *input_data )
2908{
2909 int slot = 1;
2910 psa_key_type_t key_type = key_type_arg;
2911 psa_algorithm_t alg = alg_arg;
2912 size_t key_bits;
2913 unsigned char *signature = NULL;
2914 size_t signature_size;
2915 size_t signature_length = 0xdeadbeef;
2916 psa_key_policy_t policy;
2917
2918 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2919
2920 psa_key_policy_init( &policy );
2921 psa_key_policy_set_usage( &policy,
2922 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2923 alg );
2924 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2925
2926 TEST_ASSERT( psa_import_key( slot, key_type,
2927 key_data->x,
2928 key_data->len ) == PSA_SUCCESS );
2929 TEST_ASSERT( psa_get_key_information( slot,
2930 NULL,
2931 &key_bits ) == PSA_SUCCESS );
2932
2933 /* Allocate a buffer which has the size advertized by the
2934 * library. */
2935 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2936 key_bits, alg );
2937 TEST_ASSERT( signature_size != 0 );
2938 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002939 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002940
2941 /* Perform the signature. */
2942 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2943 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002944 signature, signature_size,
2945 &signature_length ) == PSA_SUCCESS );
2946 /* Check that the signature length looks sensible. */
2947 TEST_ASSERT( signature_length <= signature_size );
2948 TEST_ASSERT( signature_length > 0 );
2949
2950 /* Use the library to verify that the signature is correct. */
2951 TEST_ASSERT( psa_asymmetric_verify(
2952 slot, alg,
2953 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002954 signature, signature_length ) == PSA_SUCCESS );
2955
2956 if( input_data->len != 0 )
2957 {
2958 /* Flip a bit in the input and verify that the signature is now
2959 * detected as invalid. Flip a bit at the beginning, not at the end,
2960 * because ECDSA may ignore the last few bits of the input. */
2961 input_data->x[0] ^= 1;
2962 TEST_ASSERT( psa_asymmetric_verify(
2963 slot, alg,
2964 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002965 signature,
2966 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2967 }
2968
2969exit:
2970 psa_destroy_key( slot );
2971 mbedtls_free( signature );
2972 mbedtls_psa_crypto_free( );
2973}
2974/* END_CASE */
2975
2976/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002977void asymmetric_verify( int key_type_arg, data_t *key_data,
2978 int alg_arg, data_t *hash_data,
2979 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002980{
2981 int slot = 1;
2982 psa_key_type_t key_type = key_type_arg;
2983 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002984 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002985
Gilles Peskine69c12672018-06-28 00:07:19 +02002986 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2987
itayzafrir5c753392018-05-08 11:18:38 +03002988 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002989 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002990 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002991 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2992 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2993 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002994
2995 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2996
2997 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002998 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002999 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3000
3001 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003002 key_data->x,
3003 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003004
3005 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003006 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003007 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003008 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003009exit:
3010 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03003011 mbedtls_psa_crypto_free( );
3012}
3013/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003014
3015/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003016void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3017 int alg_arg, data_t *hash_data,
3018 data_t *signature_data,
3019 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003020{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003021 int slot = 1;
3022 psa_key_type_t key_type = key_type_arg;
3023 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003024 psa_status_t actual_status;
3025 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003026 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003027
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003029 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003030 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003031 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3032 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3033 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003034
3035 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3036
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003037 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003038 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003039 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3040
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003041 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003042 key_data->x,
3043 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003044
3045 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003046 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003047 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003048 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003049
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050 TEST_ASSERT( actual_status == expected_status );
3051
3052exit:
3053 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003054 mbedtls_psa_crypto_free( );
3055}
3056/* END_CASE */
3057
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003059void asymmetric_encrypt( int key_type_arg,
3060 data_t *key_data,
3061 int alg_arg,
3062 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003063 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003064 int expected_output_length_arg,
3065 int expected_status_arg )
3066{
3067 int slot = 1;
3068 psa_key_type_t key_type = key_type_arg;
3069 psa_algorithm_t alg = alg_arg;
3070 size_t expected_output_length = expected_output_length_arg;
3071 size_t key_bits;
3072 unsigned char *output = NULL;
3073 size_t output_size;
3074 size_t output_length = ~0;
3075 psa_status_t actual_status;
3076 psa_status_t expected_status = expected_status_arg;
3077 psa_key_policy_t policy;
3078
3079 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3080
3081 /* Import the key */
3082 psa_key_policy_init( &policy );
3083 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
3084 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3085 TEST_ASSERT( psa_import_key( slot, key_type,
3086 key_data->x,
3087 key_data->len ) == PSA_SUCCESS );
3088
3089 /* Determine the maximum output length */
3090 TEST_ASSERT( psa_get_key_information( slot,
3091 NULL,
3092 &key_bits ) == PSA_SUCCESS );
3093 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003094 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003095
3096 /* Encrypt the input */
3097 actual_status = psa_asymmetric_encrypt( slot, alg,
3098 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003099 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003100 output, output_size,
3101 &output_length );
3102 TEST_ASSERT( actual_status == expected_status );
3103 TEST_ASSERT( output_length == expected_output_length );
3104
Gilles Peskine68428122018-06-30 18:42:41 +02003105 /* If the label is empty, the test framework puts a non-null pointer
3106 * in label->x. Test that a null pointer works as well. */
3107 if( label->len == 0 )
3108 {
3109 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003110 if( output_size != 0 )
3111 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003112 actual_status = psa_asymmetric_encrypt( slot, alg,
3113 input_data->x, input_data->len,
3114 NULL, label->len,
3115 output, output_size,
3116 &output_length );
3117 TEST_ASSERT( actual_status == expected_status );
3118 TEST_ASSERT( output_length == expected_output_length );
3119 }
3120
Gilles Peskine656896e2018-06-29 19:12:28 +02003121exit:
3122 psa_destroy_key( slot );
3123 mbedtls_free( output );
3124 mbedtls_psa_crypto_free( );
3125}
3126/* END_CASE */
3127
3128/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003129void asymmetric_encrypt_decrypt( int key_type_arg,
3130 data_t *key_data,
3131 int alg_arg,
3132 data_t *input_data,
3133 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003134{
3135 int slot = 1;
3136 psa_key_type_t key_type = key_type_arg;
3137 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003138 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003139 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003140 size_t output_size;
3141 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003142 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003143 size_t output2_size;
3144 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003145 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003149 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3150 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3151
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3153
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003154 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003155 psa_key_policy_set_usage( &policy,
3156 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003157 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003158 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3159
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003160 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003161 key_data->x,
3162 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003163
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003164
3165 /* Determine the maximum ciphertext length */
3166 TEST_ASSERT( psa_get_key_information( slot,
3167 NULL,
3168 &key_bits ) == PSA_SUCCESS );
3169 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003170 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003171 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003172 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003173
Gilles Peskineeebd7382018-06-08 18:11:54 +02003174 /* We test encryption by checking that encrypt-then-decrypt gives back
3175 * the original plaintext because of the non-optional random
3176 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02003177 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003178 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003179 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003180 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003181 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003182 /* We don't know what ciphertext length to expect, but check that
3183 * it looks sensible. */
3184 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003185
Gilles Peskine2d277862018-06-18 15:41:12 +02003186 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003187 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003188 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003189 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003190 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003191 ASSERT_COMPARE( input_data->x, input_data->len,
3192 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193
3194exit:
3195 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003196 mbedtls_free( output );
3197 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003198 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199}
3200/* END_CASE */
3201
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003203void asymmetric_decrypt( int key_type_arg,
3204 data_t *key_data,
3205 int alg_arg,
3206 data_t *input_data,
3207 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003208 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209{
3210 int slot = 1;
3211 psa_key_type_t key_type = key_type_arg;
3212 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003213 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003214 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003215 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003216 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003218 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003220 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003221 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3222 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3223 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3224
Gilles Peskine4abf7412018-06-18 16:35:34 +02003225 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003226 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003227
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3229
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003230 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003231 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003232 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3233
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003235 key_data->x,
3236 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003238 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003239 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003240 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003241 output,
3242 output_size,
3243 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003244 ASSERT_COMPARE( expected_data->x, expected_data->len,
3245 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003246
Gilles Peskine68428122018-06-30 18:42:41 +02003247 /* If the label is empty, the test framework puts a non-null pointer
3248 * in label->x. Test that a null pointer works as well. */
3249 if( label->len == 0 )
3250 {
3251 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003252 if( output_size != 0 )
3253 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003254 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3255 input_data->x, input_data->len,
3256 NULL, label->len,
3257 output,
3258 output_size,
3259 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003260 ASSERT_COMPARE( expected_data->x, expected_data->len,
3261 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003262 }
3263
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003264exit:
3265 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003266 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003268}
3269/* END_CASE */
3270
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003271/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003272void asymmetric_decrypt_fail( int key_type_arg,
3273 data_t *key_data,
3274 int alg_arg,
3275 data_t *input_data,
3276 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003277 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279 int slot = 1;
3280 psa_key_type_t key_type = key_type_arg;
3281 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003283 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003284 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285 psa_status_t actual_status;
3286 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003287 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003290 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003291 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3292 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3293
Gilles Peskine4abf7412018-06-18 16:35:34 +02003294 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003295 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003296
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003297 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3298
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003299 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003300 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003301 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3302
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003303 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003304 key_data->x,
3305 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306
Gilles Peskine2d277862018-06-18 15:41:12 +02003307 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003308 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003309 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003310 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003311 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003313 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314
Gilles Peskine68428122018-06-30 18:42:41 +02003315 /* If the label is empty, the test framework puts a non-null pointer
3316 * in label->x. Test that a null pointer works as well. */
3317 if( label->len == 0 )
3318 {
3319 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003320 if( output_size != 0 )
3321 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003322 actual_status = psa_asymmetric_decrypt( slot, alg,
3323 input_data->x, input_data->len,
3324 NULL, label->len,
3325 output, output_size,
3326 &output_length );
3327 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003328 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003329 }
3330
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003331exit:
3332 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003333 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003334 mbedtls_psa_crypto_free( );
3335}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003336/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003337
3338/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003339void derive_setup( int key_type_arg,
3340 data_t *key_data,
3341 int alg_arg,
3342 data_t *salt,
3343 data_t *label,
3344 int requested_capacity_arg,
3345 int expected_status_arg )
3346{
3347 psa_key_slot_t slot = 1;
3348 size_t key_type = key_type_arg;
3349 psa_algorithm_t alg = alg_arg;
3350 size_t requested_capacity = requested_capacity_arg;
3351 psa_status_t expected_status = expected_status_arg;
3352 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3353 psa_key_policy_t policy;
3354
3355 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3356
3357 psa_key_policy_init( &policy );
3358 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3359 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3360
3361 TEST_ASSERT( psa_import_key( slot, key_type,
3362 key_data->x,
3363 key_data->len ) == PSA_SUCCESS );
3364
3365 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3366 salt->x, salt->len,
3367 label->x, label->len,
3368 requested_capacity ) == expected_status );
3369
3370exit:
3371 psa_generator_abort( &generator );
3372 psa_destroy_key( slot );
3373 mbedtls_psa_crypto_free( );
3374}
3375/* END_CASE */
3376
3377/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003378void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003379{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003380 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003381 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003382 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003383 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003384 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003385 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003386 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3387 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3388 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003389 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003390
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003391 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3392
3393 psa_key_policy_init( &policy );
3394 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3395 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3396
3397 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003398 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003399 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003400
3401 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003402 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003403 NULL, 0,
3404 NULL, 0,
3405 capacity ) == PSA_SUCCESS );
3406
3407 /* state of generator shouldn't allow additional generation */
3408 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3409 NULL, 0,
3410 NULL, 0,
3411 capacity ) == PSA_ERROR_BAD_STATE );
3412
3413 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3414 == PSA_SUCCESS );
3415
3416 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3417 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3418
3419
3420exit:
3421 psa_generator_abort( &generator );
3422 psa_destroy_key( base_key );
3423 mbedtls_psa_crypto_free( );
3424}
3425/* END_CASE */
3426
3427
3428/* BEGIN_CASE */
3429void test_derive_invalid_generator_tests( )
3430{
3431 uint8_t output_buffer[16];
3432 size_t buffer_size = 16;
3433 size_t capacity = 0;
3434 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3435
Nir Sonnenschein50789302018-10-31 12:16:38 +02003436 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003437 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003438
3439 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003440 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003441
Nir Sonnenschein50789302018-10-31 12:16:38 +02003442 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003443
Nir Sonnenschein50789302018-10-31 12:16:38 +02003444 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003445 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003446
Nir Sonnenschein50789302018-10-31 12:16:38 +02003447 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003448 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003449
3450exit:
3451 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003452}
3453/* END_CASE */
3454
3455/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003456void derive_output( int alg_arg,
3457 data_t *key_data,
3458 data_t *salt,
3459 data_t *label,
3460 int requested_capacity_arg,
3461 data_t *expected_output1,
3462 data_t *expected_output2 )
3463{
3464 psa_key_slot_t slot = 1;
3465 psa_algorithm_t alg = alg_arg;
3466 size_t requested_capacity = requested_capacity_arg;
3467 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3468 uint8_t *expected_outputs[2] =
3469 {expected_output1->x, expected_output2->x};
3470 size_t output_sizes[2] =
3471 {expected_output1->len, expected_output2->len};
3472 size_t output_buffer_size = 0;
3473 uint8_t *output_buffer = NULL;
3474 size_t expected_capacity;
3475 size_t current_capacity;
3476 psa_key_policy_t policy;
3477 psa_status_t status;
3478 unsigned i;
3479
3480 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3481 {
3482 if( output_sizes[i] > output_buffer_size )
3483 output_buffer_size = output_sizes[i];
3484 if( output_sizes[i] == 0 )
3485 expected_outputs[i] = NULL;
3486 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003487 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003488 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3489
3490 psa_key_policy_init( &policy );
3491 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3492 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3493
3494 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3495 key_data->x,
3496 key_data->len ) == PSA_SUCCESS );
3497
3498 /* Extraction phase. */
3499 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3500 salt->x, salt->len,
3501 label->x, label->len,
3502 requested_capacity ) == PSA_SUCCESS );
3503 TEST_ASSERT( psa_get_generator_capacity( &generator,
3504 &current_capacity ) ==
3505 PSA_SUCCESS );
3506 TEST_ASSERT( current_capacity == requested_capacity );
3507 expected_capacity = requested_capacity;
3508
3509 /* Expansion phase. */
3510 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3511 {
3512 /* Read some bytes. */
3513 status = psa_generator_read( &generator,
3514 output_buffer, output_sizes[i] );
3515 if( expected_capacity == 0 && output_sizes[i] == 0 )
3516 {
3517 /* Reading 0 bytes when 0 bytes are available can go either way. */
3518 TEST_ASSERT( status == PSA_SUCCESS ||
3519 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3520 continue;
3521 }
3522 else if( expected_capacity == 0 ||
3523 output_sizes[i] > expected_capacity )
3524 {
3525 /* Capacity exceeded. */
3526 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3527 expected_capacity = 0;
3528 continue;
3529 }
3530 /* Success. Check the read data. */
3531 TEST_ASSERT( status == PSA_SUCCESS );
3532 if( output_sizes[i] != 0 )
3533 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3534 output_sizes[i] ) == 0 );
3535 /* Check the generator status. */
3536 expected_capacity -= output_sizes[i];
3537 TEST_ASSERT( psa_get_generator_capacity( &generator,
3538 &current_capacity ) ==
3539 PSA_SUCCESS );
3540 TEST_ASSERT( expected_capacity == current_capacity );
3541 }
3542 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3543
3544exit:
3545 mbedtls_free( output_buffer );
3546 psa_generator_abort( &generator );
3547 psa_destroy_key( slot );
3548 mbedtls_psa_crypto_free( );
3549}
3550/* END_CASE */
3551
3552/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003553void derive_full( int alg_arg,
3554 data_t *key_data,
3555 data_t *salt,
3556 data_t *label,
3557 int requested_capacity_arg )
3558{
3559 psa_key_slot_t slot = 1;
3560 psa_algorithm_t alg = alg_arg;
3561 size_t requested_capacity = requested_capacity_arg;
3562 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3563 unsigned char output_buffer[16];
3564 size_t expected_capacity = requested_capacity;
3565 size_t current_capacity;
3566 psa_key_policy_t policy;
3567
3568 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3569
3570 psa_key_policy_init( &policy );
3571 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3572 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3573
3574 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3575 key_data->x,
3576 key_data->len ) == PSA_SUCCESS );
3577
3578 /* Extraction phase. */
3579 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3580 salt->x, salt->len,
3581 label->x, label->len,
3582 requested_capacity ) == PSA_SUCCESS );
3583 TEST_ASSERT( psa_get_generator_capacity( &generator,
3584 &current_capacity ) ==
3585 PSA_SUCCESS );
3586 TEST_ASSERT( current_capacity == expected_capacity );
3587
3588 /* Expansion phase. */
3589 while( current_capacity > 0 )
3590 {
3591 size_t read_size = sizeof( output_buffer );
3592 if( read_size > current_capacity )
3593 read_size = current_capacity;
3594 TEST_ASSERT( psa_generator_read( &generator,
3595 output_buffer,
3596 read_size ) == PSA_SUCCESS );
3597 expected_capacity -= read_size;
3598 TEST_ASSERT( psa_get_generator_capacity( &generator,
3599 &current_capacity ) ==
3600 PSA_SUCCESS );
3601 TEST_ASSERT( current_capacity == expected_capacity );
3602 }
3603
3604 /* Check that the generator refuses to go over capacity. */
3605 TEST_ASSERT( psa_generator_read( &generator,
3606 output_buffer,
3607 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3608
3609 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3610
3611exit:
3612 psa_generator_abort( &generator );
3613 psa_destroy_key( slot );
3614 mbedtls_psa_crypto_free( );
3615}
3616/* END_CASE */
3617
3618/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003619void derive_key_exercise( int alg_arg,
3620 data_t *key_data,
3621 data_t *salt,
3622 data_t *label,
3623 int derived_type_arg,
3624 int derived_bits_arg,
3625 int derived_usage_arg,
3626 int derived_alg_arg )
3627{
3628 psa_key_slot_t base_key = 1;
3629 psa_key_slot_t derived_key = 2;
3630 psa_algorithm_t alg = alg_arg;
3631 psa_key_type_t derived_type = derived_type_arg;
3632 size_t derived_bits = derived_bits_arg;
3633 psa_key_usage_t derived_usage = derived_usage_arg;
3634 psa_algorithm_t derived_alg = derived_alg_arg;
3635 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3636 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3637 psa_key_policy_t policy;
3638 psa_key_type_t got_type;
3639 size_t got_bits;
3640
3641 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3642
3643 psa_key_policy_init( &policy );
3644 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3645 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3646 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3647 key_data->x,
3648 key_data->len ) == PSA_SUCCESS );
3649
3650 /* Derive a key. */
3651 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3652 salt->x, salt->len,
3653 label->x, label->len,
3654 capacity ) == PSA_SUCCESS );
3655 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3656 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3657 TEST_ASSERT( psa_generator_import_key( derived_key,
3658 derived_type,
3659 derived_bits,
3660 &generator ) == PSA_SUCCESS );
3661
3662 /* Test the key information */
3663 TEST_ASSERT( psa_get_key_information( derived_key,
3664 &got_type,
3665 &got_bits ) == PSA_SUCCESS );
3666 TEST_ASSERT( got_type == derived_type );
3667 TEST_ASSERT( got_bits == derived_bits );
3668
3669 /* Exercise the derived key. */
3670 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3671 goto exit;
3672
3673exit:
3674 psa_generator_abort( &generator );
3675 psa_destroy_key( base_key );
3676 psa_destroy_key( derived_key );
3677 mbedtls_psa_crypto_free( );
3678}
3679/* END_CASE */
3680
3681/* BEGIN_CASE */
3682void derive_key_export( int alg_arg,
3683 data_t *key_data,
3684 data_t *salt,
3685 data_t *label,
3686 int bytes1_arg,
3687 int bytes2_arg )
3688{
3689 psa_key_slot_t base_key = 1;
3690 psa_key_slot_t derived_key = 2;
3691 psa_algorithm_t alg = alg_arg;
3692 size_t bytes1 = bytes1_arg;
3693 size_t bytes2 = bytes2_arg;
3694 size_t capacity = bytes1 + bytes2;
3695 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003696 uint8_t *output_buffer = NULL;
3697 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003698 psa_key_policy_t policy;
3699 size_t length;
3700
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003701 ASSERT_ALLOC( output_buffer, capacity );
3702 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003703 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3704
3705 psa_key_policy_init( &policy );
3706 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3707 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3708 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3709 key_data->x,
3710 key_data->len ) == PSA_SUCCESS );
3711
3712 /* Derive some material and output it. */
3713 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3714 salt->x, salt->len,
3715 label->x, label->len,
3716 capacity ) == PSA_SUCCESS );
3717 TEST_ASSERT( psa_generator_read( &generator,
3718 output_buffer,
3719 capacity ) == PSA_SUCCESS );
3720 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3721
3722 /* Derive the same output again, but this time store it in key objects. */
3723 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3724 salt->x, salt->len,
3725 label->x, label->len,
3726 capacity ) == PSA_SUCCESS );
3727 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3728 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3729 TEST_ASSERT( psa_generator_import_key( derived_key,
3730 PSA_KEY_TYPE_RAW_DATA,
3731 PSA_BYTES_TO_BITS( bytes1 ),
3732 &generator ) == PSA_SUCCESS );
3733 TEST_ASSERT( psa_export_key( derived_key,
3734 export_buffer, bytes1,
3735 &length ) == PSA_SUCCESS );
3736 TEST_ASSERT( length == bytes1 );
3737 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3738 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3739 TEST_ASSERT( psa_generator_import_key( derived_key,
3740 PSA_KEY_TYPE_RAW_DATA,
3741 PSA_BYTES_TO_BITS( bytes2 ),
3742 &generator ) == PSA_SUCCESS );
3743 TEST_ASSERT( psa_export_key( derived_key,
3744 export_buffer + bytes1, bytes2,
3745 &length ) == PSA_SUCCESS );
3746 TEST_ASSERT( length == bytes2 );
3747
3748 /* Compare the outputs from the two runs. */
3749 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3750
3751exit:
3752 mbedtls_free( output_buffer );
3753 mbedtls_free( export_buffer );
3754 psa_generator_abort( &generator );
3755 psa_destroy_key( base_key );
3756 psa_destroy_key( derived_key );
3757 mbedtls_psa_crypto_free( );
3758}
3759/* END_CASE */
3760
3761/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003762void key_agreement_setup( int alg_arg,
3763 int our_key_type_arg, data_t *our_key_data,
3764 data_t *peer_key_data,
3765 int expected_status_arg )
3766{
3767 psa_key_slot_t our_key = 1;
3768 psa_algorithm_t alg = alg_arg;
3769 psa_key_type_t our_key_type = our_key_type_arg;
3770 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3771 psa_key_policy_t policy;
3772
3773 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3774
3775 psa_key_policy_init( &policy );
3776 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3777 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3778 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3779 our_key_data->x,
3780 our_key_data->len ) == PSA_SUCCESS );
3781
3782 TEST_ASSERT( psa_key_agreement( &generator,
3783 our_key,
3784 peer_key_data->x, peer_key_data->len,
3785 alg ) == expected_status_arg );
3786
3787exit:
3788 psa_generator_abort( &generator );
3789 psa_destroy_key( our_key );
3790 mbedtls_psa_crypto_free( );
3791}
3792/* END_CASE */
3793
3794/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003795void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003796{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003797 size_t bytes = bytes_arg;
3798 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003799 unsigned char *output = NULL;
3800 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003801 size_t i;
3802 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003803
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003804 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3805 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003806 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003807
3808 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3809
Gilles Peskinea50d7392018-06-21 10:22:13 +02003810 /* Run several times, to ensure that every output byte will be
3811 * nonzero at least once with overwhelming probability
3812 * (2^(-8*number_of_runs)). */
3813 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003814 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003815 if( bytes != 0 )
3816 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003817 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3818
3819 /* Check that no more than bytes have been overwritten */
3820 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3821
3822 for( i = 0; i < bytes; i++ )
3823 {
3824 if( output[i] != 0 )
3825 ++changed[i];
3826 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003827 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003828
3829 /* Check that every byte was changed to nonzero at least once. This
3830 * validates that psa_generate_random is overwriting every byte of
3831 * the output buffer. */
3832 for( i = 0; i < bytes; i++ )
3833 {
3834 TEST_ASSERT( changed[i] != 0 );
3835 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003836
3837exit:
3838 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003839 mbedtls_free( output );
3840 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003841}
3842/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003843
3844/* BEGIN_CASE */
3845void generate_key( int type_arg,
3846 int bits_arg,
3847 int usage_arg,
3848 int alg_arg,
3849 int expected_status_arg )
3850{
3851 int slot = 1;
3852 psa_key_type_t type = type_arg;
3853 psa_key_usage_t usage = usage_arg;
3854 size_t bits = bits_arg;
3855 psa_algorithm_t alg = alg_arg;
3856 psa_status_t expected_status = expected_status_arg;
3857 psa_key_type_t got_type;
3858 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003859 psa_status_t expected_info_status =
3860 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3861 psa_key_policy_t policy;
3862
3863 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3864
3865 psa_key_policy_init( &policy );
3866 psa_key_policy_set_usage( &policy, usage, alg );
3867 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3868
3869 /* Generate a key */
3870 TEST_ASSERT( psa_generate_key( slot, type, bits,
3871 NULL, 0 ) == expected_status );
3872
3873 /* Test the key information */
3874 TEST_ASSERT( psa_get_key_information( slot,
3875 &got_type,
3876 &got_bits ) == expected_info_status );
3877 if( expected_info_status != PSA_SUCCESS )
3878 goto exit;
3879 TEST_ASSERT( got_type == type );
3880 TEST_ASSERT( got_bits == bits );
3881
Gilles Peskine818ca122018-06-20 18:16:48 +02003882 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003883 if( ! exercise_key( slot, usage, alg ) )
3884 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003885
3886exit:
3887 psa_destroy_key( slot );
3888 mbedtls_psa_crypto_free( );
3889}
3890/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003891
3892/* BEGIN_CASE */
3893void validate_module_init_generate_random( )
3894{
3895 psa_status_t status;
3896 uint8_t random[10] = { 0 };
3897 status = psa_generate_random( random, sizeof( random ) );
3898 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3899}
3900/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003901
3902/* BEGIN_CASE */
3903void validate_module_init_key_based( )
3904{
3905 psa_status_t status;
3906 uint8_t data[10] = { 0 };
3907 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3908 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3909}
3910/* END_CASE */