blob: 5685a61cf58908ab8c333398b24b3a45e34f3a93 [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:
Gilles Peskine1d7c0822018-10-08 19:05:22 +02001687 mbedtls_free( public_key );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001688 psa_generator_abort( &generator );
1689 psa_destroy_key( key_slot );
1690 mbedtls_psa_crypto_free( );
1691}
1692/* END_CASE */
1693
1694/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001695void agreement_key_policy( int policy_usage,
1696 int policy_alg,
1697 int key_type_arg,
1698 data_t *key_data,
1699 int exercise_alg )
1700{
1701 int key_slot = 1;
1702 psa_key_policy_t policy;
1703 psa_key_type_t key_type = key_type_arg;
1704 psa_key_type_t public_key_type;
1705 size_t key_bits;
1706 uint8_t *public_key = NULL;
1707 size_t public_key_length;
1708 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1709 psa_status_t status;
1710
1711 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1712
1713 psa_key_policy_init( &policy );
1714 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1715 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1716
1717 TEST_ASSERT( psa_import_key( key_slot, key_type,
1718 key_data->x, key_data->len ) == PSA_SUCCESS );
1719
1720 /* We need two keys to exercise key agreement. Exercise the
1721 * private key against its own public key. */
1722 TEST_ASSERT( psa_get_key_information( key_slot,
1723 &key_type,
1724 &key_bits ) == PSA_SUCCESS );
1725 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( key_type );
1726 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
1727 public_key = mbedtls_calloc( 1, public_key_length );
1728 TEST_ASSERT( public_key != NULL );
1729 TEST_ASSERT( psa_export_public_key( key_slot,
1730 public_key, public_key_length,
1731 &public_key_length ) == PSA_SUCCESS );
1732
1733 status = psa_key_agreement( &generator, key_slot,
1734 public_key, public_key_length,
1735 exercise_alg );
1736 if( policy_alg == exercise_alg &&
1737 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1738 TEST_ASSERT( status == PSA_SUCCESS );
1739 else
1740 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1741
1742exit:
1743 psa_generator_abort( &generator );
1744 psa_destroy_key( key_slot );
1745 mbedtls_psa_crypto_free( );
1746}
1747/* END_CASE */
1748
1749/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001750void key_lifetime( int lifetime_arg )
1751{
1752 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001753 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001754 unsigned char key[32] = {0};
1755 psa_key_lifetime_t lifetime_set = lifetime_arg;
1756 psa_key_lifetime_t lifetime_get;
1757
1758 memset( key, 0x2a, sizeof( key ) );
1759
1760 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1761
1762 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1763 lifetime_set ) == PSA_SUCCESS );
1764
1765 TEST_ASSERT( psa_import_key( key_slot, key_type,
1766 key, sizeof( key ) ) == PSA_SUCCESS );
1767
1768 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1769 &lifetime_get ) == PSA_SUCCESS );
1770
1771 TEST_ASSERT( lifetime_get == lifetime_set );
1772
1773exit:
1774 psa_destroy_key( key_slot );
1775 mbedtls_psa_crypto_free( );
1776}
1777/* END_CASE */
1778
1779/* BEGIN_CASE */
1780void key_lifetime_set_fail( int key_slot_arg,
1781 int lifetime_arg,
1782 int expected_status_arg )
1783{
1784 psa_key_slot_t key_slot = key_slot_arg;
1785 psa_key_lifetime_t lifetime_set = lifetime_arg;
1786 psa_status_t actual_status;
1787 psa_status_t expected_status = expected_status_arg;
1788
1789 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1790
1791 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1792
1793 if( actual_status == PSA_SUCCESS )
1794 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1795
1796 TEST_ASSERT( expected_status == actual_status );
1797
1798exit:
1799 psa_destroy_key( key_slot );
1800 mbedtls_psa_crypto_free( );
1801}
1802/* END_CASE */
1803
1804/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001805void hash_setup( int alg_arg,
1806 int expected_status_arg )
1807{
1808 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001809 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001810 psa_hash_operation_t operation;
1811 psa_status_t status;
1812
1813 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1814
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001815 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001816 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001817 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001818
1819exit:
1820 mbedtls_psa_crypto_free( );
1821}
1822/* END_CASE */
1823
1824/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001825void hash_bad_order( )
1826{
1827 unsigned char input[] = "";
1828 /* SHA-256 hash of an empty string */
1829 unsigned char hash[] = {
1830 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1831 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1832 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1833 size_t hash_len;
1834 psa_hash_operation_t operation;
1835
1836 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1837
1838 /* psa_hash_update without calling psa_hash_setup beforehand */
1839 memset( &operation, 0, sizeof( operation ) );
1840 TEST_ASSERT( psa_hash_update( &operation,
1841 input, sizeof( input ) ) ==
1842 PSA_ERROR_INVALID_ARGUMENT );
1843
1844 /* psa_hash_verify without calling psa_hash_setup beforehand */
1845 memset( &operation, 0, sizeof( operation ) );
1846 TEST_ASSERT( psa_hash_verify( &operation,
1847 hash, sizeof( hash ) ) ==
1848 PSA_ERROR_INVALID_ARGUMENT );
1849
1850 /* psa_hash_finish without calling psa_hash_setup beforehand */
1851 memset( &operation, 0, sizeof( operation ) );
1852 TEST_ASSERT( psa_hash_finish( &operation,
1853 hash, sizeof( hash ), &hash_len ) ==
1854 PSA_ERROR_INVALID_ARGUMENT );
1855
1856exit:
1857 mbedtls_psa_crypto_free( );
1858}
1859/* END_CASE */
1860
itayzafrir27e69452018-11-01 14:26:34 +02001861/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1862void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001863{
1864 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001865 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1866 * appended to it */
1867 unsigned char hash[] = {
1868 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1869 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1870 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001871 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001872 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001873
1874 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1875
itayzafrir27e69452018-11-01 14:26:34 +02001876 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001877 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1878 TEST_ASSERT( psa_hash_verify( &operation,
1879 hash, expected_size - 1 ) ==
1880 PSA_ERROR_INVALID_SIGNATURE );
1881
itayzafrir27e69452018-11-01 14:26:34 +02001882 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001883 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001884 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001885 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001886 PSA_ERROR_INVALID_SIGNATURE );
1887
itayzafrir27e69452018-11-01 14:26:34 +02001888 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001889 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001890 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001891 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001892 PSA_ERROR_INVALID_SIGNATURE );
1893
itayzafrirec93d302018-10-18 18:01:10 +03001894exit:
1895 mbedtls_psa_crypto_free( );
1896}
1897/* END_CASE */
1898
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001899/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1900void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001901{
1902 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001903 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001904 size_t expected_size = PSA_HASH_SIZE( alg );
1905 psa_hash_operation_t operation;
1906 size_t hash_len;
1907
1908 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1909
itayzafrir58028322018-10-25 10:22:01 +03001910 /* psa_hash_finish with a smaller hash buffer than expected */
1911 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1912 TEST_ASSERT( psa_hash_finish( &operation,
1913 hash, expected_size - 1,
1914 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1915
1916exit:
1917 mbedtls_psa_crypto_free( );
1918}
1919/* END_CASE */
1920
1921/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001922void mac_setup( int key_type_arg,
1923 data_t *key,
1924 int alg_arg,
1925 int expected_status_arg )
1926{
1927 int key_slot = 1;
1928 psa_key_type_t key_type = key_type_arg;
1929 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001930 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001931 psa_mac_operation_t operation;
1932 psa_key_policy_t policy;
1933 psa_status_t status;
1934
1935 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1936
1937 psa_key_policy_init( &policy );
1938 psa_key_policy_set_usage( &policy,
1939 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1940 alg );
1941 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1942
1943 TEST_ASSERT( psa_import_key( key_slot, key_type,
1944 key->x, key->len ) == PSA_SUCCESS );
1945
Gilles Peskine89167cb2018-07-08 20:12:23 +02001946 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001947 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001948 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001949
1950exit:
1951 psa_destroy_key( key_slot );
1952 mbedtls_psa_crypto_free( );
1953}
1954/* END_CASE */
1955
1956/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001957void mac_sign( int key_type_arg,
1958 data_t *key,
1959 int alg_arg,
1960 data_t *input,
1961 data_t *expected_mac )
1962{
1963 int key_slot = 1;
1964 psa_key_type_t key_type = key_type_arg;
1965 psa_algorithm_t alg = alg_arg;
1966 psa_mac_operation_t operation;
1967 psa_key_policy_t policy;
1968 /* Leave a little extra room in the output buffer. At the end of the
1969 * test, we'll check that the implementation didn't overwrite onto
1970 * this extra room. */
1971 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1972 size_t mac_buffer_size =
1973 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1974 size_t mac_length = 0;
1975
1976 memset( actual_mac, '+', sizeof( actual_mac ) );
1977 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1978 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1979
1980 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1981
1982 psa_key_policy_init( &policy );
1983 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1984 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1985
1986 TEST_ASSERT( psa_import_key( key_slot, key_type,
1987 key->x, key->len ) == PSA_SUCCESS );
1988
1989 /* Calculate the MAC. */
1990 TEST_ASSERT( psa_mac_sign_setup( &operation,
1991 key_slot, alg ) == PSA_SUCCESS );
1992 TEST_ASSERT( psa_mac_update( &operation,
1993 input->x, input->len ) == PSA_SUCCESS );
1994 TEST_ASSERT( psa_mac_sign_finish( &operation,
1995 actual_mac, mac_buffer_size,
1996 &mac_length ) == PSA_SUCCESS );
1997
1998 /* Compare with the expected value. */
1999 TEST_ASSERT( mac_length == expected_mac->len );
2000 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
2001
2002 /* Verify that the end of the buffer is untouched. */
2003 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2004 sizeof( actual_mac ) - mac_length ) );
2005
2006exit:
2007 psa_destroy_key( key_slot );
2008 mbedtls_psa_crypto_free( );
2009}
2010/* END_CASE */
2011
2012/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002013void mac_verify( int key_type_arg,
2014 data_t *key,
2015 int alg_arg,
2016 data_t *input,
2017 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018{
2019 int key_slot = 1;
2020 psa_key_type_t key_type = key_type_arg;
2021 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002023 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024
Gilles Peskine69c12672018-06-28 00:07:19 +02002025 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2026
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002029 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002030 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002031 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2032 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002033
2034 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2035
mohammad16036df908f2018-04-02 08:34:15 -07002036 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002037 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07002038 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2039
Gilles Peskine8c9def32018-02-08 10:02:12 +01002040 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002041 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002042
Gilles Peskine89167cb2018-07-08 20:12:23 +02002043 TEST_ASSERT( psa_mac_verify_setup( &operation,
2044 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
2046 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002047 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02002048 TEST_ASSERT( psa_mac_verify_finish( &operation,
2049 expected_mac->x,
2050 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002051
2052exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053 psa_destroy_key( key_slot );
2054 mbedtls_psa_crypto_free( );
2055}
2056/* END_CASE */
2057
2058/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002059void cipher_setup( int key_type_arg,
2060 data_t *key,
2061 int alg_arg,
2062 int expected_status_arg )
2063{
2064 int key_slot = 1;
2065 psa_key_type_t key_type = key_type_arg;
2066 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002067 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002068 psa_cipher_operation_t operation;
2069 psa_key_policy_t policy;
2070 psa_status_t status;
2071
2072 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2073
2074 psa_key_policy_init( &policy );
2075 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2076 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2077
2078 TEST_ASSERT( psa_import_key( key_slot, key_type,
2079 key->x, key->len ) == PSA_SUCCESS );
2080
Gilles Peskinefe119512018-07-08 21:39:34 +02002081 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002082 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002083 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002084
2085exit:
2086 psa_destroy_key( key_slot );
2087 mbedtls_psa_crypto_free( );
2088}
2089/* END_CASE */
2090
2091/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002092void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002093 data_t *key,
2094 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002095 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002096{
2097 int key_slot = 1;
2098 psa_status_t status;
2099 psa_key_type_t key_type = key_type_arg;
2100 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002101 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002102 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002103 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002104 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002105 size_t output_buffer_size = 0;
2106 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002107 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002108 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002109 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002110
Gilles Peskine50e586b2018-06-08 14:28:46 +02002111 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002112 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002114 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2115 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2116 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002117
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002118 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2119 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002120
2121 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2122
Moran Pekered346952018-07-05 15:22:45 +03002123 psa_key_policy_init( &policy );
2124 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2125 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2126
Gilles Peskine50e586b2018-06-08 14:28:46 +02002127 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002128 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002129
Gilles Peskinefe119512018-07-08 21:39:34 +02002130 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2131 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002132
Gilles Peskinefe119512018-07-08 21:39:34 +02002133 TEST_ASSERT( psa_cipher_set_iv( &operation,
2134 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002135 output_buffer_size = (size_t) input->len +
2136 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002137 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002138
Gilles Peskine4abf7412018-06-18 16:35:34 +02002139 TEST_ASSERT( psa_cipher_update( &operation,
2140 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002141 output, output_buffer_size,
2142 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002143 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002144 status = psa_cipher_finish( &operation,
2145 output + function_output_length,
2146 output_buffer_size,
2147 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002148 total_output_length += function_output_length;
2149
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002150 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151 if( expected_status == PSA_SUCCESS )
2152 {
2153 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002154 ASSERT_COMPARE( expected_output->x, expected_output->len,
2155 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002156 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002157
Gilles Peskine50e586b2018-06-08 14:28:46 +02002158exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002159 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002160 psa_destroy_key( key_slot );
2161 mbedtls_psa_crypto_free( );
2162}
2163/* END_CASE */
2164
2165/* BEGIN_CASE */
2166void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002167 data_t *key,
2168 data_t *input,
2169 int first_part_size,
2170 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002171{
2172 int key_slot = 1;
2173 psa_key_type_t key_type = key_type_arg;
2174 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002175 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002176 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002177 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002178 size_t output_buffer_size = 0;
2179 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002180 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002181 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002182 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002183
Gilles Peskine50e586b2018-06-08 14:28:46 +02002184 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002186 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002187 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2188 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2189 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002190
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002191 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2192 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002193
2194 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2195
Moran Pekered346952018-07-05 15:22:45 +03002196 psa_key_policy_init( &policy );
2197 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2198 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2199
Gilles Peskine50e586b2018-06-08 14:28:46 +02002200 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002201 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202
Gilles Peskinefe119512018-07-08 21:39:34 +02002203 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2204 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002205
Gilles Peskinefe119512018-07-08 21:39:34 +02002206 TEST_ASSERT( psa_cipher_set_iv( &operation,
2207 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002208 output_buffer_size = (size_t) input->len +
2209 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002210 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002211
Gilles Peskine4abf7412018-06-18 16:35:34 +02002212 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002213 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002214 output, output_buffer_size,
2215 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002216 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002217 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002218 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002219 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002220 output, output_buffer_size,
2221 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002222 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002223 TEST_ASSERT( psa_cipher_finish( &operation,
2224 output + function_output_length,
2225 output_buffer_size,
2226 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002227 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002228 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2229
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002230 ASSERT_COMPARE( expected_output->x, expected_output->len,
2231 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002232
2233exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002234 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002235 psa_destroy_key( key_slot );
2236 mbedtls_psa_crypto_free( );
2237}
2238/* END_CASE */
2239
2240/* BEGIN_CASE */
2241void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002242 data_t *key,
2243 data_t *input,
2244 int first_part_size,
2245 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002246{
2247 int key_slot = 1;
2248
2249 psa_key_type_t key_type = key_type_arg;
2250 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002251 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002252 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002253 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002254 size_t output_buffer_size = 0;
2255 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002256 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002257 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002258 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002259
Gilles Peskine50e586b2018-06-08 14:28:46 +02002260 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002261 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002263 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2264 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2265 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002266
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002267 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2268 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002269
2270 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2271
Moran Pekered346952018-07-05 15:22:45 +03002272 psa_key_policy_init( &policy );
2273 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2274 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2275
Gilles Peskine50e586b2018-06-08 14:28:46 +02002276 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002277 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002278
Gilles Peskinefe119512018-07-08 21:39:34 +02002279 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2280 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002281
Gilles Peskinefe119512018-07-08 21:39:34 +02002282 TEST_ASSERT( psa_cipher_set_iv( &operation,
2283 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002284
mohammad16033d91abe2018-07-03 13:15:54 +03002285 output_buffer_size = (size_t) input->len +
2286 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002287 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002288
Gilles Peskine4abf7412018-06-18 16:35:34 +02002289 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2290 TEST_ASSERT( psa_cipher_update( &operation,
2291 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002292 output, output_buffer_size,
2293 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002294 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002295 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002296 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002297 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002298 output, output_buffer_size,
2299 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002300 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002301 TEST_ASSERT( psa_cipher_finish( &operation,
2302 output + function_output_length,
2303 output_buffer_size,
2304 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002305 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002306 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2307
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002308 ASSERT_COMPARE( expected_output->x, expected_output->len,
2309 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002310
2311exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002312 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002313 psa_destroy_key( key_slot );
2314 mbedtls_psa_crypto_free( );
2315}
2316/* END_CASE */
2317
Gilles Peskine50e586b2018-06-08 14:28:46 +02002318/* BEGIN_CASE */
2319void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002320 data_t *key,
2321 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002322 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323{
2324 int key_slot = 1;
2325 psa_status_t status;
2326 psa_key_type_t key_type = key_type_arg;
2327 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002328 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002329 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002330 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002331 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002332 size_t output_buffer_size = 0;
2333 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002334 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002335 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002336 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002340 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002341 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2343 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002344
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002345 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2346 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002347
2348 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2349
Moran Pekered346952018-07-05 15:22:45 +03002350 psa_key_policy_init( &policy );
2351 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2352 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2353
Gilles Peskine50e586b2018-06-08 14:28:46 +02002354 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002355 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356
Gilles Peskinefe119512018-07-08 21:39:34 +02002357 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2358 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002359
Gilles Peskinefe119512018-07-08 21:39:34 +02002360 TEST_ASSERT( psa_cipher_set_iv( &operation,
2361 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002362
mohammad16033d91abe2018-07-03 13:15:54 +03002363 output_buffer_size = (size_t) input->len +
2364 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002365 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002366
Gilles Peskine4abf7412018-06-18 16:35:34 +02002367 TEST_ASSERT( psa_cipher_update( &operation,
2368 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002369 output, output_buffer_size,
2370 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002371 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002372 status = psa_cipher_finish( &operation,
2373 output + function_output_length,
2374 output_buffer_size,
2375 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002376 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002377 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378
2379 if( expected_status == PSA_SUCCESS )
2380 {
2381 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002382 ASSERT_COMPARE( expected_output->x, expected_output->len,
2383 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002384 }
2385
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002387 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002388 psa_destroy_key( key_slot );
2389 mbedtls_psa_crypto_free( );
2390}
2391/* END_CASE */
2392
Gilles Peskine50e586b2018-06-08 14:28:46 +02002393/* BEGIN_CASE */
2394void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002395 data_t *key,
2396 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002397{
2398 int key_slot = 1;
2399 psa_key_type_t key_type = key_type_arg;
2400 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002401 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002402 size_t iv_size = 16;
2403 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002404 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002405 size_t output1_size = 0;
2406 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002407 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002408 size_t output2_size = 0;
2409 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002410 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002411 psa_cipher_operation_t operation1;
2412 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002413 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002414
mohammad1603d7d7ba52018-03-12 18:51:53 +02002415 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002416 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002419
mohammad1603d7d7ba52018-03-12 18:51:53 +02002420 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2421
Moran Pekered346952018-07-05 15:22:45 +03002422 psa_key_policy_init( &policy );
2423 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2424 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2425
mohammad1603d7d7ba52018-03-12 18:51:53 +02002426 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002427 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002428
Gilles Peskinefe119512018-07-08 21:39:34 +02002429 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2430 key_slot, alg ) == PSA_SUCCESS );
2431 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2432 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002433
Gilles Peskinefe119512018-07-08 21:39:34 +02002434 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2435 iv, iv_size,
2436 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002437 output1_size = (size_t) input->len +
2438 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002439 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002440
Gilles Peskine4abf7412018-06-18 16:35:34 +02002441 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002442 output1, output1_size,
2443 &output1_length ) == PSA_SUCCESS );
2444 TEST_ASSERT( psa_cipher_finish( &operation1,
2445 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002446 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002447
Gilles Peskine048b7f02018-06-08 14:20:49 +02002448 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002449
2450 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2451
2452 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002453 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002454
Gilles Peskinefe119512018-07-08 21:39:34 +02002455 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2456 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002457 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2458 output2, output2_size,
2459 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002460 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002461 TEST_ASSERT( psa_cipher_finish( &operation2,
2462 output2 + output2_length,
2463 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002464 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002465
Gilles Peskine048b7f02018-06-08 14:20:49 +02002466 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002467
Janos Follath25c4fa82018-07-06 16:23:25 +01002468 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002469
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002470 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002471
2472exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002473 mbedtls_free( output1 );
2474 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002475 psa_destroy_key( key_slot );
2476 mbedtls_psa_crypto_free( );
2477}
2478/* END_CASE */
2479
2480/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481void cipher_verify_output_multipart( int alg_arg,
2482 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002483 data_t *key,
2484 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002485 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002486{
2487 int key_slot = 1;
2488 psa_key_type_t key_type = key_type_arg;
2489 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002490 unsigned char iv[16] = {0};
2491 size_t iv_size = 16;
2492 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002493 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002494 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002495 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002497 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002498 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002499 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002500 psa_cipher_operation_t operation1;
2501 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002502 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002503
Moran Pekerded84402018-06-06 16:36:50 +03002504 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002505 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002506 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2507 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002508
Moran Pekerded84402018-06-06 16:36:50 +03002509 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2510
Moran Pekered346952018-07-05 15:22:45 +03002511 psa_key_policy_init( &policy );
2512 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2513 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2514
Moran Pekerded84402018-06-06 16:36:50 +03002515 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002516 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002517
Gilles Peskinefe119512018-07-08 21:39:34 +02002518 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2519 key_slot, alg ) == PSA_SUCCESS );
2520 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2521 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002522
Gilles Peskinefe119512018-07-08 21:39:34 +02002523 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2524 iv, iv_size,
2525 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002526 output1_buffer_size = (size_t) input->len +
2527 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002528 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002529
Gilles Peskine4abf7412018-06-18 16:35:34 +02002530 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002531
itayzafrir3e02b3b2018-06-12 17:06:52 +03002532 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002533 output1, output1_buffer_size,
2534 &function_output_length ) == PSA_SUCCESS );
2535 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002536
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002537 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002538 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002539 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002540 output1, output1_buffer_size,
2541 &function_output_length ) == PSA_SUCCESS );
2542 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002543
Gilles Peskine048b7f02018-06-08 14:20:49 +02002544 TEST_ASSERT( psa_cipher_finish( &operation1,
2545 output1 + output1_length,
2546 output1_buffer_size - output1_length,
2547 &function_output_length ) == PSA_SUCCESS );
2548 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002549
2550 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2551
Gilles Peskine048b7f02018-06-08 14:20:49 +02002552 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002553 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002554
Gilles Peskinefe119512018-07-08 21:39:34 +02002555 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2556 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002557
2558 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002559 output2, output2_buffer_size,
2560 &function_output_length ) == PSA_SUCCESS );
2561 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002562
Gilles Peskine048b7f02018-06-08 14:20:49 +02002563 TEST_ASSERT( psa_cipher_update( &operation2,
2564 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002565 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002566 output2, output2_buffer_size,
2567 &function_output_length ) == PSA_SUCCESS );
2568 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002569
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002570 TEST_ASSERT( psa_cipher_finish( &operation2,
2571 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002572 output2_buffer_size - output2_length,
2573 &function_output_length ) == PSA_SUCCESS );
2574 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002575
Janos Follath25c4fa82018-07-06 16:23:25 +01002576 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002577
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002578 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002579
2580exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002581 mbedtls_free( output1 );
2582 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002583 psa_destroy_key( key_slot );
2584 mbedtls_psa_crypto_free( );
2585}
2586/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002587
Gilles Peskine20035e32018-02-03 22:44:14 +01002588/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002589void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002590 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002591 data_t *nonce,
2592 data_t *additional_data,
2593 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002594 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002595{
2596 int slot = 1;
2597 psa_key_type_t key_type = key_type_arg;
2598 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002599 unsigned char *output_data = NULL;
2600 size_t output_size = 0;
2601 size_t output_length = 0;
2602 unsigned char *output_data2 = NULL;
2603 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002604 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002605 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002606 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002607
Gilles Peskinea1cac842018-06-11 19:33:02 +02002608 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002609 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002610 TEST_ASSERT( nonce != NULL );
2611 TEST_ASSERT( additional_data != NULL );
2612 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2613 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2614 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2615 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2616
Gilles Peskine4abf7412018-06-18 16:35:34 +02002617 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002618 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002619
2620 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2621
2622 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002623 psa_key_policy_set_usage( &policy,
2624 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2625 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002626 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2627
2628 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002629 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002630
2631 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002632 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002633 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002634 additional_data->len,
2635 input_data->x, input_data->len,
2636 output_data, output_size,
2637 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002638
2639 if( PSA_SUCCESS == expected_result )
2640 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002641 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002642
2643 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002644 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002645 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002646 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002647 output_data, output_length,
2648 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002649 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002650
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002651 ASSERT_COMPARE( input_data->x, input_data->len,
2652 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002653 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002654
Gilles Peskinea1cac842018-06-11 19:33:02 +02002655exit:
2656 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002657 mbedtls_free( output_data );
2658 mbedtls_free( output_data2 );
2659 mbedtls_psa_crypto_free( );
2660}
2661/* END_CASE */
2662
2663/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002664void aead_encrypt( int key_type_arg, data_t *key_data,
2665 int alg_arg,
2666 data_t *nonce,
2667 data_t *additional_data,
2668 data_t *input_data,
2669 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002670{
2671 int slot = 1;
2672 psa_key_type_t key_type = key_type_arg;
2673 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002674 unsigned char *output_data = NULL;
2675 size_t output_size = 0;
2676 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002677 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002678 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002679
Gilles Peskinea1cac842018-06-11 19:33:02 +02002680 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002681 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002682 TEST_ASSERT( additional_data != NULL );
2683 TEST_ASSERT( nonce != NULL );
2684 TEST_ASSERT( expected_result != NULL );
2685 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2686 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2687 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2688 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2689 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2690
Gilles Peskine4abf7412018-06-18 16:35:34 +02002691 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002692 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002693
Gilles Peskinea1cac842018-06-11 19:33:02 +02002694 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2695
2696 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002697 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002698 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2699
2700 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002701 key_data->x,
2702 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002703
2704 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002705 nonce->x, nonce->len,
2706 additional_data->x, additional_data->len,
2707 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002708 output_data, output_size,
2709 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002710
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002711 ASSERT_COMPARE( expected_result->x, expected_result->len,
2712 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002713
Gilles Peskinea1cac842018-06-11 19:33:02 +02002714exit:
2715 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002717 mbedtls_psa_crypto_free( );
2718}
2719/* END_CASE */
2720
2721/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002722void aead_decrypt( int key_type_arg, data_t *key_data,
2723 int alg_arg,
2724 data_t *nonce,
2725 data_t *additional_data,
2726 data_t *input_data,
2727 data_t *expected_data,
2728 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002729{
2730 int slot = 1;
2731 psa_key_type_t key_type = key_type_arg;
2732 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002733 unsigned char *output_data = NULL;
2734 size_t output_size = 0;
2735 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002736 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002737 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002738 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002739
Gilles Peskinea1cac842018-06-11 19:33:02 +02002740 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002741 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002742 TEST_ASSERT( additional_data != NULL );
2743 TEST_ASSERT( nonce != NULL );
2744 TEST_ASSERT( expected_data != NULL );
2745 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2746 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2747 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2748 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2749 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2750
Gilles Peskine4abf7412018-06-18 16:35:34 +02002751 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002752 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002753
Gilles Peskinea1cac842018-06-11 19:33:02 +02002754 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2755
2756 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002757 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002758 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2759
2760 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002761 key_data->x,
2762 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002763
2764 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002765 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002766 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002767 additional_data->len,
2768 input_data->x, input_data->len,
2769 output_data, output_size,
2770 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771
Gilles Peskine2d277862018-06-18 15:41:12 +02002772 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002773 ASSERT_COMPARE( expected_data->x, expected_data->len,
2774 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002775
Gilles Peskinea1cac842018-06-11 19:33:02 +02002776exit:
2777 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002778 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002779 mbedtls_psa_crypto_free( );
2780}
2781/* END_CASE */
2782
2783/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002784void signature_size( int type_arg,
2785 int bits,
2786 int alg_arg,
2787 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002788{
2789 psa_key_type_t type = type_arg;
2790 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002791 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002792 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2793exit:
2794 ;
2795}
2796/* END_CASE */
2797
2798/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002799void sign_deterministic( int key_type_arg, data_t *key_data,
2800 int alg_arg, data_t *input_data,
2801 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002802{
2803 int slot = 1;
2804 psa_key_type_t key_type = key_type_arg;
2805 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002806 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002807 unsigned char *signature = NULL;
2808 size_t signature_size;
2809 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002810 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002811
Gilles Peskine20035e32018-02-03 22:44:14 +01002812 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002813 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002814 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002815 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2816 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2817 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002818
2819 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2820
mohammad1603a97cb8c2018-03-28 03:46:26 -07002821 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002822 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002823 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2824
Gilles Peskine20035e32018-02-03 22:44:14 +01002825 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002826 key_data->x,
2827 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002828 TEST_ASSERT( psa_get_key_information( slot,
2829 NULL,
2830 &key_bits ) == PSA_SUCCESS );
2831
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002832 /* Allocate a buffer which has the size advertized by the
2833 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002834 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2835 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002836 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002837 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002838 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002839
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002840 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002841 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002842 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002843 signature, signature_size,
2844 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002845 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002846 ASSERT_COMPARE( output_data->x, output_data->len,
2847 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002848
2849exit:
2850 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002851 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002852 mbedtls_psa_crypto_free( );
2853}
2854/* END_CASE */
2855
2856/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002857void sign_fail( int key_type_arg, data_t *key_data,
2858 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002859 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002860{
2861 int slot = 1;
2862 psa_key_type_t key_type = key_type_arg;
2863 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002864 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002865 psa_status_t actual_status;
2866 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002867 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002868 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002869 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002870
Gilles Peskine20035e32018-02-03 22:44:14 +01002871 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002872 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002873 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2874 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2875
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002876 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002877
2878 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2879
mohammad1603a97cb8c2018-03-28 03:46:26 -07002880 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002881 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002882 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2883
Gilles Peskine20035e32018-02-03 22:44:14 +01002884 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002885 key_data->x,
2886 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002887
2888 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002889 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002890 signature, signature_size,
2891 &signature_length );
2892 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002893 /* The value of *signature_length is unspecified on error, but
2894 * whatever it is, it should be less than signature_size, so that
2895 * if the caller tries to read *signature_length bytes without
2896 * checking the error code then they don't overflow a buffer. */
2897 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002898
2899exit:
2900 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002901 mbedtls_free( signature );
2902 mbedtls_psa_crypto_free( );
2903}
2904/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002905
2906/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002907void sign_verify( int key_type_arg, data_t *key_data,
2908 int alg_arg, data_t *input_data )
2909{
2910 int slot = 1;
2911 psa_key_type_t key_type = key_type_arg;
2912 psa_algorithm_t alg = alg_arg;
2913 size_t key_bits;
2914 unsigned char *signature = NULL;
2915 size_t signature_size;
2916 size_t signature_length = 0xdeadbeef;
2917 psa_key_policy_t policy;
2918
2919 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2920
2921 psa_key_policy_init( &policy );
2922 psa_key_policy_set_usage( &policy,
2923 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2924 alg );
2925 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2926
2927 TEST_ASSERT( psa_import_key( slot, key_type,
2928 key_data->x,
2929 key_data->len ) == PSA_SUCCESS );
2930 TEST_ASSERT( psa_get_key_information( slot,
2931 NULL,
2932 &key_bits ) == PSA_SUCCESS );
2933
2934 /* Allocate a buffer which has the size advertized by the
2935 * library. */
2936 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2937 key_bits, alg );
2938 TEST_ASSERT( signature_size != 0 );
2939 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002940 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002941
2942 /* Perform the signature. */
2943 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2944 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002945 signature, signature_size,
2946 &signature_length ) == PSA_SUCCESS );
2947 /* Check that the signature length looks sensible. */
2948 TEST_ASSERT( signature_length <= signature_size );
2949 TEST_ASSERT( signature_length > 0 );
2950
2951 /* Use the library to verify that the signature is correct. */
2952 TEST_ASSERT( psa_asymmetric_verify(
2953 slot, alg,
2954 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002955 signature, signature_length ) == PSA_SUCCESS );
2956
2957 if( input_data->len != 0 )
2958 {
2959 /* Flip a bit in the input and verify that the signature is now
2960 * detected as invalid. Flip a bit at the beginning, not at the end,
2961 * because ECDSA may ignore the last few bits of the input. */
2962 input_data->x[0] ^= 1;
2963 TEST_ASSERT( psa_asymmetric_verify(
2964 slot, alg,
2965 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002966 signature,
2967 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2968 }
2969
2970exit:
2971 psa_destroy_key( slot );
2972 mbedtls_free( signature );
2973 mbedtls_psa_crypto_free( );
2974}
2975/* END_CASE */
2976
2977/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002978void asymmetric_verify( int key_type_arg, data_t *key_data,
2979 int alg_arg, data_t *hash_data,
2980 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002981{
2982 int slot = 1;
2983 psa_key_type_t key_type = key_type_arg;
2984 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002985 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002986
Gilles Peskine69c12672018-06-28 00:07:19 +02002987 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2988
itayzafrir5c753392018-05-08 11:18:38 +03002989 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002990 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002991 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002992 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2993 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2994 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002995
2996 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2997
2998 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002999 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03003000 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3001
3002 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003003 key_data->x,
3004 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003005
3006 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003007 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003008 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003009 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003010exit:
3011 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03003012 mbedtls_psa_crypto_free( );
3013}
3014/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003015
3016/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003017void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3018 int alg_arg, data_t *hash_data,
3019 data_t *signature_data,
3020 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003021{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003022 int slot = 1;
3023 psa_key_type_t key_type = key_type_arg;
3024 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025 psa_status_t actual_status;
3026 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003027 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003029 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003030 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003031 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003032 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3033 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3034 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003035
3036 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3037
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003038 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003039 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003040 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3041
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003043 key_data->x,
3044 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003045
3046 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003047 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003048 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003049 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003051 TEST_ASSERT( actual_status == expected_status );
3052
3053exit:
3054 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003055 mbedtls_psa_crypto_free( );
3056}
3057/* END_CASE */
3058
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003059/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003060void asymmetric_encrypt( int key_type_arg,
3061 data_t *key_data,
3062 int alg_arg,
3063 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003064 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003065 int expected_output_length_arg,
3066 int expected_status_arg )
3067{
3068 int slot = 1;
3069 psa_key_type_t key_type = key_type_arg;
3070 psa_algorithm_t alg = alg_arg;
3071 size_t expected_output_length = expected_output_length_arg;
3072 size_t key_bits;
3073 unsigned char *output = NULL;
3074 size_t output_size;
3075 size_t output_length = ~0;
3076 psa_status_t actual_status;
3077 psa_status_t expected_status = expected_status_arg;
3078 psa_key_policy_t policy;
3079
3080 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3081
3082 /* Import the key */
3083 psa_key_policy_init( &policy );
3084 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
3085 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3086 TEST_ASSERT( psa_import_key( slot, key_type,
3087 key_data->x,
3088 key_data->len ) == PSA_SUCCESS );
3089
3090 /* Determine the maximum output length */
3091 TEST_ASSERT( psa_get_key_information( slot,
3092 NULL,
3093 &key_bits ) == PSA_SUCCESS );
3094 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003095 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003096
3097 /* Encrypt the input */
3098 actual_status = psa_asymmetric_encrypt( slot, alg,
3099 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003100 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003101 output, output_size,
3102 &output_length );
3103 TEST_ASSERT( actual_status == expected_status );
3104 TEST_ASSERT( output_length == expected_output_length );
3105
Gilles Peskine68428122018-06-30 18:42:41 +02003106 /* If the label is empty, the test framework puts a non-null pointer
3107 * in label->x. Test that a null pointer works as well. */
3108 if( label->len == 0 )
3109 {
3110 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003111 if( output_size != 0 )
3112 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003113 actual_status = psa_asymmetric_encrypt( slot, alg,
3114 input_data->x, input_data->len,
3115 NULL, label->len,
3116 output, output_size,
3117 &output_length );
3118 TEST_ASSERT( actual_status == expected_status );
3119 TEST_ASSERT( output_length == expected_output_length );
3120 }
3121
Gilles Peskine656896e2018-06-29 19:12:28 +02003122exit:
3123 psa_destroy_key( slot );
3124 mbedtls_free( output );
3125 mbedtls_psa_crypto_free( );
3126}
3127/* END_CASE */
3128
3129/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003130void asymmetric_encrypt_decrypt( int key_type_arg,
3131 data_t *key_data,
3132 int alg_arg,
3133 data_t *input_data,
3134 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135{
3136 int slot = 1;
3137 psa_key_type_t key_type = key_type_arg;
3138 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003139 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003141 size_t output_size;
3142 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003143 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003144 size_t output2_size;
3145 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003146 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003149 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003150 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3151 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3152
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003153 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3154
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003155 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003156 psa_key_policy_set_usage( &policy,
3157 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003158 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003159 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3160
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003162 key_data->x,
3163 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003165
3166 /* Determine the maximum ciphertext length */
3167 TEST_ASSERT( psa_get_key_information( slot,
3168 NULL,
3169 &key_bits ) == PSA_SUCCESS );
3170 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003171 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003172 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003173 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003174
Gilles Peskineeebd7382018-06-08 18:11:54 +02003175 /* We test encryption by checking that encrypt-then-decrypt gives back
3176 * the original plaintext because of the non-optional random
3177 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02003178 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003179 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003180 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003181 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003182 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003183 /* We don't know what ciphertext length to expect, but check that
3184 * it looks sensible. */
3185 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003186
Gilles Peskine2d277862018-06-18 15:41:12 +02003187 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003188 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003189 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003190 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003191 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003192 ASSERT_COMPARE( input_data->x, input_data->len,
3193 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194
3195exit:
3196 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003197 mbedtls_free( output );
3198 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003200}
3201/* END_CASE */
3202
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003204void asymmetric_decrypt( int key_type_arg,
3205 data_t *key_data,
3206 int alg_arg,
3207 data_t *input_data,
3208 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003209 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003210{
3211 int slot = 1;
3212 psa_key_type_t key_type = key_type_arg;
3213 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003215 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003216 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003217 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003218
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003220 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003222 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3223 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3224 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3225
Gilles Peskine4abf7412018-06-18 16:35:34 +02003226 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003227 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003228
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3230
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003231 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003232 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003233 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3234
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003236 key_data->x,
3237 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003239 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003240 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003241 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003242 output,
3243 output_size,
3244 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003245 ASSERT_COMPARE( expected_data->x, expected_data->len,
3246 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247
Gilles Peskine68428122018-06-30 18:42:41 +02003248 /* If the label is empty, the test framework puts a non-null pointer
3249 * in label->x. Test that a null pointer works as well. */
3250 if( label->len == 0 )
3251 {
3252 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003253 if( output_size != 0 )
3254 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003255 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3256 input_data->x, input_data->len,
3257 NULL, label->len,
3258 output,
3259 output_size,
3260 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003261 ASSERT_COMPARE( expected_data->x, expected_data->len,
3262 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003263 }
3264
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265exit:
3266 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003267 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003268 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003269}
3270/* END_CASE */
3271
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003273void asymmetric_decrypt_fail( int key_type_arg,
3274 data_t *key_data,
3275 int alg_arg,
3276 data_t *input_data,
3277 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003278 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003280 int slot = 1;
3281 psa_key_type_t key_type = key_type_arg;
3282 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003284 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003285 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286 psa_status_t actual_status;
3287 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003288 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003290 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003292 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3293 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3294
Gilles Peskine4abf7412018-06-18 16:35:34 +02003295 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003296 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003297
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3299
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003300 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003301 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003302 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3303
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003305 key_data->x,
3306 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003307
Gilles Peskine2d277862018-06-18 15:41:12 +02003308 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003309 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003310 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003311 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003312 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003313 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003314 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315
Gilles Peskine68428122018-06-30 18:42:41 +02003316 /* If the label is empty, the test framework puts a non-null pointer
3317 * in label->x. Test that a null pointer works as well. */
3318 if( label->len == 0 )
3319 {
3320 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003321 if( output_size != 0 )
3322 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003323 actual_status = psa_asymmetric_decrypt( slot, alg,
3324 input_data->x, input_data->len,
3325 NULL, label->len,
3326 output, output_size,
3327 &output_length );
3328 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003329 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003330 }
3331
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332exit:
3333 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003334 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335 mbedtls_psa_crypto_free( );
3336}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003337/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003338
3339/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003340void derive_setup( int key_type_arg,
3341 data_t *key_data,
3342 int alg_arg,
3343 data_t *salt,
3344 data_t *label,
3345 int requested_capacity_arg,
3346 int expected_status_arg )
3347{
3348 psa_key_slot_t slot = 1;
3349 size_t key_type = key_type_arg;
3350 psa_algorithm_t alg = alg_arg;
3351 size_t requested_capacity = requested_capacity_arg;
3352 psa_status_t expected_status = expected_status_arg;
3353 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3354 psa_key_policy_t policy;
3355
3356 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3357
3358 psa_key_policy_init( &policy );
3359 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3360 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3361
3362 TEST_ASSERT( psa_import_key( slot, key_type,
3363 key_data->x,
3364 key_data->len ) == PSA_SUCCESS );
3365
3366 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3367 salt->x, salt->len,
3368 label->x, label->len,
3369 requested_capacity ) == expected_status );
3370
3371exit:
3372 psa_generator_abort( &generator );
3373 psa_destroy_key( slot );
3374 mbedtls_psa_crypto_free( );
3375}
3376/* END_CASE */
3377
3378/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003379void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003380{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003381 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003382 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003383 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003384 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003385 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003386 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003387 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3388 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3389 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003390 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003391
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003392 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3393
3394 psa_key_policy_init( &policy );
3395 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3396 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3397
3398 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003399 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003400 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003401
3402 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003403 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003404 NULL, 0,
3405 NULL, 0,
3406 capacity ) == PSA_SUCCESS );
3407
3408 /* state of generator shouldn't allow additional generation */
3409 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3410 NULL, 0,
3411 NULL, 0,
3412 capacity ) == PSA_ERROR_BAD_STATE );
3413
3414 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3415 == PSA_SUCCESS );
3416
3417 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3418 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3419
3420
3421exit:
3422 psa_generator_abort( &generator );
3423 psa_destroy_key( base_key );
3424 mbedtls_psa_crypto_free( );
3425}
3426/* END_CASE */
3427
3428
3429/* BEGIN_CASE */
3430void test_derive_invalid_generator_tests( )
3431{
3432 uint8_t output_buffer[16];
3433 size_t buffer_size = 16;
3434 size_t capacity = 0;
3435 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3436
Nir Sonnenschein50789302018-10-31 12:16:38 +02003437 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003438 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003439
3440 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003441 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003442
Nir Sonnenschein50789302018-10-31 12:16:38 +02003443 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003444
Nir Sonnenschein50789302018-10-31 12:16:38 +02003445 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003446 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003447
Nir Sonnenschein50789302018-10-31 12:16:38 +02003448 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003449 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003450
3451exit:
3452 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003453}
3454/* END_CASE */
3455
3456/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003457void derive_output( int alg_arg,
3458 data_t *key_data,
3459 data_t *salt,
3460 data_t *label,
3461 int requested_capacity_arg,
3462 data_t *expected_output1,
3463 data_t *expected_output2 )
3464{
3465 psa_key_slot_t slot = 1;
3466 psa_algorithm_t alg = alg_arg;
3467 size_t requested_capacity = requested_capacity_arg;
3468 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3469 uint8_t *expected_outputs[2] =
3470 {expected_output1->x, expected_output2->x};
3471 size_t output_sizes[2] =
3472 {expected_output1->len, expected_output2->len};
3473 size_t output_buffer_size = 0;
3474 uint8_t *output_buffer = NULL;
3475 size_t expected_capacity;
3476 size_t current_capacity;
3477 psa_key_policy_t policy;
3478 psa_status_t status;
3479 unsigned i;
3480
3481 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3482 {
3483 if( output_sizes[i] > output_buffer_size )
3484 output_buffer_size = output_sizes[i];
3485 if( output_sizes[i] == 0 )
3486 expected_outputs[i] = NULL;
3487 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003488 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003489 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3490
3491 psa_key_policy_init( &policy );
3492 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3493 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3494
3495 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3496 key_data->x,
3497 key_data->len ) == PSA_SUCCESS );
3498
3499 /* Extraction phase. */
3500 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3501 salt->x, salt->len,
3502 label->x, label->len,
3503 requested_capacity ) == PSA_SUCCESS );
3504 TEST_ASSERT( psa_get_generator_capacity( &generator,
3505 &current_capacity ) ==
3506 PSA_SUCCESS );
3507 TEST_ASSERT( current_capacity == requested_capacity );
3508 expected_capacity = requested_capacity;
3509
3510 /* Expansion phase. */
3511 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3512 {
3513 /* Read some bytes. */
3514 status = psa_generator_read( &generator,
3515 output_buffer, output_sizes[i] );
3516 if( expected_capacity == 0 && output_sizes[i] == 0 )
3517 {
3518 /* Reading 0 bytes when 0 bytes are available can go either way. */
3519 TEST_ASSERT( status == PSA_SUCCESS ||
3520 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3521 continue;
3522 }
3523 else if( expected_capacity == 0 ||
3524 output_sizes[i] > expected_capacity )
3525 {
3526 /* Capacity exceeded. */
3527 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3528 expected_capacity = 0;
3529 continue;
3530 }
3531 /* Success. Check the read data. */
3532 TEST_ASSERT( status == PSA_SUCCESS );
3533 if( output_sizes[i] != 0 )
3534 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3535 output_sizes[i] ) == 0 );
3536 /* Check the generator status. */
3537 expected_capacity -= output_sizes[i];
3538 TEST_ASSERT( psa_get_generator_capacity( &generator,
3539 &current_capacity ) ==
3540 PSA_SUCCESS );
3541 TEST_ASSERT( expected_capacity == current_capacity );
3542 }
3543 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3544
3545exit:
3546 mbedtls_free( output_buffer );
3547 psa_generator_abort( &generator );
3548 psa_destroy_key( slot );
3549 mbedtls_psa_crypto_free( );
3550}
3551/* END_CASE */
3552
3553/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003554void derive_full( int alg_arg,
3555 data_t *key_data,
3556 data_t *salt,
3557 data_t *label,
3558 int requested_capacity_arg )
3559{
3560 psa_key_slot_t slot = 1;
3561 psa_algorithm_t alg = alg_arg;
3562 size_t requested_capacity = requested_capacity_arg;
3563 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3564 unsigned char output_buffer[16];
3565 size_t expected_capacity = requested_capacity;
3566 size_t current_capacity;
3567 psa_key_policy_t policy;
3568
3569 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3570
3571 psa_key_policy_init( &policy );
3572 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3573 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3574
3575 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3576 key_data->x,
3577 key_data->len ) == PSA_SUCCESS );
3578
3579 /* Extraction phase. */
3580 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3581 salt->x, salt->len,
3582 label->x, label->len,
3583 requested_capacity ) == PSA_SUCCESS );
3584 TEST_ASSERT( psa_get_generator_capacity( &generator,
3585 &current_capacity ) ==
3586 PSA_SUCCESS );
3587 TEST_ASSERT( current_capacity == expected_capacity );
3588
3589 /* Expansion phase. */
3590 while( current_capacity > 0 )
3591 {
3592 size_t read_size = sizeof( output_buffer );
3593 if( read_size > current_capacity )
3594 read_size = current_capacity;
3595 TEST_ASSERT( psa_generator_read( &generator,
3596 output_buffer,
3597 read_size ) == PSA_SUCCESS );
3598 expected_capacity -= read_size;
3599 TEST_ASSERT( psa_get_generator_capacity( &generator,
3600 &current_capacity ) ==
3601 PSA_SUCCESS );
3602 TEST_ASSERT( current_capacity == expected_capacity );
3603 }
3604
3605 /* Check that the generator refuses to go over capacity. */
3606 TEST_ASSERT( psa_generator_read( &generator,
3607 output_buffer,
3608 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3609
3610 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3611
3612exit:
3613 psa_generator_abort( &generator );
3614 psa_destroy_key( slot );
3615 mbedtls_psa_crypto_free( );
3616}
3617/* END_CASE */
3618
3619/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003620void derive_key_exercise( int alg_arg,
3621 data_t *key_data,
3622 data_t *salt,
3623 data_t *label,
3624 int derived_type_arg,
3625 int derived_bits_arg,
3626 int derived_usage_arg,
3627 int derived_alg_arg )
3628{
3629 psa_key_slot_t base_key = 1;
3630 psa_key_slot_t derived_key = 2;
3631 psa_algorithm_t alg = alg_arg;
3632 psa_key_type_t derived_type = derived_type_arg;
3633 size_t derived_bits = derived_bits_arg;
3634 psa_key_usage_t derived_usage = derived_usage_arg;
3635 psa_algorithm_t derived_alg = derived_alg_arg;
3636 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3637 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3638 psa_key_policy_t policy;
3639 psa_key_type_t got_type;
3640 size_t got_bits;
3641
3642 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3643
3644 psa_key_policy_init( &policy );
3645 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3646 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3647 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3648 key_data->x,
3649 key_data->len ) == PSA_SUCCESS );
3650
3651 /* Derive a key. */
3652 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3653 salt->x, salt->len,
3654 label->x, label->len,
3655 capacity ) == PSA_SUCCESS );
3656 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3657 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3658 TEST_ASSERT( psa_generator_import_key( derived_key,
3659 derived_type,
3660 derived_bits,
3661 &generator ) == PSA_SUCCESS );
3662
3663 /* Test the key information */
3664 TEST_ASSERT( psa_get_key_information( derived_key,
3665 &got_type,
3666 &got_bits ) == PSA_SUCCESS );
3667 TEST_ASSERT( got_type == derived_type );
3668 TEST_ASSERT( got_bits == derived_bits );
3669
3670 /* Exercise the derived key. */
3671 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3672 goto exit;
3673
3674exit:
3675 psa_generator_abort( &generator );
3676 psa_destroy_key( base_key );
3677 psa_destroy_key( derived_key );
3678 mbedtls_psa_crypto_free( );
3679}
3680/* END_CASE */
3681
3682/* BEGIN_CASE */
3683void derive_key_export( int alg_arg,
3684 data_t *key_data,
3685 data_t *salt,
3686 data_t *label,
3687 int bytes1_arg,
3688 int bytes2_arg )
3689{
3690 psa_key_slot_t base_key = 1;
3691 psa_key_slot_t derived_key = 2;
3692 psa_algorithm_t alg = alg_arg;
3693 size_t bytes1 = bytes1_arg;
3694 size_t bytes2 = bytes2_arg;
3695 size_t capacity = bytes1 + bytes2;
3696 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003697 uint8_t *output_buffer = NULL;
3698 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003699 psa_key_policy_t policy;
3700 size_t length;
3701
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003702 ASSERT_ALLOC( output_buffer, capacity );
3703 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003704 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3705
3706 psa_key_policy_init( &policy );
3707 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3708 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3709 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3710 key_data->x,
3711 key_data->len ) == PSA_SUCCESS );
3712
3713 /* Derive some material and output it. */
3714 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3715 salt->x, salt->len,
3716 label->x, label->len,
3717 capacity ) == PSA_SUCCESS );
3718 TEST_ASSERT( psa_generator_read( &generator,
3719 output_buffer,
3720 capacity ) == PSA_SUCCESS );
3721 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3722
3723 /* Derive the same output again, but this time store it in key objects. */
3724 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3725 salt->x, salt->len,
3726 label->x, label->len,
3727 capacity ) == PSA_SUCCESS );
3728 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3729 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3730 TEST_ASSERT( psa_generator_import_key( derived_key,
3731 PSA_KEY_TYPE_RAW_DATA,
3732 PSA_BYTES_TO_BITS( bytes1 ),
3733 &generator ) == PSA_SUCCESS );
3734 TEST_ASSERT( psa_export_key( derived_key,
3735 export_buffer, bytes1,
3736 &length ) == PSA_SUCCESS );
3737 TEST_ASSERT( length == bytes1 );
3738 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3739 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3740 TEST_ASSERT( psa_generator_import_key( derived_key,
3741 PSA_KEY_TYPE_RAW_DATA,
3742 PSA_BYTES_TO_BITS( bytes2 ),
3743 &generator ) == PSA_SUCCESS );
3744 TEST_ASSERT( psa_export_key( derived_key,
3745 export_buffer + bytes1, bytes2,
3746 &length ) == PSA_SUCCESS );
3747 TEST_ASSERT( length == bytes2 );
3748
3749 /* Compare the outputs from the two runs. */
3750 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3751
3752exit:
3753 mbedtls_free( output_buffer );
3754 mbedtls_free( export_buffer );
3755 psa_generator_abort( &generator );
3756 psa_destroy_key( base_key );
3757 psa_destroy_key( derived_key );
3758 mbedtls_psa_crypto_free( );
3759}
3760/* END_CASE */
3761
3762/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003763void key_agreement_setup( int alg_arg,
3764 int our_key_type_arg, data_t *our_key_data,
3765 data_t *peer_key_data,
3766 int expected_status_arg )
3767{
3768 psa_key_slot_t our_key = 1;
3769 psa_algorithm_t alg = alg_arg;
3770 psa_key_type_t our_key_type = our_key_type_arg;
3771 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3772 psa_key_policy_t policy;
3773
3774 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3775
3776 psa_key_policy_init( &policy );
3777 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3778 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3779 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3780 our_key_data->x,
3781 our_key_data->len ) == PSA_SUCCESS );
3782
3783 TEST_ASSERT( psa_key_agreement( &generator,
3784 our_key,
3785 peer_key_data->x, peer_key_data->len,
3786 alg ) == expected_status_arg );
3787
3788exit:
3789 psa_generator_abort( &generator );
3790 psa_destroy_key( our_key );
3791 mbedtls_psa_crypto_free( );
3792}
3793/* END_CASE */
3794
3795/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003796void key_agreement_capacity( int alg_arg,
3797 int our_key_type_arg, data_t *our_key_data,
3798 data_t *peer_key_data,
3799 int expected_capacity_arg )
3800{
3801 psa_key_slot_t our_key = 1;
3802 psa_algorithm_t alg = alg_arg;
3803 psa_key_type_t our_key_type = our_key_type_arg;
3804 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3805 psa_key_policy_t policy;
3806 size_t actual_capacity;
3807
3808 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3809
3810 psa_key_policy_init( &policy );
3811 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3812 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3813 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3814 our_key_data->x,
3815 our_key_data->len ) == PSA_SUCCESS );
3816
3817 TEST_ASSERT( psa_key_agreement( &generator,
3818 our_key,
3819 peer_key_data->x, peer_key_data->len,
3820 alg ) == PSA_SUCCESS );
3821
3822 TEST_ASSERT( psa_get_generator_capacity(
3823 &generator, &actual_capacity ) == PSA_SUCCESS );
3824 TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg );
3825
3826exit:
3827 psa_generator_abort( &generator );
3828 psa_destroy_key( our_key );
3829 mbedtls_psa_crypto_free( );
3830}
3831/* END_CASE */
3832
3833/* BEGIN_CASE */
3834void key_agreement_output( int alg_arg,
3835 int our_key_type_arg, data_t *our_key_data,
3836 data_t *peer_key_data,
3837 data_t *expected_output )
3838{
3839 psa_key_slot_t our_key = 1;
3840 psa_algorithm_t alg = alg_arg;
3841 psa_key_type_t our_key_type = our_key_type_arg;
3842 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3843 psa_key_policy_t policy;
3844 uint8_t *actual_output = mbedtls_calloc( 1, expected_output->len );
3845
3846 TEST_ASSERT( actual_output != NULL );
3847
3848 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3849
3850 psa_key_policy_init( &policy );
3851 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3852 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3853 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3854 our_key_data->x,
3855 our_key_data->len ) == PSA_SUCCESS );
3856
3857 TEST_ASSERT( psa_key_agreement( &generator,
3858 our_key,
3859 peer_key_data->x, peer_key_data->len,
3860 alg ) == PSA_SUCCESS );
3861
3862 TEST_ASSERT( psa_generator_read( &generator,
3863 actual_output,
3864 expected_output->len ) == PSA_SUCCESS );
3865 TEST_ASSERT( memcmp( actual_output, expected_output->x,
3866 expected_output->len ) == 0 );
3867
3868exit:
3869 psa_generator_abort( &generator );
3870 psa_destroy_key( our_key );
3871 mbedtls_psa_crypto_free( );
3872 mbedtls_free( actual_output );
3873}
3874/* END_CASE */
3875
3876/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003877void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003878{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003879 size_t bytes = bytes_arg;
3880 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003881 unsigned char *output = NULL;
3882 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003883 size_t i;
3884 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003885
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003886 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3887 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003888 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003889
3890 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3891
Gilles Peskinea50d7392018-06-21 10:22:13 +02003892 /* Run several times, to ensure that every output byte will be
3893 * nonzero at least once with overwhelming probability
3894 * (2^(-8*number_of_runs)). */
3895 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003896 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003897 if( bytes != 0 )
3898 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003899 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3900
3901 /* Check that no more than bytes have been overwritten */
3902 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3903
3904 for( i = 0; i < bytes; i++ )
3905 {
3906 if( output[i] != 0 )
3907 ++changed[i];
3908 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003909 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003910
3911 /* Check that every byte was changed to nonzero at least once. This
3912 * validates that psa_generate_random is overwriting every byte of
3913 * the output buffer. */
3914 for( i = 0; i < bytes; i++ )
3915 {
3916 TEST_ASSERT( changed[i] != 0 );
3917 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003918
3919exit:
3920 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003921 mbedtls_free( output );
3922 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003923}
3924/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003925
3926/* BEGIN_CASE */
3927void generate_key( int type_arg,
3928 int bits_arg,
3929 int usage_arg,
3930 int alg_arg,
3931 int expected_status_arg )
3932{
3933 int slot = 1;
3934 psa_key_type_t type = type_arg;
3935 psa_key_usage_t usage = usage_arg;
3936 size_t bits = bits_arg;
3937 psa_algorithm_t alg = alg_arg;
3938 psa_status_t expected_status = expected_status_arg;
3939 psa_key_type_t got_type;
3940 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003941 psa_status_t expected_info_status =
3942 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3943 psa_key_policy_t policy;
3944
3945 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3946
3947 psa_key_policy_init( &policy );
3948 psa_key_policy_set_usage( &policy, usage, alg );
3949 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3950
3951 /* Generate a key */
3952 TEST_ASSERT( psa_generate_key( slot, type, bits,
3953 NULL, 0 ) == expected_status );
3954
3955 /* Test the key information */
3956 TEST_ASSERT( psa_get_key_information( slot,
3957 &got_type,
3958 &got_bits ) == expected_info_status );
3959 if( expected_info_status != PSA_SUCCESS )
3960 goto exit;
3961 TEST_ASSERT( got_type == type );
3962 TEST_ASSERT( got_bits == bits );
3963
Gilles Peskine818ca122018-06-20 18:16:48 +02003964 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003965 if( ! exercise_key( slot, usage, alg ) )
3966 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003967
3968exit:
3969 psa_destroy_key( slot );
3970 mbedtls_psa_crypto_free( );
3971}
3972/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003973
3974/* BEGIN_CASE */
3975void validate_module_init_generate_random( )
3976{
3977 psa_status_t status;
3978 uint8_t random[10] = { 0 };
3979 status = psa_generate_random( random, sizeof( random ) );
3980 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3981}
3982/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003983
3984/* BEGIN_CASE */
3985void validate_module_init_key_based( )
3986{
3987 psa_status_t status;
3988 uint8_t data[10] = { 0 };
3989 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3990 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3991}
3992/* END_CASE */