blob: 2245cfd34f69894bcf32fb8a27d780a7e7fd9aa9 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinefc411f12018-10-25 22:34:48 +020014#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
15
Gilles Peskine96ee5c72018-07-12 17:24:54 +020016#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
17
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#else
Gilles Peskine2d277862018-06-18 15:41:12 +020021#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030022#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023
Jaeden Amerof24c7f82018-06-27 17:20:43 +010024/** An invalid export length that will never be set by psa_export_key(). */
25static const size_t INVALID_EXPORT_LENGTH = ~0U;
26
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027/** Test if a buffer contains a constant byte value.
28 *
29 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020030 *
31 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020032 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 * \param size Size of the buffer in bytes.
34 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020035 * \return 1 if the buffer is all-bits-zero.
36 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039{
40 size_t i;
41 for( i = 0; i < size; i++ )
42 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020043 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020046 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020047}
Gilles Peskine818ca122018-06-20 18:16:48 +020048
Gilles Peskine0b352bc2018-06-28 00:16:11 +020049/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
50static int asn1_write_10x( unsigned char **p,
51 unsigned char *start,
52 size_t bits,
53 unsigned char x )
54{
55 int ret;
56 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020057 if( bits == 0 )
58 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
59 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030061 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020062 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
63 *p -= len;
64 ( *p )[len-1] = x;
65 if( bits % 8 == 0 )
66 ( *p )[1] |= 1;
67 else
68 ( *p )[0] |= 1 << ( bits % 8 );
69 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
70 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
71 MBEDTLS_ASN1_INTEGER ) );
72 return( len );
73}
74
75static int construct_fake_rsa_key( unsigned char *buffer,
76 size_t buffer_size,
77 unsigned char **p,
78 size_t bits,
79 int keypair )
80{
81 size_t half_bits = ( bits + 1 ) / 2;
82 int ret;
83 int len = 0;
84 /* Construct something that looks like a DER encoding of
85 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
86 * RSAPrivateKey ::= SEQUENCE {
87 * version Version,
88 * modulus INTEGER, -- n
89 * publicExponent INTEGER, -- e
90 * privateExponent INTEGER, -- d
91 * prime1 INTEGER, -- p
92 * prime2 INTEGER, -- q
93 * exponent1 INTEGER, -- d mod (p-1)
94 * exponent2 INTEGER, -- d mod (q-1)
95 * coefficient INTEGER, -- (inverse of q) mod p
96 * otherPrimeInfos OtherPrimeInfos OPTIONAL
97 * }
98 * Or, for a public key, the same structure with only
99 * version, modulus and publicExponent.
100 */
101 *p = buffer + buffer_size;
102 if( keypair )
103 {
104 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* q */
111 asn1_write_10x( p, buffer, half_bits, 1 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
113 asn1_write_10x( p, buffer, half_bits, 3 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* d */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 }
117 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
118 asn1_write_10x( p, buffer, 17, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* n */
120 asn1_write_10x( p, buffer, bits, 1 ) );
121 if( keypair )
122 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
123 mbedtls_asn1_write_int( p, buffer, 0 ) );
124 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
125 {
126 const unsigned char tag =
127 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
128 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
129 }
130 return( len );
131}
132
Gilles Peskine818ca122018-06-20 18:16:48 +0200133static int exercise_mac_key( psa_key_slot_t key,
134 psa_key_usage_t usage,
135 psa_algorithm_t alg )
136{
137 psa_mac_operation_t operation;
138 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200139 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200140 size_t mac_length = sizeof( mac );
141
142 if( usage & PSA_KEY_USAGE_SIGN )
143 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200144 TEST_ASSERT( psa_mac_sign_setup( &operation,
145 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200146 TEST_ASSERT( psa_mac_update( &operation,
147 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200149 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200150 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200151 }
152
153 if( usage & PSA_KEY_USAGE_VERIFY )
154 {
155 psa_status_t verify_status =
156 ( usage & PSA_KEY_USAGE_SIGN ?
157 PSA_SUCCESS :
158 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200159 TEST_ASSERT( psa_mac_verify_setup( &operation,
160 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200161 TEST_ASSERT( psa_mac_update( &operation,
162 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200163 TEST_ASSERT( psa_mac_verify_finish( &operation,
164 mac,
165 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200166 }
167
168 return( 1 );
169
170exit:
171 psa_mac_abort( &operation );
172 return( 0 );
173}
174
175static int exercise_cipher_key( psa_key_slot_t key,
176 psa_key_usage_t usage,
177 psa_algorithm_t alg )
178{
179 psa_cipher_operation_t operation;
180 unsigned char iv[16] = {0};
181 size_t iv_length = sizeof( iv );
182 const unsigned char plaintext[16] = "Hello, world...";
183 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
184 size_t ciphertext_length = sizeof( ciphertext );
185 unsigned char decrypted[sizeof( ciphertext )];
186 size_t part_length;
187
188 if( usage & PSA_KEY_USAGE_ENCRYPT )
189 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200190 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
191 key, alg ) == PSA_SUCCESS );
192 TEST_ASSERT( psa_cipher_generate_iv( &operation,
193 iv, sizeof( iv ),
194 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200195 TEST_ASSERT( psa_cipher_update( &operation,
196 plaintext, sizeof( plaintext ),
197 ciphertext, sizeof( ciphertext ),
198 &ciphertext_length ) == PSA_SUCCESS );
199 TEST_ASSERT( psa_cipher_finish( &operation,
200 ciphertext + ciphertext_length,
201 sizeof( ciphertext ) - ciphertext_length,
202 &part_length ) == PSA_SUCCESS );
203 ciphertext_length += part_length;
204 }
205
206 if( usage & PSA_KEY_USAGE_DECRYPT )
207 {
208 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700209 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
211 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200212 size_t bits;
213 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
214 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
215 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200216 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
217 key, alg ) == PSA_SUCCESS );
218 TEST_ASSERT( psa_cipher_set_iv( &operation,
219 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200220 TEST_ASSERT( psa_cipher_update( &operation,
221 ciphertext, ciphertext_length,
222 decrypted, sizeof( decrypted ),
223 &part_length ) == PSA_SUCCESS );
224 status = psa_cipher_finish( &operation,
225 decrypted + part_length,
226 sizeof( decrypted ) - part_length,
227 &part_length );
228 /* For a stream cipher, all inputs are valid. For a block cipher,
229 * if the input is some aribtrary data rather than an actual
230 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700231 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700232 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200233 TEST_ASSERT( status == PSA_SUCCESS );
234 else
235 TEST_ASSERT( status == PSA_SUCCESS ||
236 status == PSA_ERROR_INVALID_PADDING );
237 }
238
239 return( 1 );
240
241exit:
242 psa_cipher_abort( &operation );
243 return( 0 );
244}
245
246static int exercise_aead_key( psa_key_slot_t key,
247 psa_key_usage_t usage,
248 psa_algorithm_t alg )
249{
250 unsigned char nonce[16] = {0};
251 size_t nonce_length = sizeof( nonce );
252 unsigned char plaintext[16] = "Hello, world...";
253 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
254 size_t ciphertext_length = sizeof( ciphertext );
255 size_t plaintext_length = sizeof( ciphertext );
256
257 if( usage & PSA_KEY_USAGE_ENCRYPT )
258 {
259 TEST_ASSERT( psa_aead_encrypt( key, alg,
260 nonce, nonce_length,
261 NULL, 0,
262 plaintext, sizeof( plaintext ),
263 ciphertext, sizeof( ciphertext ),
264 &ciphertext_length ) == PSA_SUCCESS );
265 }
266
267 if( usage & PSA_KEY_USAGE_DECRYPT )
268 {
269 psa_status_t verify_status =
270 ( usage & PSA_KEY_USAGE_ENCRYPT ?
271 PSA_SUCCESS :
272 PSA_ERROR_INVALID_SIGNATURE );
273 TEST_ASSERT( psa_aead_decrypt( key, alg,
274 nonce, nonce_length,
275 NULL, 0,
276 ciphertext, ciphertext_length,
277 plaintext, sizeof( plaintext ),
278 &plaintext_length ) == verify_status );
279 }
280
281 return( 1 );
282
283exit:
284 return( 0 );
285}
286
287static int exercise_signature_key( psa_key_slot_t key,
288 psa_key_usage_t usage,
289 psa_algorithm_t alg )
290{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200291 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
292 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200293 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200294 size_t signature_length = sizeof( signature );
295
296 if( usage & PSA_KEY_USAGE_SIGN )
297 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200298 /* Some algorithms require the payload to have the size of
299 * the hash encoded in the algorithm. Use this input size
300 * even for algorithms that allow other input sizes. */
301 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
302 if( hash_alg != 0 )
303 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 TEST_ASSERT( psa_asymmetric_sign( key, alg,
305 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 signature, sizeof( signature ),
307 &signature_length ) == PSA_SUCCESS );
308 }
309
310 if( usage & PSA_KEY_USAGE_VERIFY )
311 {
312 psa_status_t verify_status =
313 ( usage & PSA_KEY_USAGE_SIGN ?
314 PSA_SUCCESS :
315 PSA_ERROR_INVALID_SIGNATURE );
316 TEST_ASSERT( psa_asymmetric_verify( key, alg,
317 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200318 signature, signature_length ) ==
319 verify_status );
320 }
321
322 return( 1 );
323
324exit:
325 return( 0 );
326}
327
328static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
329 psa_key_usage_t usage,
330 psa_algorithm_t alg )
331{
332 unsigned char plaintext[256] = "Hello, world...";
333 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
334 size_t ciphertext_length = sizeof( ciphertext );
335 size_t plaintext_length = 16;
336
337 if( usage & PSA_KEY_USAGE_ENCRYPT )
338 {
339 TEST_ASSERT(
340 psa_asymmetric_encrypt( key, alg,
341 plaintext, plaintext_length,
342 NULL, 0,
343 ciphertext, sizeof( ciphertext ),
344 &ciphertext_length ) == PSA_SUCCESS );
345 }
346
347 if( usage & PSA_KEY_USAGE_DECRYPT )
348 {
349 psa_status_t status =
350 psa_asymmetric_decrypt( key, alg,
351 ciphertext, ciphertext_length,
352 NULL, 0,
353 plaintext, sizeof( plaintext ),
354 &plaintext_length );
355 TEST_ASSERT( status == PSA_SUCCESS ||
356 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
357 ( status == PSA_ERROR_INVALID_ARGUMENT ||
358 status == PSA_ERROR_INVALID_PADDING ) ) );
359 }
360
361 return( 1 );
362
363exit:
364 return( 0 );
365}
Gilles Peskine02b75072018-07-01 22:31:34 +0200366
Gilles Peskineea0fb492018-07-12 17:17:20 +0200367static int exercise_key_derivation_key( psa_key_slot_t key,
368 psa_key_usage_t usage,
369 psa_algorithm_t alg )
370{
371 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
372 unsigned char label[16] = "This is a label.";
373 size_t label_length = sizeof( label );
374 unsigned char seed[16] = "abcdefghijklmnop";
375 size_t seed_length = sizeof( seed );
376 unsigned char output[1];
377
378 if( usage & PSA_KEY_USAGE_DERIVE )
379 {
380 TEST_ASSERT( psa_key_derivation( &generator,
381 key, alg,
382 label, label_length,
383 seed, seed_length,
384 sizeof( output ) ) == PSA_SUCCESS );
385 TEST_ASSERT( psa_generator_read( &generator,
386 output,
387 sizeof( output ) ) == PSA_SUCCESS );
388 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
389 }
390
391 return( 1 );
392
393exit:
394 return( 0 );
395}
396
Gilles Peskine01d718c2018-09-18 12:01:02 +0200397static int exercise_key_agreement_key( psa_key_slot_t key,
398 psa_key_usage_t usage,
399 psa_algorithm_t alg )
400{
401 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
402 psa_key_type_t key_type;
403 psa_key_type_t public_key_type;
404 size_t key_bits;
405 uint8_t *public_key = NULL;
406 size_t public_key_length;
407 unsigned char output[1];
408 int ok = 0;
409
410 if( usage & PSA_KEY_USAGE_DERIVE )
411 {
412 /* We need two keys to exercise key agreement. Exercise the
413 * private key against its own public key. */
414 TEST_ASSERT( psa_get_key_information( key,
415 &key_type,
416 &key_bits ) == PSA_SUCCESS );
417 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( key_type );
418 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type,
419 key_bits );
Gilles Peskinefc411f12018-10-25 22:34:48 +0200420 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 TEST_ASSERT( public_key != NULL );
422 TEST_ASSERT(
423 psa_export_public_key( key,
424 public_key, public_key_length,
425 &public_key_length ) == PSA_SUCCESS );
426 TEST_ASSERT( psa_key_agreement( &generator,
427 key,
428 public_key, public_key_length,
429 alg ) == PSA_SUCCESS );
430 TEST_ASSERT( psa_generator_read( &generator,
431 output,
432 sizeof( output ) ) == PSA_SUCCESS );
433 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
434 }
435 ok = 1;
436
437exit:
438 mbedtls_free( public_key );
439 return( ok );
440}
441
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200442static int is_oid_of_key_type( psa_key_type_t type,
443 const uint8_t *oid, size_t oid_length )
444{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200445 const uint8_t *expected_oid = NULL;
446 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200447#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200448 if( PSA_KEY_TYPE_IS_RSA( type ) )
449 {
450 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
451 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
452 }
453 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200454#endif /* MBEDTLS_RSA_C */
455#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200456 if( PSA_KEY_TYPE_IS_ECC( type ) )
457 {
458 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
459 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
460 }
461 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200462#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200463 {
464 char message[40];
465 mbedtls_snprintf( message, sizeof( message ),
466 "OID not known for key type=0x%08lx",
467 (unsigned long) type );
468 test_fail( message, __LINE__, __FILE__ );
469 return( 0 );
470 }
471
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200472 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200473 return( 1 );
474
475exit:
476 return( 0 );
477}
478
479static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
480 size_t min_bits, size_t max_bits,
481 int must_be_odd )
482{
483 size_t len;
484 size_t actual_bits;
485 unsigned char msb;
486 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
487 MBEDTLS_ASN1_INTEGER ) == 0 );
488 /* Tolerate a slight departure from DER encoding:
489 * - 0 may be represented by an empty string or a 1-byte string.
490 * - The sign bit may be used as a value bit. */
491 if( ( len == 1 && ( *p )[0] == 0 ) ||
492 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
493 {
494 ++( *p );
495 --len;
496 }
497 if( min_bits == 0 && len == 0 )
498 return( 1 );
499 msb = ( *p )[0];
500 TEST_ASSERT( msb != 0 );
501 actual_bits = 8 * ( len - 1 );
502 while( msb != 0 )
503 {
504 msb >>= 1;
505 ++actual_bits;
506 }
507 TEST_ASSERT( actual_bits >= min_bits );
508 TEST_ASSERT( actual_bits <= max_bits );
509 if( must_be_odd )
510 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
511 *p += len;
512 return( 1 );
513exit:
514 return( 0 );
515}
516
517static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
518 size_t *len,
519 unsigned char n, unsigned char tag )
520{
521 int ret;
522 ret = mbedtls_asn1_get_tag( p, end, len,
523 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
524 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
525 if( ret != 0 )
526 return( ret );
527 end = *p + *len;
528 ret = mbedtls_asn1_get_tag( p, end, len, tag );
529 if( ret != 0 )
530 return( ret );
531 if( *p + *len != end )
532 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
533 return( 0 );
534}
535
536static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
537 uint8_t *exported, size_t exported_length )
538{
539 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200540 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200541 else
542 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200543
544#if defined(MBEDTLS_DES_C)
545 if( type == PSA_KEY_TYPE_DES )
546 {
547 /* Check the parity bits. */
548 unsigned i;
549 for( i = 0; i < bits / 8; i++ )
550 {
551 unsigned bit_count = 0;
552 unsigned m;
553 for( m = 1; m <= 0x100; m <<= 1 )
554 {
555 if( exported[i] & m )
556 ++bit_count;
557 }
558 TEST_ASSERT( bit_count % 2 != 0 );
559 }
560 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200561 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200562#endif
563
564#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
565 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
566 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200567 uint8_t *p = exported;
568 uint8_t *end = exported + exported_length;
569 size_t len;
570 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200571 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200572 * modulus INTEGER, -- n
573 * publicExponent INTEGER, -- e
574 * privateExponent INTEGER, -- d
575 * prime1 INTEGER, -- p
576 * prime2 INTEGER, -- q
577 * exponent1 INTEGER, -- d mod (p-1)
578 * exponent2 INTEGER, -- d mod (q-1)
579 * coefficient INTEGER, -- (inverse of q) mod p
580 * }
581 */
582 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
583 MBEDTLS_ASN1_SEQUENCE |
584 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
585 TEST_ASSERT( p + len == end );
586 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
587 goto exit;
588 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
589 goto exit;
590 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
591 goto exit;
592 /* Require d to be at least half the size of n. */
593 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
594 goto exit;
595 /* Require p and q to be at most half the size of n, rounded up. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
597 goto exit;
598 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
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 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
605 goto exit;
606 TEST_ASSERT( p == end );
607 }
608 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200609#endif /* MBEDTLS_RSA_C */
610
611#if defined(MBEDTLS_ECP_C)
612 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
613 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100614 /* Just the secret value */
615 TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) );
616 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200617 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200618#endif /* MBEDTLS_ECP_C */
619
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
621 {
622 uint8_t *p = exported;
623 uint8_t *end = exported + exported_length;
624 size_t len;
625 mbedtls_asn1_buf alg;
626 mbedtls_asn1_buf params;
627 mbedtls_asn1_bitstring bitstring;
628 /* SubjectPublicKeyInfo ::= SEQUENCE {
629 * algorithm AlgorithmIdentifier,
630 * subjectPublicKey BIT STRING }
631 * AlgorithmIdentifier ::= SEQUENCE {
632 * algorithm OBJECT IDENTIFIER,
633 * parameters ANY DEFINED BY algorithm OPTIONAL }
634 */
635 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
636 MBEDTLS_ASN1_SEQUENCE |
637 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
638 TEST_ASSERT( p + len == end );
639 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
640 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
641 goto exit;
642 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
643 TEST_ASSERT( p == end );
644 p = bitstring.p;
645#if defined(MBEDTLS_RSA_C)
646 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
647 {
648 /* RSAPublicKey ::= SEQUENCE {
649 * modulus INTEGER, -- n
650 * publicExponent INTEGER } -- e
651 */
652 TEST_ASSERT( bitstring.unused_bits == 0 );
653 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
654 MBEDTLS_ASN1_SEQUENCE |
655 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
656 TEST_ASSERT( p + len == end );
657 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
658 goto exit;
659 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
660 goto exit;
661 TEST_ASSERT( p == end );
662 }
663 else
664#endif /* MBEDTLS_RSA_C */
665#if defined(MBEDTLS_ECP_C)
666 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
667 {
668 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200669 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200670 * -- then x_P as an n-bit string, big endian;
671 * -- then y_P as a n-bit string, big endian,
672 * -- where n is the order of the curve.
673 */
674 TEST_ASSERT( bitstring.unused_bits == 0 );
675 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
676 TEST_ASSERT( p[0] == 4 );
677 }
678 else
679#endif /* MBEDTLS_ECP_C */
680 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100681 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 mbedtls_snprintf( message, sizeof( message ),
683 "No sanity check for public key type=0x%08lx",
684 (unsigned long) type );
685 test_fail( message, __LINE__, __FILE__ );
686 return( 0 );
687 }
688 }
689 else
690
691 {
692 /* No sanity checks for other types */
693 }
694
695 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200696
697exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200698 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200699}
700
701static int exercise_export_key( psa_key_slot_t slot,
702 psa_key_usage_t usage )
703{
704 psa_key_type_t type;
705 size_t bits;
706 uint8_t *exported = NULL;
707 size_t exported_size = 0;
708 size_t exported_length = 0;
709 int ok = 0;
710
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200711 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
712
713 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
714 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200715 {
716 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
717 PSA_ERROR_NOT_PERMITTED );
718 return( 1 );
719 }
720
Gilles Peskined14664a2018-08-10 19:07:32 +0200721 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200722 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723
724 TEST_ASSERT( psa_export_key( slot,
725 exported, exported_size,
726 &exported_length ) == PSA_SUCCESS );
727 ok = exported_key_sanity_check( type, bits, exported, exported_length );
728
729exit:
730 mbedtls_free( exported );
731 return( ok );
732}
733
734static int exercise_export_public_key( psa_key_slot_t slot )
735{
736 psa_key_type_t type;
737 psa_key_type_t public_type;
738 size_t bits;
739 uint8_t *exported = NULL;
740 size_t exported_size = 0;
741 size_t exported_length = 0;
742 int ok = 0;
743
744 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
745 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
746 {
747 TEST_ASSERT( psa_export_public_key( slot,
748 NULL, 0, &exported_length ) ==
749 PSA_ERROR_INVALID_ARGUMENT );
750 return( 1 );
751 }
752
753 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
754 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200755 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200756
757 TEST_ASSERT( psa_export_public_key( slot,
758 exported, exported_size,
759 &exported_length ) == PSA_SUCCESS );
760 ok = exported_key_sanity_check( public_type, bits,
761 exported, exported_length );
762
763exit:
764 mbedtls_free( exported );
765 return( ok );
766}
767
Gilles Peskine02b75072018-07-01 22:31:34 +0200768static int exercise_key( psa_key_slot_t slot,
769 psa_key_usage_t usage,
770 psa_algorithm_t alg )
771{
772 int ok;
773 if( alg == 0 )
774 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
775 else if( PSA_ALG_IS_MAC( alg ) )
776 ok = exercise_mac_key( slot, usage, alg );
777 else if( PSA_ALG_IS_CIPHER( alg ) )
778 ok = exercise_cipher_key( slot, usage, alg );
779 else if( PSA_ALG_IS_AEAD( alg ) )
780 ok = exercise_aead_key( slot, usage, alg );
781 else if( PSA_ALG_IS_SIGN( alg ) )
782 ok = exercise_signature_key( slot, usage, alg );
783 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
784 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200785 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
786 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200787 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
788 ok = exercise_key_agreement_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200789 else
790 {
791 char message[40];
792 mbedtls_snprintf( message, sizeof( message ),
793 "No code to exercise alg=0x%08lx",
794 (unsigned long) alg );
795 test_fail( message, __LINE__, __FILE__ );
796 ok = 0;
797 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200798
799 ok = ok && exercise_export_key( slot, usage );
800 ok = ok && exercise_export_public_key( slot );
801
Gilles Peskine02b75072018-07-01 22:31:34 +0200802 return( ok );
803}
804
Gilles Peskinee59236f2018-01-27 23:32:46 +0100805/* END_HEADER */
806
807/* BEGIN_DEPENDENCIES
808 * depends_on:MBEDTLS_PSA_CRYPTO_C
809 * END_DEPENDENCIES
810 */
811
812/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200813void static_checks( )
814{
815 size_t max_truncated_mac_size =
816 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
817
818 /* Check that the length for a truncated MAC always fits in the algorithm
819 * encoding. The shifted mask is the maximum truncated value. The
820 * untruncated algorithm may be one byte larger. */
821 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
822}
823/* END_CASE */
824
825/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200826void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100827{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100828 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100829 int i;
830 for( i = 0; i <= 1; i++ )
831 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832 status = psa_crypto_init( );
833 TEST_ASSERT( status == PSA_SUCCESS );
834 status = psa_crypto_init( );
835 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100836 mbedtls_psa_crypto_free( );
837 }
838}
839/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840
841/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200842void fill_slots( int max_arg )
843{
844 /* Fill all the slots until we run out of memory or out of slots,
845 * or until some limit specified in the test data for the sake of
846 * implementations with an essentially unlimited number of slots.
847 * This test assumes that available slots are numbered from 1. */
848
849 psa_key_slot_t slot;
850 psa_key_slot_t max = 0;
851 psa_key_policy_t policy;
852 uint8_t exported[sizeof( max )];
853 size_t exported_size;
854 psa_status_t status;
855
856 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
857
858 psa_key_policy_init( &policy );
859 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
860
861 for( max = 1; max <= (size_t) max_arg; max++ )
862 {
863 status = psa_set_key_policy( max, &policy );
864 /* Stop filling slots if we run out of memory or out of
865 * available slots. */
866 TEST_ASSERT( status == PSA_SUCCESS ||
867 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
868 status == PSA_ERROR_INVALID_ARGUMENT );
869 if( status != PSA_SUCCESS )
870 break;
871 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
872 (uint8_t*) &max, sizeof( max ) );
873 /* Since psa_set_key_policy succeeded, we know that the slot
874 * number is valid. But we may legitimately run out of memory. */
875 TEST_ASSERT( status == PSA_SUCCESS ||
876 status == PSA_ERROR_INSUFFICIENT_MEMORY );
877 if( status != PSA_SUCCESS )
878 break;
879 }
880 /* `max` is now the first slot number that wasn't filled. */
881 max -= 1;
882
883 for( slot = 1; slot <= max; slot++ )
884 {
885 TEST_ASSERT( psa_export_key( slot,
886 exported, sizeof( exported ),
887 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200888 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200889 }
890
891exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200892 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200893 mbedtls_psa_crypto_free( );
894}
895/* END_CASE */
896
897/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200898void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100899{
900 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200901 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100903
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100904 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300905 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
907
Gilles Peskine4abf7412018-06-18 16:35:34 +0200908 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200909 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100910 if( status == PSA_SUCCESS )
911 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
912
913exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100914 mbedtls_psa_crypto_free( );
915}
916/* END_CASE */
917
918/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200919void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
920{
921 int slot = 1;
922 size_t bits = bits_arg;
923 psa_status_t expected_status = expected_status_arg;
924 psa_status_t status;
925 psa_key_type_t type =
926 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
927 size_t buffer_size = /* Slight overapproximations */
928 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200929 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200930 unsigned char *p;
931 int ret;
932 size_t length;
933
934 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200935 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200936
937 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
938 bits, keypair ) ) >= 0 );
939 length = ret;
940
941 /* Try importing the key */
942 status = psa_import_key( slot, type, p, length );
943 TEST_ASSERT( status == expected_status );
944 if( status == PSA_SUCCESS )
945 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
946
947exit:
948 mbedtls_free( buffer );
949 mbedtls_psa_crypto_free( );
950}
951/* END_CASE */
952
953/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300954void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300955 int type_arg,
956 int alg_arg,
957 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958 int expected_bits,
959 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200960 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961 int canonical_input )
962{
963 int slot = 1;
964 int slot2 = slot + 1;
965 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200966 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200967 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969 unsigned char *exported = NULL;
970 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100972 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973 size_t reexported_length;
974 psa_key_type_t got_type;
975 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200976 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300979 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300980 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200981 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200983 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
985
mohammad1603a97cb8c2018-03-28 03:46:26 -0700986 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200987 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700988 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
989
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990 /* Import the key */
991 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200992 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993
994 /* Test the key information */
995 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200996 &got_type,
997 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 TEST_ASSERT( got_type == type );
999 TEST_ASSERT( got_bits == (size_t) expected_bits );
1000
1001 /* Export the key */
1002 status = psa_export_key( slot,
1003 exported, export_size,
1004 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001005 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001006
1007 /* The exported length must be set by psa_export_key() to a value between 0
1008 * and export_size. On errors, the exported length must be 0. */
1009 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1010 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1011 TEST_ASSERT( exported_length <= export_size );
1012
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001013 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001014 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001016 {
1017 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001019 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020
Gilles Peskine8f609232018-08-11 01:24:55 +02001021 if( ! exercise_export_key( slot, usage_arg ) )
1022 goto exit;
1023
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001025 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026 else
1027 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001028 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1029
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001031 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001032 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001034 reexported,
1035 export_size,
1036 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001037 ASSERT_COMPARE( exported, exported_length,
1038 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001039 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001040 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001041
1042destroy:
1043 /* Destroy the key */
1044 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1045 TEST_ASSERT( psa_get_key_information(
1046 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1047
1048exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001049 mbedtls_free( exported );
1050 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001051 mbedtls_psa_crypto_free( );
1052}
1053/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001054
Moran Pekerf709f4a2018-06-06 17:26:04 +03001055/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001056void import_key_nonempty_slot( )
1057{
1058 int slot = 1;
1059 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1060 psa_status_t status;
1061 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1062 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1063
1064 /* Import the key */
1065 TEST_ASSERT( psa_import_key( slot, type,
1066 data, sizeof( data ) ) == PSA_SUCCESS );
1067
1068 /* Import the key again */
1069 status = psa_import_key( slot, type, data, sizeof( data ) );
1070 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1071
1072exit:
1073 mbedtls_psa_crypto_free( );
1074}
1075/* END_CASE */
1076
1077/* BEGIN_CASE */
1078void export_invalid_slot( int slot, int expected_export_status_arg )
1079{
1080 psa_status_t status;
1081 unsigned char *exported = NULL;
1082 size_t export_size = 0;
1083 size_t exported_length = INVALID_EXPORT_LENGTH;
1084 psa_status_t expected_export_status = expected_export_status_arg;
1085
1086 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1087
1088 /* Export the key */
1089 status = psa_export_key( slot,
1090 exported, export_size,
1091 &exported_length );
1092 TEST_ASSERT( status == expected_export_status );
1093
1094exit:
1095 mbedtls_psa_crypto_free( );
1096}
1097/* END_CASE */
1098
1099/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001100void export_with_no_key_activity( )
1101{
1102 int slot = 1;
1103 psa_algorithm_t alg = PSA_ALG_CTR;
1104 psa_status_t status;
1105 psa_key_policy_t policy;
1106 unsigned char *exported = NULL;
1107 size_t export_size = 0;
1108 size_t exported_length = INVALID_EXPORT_LENGTH;
1109
1110 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1111
1112 psa_key_policy_init( &policy );
1113 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1114 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1115
1116 /* Export the key */
1117 status = psa_export_key( slot,
1118 exported, export_size,
1119 &exported_length );
1120 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1121
1122exit:
1123 mbedtls_psa_crypto_free( );
1124}
1125/* END_CASE */
1126
1127/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001128void cipher_with_no_key_activity( )
1129{
1130 int slot = 1;
1131 psa_status_t status;
1132 psa_key_policy_t policy;
1133 psa_cipher_operation_t operation;
1134 int exercise_alg = PSA_ALG_CTR;
1135
1136 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1137
1138 psa_key_policy_init( &policy );
1139 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
1140 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1141
1142 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1143 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1144
1145exit:
1146 psa_cipher_abort( &operation );
1147 mbedtls_psa_crypto_free( );
1148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001152void export_after_import_failure( data_t *data, int type_arg,
1153 int expected_import_status_arg )
1154{
1155 int slot = 1;
1156 psa_key_type_t type = type_arg;
1157 psa_status_t status;
1158 unsigned char *exported = NULL;
1159 size_t export_size = 0;
1160 psa_status_t expected_import_status = expected_import_status_arg;
1161 size_t exported_length = INVALID_EXPORT_LENGTH;
1162
1163 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1164
1165 /* Import the key - expect failure */
1166 status = psa_import_key( slot, type,
1167 data->x, data->len );
1168 TEST_ASSERT( status == expected_import_status );
1169
1170 /* Export the key */
1171 status = psa_export_key( slot,
1172 exported, export_size,
1173 &exported_length );
1174 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1175
1176exit:
1177 mbedtls_psa_crypto_free( );
1178}
1179/* END_CASE */
1180
1181/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001182void cipher_after_import_failure( data_t *data, int type_arg,
1183 int expected_import_status_arg )
1184{
1185 int slot = 1;
1186 psa_cipher_operation_t operation;
1187 psa_key_type_t type = type_arg;
1188 psa_status_t status;
1189 psa_status_t expected_import_status = expected_import_status_arg;
1190 int exercise_alg = PSA_ALG_CTR;
1191
1192 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1193
1194 /* Import the key - expect failure */
1195 status = psa_import_key( slot, type,
1196 data->x, data->len );
1197 TEST_ASSERT( status == expected_import_status );
1198
1199 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1200 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1201
1202exit:
1203 psa_cipher_abort( &operation );
1204 mbedtls_psa_crypto_free( );
1205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001209void export_after_destroy_key( data_t *data, int type_arg )
1210{
1211 int slot = 1;
1212 psa_key_type_t type = type_arg;
1213 psa_status_t status;
1214 psa_key_policy_t policy;
1215 psa_algorithm_t alg = PSA_ALG_CTR;
1216 unsigned char *exported = NULL;
1217 size_t export_size = 0;
1218 size_t exported_length = INVALID_EXPORT_LENGTH;
1219
1220 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1221
1222 psa_key_policy_init( &policy );
1223 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1224 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1225 export_size = (ptrdiff_t) data->len;
1226 ASSERT_ALLOC( exported, export_size );
1227
1228 /* Import the key */
1229 TEST_ASSERT( psa_import_key( slot, type,
1230 data->x, data->len ) == PSA_SUCCESS );
1231
1232 TEST_ASSERT( psa_export_key( slot, exported, export_size,
1233 &exported_length ) == PSA_SUCCESS );
1234
1235 /* Destroy the key */
1236 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1237
1238 /* Export the key */
1239 status = psa_export_key( slot, exported, export_size,
1240 &exported_length );
1241 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1242
1243exit:
1244 mbedtls_free( exported );
1245 mbedtls_psa_crypto_free( );
1246}
1247/* END_CASE */
1248
1249/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001250void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001251 int type_arg,
1252 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001253 int export_size_delta,
1254 int expected_export_status_arg,
1255 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001256{
1257 int slot = 1;
1258 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001259 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001260 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001261 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001262 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001263 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001264 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001265 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001266
Moran Pekerf709f4a2018-06-06 17:26:04 +03001267 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1268
1269 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001270 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001271 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1272
1273 /* Import the key */
1274 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001275 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001276
Gilles Peskine49c25912018-10-29 15:15:31 +01001277 /* Export the public key */
1278 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001279 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001280 exported, export_size,
1281 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001282 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001283 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001284 {
1285 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1286 size_t bits;
1287 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1288 PSA_SUCCESS );
1289 TEST_ASSERT( expected_public_key->len <=
1290 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001291 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1292 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001293 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001294
1295exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001296 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001297 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001298 mbedtls_psa_crypto_free( );
1299}
1300/* END_CASE */
1301
Gilles Peskine20035e32018-02-03 22:44:14 +01001302/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001303void import_and_exercise_key( data_t *data,
1304 int type_arg,
1305 int bits_arg,
1306 int alg_arg )
1307{
1308 int slot = 1;
1309 psa_key_type_t type = type_arg;
1310 size_t bits = bits_arg;
1311 psa_algorithm_t alg = alg_arg;
1312 psa_key_usage_t usage =
1313 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1314 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1315 PSA_KEY_USAGE_VERIFY :
1316 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1317 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1318 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1319 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1320 PSA_KEY_USAGE_ENCRYPT :
1321 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001322 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskine01d718c2018-09-18 12:01:02 +02001323 PSA_ALG_IS_KEY_AGREEMENT( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001324 0 );
1325 psa_key_policy_t policy;
1326 psa_key_type_t got_type;
1327 size_t got_bits;
1328 psa_status_t status;
1329
1330 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1331
1332 psa_key_policy_init( &policy );
1333 psa_key_policy_set_usage( &policy, usage, alg );
1334 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1335
1336 /* Import the key */
1337 status = psa_import_key( slot, type, data->x, data->len );
1338 TEST_ASSERT( status == PSA_SUCCESS );
1339
1340 /* Test the key information */
1341 TEST_ASSERT( psa_get_key_information( slot,
1342 &got_type,
1343 &got_bits ) == PSA_SUCCESS );
1344 TEST_ASSERT( got_type == type );
1345 TEST_ASSERT( got_bits == bits );
1346
1347 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001348 if( ! exercise_key( slot, usage, alg ) )
1349 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001350
1351exit:
1352 psa_destroy_key( slot );
1353 mbedtls_psa_crypto_free( );
1354}
1355/* END_CASE */
1356
1357/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001358void key_policy( int usage_arg, int alg_arg )
1359{
1360 int key_slot = 1;
1361 psa_algorithm_t alg = alg_arg;
1362 psa_key_usage_t usage = usage_arg;
1363 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1364 unsigned char key[32] = {0};
1365 psa_key_policy_t policy_set;
1366 psa_key_policy_t policy_get;
1367
1368 memset( key, 0x2a, sizeof( key ) );
1369
1370 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1371
1372 psa_key_policy_init( &policy_set );
1373 psa_key_policy_init( &policy_get );
1374
1375 psa_key_policy_set_usage( &policy_set, usage, alg );
1376
1377 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1378 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1379 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1380
1381 TEST_ASSERT( psa_import_key( key_slot, key_type,
1382 key, sizeof( key ) ) == PSA_SUCCESS );
1383
1384 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1385
1386 TEST_ASSERT( policy_get.usage == policy_set.usage );
1387 TEST_ASSERT( policy_get.alg == policy_set.alg );
1388
1389exit:
1390 psa_destroy_key( key_slot );
1391 mbedtls_psa_crypto_free( );
1392}
1393/* END_CASE */
1394
1395/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001396void mac_key_policy( int policy_usage,
1397 int policy_alg,
1398 int key_type,
1399 data_t *key_data,
1400 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001401{
1402 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001403 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001404 psa_mac_operation_t operation;
1405 psa_status_t status;
1406 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001407
1408 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1409
1410 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001411 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001412 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1413
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001414 TEST_ASSERT( psa_import_key( key_slot, key_type,
1415 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001416
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001418 if( policy_alg == exercise_alg &&
1419 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1420 TEST_ASSERT( status == PSA_SUCCESS );
1421 else
1422 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1423 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001424
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001425 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001426 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001427 if( policy_alg == exercise_alg &&
1428 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001429 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001430 else
1431 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1432
1433exit:
1434 psa_mac_abort( &operation );
1435 psa_destroy_key( key_slot );
1436 mbedtls_psa_crypto_free( );
1437}
1438/* END_CASE */
1439
1440/* BEGIN_CASE */
1441void cipher_key_policy( int policy_usage,
1442 int policy_alg,
1443 int key_type,
1444 data_t *key_data,
1445 int exercise_alg )
1446{
1447 int key_slot = 1;
1448 psa_key_policy_t policy;
1449 psa_cipher_operation_t operation;
1450 psa_status_t status;
1451
1452 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1453
1454 psa_key_policy_init( &policy );
1455 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1456 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1457
1458 TEST_ASSERT( psa_import_key( key_slot, key_type,
1459 key_data->x, key_data->len ) == PSA_SUCCESS );
1460
Gilles Peskinefe119512018-07-08 21:39:34 +02001461 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001462 if( policy_alg == exercise_alg &&
1463 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1464 TEST_ASSERT( status == PSA_SUCCESS );
1465 else
1466 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1467 psa_cipher_abort( &operation );
1468
Gilles Peskinefe119512018-07-08 21:39:34 +02001469 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001470 if( policy_alg == exercise_alg &&
1471 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1472 TEST_ASSERT( status == PSA_SUCCESS );
1473 else
1474 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1475
1476exit:
1477 psa_cipher_abort( &operation );
1478 psa_destroy_key( key_slot );
1479 mbedtls_psa_crypto_free( );
1480}
1481/* END_CASE */
1482
1483/* BEGIN_CASE */
1484void aead_key_policy( int policy_usage,
1485 int policy_alg,
1486 int key_type,
1487 data_t *key_data,
1488 int nonce_length_arg,
1489 int tag_length_arg,
1490 int exercise_alg )
1491{
1492 int key_slot = 1;
1493 psa_key_policy_t policy;
1494 psa_status_t status;
1495 unsigned char nonce[16] = {0};
1496 size_t nonce_length = nonce_length_arg;
1497 unsigned char tag[16];
1498 size_t tag_length = tag_length_arg;
1499 size_t output_length;
1500
1501 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1502 TEST_ASSERT( tag_length <= sizeof( tag ) );
1503
1504 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1505
1506 psa_key_policy_init( &policy );
1507 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1508 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1509
1510 TEST_ASSERT( psa_import_key( key_slot, key_type,
1511 key_data->x, key_data->len ) == PSA_SUCCESS );
1512
1513 status = psa_aead_encrypt( key_slot, exercise_alg,
1514 nonce, nonce_length,
1515 NULL, 0,
1516 NULL, 0,
1517 tag, tag_length,
1518 &output_length );
1519 if( policy_alg == exercise_alg &&
1520 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1521 TEST_ASSERT( status == PSA_SUCCESS );
1522 else
1523 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1524
1525 memset( tag, 0, sizeof( tag ) );
1526 status = psa_aead_decrypt( key_slot, exercise_alg,
1527 nonce, nonce_length,
1528 NULL, 0,
1529 tag, tag_length,
1530 NULL, 0,
1531 &output_length );
1532 if( policy_alg == exercise_alg &&
1533 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1534 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1535 else
1536 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1537
1538exit:
1539 psa_destroy_key( key_slot );
1540 mbedtls_psa_crypto_free( );
1541}
1542/* END_CASE */
1543
1544/* BEGIN_CASE */
1545void asymmetric_encryption_key_policy( int policy_usage,
1546 int policy_alg,
1547 int key_type,
1548 data_t *key_data,
1549 int exercise_alg )
1550{
1551 int key_slot = 1;
1552 psa_key_policy_t policy;
1553 psa_status_t status;
1554 size_t key_bits;
1555 size_t buffer_length;
1556 unsigned char *buffer = NULL;
1557 size_t output_length;
1558
1559 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1560
1561 psa_key_policy_init( &policy );
1562 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1563 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1564
1565 TEST_ASSERT( psa_import_key( key_slot, key_type,
1566 key_data->x, key_data->len ) == PSA_SUCCESS );
1567
1568 TEST_ASSERT( psa_get_key_information( key_slot,
1569 NULL,
1570 &key_bits ) == PSA_SUCCESS );
1571 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1572 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001573 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
1575 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1576 NULL, 0,
1577 NULL, 0,
1578 buffer, buffer_length,
1579 &output_length );
1580 if( policy_alg == exercise_alg &&
1581 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1582 TEST_ASSERT( status == PSA_SUCCESS );
1583 else
1584 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1585
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001586 if( buffer_length != 0 )
1587 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001588 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1589 buffer, buffer_length,
1590 NULL, 0,
1591 buffer, buffer_length,
1592 &output_length );
1593 if( policy_alg == exercise_alg &&
1594 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1595 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1596 else
1597 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1598
1599exit:
1600 psa_destroy_key( key_slot );
1601 mbedtls_psa_crypto_free( );
1602 mbedtls_free( buffer );
1603}
1604/* END_CASE */
1605
1606/* BEGIN_CASE */
1607void asymmetric_signature_key_policy( int policy_usage,
1608 int policy_alg,
1609 int key_type,
1610 data_t *key_data,
1611 int exercise_alg )
1612{
1613 int key_slot = 1;
1614 psa_key_policy_t policy;
1615 psa_status_t status;
1616 unsigned char payload[16] = {1};
1617 size_t payload_length = sizeof( payload );
1618 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1619 size_t signature_length;
1620
1621 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1622
1623 psa_key_policy_init( &policy );
1624 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1625 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1626
1627 TEST_ASSERT( psa_import_key( key_slot, key_type,
1628 key_data->x, key_data->len ) == PSA_SUCCESS );
1629
1630 status = psa_asymmetric_sign( key_slot, exercise_alg,
1631 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 signature, sizeof( signature ),
1633 &signature_length );
1634 if( policy_alg == exercise_alg &&
1635 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1636 TEST_ASSERT( status == PSA_SUCCESS );
1637 else
1638 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1639
1640 memset( signature, 0, sizeof( signature ) );
1641 status = psa_asymmetric_verify( key_slot, exercise_alg,
1642 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001643 signature, sizeof( signature ) );
1644 if( policy_alg == exercise_alg &&
1645 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1646 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1647 else
1648 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001649
1650exit:
1651 psa_destroy_key( key_slot );
1652 mbedtls_psa_crypto_free( );
1653}
1654/* END_CASE */
1655
1656/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001657void derive_key_policy( int policy_usage,
1658 int policy_alg,
1659 int key_type,
1660 data_t *key_data,
1661 int exercise_alg )
1662{
1663 int key_slot = 1;
1664 psa_key_policy_t policy;
1665 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1666 psa_status_t status;
1667
1668 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1669
1670 psa_key_policy_init( &policy );
1671 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1672 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1673
1674 TEST_ASSERT( psa_import_key( key_slot, key_type,
1675 key_data->x, key_data->len ) == PSA_SUCCESS );
1676
1677 status = psa_key_derivation( &generator, key_slot,
1678 exercise_alg,
1679 NULL, 0,
1680 NULL, 0,
1681 1 );
1682 if( policy_alg == exercise_alg &&
1683 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1684 TEST_ASSERT( status == PSA_SUCCESS );
1685 else
1686 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1687
1688exit:
Gilles Peskine1d7c0822018-10-08 19:05:22 +02001689 mbedtls_free( public_key );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001690 psa_generator_abort( &generator );
1691 psa_destroy_key( key_slot );
1692 mbedtls_psa_crypto_free( );
1693}
1694/* END_CASE */
1695
1696/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001697void agreement_key_policy( int policy_usage,
1698 int policy_alg,
1699 int key_type_arg,
1700 data_t *key_data,
1701 int exercise_alg )
1702{
1703 int key_slot = 1;
1704 psa_key_policy_t policy;
1705 psa_key_type_t key_type = key_type_arg;
1706 psa_key_type_t public_key_type;
1707 size_t key_bits;
1708 uint8_t *public_key = NULL;
1709 size_t public_key_length;
1710 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1711 psa_status_t status;
1712
1713 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1714
1715 psa_key_policy_init( &policy );
1716 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1717 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1718
1719 TEST_ASSERT( psa_import_key( key_slot, key_type,
1720 key_data->x, key_data->len ) == PSA_SUCCESS );
1721
1722 /* We need two keys to exercise key agreement. Exercise the
1723 * private key against its own public key. */
1724 TEST_ASSERT( psa_get_key_information( key_slot,
1725 &key_type,
1726 &key_bits ) == PSA_SUCCESS );
1727 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( key_type );
1728 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
Gilles Peskinefc411f12018-10-25 22:34:48 +02001729 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001730 TEST_ASSERT( public_key != NULL );
1731 TEST_ASSERT( psa_export_public_key( key_slot,
1732 public_key, public_key_length,
1733 &public_key_length ) == PSA_SUCCESS );
1734
1735 status = psa_key_agreement( &generator, key_slot,
1736 public_key, public_key_length,
1737 exercise_alg );
1738 if( policy_alg == exercise_alg &&
1739 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1740 TEST_ASSERT( status == PSA_SUCCESS );
1741 else
1742 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1743
1744exit:
1745 psa_generator_abort( &generator );
1746 psa_destroy_key( key_slot );
1747 mbedtls_psa_crypto_free( );
1748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001752void key_lifetime( int lifetime_arg )
1753{
1754 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001755 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001756 unsigned char key[32] = {0};
1757 psa_key_lifetime_t lifetime_set = lifetime_arg;
1758 psa_key_lifetime_t lifetime_get;
1759
1760 memset( key, 0x2a, sizeof( key ) );
1761
1762 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1763
1764 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1765 lifetime_set ) == PSA_SUCCESS );
1766
1767 TEST_ASSERT( psa_import_key( key_slot, key_type,
1768 key, sizeof( key ) ) == PSA_SUCCESS );
1769
1770 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1771 &lifetime_get ) == PSA_SUCCESS );
1772
1773 TEST_ASSERT( lifetime_get == lifetime_set );
1774
1775exit:
1776 psa_destroy_key( key_slot );
1777 mbedtls_psa_crypto_free( );
1778}
1779/* END_CASE */
1780
1781/* BEGIN_CASE */
1782void key_lifetime_set_fail( int key_slot_arg,
1783 int lifetime_arg,
1784 int expected_status_arg )
1785{
1786 psa_key_slot_t key_slot = key_slot_arg;
1787 psa_key_lifetime_t lifetime_set = lifetime_arg;
1788 psa_status_t actual_status;
1789 psa_status_t expected_status = expected_status_arg;
1790
1791 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1792
1793 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1794
1795 if( actual_status == PSA_SUCCESS )
1796 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1797
1798 TEST_ASSERT( expected_status == actual_status );
1799
1800exit:
1801 psa_destroy_key( key_slot );
1802 mbedtls_psa_crypto_free( );
1803}
1804/* END_CASE */
1805
1806/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001807void hash_setup( int alg_arg,
1808 int expected_status_arg )
1809{
1810 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001811 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001812 psa_hash_operation_t operation;
1813 psa_status_t status;
1814
1815 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1816
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001817 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001818 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001819 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001820
1821exit:
1822 mbedtls_psa_crypto_free( );
1823}
1824/* END_CASE */
1825
1826/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001827void hash_bad_order( )
1828{
1829 unsigned char input[] = "";
1830 /* SHA-256 hash of an empty string */
1831 unsigned char hash[] = {
1832 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1833 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1834 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1835 size_t hash_len;
1836 psa_hash_operation_t operation;
1837
1838 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1839
1840 /* psa_hash_update without calling psa_hash_setup beforehand */
1841 memset( &operation, 0, sizeof( operation ) );
1842 TEST_ASSERT( psa_hash_update( &operation,
1843 input, sizeof( input ) ) ==
1844 PSA_ERROR_INVALID_ARGUMENT );
1845
1846 /* psa_hash_verify without calling psa_hash_setup beforehand */
1847 memset( &operation, 0, sizeof( operation ) );
1848 TEST_ASSERT( psa_hash_verify( &operation,
1849 hash, sizeof( hash ) ) ==
1850 PSA_ERROR_INVALID_ARGUMENT );
1851
1852 /* psa_hash_finish without calling psa_hash_setup beforehand */
1853 memset( &operation, 0, sizeof( operation ) );
1854 TEST_ASSERT( psa_hash_finish( &operation,
1855 hash, sizeof( hash ), &hash_len ) ==
1856 PSA_ERROR_INVALID_ARGUMENT );
1857
1858exit:
1859 mbedtls_psa_crypto_free( );
1860}
1861/* END_CASE */
1862
itayzafrir27e69452018-11-01 14:26:34 +02001863/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1864void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001865{
1866 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001867 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1868 * appended to it */
1869 unsigned char hash[] = {
1870 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1871 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1872 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001873 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001874 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001875
1876 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1877
itayzafrir27e69452018-11-01 14:26:34 +02001878 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001879 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1880 TEST_ASSERT( psa_hash_verify( &operation,
1881 hash, expected_size - 1 ) ==
1882 PSA_ERROR_INVALID_SIGNATURE );
1883
itayzafrir27e69452018-11-01 14:26:34 +02001884 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001885 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001886 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001887 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001888 PSA_ERROR_INVALID_SIGNATURE );
1889
itayzafrir27e69452018-11-01 14:26:34 +02001890 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001891 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001892 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001893 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001894 PSA_ERROR_INVALID_SIGNATURE );
1895
itayzafrirec93d302018-10-18 18:01:10 +03001896exit:
1897 mbedtls_psa_crypto_free( );
1898}
1899/* END_CASE */
1900
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001901/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1902void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001903{
1904 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001905 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001906 size_t expected_size = PSA_HASH_SIZE( alg );
1907 psa_hash_operation_t operation;
1908 size_t hash_len;
1909
1910 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1911
itayzafrir58028322018-10-25 10:22:01 +03001912 /* psa_hash_finish with a smaller hash buffer than expected */
1913 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1914 TEST_ASSERT( psa_hash_finish( &operation,
1915 hash, expected_size - 1,
1916 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1917
1918exit:
1919 mbedtls_psa_crypto_free( );
1920}
1921/* END_CASE */
1922
1923/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001924void mac_setup( int key_type_arg,
1925 data_t *key,
1926 int alg_arg,
1927 int expected_status_arg )
1928{
1929 int key_slot = 1;
1930 psa_key_type_t key_type = key_type_arg;
1931 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001932 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001933 psa_mac_operation_t operation;
1934 psa_key_policy_t policy;
1935 psa_status_t status;
1936
1937 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1938
1939 psa_key_policy_init( &policy );
1940 psa_key_policy_set_usage( &policy,
1941 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1942 alg );
1943 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1944
1945 TEST_ASSERT( psa_import_key( key_slot, key_type,
1946 key->x, key->len ) == PSA_SUCCESS );
1947
Gilles Peskine89167cb2018-07-08 20:12:23 +02001948 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001949 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001950 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001951
1952exit:
1953 psa_destroy_key( key_slot );
1954 mbedtls_psa_crypto_free( );
1955}
1956/* END_CASE */
1957
1958/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001959void mac_sign( int key_type_arg,
1960 data_t *key,
1961 int alg_arg,
1962 data_t *input,
1963 data_t *expected_mac )
1964{
1965 int key_slot = 1;
1966 psa_key_type_t key_type = key_type_arg;
1967 psa_algorithm_t alg = alg_arg;
1968 psa_mac_operation_t operation;
1969 psa_key_policy_t policy;
1970 /* Leave a little extra room in the output buffer. At the end of the
1971 * test, we'll check that the implementation didn't overwrite onto
1972 * this extra room. */
1973 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1974 size_t mac_buffer_size =
1975 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1976 size_t mac_length = 0;
1977
1978 memset( actual_mac, '+', sizeof( actual_mac ) );
1979 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1980 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1981
1982 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1983
1984 psa_key_policy_init( &policy );
1985 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1986 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1987
1988 TEST_ASSERT( psa_import_key( key_slot, key_type,
1989 key->x, key->len ) == PSA_SUCCESS );
1990
1991 /* Calculate the MAC. */
1992 TEST_ASSERT( psa_mac_sign_setup( &operation,
1993 key_slot, alg ) == PSA_SUCCESS );
1994 TEST_ASSERT( psa_mac_update( &operation,
1995 input->x, input->len ) == PSA_SUCCESS );
1996 TEST_ASSERT( psa_mac_sign_finish( &operation,
1997 actual_mac, mac_buffer_size,
1998 &mac_length ) == PSA_SUCCESS );
1999
2000 /* Compare with the expected value. */
2001 TEST_ASSERT( mac_length == expected_mac->len );
2002 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
2003
2004 /* Verify that the end of the buffer is untouched. */
2005 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2006 sizeof( actual_mac ) - mac_length ) );
2007
2008exit:
2009 psa_destroy_key( key_slot );
2010 mbedtls_psa_crypto_free( );
2011}
2012/* END_CASE */
2013
2014/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002015void mac_verify( int key_type_arg,
2016 data_t *key,
2017 int alg_arg,
2018 data_t *input,
2019 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020{
2021 int key_slot = 1;
2022 psa_key_type_t key_type = key_type_arg;
2023 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002025 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026
Gilles Peskine69c12672018-06-28 00:07:19 +02002027 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2028
Gilles Peskine8c9def32018-02-08 10:02:12 +01002029 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002032 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002033 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2034 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035
2036 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2037
mohammad16036df908f2018-04-02 08:34:15 -07002038 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002039 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07002040 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2041
Gilles Peskine8c9def32018-02-08 10:02:12 +01002042 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002043 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002044
Gilles Peskine89167cb2018-07-08 20:12:23 +02002045 TEST_ASSERT( psa_mac_verify_setup( &operation,
2046 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002047 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
2048 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002049 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02002050 TEST_ASSERT( psa_mac_verify_finish( &operation,
2051 expected_mac->x,
2052 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053
2054exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002055 psa_destroy_key( key_slot );
2056 mbedtls_psa_crypto_free( );
2057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002061void cipher_setup( int key_type_arg,
2062 data_t *key,
2063 int alg_arg,
2064 int expected_status_arg )
2065{
2066 int key_slot = 1;
2067 psa_key_type_t key_type = key_type_arg;
2068 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002069 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002070 psa_cipher_operation_t operation;
2071 psa_key_policy_t policy;
2072 psa_status_t status;
2073
2074 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2075
2076 psa_key_policy_init( &policy );
2077 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2078 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2079
2080 TEST_ASSERT( psa_import_key( key_slot, key_type,
2081 key->x, key->len ) == PSA_SUCCESS );
2082
Gilles Peskinefe119512018-07-08 21:39:34 +02002083 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002084 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002085 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002086
2087exit:
2088 psa_destroy_key( key_slot );
2089 mbedtls_psa_crypto_free( );
2090}
2091/* END_CASE */
2092
2093/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002094void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002095 data_t *key,
2096 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002097 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002098{
2099 int key_slot = 1;
2100 psa_status_t status;
2101 psa_key_type_t key_type = key_type_arg;
2102 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002103 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002104 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002105 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002106 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002107 size_t output_buffer_size = 0;
2108 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002109 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002110 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002111 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002112
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002114 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002115 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002116 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2117 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2118 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002119
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002120 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2121 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002122
2123 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2124
Moran Pekered346952018-07-05 15:22:45 +03002125 psa_key_policy_init( &policy );
2126 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2127 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2128
Gilles Peskine50e586b2018-06-08 14:28:46 +02002129 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002130 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002131
Gilles Peskinefe119512018-07-08 21:39:34 +02002132 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2133 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002134
Gilles Peskinefe119512018-07-08 21:39:34 +02002135 TEST_ASSERT( psa_cipher_set_iv( &operation,
2136 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002137 output_buffer_size = (size_t) input->len +
2138 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002139 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002140
Gilles Peskine4abf7412018-06-18 16:35:34 +02002141 TEST_ASSERT( psa_cipher_update( &operation,
2142 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002143 output, output_buffer_size,
2144 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002145 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146 status = psa_cipher_finish( &operation,
2147 output + function_output_length,
2148 output_buffer_size,
2149 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002150 total_output_length += function_output_length;
2151
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002152 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153 if( expected_status == PSA_SUCCESS )
2154 {
2155 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002156 ASSERT_COMPARE( expected_output->x, expected_output->len,
2157 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002158 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002159
Gilles Peskine50e586b2018-06-08 14:28:46 +02002160exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002161 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002162 psa_destroy_key( key_slot );
2163 mbedtls_psa_crypto_free( );
2164}
2165/* END_CASE */
2166
2167/* BEGIN_CASE */
2168void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002169 data_t *key,
2170 data_t *input,
2171 int first_part_size,
2172 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002173{
2174 int key_slot = 1;
2175 psa_key_type_t key_type = key_type_arg;
2176 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002177 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002178 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002179 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180 size_t output_buffer_size = 0;
2181 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002182 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002183 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002184 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185
Gilles Peskine50e586b2018-06-08 14:28:46 +02002186 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002187 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002188 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002189 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2190 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2191 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002192
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002193 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2194 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195
2196 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2197
Moran Pekered346952018-07-05 15:22:45 +03002198 psa_key_policy_init( &policy );
2199 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2200 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2201
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002203 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002204
Gilles Peskinefe119512018-07-08 21:39:34 +02002205 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2206 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002207
Gilles Peskinefe119512018-07-08 21:39:34 +02002208 TEST_ASSERT( psa_cipher_set_iv( &operation,
2209 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002210 output_buffer_size = (size_t) input->len +
2211 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002212 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002213
Gilles Peskine4abf7412018-06-18 16:35:34 +02002214 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002215 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002216 output, output_buffer_size,
2217 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002218 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002219 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002220 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002221 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002222 output, output_buffer_size,
2223 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002224 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002225 TEST_ASSERT( psa_cipher_finish( &operation,
2226 output + function_output_length,
2227 output_buffer_size,
2228 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002229 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002230 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2231
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002232 ASSERT_COMPARE( expected_output->x, expected_output->len,
2233 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002234
2235exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002236 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002237 psa_destroy_key( key_slot );
2238 mbedtls_psa_crypto_free( );
2239}
2240/* END_CASE */
2241
2242/* BEGIN_CASE */
2243void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002244 data_t *key,
2245 data_t *input,
2246 int first_part_size,
2247 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002248{
2249 int key_slot = 1;
2250
2251 psa_key_type_t key_type = key_type_arg;
2252 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002253 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002254 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002255 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002256 size_t output_buffer_size = 0;
2257 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002258 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002259 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002260 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002261
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002263 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002264 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002265 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2266 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2267 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002268
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002269 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2270 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271
2272 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2273
Moran Pekered346952018-07-05 15:22:45 +03002274 psa_key_policy_init( &policy );
2275 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2276 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2277
Gilles Peskine50e586b2018-06-08 14:28:46 +02002278 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002279 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002280
Gilles Peskinefe119512018-07-08 21:39:34 +02002281 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2282 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002283
Gilles Peskinefe119512018-07-08 21:39:34 +02002284 TEST_ASSERT( psa_cipher_set_iv( &operation,
2285 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002286
mohammad16033d91abe2018-07-03 13:15:54 +03002287 output_buffer_size = (size_t) input->len +
2288 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002289 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002290
Gilles Peskine4abf7412018-06-18 16:35:34 +02002291 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2292 TEST_ASSERT( psa_cipher_update( &operation,
2293 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002294 output, output_buffer_size,
2295 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002296 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002297 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002298 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002299 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002300 output, output_buffer_size,
2301 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002302 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002303 TEST_ASSERT( psa_cipher_finish( &operation,
2304 output + function_output_length,
2305 output_buffer_size,
2306 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002307 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002308 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2309
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002310 ASSERT_COMPARE( expected_output->x, expected_output->len,
2311 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002312
2313exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002314 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002315 psa_destroy_key( key_slot );
2316 mbedtls_psa_crypto_free( );
2317}
2318/* END_CASE */
2319
Gilles Peskine50e586b2018-06-08 14:28:46 +02002320/* BEGIN_CASE */
2321void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002322 data_t *key,
2323 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002324 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002325{
2326 int key_slot = 1;
2327 psa_status_t status;
2328 psa_key_type_t key_type = key_type_arg;
2329 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002330 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002331 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002332 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002333 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002334 size_t output_buffer_size = 0;
2335 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002336 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002338 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339
Gilles Peskine50e586b2018-06-08 14:28:46 +02002340 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002341 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002343 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2344 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2345 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002347 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2348 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002349
2350 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2351
Moran Pekered346952018-07-05 15:22:45 +03002352 psa_key_policy_init( &policy );
2353 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2354 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2355
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002357 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002358
Gilles Peskinefe119512018-07-08 21:39:34 +02002359 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2360 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002361
Gilles Peskinefe119512018-07-08 21:39:34 +02002362 TEST_ASSERT( psa_cipher_set_iv( &operation,
2363 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002364
mohammad16033d91abe2018-07-03 13:15:54 +03002365 output_buffer_size = (size_t) input->len +
2366 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002367 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002368
Gilles Peskine4abf7412018-06-18 16:35:34 +02002369 TEST_ASSERT( psa_cipher_update( &operation,
2370 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002371 output, output_buffer_size,
2372 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002373 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002374 status = psa_cipher_finish( &operation,
2375 output + function_output_length,
2376 output_buffer_size,
2377 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002378 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002379 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380
2381 if( expected_status == PSA_SUCCESS )
2382 {
2383 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002384 ASSERT_COMPARE( expected_output->x, expected_output->len,
2385 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386 }
2387
Gilles Peskine50e586b2018-06-08 14:28:46 +02002388exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002389 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002390 psa_destroy_key( key_slot );
2391 mbedtls_psa_crypto_free( );
2392}
2393/* END_CASE */
2394
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395/* BEGIN_CASE */
2396void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002397 data_t *key,
2398 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002399{
2400 int key_slot = 1;
2401 psa_key_type_t key_type = key_type_arg;
2402 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002403 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002404 size_t iv_size = 16;
2405 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002406 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002407 size_t output1_size = 0;
2408 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002409 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002410 size_t output2_size = 0;
2411 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002412 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002413 psa_cipher_operation_t operation1;
2414 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002415 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002416
mohammad1603d7d7ba52018-03-12 18:51:53 +02002417 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002418 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2420 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002421
mohammad1603d7d7ba52018-03-12 18:51:53 +02002422 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2423
Moran Pekered346952018-07-05 15:22:45 +03002424 psa_key_policy_init( &policy );
2425 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2426 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2427
mohammad1603d7d7ba52018-03-12 18:51:53 +02002428 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002429 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002430
Gilles Peskinefe119512018-07-08 21:39:34 +02002431 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2432 key_slot, alg ) == PSA_SUCCESS );
2433 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2434 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002435
Gilles Peskinefe119512018-07-08 21:39:34 +02002436 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2437 iv, iv_size,
2438 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002439 output1_size = (size_t) input->len +
2440 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002441 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002442
Gilles Peskine4abf7412018-06-18 16:35:34 +02002443 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002444 output1, output1_size,
2445 &output1_length ) == PSA_SUCCESS );
2446 TEST_ASSERT( psa_cipher_finish( &operation1,
2447 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002448 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002449
Gilles Peskine048b7f02018-06-08 14:20:49 +02002450 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002451
2452 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2453
2454 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002455 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002456
Gilles Peskinefe119512018-07-08 21:39:34 +02002457 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2458 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002459 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2460 output2, output2_size,
2461 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002462 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002463 TEST_ASSERT( psa_cipher_finish( &operation2,
2464 output2 + output2_length,
2465 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002466 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002467
Gilles Peskine048b7f02018-06-08 14:20:49 +02002468 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002469
Janos Follath25c4fa82018-07-06 16:23:25 +01002470 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002471
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002472 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002473
2474exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002475 mbedtls_free( output1 );
2476 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002477 psa_destroy_key( key_slot );
2478 mbedtls_psa_crypto_free( );
2479}
2480/* END_CASE */
2481
2482/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002483void cipher_verify_output_multipart( int alg_arg,
2484 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002485 data_t *key,
2486 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002488{
2489 int key_slot = 1;
2490 psa_key_type_t key_type = key_type_arg;
2491 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002492 unsigned char iv[16] = {0};
2493 size_t iv_size = 16;
2494 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002495 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002496 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002497 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002498 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002499 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002500 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002501 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002502 psa_cipher_operation_t operation1;
2503 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002504 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002505
Moran Pekerded84402018-06-06 16:36:50 +03002506 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002507 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002508 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2509 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002510
Moran Pekerded84402018-06-06 16:36:50 +03002511 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2512
Moran Pekered346952018-07-05 15:22:45 +03002513 psa_key_policy_init( &policy );
2514 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2515 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2516
Moran Pekerded84402018-06-06 16:36:50 +03002517 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002518 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002519
Gilles Peskinefe119512018-07-08 21:39:34 +02002520 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2521 key_slot, alg ) == PSA_SUCCESS );
2522 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2523 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002524
Gilles Peskinefe119512018-07-08 21:39:34 +02002525 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2526 iv, iv_size,
2527 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002528 output1_buffer_size = (size_t) input->len +
2529 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002530 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002531
Gilles Peskine4abf7412018-06-18 16:35:34 +02002532 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002533
itayzafrir3e02b3b2018-06-12 17:06:52 +03002534 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002535 output1, output1_buffer_size,
2536 &function_output_length ) == PSA_SUCCESS );
2537 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002538
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002539 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002540 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002541 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002542 output1, output1_buffer_size,
2543 &function_output_length ) == PSA_SUCCESS );
2544 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002545
Gilles Peskine048b7f02018-06-08 14:20:49 +02002546 TEST_ASSERT( psa_cipher_finish( &operation1,
2547 output1 + output1_length,
2548 output1_buffer_size - output1_length,
2549 &function_output_length ) == PSA_SUCCESS );
2550 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002551
2552 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2553
Gilles Peskine048b7f02018-06-08 14:20:49 +02002554 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002555 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002556
Gilles Peskinefe119512018-07-08 21:39:34 +02002557 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2558 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002559
2560 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002561 output2, output2_buffer_size,
2562 &function_output_length ) == PSA_SUCCESS );
2563 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002564
Gilles Peskine048b7f02018-06-08 14:20:49 +02002565 TEST_ASSERT( psa_cipher_update( &operation2,
2566 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002567 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002568 output2, output2_buffer_size,
2569 &function_output_length ) == PSA_SUCCESS );
2570 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002571
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002572 TEST_ASSERT( psa_cipher_finish( &operation2,
2573 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002574 output2_buffer_size - output2_length,
2575 &function_output_length ) == PSA_SUCCESS );
2576 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002577
Janos Follath25c4fa82018-07-06 16:23:25 +01002578 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002579
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002580 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002581
2582exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002583 mbedtls_free( output1 );
2584 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002585 psa_destroy_key( key_slot );
2586 mbedtls_psa_crypto_free( );
2587}
2588/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002589
Gilles Peskine20035e32018-02-03 22:44:14 +01002590/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002591void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002592 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002593 data_t *nonce,
2594 data_t *additional_data,
2595 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002596 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002597{
2598 int slot = 1;
2599 psa_key_type_t key_type = key_type_arg;
2600 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002601 unsigned char *output_data = NULL;
2602 size_t output_size = 0;
2603 size_t output_length = 0;
2604 unsigned char *output_data2 = NULL;
2605 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002606 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002607 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002608 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002609
Gilles Peskinea1cac842018-06-11 19:33:02 +02002610 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002611 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002612 TEST_ASSERT( nonce != NULL );
2613 TEST_ASSERT( additional_data != NULL );
2614 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2615 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2616 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2617 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2618
Gilles Peskine4abf7412018-06-18 16:35:34 +02002619 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002620 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002621
2622 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2623
2624 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002625 psa_key_policy_set_usage( &policy,
2626 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2627 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002628 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2629
2630 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002631 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002632
2633 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002634 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002635 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002636 additional_data->len,
2637 input_data->x, input_data->len,
2638 output_data, output_size,
2639 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002640
2641 if( PSA_SUCCESS == expected_result )
2642 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002643 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002644
2645 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002646 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002647 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002648 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002649 output_data, output_length,
2650 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002651 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002652
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002653 ASSERT_COMPARE( input_data->x, input_data->len,
2654 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002655 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002656
Gilles Peskinea1cac842018-06-11 19:33:02 +02002657exit:
2658 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002659 mbedtls_free( output_data );
2660 mbedtls_free( output_data2 );
2661 mbedtls_psa_crypto_free( );
2662}
2663/* END_CASE */
2664
2665/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002666void aead_encrypt( int key_type_arg, data_t *key_data,
2667 int alg_arg,
2668 data_t *nonce,
2669 data_t *additional_data,
2670 data_t *input_data,
2671 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672{
2673 int slot = 1;
2674 psa_key_type_t key_type = key_type_arg;
2675 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002676 unsigned char *output_data = NULL;
2677 size_t output_size = 0;
2678 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002679 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002680 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002681
Gilles Peskinea1cac842018-06-11 19:33:02 +02002682 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002683 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002684 TEST_ASSERT( additional_data != NULL );
2685 TEST_ASSERT( nonce != NULL );
2686 TEST_ASSERT( expected_result != NULL );
2687 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2688 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2689 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2690 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2691 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2692
Gilles Peskine4abf7412018-06-18 16:35:34 +02002693 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002694 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002695
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2697
2698 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002699 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002700 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2701
2702 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002703 key_data->x,
2704 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002705
2706 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002707 nonce->x, nonce->len,
2708 additional_data->x, additional_data->len,
2709 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002710 output_data, output_size,
2711 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002712
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002713 ASSERT_COMPARE( expected_result->x, expected_result->len,
2714 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002715
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716exit:
2717 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002718 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002719 mbedtls_psa_crypto_free( );
2720}
2721/* END_CASE */
2722
2723/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002724void aead_decrypt( int key_type_arg, data_t *key_data,
2725 int alg_arg,
2726 data_t *nonce,
2727 data_t *additional_data,
2728 data_t *input_data,
2729 data_t *expected_data,
2730 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002731{
2732 int slot = 1;
2733 psa_key_type_t key_type = key_type_arg;
2734 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002735 unsigned char *output_data = NULL;
2736 size_t output_size = 0;
2737 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002738 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002739 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002740 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002741
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002743 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002744 TEST_ASSERT( additional_data != NULL );
2745 TEST_ASSERT( nonce != NULL );
2746 TEST_ASSERT( expected_data != NULL );
2747 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2748 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2749 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2750 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2751 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2752
Gilles Peskine4abf7412018-06-18 16:35:34 +02002753 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002754 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002755
Gilles Peskinea1cac842018-06-11 19:33:02 +02002756 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2757
2758 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002759 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002760 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2761
2762 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002763 key_data->x,
2764 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002765
2766 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002767 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002768 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002769 additional_data->len,
2770 input_data->x, input_data->len,
2771 output_data, output_size,
2772 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002773
Gilles Peskine2d277862018-06-18 15:41:12 +02002774 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002775 ASSERT_COMPARE( expected_data->x, expected_data->len,
2776 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002777
Gilles Peskinea1cac842018-06-11 19:33:02 +02002778exit:
2779 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002780 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002781 mbedtls_psa_crypto_free( );
2782}
2783/* END_CASE */
2784
2785/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002786void signature_size( int type_arg,
2787 int bits,
2788 int alg_arg,
2789 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002790{
2791 psa_key_type_t type = type_arg;
2792 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002793 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002794 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2795exit:
2796 ;
2797}
2798/* END_CASE */
2799
2800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002801void sign_deterministic( int key_type_arg, data_t *key_data,
2802 int alg_arg, data_t *input_data,
2803 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002804{
2805 int slot = 1;
2806 psa_key_type_t key_type = key_type_arg;
2807 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002808 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002809 unsigned char *signature = NULL;
2810 size_t signature_size;
2811 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002812 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002813
Gilles Peskine20035e32018-02-03 22:44:14 +01002814 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002815 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002816 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002817 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2818 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2819 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002820
2821 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2822
mohammad1603a97cb8c2018-03-28 03:46:26 -07002823 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002824 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002825 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2826
Gilles Peskine20035e32018-02-03 22:44:14 +01002827 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002828 key_data->x,
2829 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002830 TEST_ASSERT( psa_get_key_information( slot,
2831 NULL,
2832 &key_bits ) == PSA_SUCCESS );
2833
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002834 /* Allocate a buffer which has the size advertized by the
2835 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002836 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2837 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002838 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002839 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002840 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002841
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002842 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002843 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002844 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002845 signature, signature_size,
2846 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002847 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002848 ASSERT_COMPARE( output_data->x, output_data->len,
2849 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002850
2851exit:
2852 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002853 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002854 mbedtls_psa_crypto_free( );
2855}
2856/* END_CASE */
2857
2858/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002859void sign_fail( int key_type_arg, data_t *key_data,
2860 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002861 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002862{
2863 int slot = 1;
2864 psa_key_type_t key_type = key_type_arg;
2865 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002866 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002867 psa_status_t actual_status;
2868 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002869 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002870 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002871 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002872
Gilles Peskine20035e32018-02-03 22:44:14 +01002873 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002874 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002875 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2876 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2877
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002878 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002879
2880 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2881
mohammad1603a97cb8c2018-03-28 03:46:26 -07002882 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002883 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002884 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2885
Gilles Peskine20035e32018-02-03 22:44:14 +01002886 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002887 key_data->x,
2888 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002889
2890 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002891 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002892 signature, signature_size,
2893 &signature_length );
2894 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002895 /* The value of *signature_length is unspecified on error, but
2896 * whatever it is, it should be less than signature_size, so that
2897 * if the caller tries to read *signature_length bytes without
2898 * checking the error code then they don't overflow a buffer. */
2899 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002900
2901exit:
2902 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002903 mbedtls_free( signature );
2904 mbedtls_psa_crypto_free( );
2905}
2906/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002907
2908/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002909void sign_verify( int key_type_arg, data_t *key_data,
2910 int alg_arg, data_t *input_data )
2911{
2912 int slot = 1;
2913 psa_key_type_t key_type = key_type_arg;
2914 psa_algorithm_t alg = alg_arg;
2915 size_t key_bits;
2916 unsigned char *signature = NULL;
2917 size_t signature_size;
2918 size_t signature_length = 0xdeadbeef;
2919 psa_key_policy_t policy;
2920
2921 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2922
2923 psa_key_policy_init( &policy );
2924 psa_key_policy_set_usage( &policy,
2925 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2926 alg );
2927 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2928
2929 TEST_ASSERT( psa_import_key( slot, key_type,
2930 key_data->x,
2931 key_data->len ) == PSA_SUCCESS );
2932 TEST_ASSERT( psa_get_key_information( slot,
2933 NULL,
2934 &key_bits ) == PSA_SUCCESS );
2935
2936 /* Allocate a buffer which has the size advertized by the
2937 * library. */
2938 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2939 key_bits, alg );
2940 TEST_ASSERT( signature_size != 0 );
2941 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002942 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002943
2944 /* Perform the signature. */
2945 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2946 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002947 signature, signature_size,
2948 &signature_length ) == PSA_SUCCESS );
2949 /* Check that the signature length looks sensible. */
2950 TEST_ASSERT( signature_length <= signature_size );
2951 TEST_ASSERT( signature_length > 0 );
2952
2953 /* Use the library to verify that the signature is correct. */
2954 TEST_ASSERT( psa_asymmetric_verify(
2955 slot, alg,
2956 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002957 signature, signature_length ) == PSA_SUCCESS );
2958
2959 if( input_data->len != 0 )
2960 {
2961 /* Flip a bit in the input and verify that the signature is now
2962 * detected as invalid. Flip a bit at the beginning, not at the end,
2963 * because ECDSA may ignore the last few bits of the input. */
2964 input_data->x[0] ^= 1;
2965 TEST_ASSERT( psa_asymmetric_verify(
2966 slot, alg,
2967 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002968 signature,
2969 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2970 }
2971
2972exit:
2973 psa_destroy_key( slot );
2974 mbedtls_free( signature );
2975 mbedtls_psa_crypto_free( );
2976}
2977/* END_CASE */
2978
2979/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980void asymmetric_verify( int key_type_arg, data_t *key_data,
2981 int alg_arg, data_t *hash_data,
2982 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002983{
2984 int slot = 1;
2985 psa_key_type_t key_type = key_type_arg;
2986 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002987 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002988
Gilles Peskine69c12672018-06-28 00:07:19 +02002989 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2990
itayzafrir5c753392018-05-08 11:18:38 +03002991 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002992 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002993 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002994 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2995 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2996 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002997
2998 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2999
3000 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003001 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03003002 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3003
3004 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003005 key_data->x,
3006 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003007
3008 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003009 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003010 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003011 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003012exit:
3013 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03003014 mbedtls_psa_crypto_free( );
3015}
3016/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003017
3018/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003019void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3020 int alg_arg, data_t *hash_data,
3021 data_t *signature_data,
3022 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003023{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003024 int slot = 1;
3025 psa_key_type_t key_type = key_type_arg;
3026 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003027 psa_status_t actual_status;
3028 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003029 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003030
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003031 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003034 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3035 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3036 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003037
3038 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3039
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003040 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003041 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003042 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3043
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003044 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003045 key_data->x,
3046 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047
3048 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003049 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003050 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003051 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003052
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003053 TEST_ASSERT( actual_status == expected_status );
3054
3055exit:
3056 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003057 mbedtls_psa_crypto_free( );
3058}
3059/* END_CASE */
3060
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003062void asymmetric_encrypt( int key_type_arg,
3063 data_t *key_data,
3064 int alg_arg,
3065 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003066 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003067 int expected_output_length_arg,
3068 int expected_status_arg )
3069{
3070 int slot = 1;
3071 psa_key_type_t key_type = key_type_arg;
3072 psa_algorithm_t alg = alg_arg;
3073 size_t expected_output_length = expected_output_length_arg;
3074 size_t key_bits;
3075 unsigned char *output = NULL;
3076 size_t output_size;
3077 size_t output_length = ~0;
3078 psa_status_t actual_status;
3079 psa_status_t expected_status = expected_status_arg;
3080 psa_key_policy_t policy;
3081
3082 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3083
3084 /* Import the key */
3085 psa_key_policy_init( &policy );
3086 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
3087 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3088 TEST_ASSERT( psa_import_key( slot, key_type,
3089 key_data->x,
3090 key_data->len ) == PSA_SUCCESS );
3091
3092 /* Determine the maximum output length */
3093 TEST_ASSERT( psa_get_key_information( slot,
3094 NULL,
3095 &key_bits ) == PSA_SUCCESS );
3096 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003097 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003098
3099 /* Encrypt the input */
3100 actual_status = psa_asymmetric_encrypt( slot, alg,
3101 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003102 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003103 output, output_size,
3104 &output_length );
3105 TEST_ASSERT( actual_status == expected_status );
3106 TEST_ASSERT( output_length == expected_output_length );
3107
Gilles Peskine68428122018-06-30 18:42:41 +02003108 /* If the label is empty, the test framework puts a non-null pointer
3109 * in label->x. Test that a null pointer works as well. */
3110 if( label->len == 0 )
3111 {
3112 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003113 if( output_size != 0 )
3114 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003115 actual_status = psa_asymmetric_encrypt( slot, alg,
3116 input_data->x, input_data->len,
3117 NULL, label->len,
3118 output, output_size,
3119 &output_length );
3120 TEST_ASSERT( actual_status == expected_status );
3121 TEST_ASSERT( output_length == expected_output_length );
3122 }
3123
Gilles Peskine656896e2018-06-29 19:12:28 +02003124exit:
3125 psa_destroy_key( slot );
3126 mbedtls_free( output );
3127 mbedtls_psa_crypto_free( );
3128}
3129/* END_CASE */
3130
3131/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003132void asymmetric_encrypt_decrypt( int key_type_arg,
3133 data_t *key_data,
3134 int alg_arg,
3135 data_t *input_data,
3136 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137{
3138 int slot = 1;
3139 psa_key_type_t key_type = key_type_arg;
3140 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003141 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003143 size_t output_size;
3144 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003145 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003146 size_t output2_size;
3147 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003148 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003149
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003152 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3153 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3154
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3156
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003157 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003158 psa_key_policy_set_usage( &policy,
3159 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003160 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003161 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3162
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003163 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003164 key_data->x,
3165 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003167
3168 /* Determine the maximum ciphertext length */
3169 TEST_ASSERT( psa_get_key_information( slot,
3170 NULL,
3171 &key_bits ) == PSA_SUCCESS );
3172 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003173 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003174 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003175 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003176
Gilles Peskineeebd7382018-06-08 18:11:54 +02003177 /* We test encryption by checking that encrypt-then-decrypt gives back
3178 * the original plaintext because of the non-optional random
3179 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02003180 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003181 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003182 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003183 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003184 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003185 /* We don't know what ciphertext length to expect, but check that
3186 * it looks sensible. */
3187 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003188
Gilles Peskine2d277862018-06-18 15:41:12 +02003189 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003190 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003191 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003192 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003193 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003194 ASSERT_COMPARE( input_data->x, input_data->len,
3195 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196
3197exit:
3198 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003199 mbedtls_free( output );
3200 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202}
3203/* END_CASE */
3204
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003205/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003206void asymmetric_decrypt( int key_type_arg,
3207 data_t *key_data,
3208 int alg_arg,
3209 data_t *input_data,
3210 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003211 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212{
3213 int slot = 1;
3214 psa_key_type_t key_type = key_type_arg;
3215 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003217 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003218 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003219 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003220
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003223 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003224 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3225 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3226 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3227
Gilles Peskine4abf7412018-06-18 16:35:34 +02003228 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003229 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003230
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003231 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3232
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003233 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003234 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003235 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3236
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003238 key_data->x,
3239 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003241 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003242 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003243 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003244 output,
3245 output_size,
3246 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003247 ASSERT_COMPARE( expected_data->x, expected_data->len,
3248 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249
Gilles Peskine68428122018-06-30 18:42:41 +02003250 /* If the label is empty, the test framework puts a non-null pointer
3251 * in label->x. Test that a null pointer works as well. */
3252 if( label->len == 0 )
3253 {
3254 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003255 if( output_size != 0 )
3256 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003257 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3258 input_data->x, input_data->len,
3259 NULL, label->len,
3260 output,
3261 output_size,
3262 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003263 ASSERT_COMPARE( expected_data->x, expected_data->len,
3264 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003265 }
3266
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267exit:
3268 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003269 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003271}
3272/* END_CASE */
3273
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003274/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003275void asymmetric_decrypt_fail( int key_type_arg,
3276 data_t *key_data,
3277 int alg_arg,
3278 data_t *input_data,
3279 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003280 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282 int slot = 1;
3283 psa_key_type_t key_type = key_type_arg;
3284 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003286 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003287 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288 psa_status_t actual_status;
3289 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003290 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003294 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3295 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3296
Gilles Peskine4abf7412018-06-18 16:35:34 +02003297 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003298 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003299
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3301
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003302 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003303 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003304 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3305
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003307 key_data->x,
3308 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003309
Gilles Peskine2d277862018-06-18 15:41:12 +02003310 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003311 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003312 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003313 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003314 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003316 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317
Gilles Peskine68428122018-06-30 18:42:41 +02003318 /* If the label is empty, the test framework puts a non-null pointer
3319 * in label->x. Test that a null pointer works as well. */
3320 if( label->len == 0 )
3321 {
3322 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003323 if( output_size != 0 )
3324 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003325 actual_status = psa_asymmetric_decrypt( slot, alg,
3326 input_data->x, input_data->len,
3327 NULL, label->len,
3328 output, output_size,
3329 &output_length );
3330 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003331 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003332 }
3333
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003334exit:
3335 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003336 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337 mbedtls_psa_crypto_free( );
3338}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003339/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003340
3341/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003342void derive_setup( int key_type_arg,
3343 data_t *key_data,
3344 int alg_arg,
3345 data_t *salt,
3346 data_t *label,
3347 int requested_capacity_arg,
3348 int expected_status_arg )
3349{
3350 psa_key_slot_t slot = 1;
3351 size_t key_type = key_type_arg;
3352 psa_algorithm_t alg = alg_arg;
3353 size_t requested_capacity = requested_capacity_arg;
3354 psa_status_t expected_status = expected_status_arg;
3355 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3356 psa_key_policy_t policy;
3357
3358 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3359
3360 psa_key_policy_init( &policy );
3361 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3362 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3363
3364 TEST_ASSERT( psa_import_key( slot, key_type,
3365 key_data->x,
3366 key_data->len ) == PSA_SUCCESS );
3367
3368 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3369 salt->x, salt->len,
3370 label->x, label->len,
3371 requested_capacity ) == expected_status );
3372
3373exit:
3374 psa_generator_abort( &generator );
3375 psa_destroy_key( slot );
3376 mbedtls_psa_crypto_free( );
3377}
3378/* END_CASE */
3379
3380/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003381void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003382{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003383 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003384 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003385 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003386 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003387 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003388 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003389 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3390 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3391 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003392 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003393
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003394 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3395
3396 psa_key_policy_init( &policy );
3397 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3398 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3399
3400 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003401 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003402 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003403
3404 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003405 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003406 NULL, 0,
3407 NULL, 0,
3408 capacity ) == PSA_SUCCESS );
3409
3410 /* state of generator shouldn't allow additional generation */
3411 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3412 NULL, 0,
3413 NULL, 0,
3414 capacity ) == PSA_ERROR_BAD_STATE );
3415
3416 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3417 == PSA_SUCCESS );
3418
3419 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3420 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3421
3422
3423exit:
3424 psa_generator_abort( &generator );
3425 psa_destroy_key( base_key );
3426 mbedtls_psa_crypto_free( );
3427}
3428/* END_CASE */
3429
3430
3431/* BEGIN_CASE */
3432void test_derive_invalid_generator_tests( )
3433{
3434 uint8_t output_buffer[16];
3435 size_t buffer_size = 16;
3436 size_t capacity = 0;
3437 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3438
Nir Sonnenschein50789302018-10-31 12:16:38 +02003439 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003440 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003441
3442 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003443 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003444
Nir Sonnenschein50789302018-10-31 12:16:38 +02003445 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003446
Nir Sonnenschein50789302018-10-31 12:16:38 +02003447 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003448 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003449
Nir Sonnenschein50789302018-10-31 12:16:38 +02003450 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003451 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003452
3453exit:
3454 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003455}
3456/* END_CASE */
3457
3458/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003459void derive_output( int alg_arg,
3460 data_t *key_data,
3461 data_t *salt,
3462 data_t *label,
3463 int requested_capacity_arg,
3464 data_t *expected_output1,
3465 data_t *expected_output2 )
3466{
3467 psa_key_slot_t slot = 1;
3468 psa_algorithm_t alg = alg_arg;
3469 size_t requested_capacity = requested_capacity_arg;
3470 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3471 uint8_t *expected_outputs[2] =
3472 {expected_output1->x, expected_output2->x};
3473 size_t output_sizes[2] =
3474 {expected_output1->len, expected_output2->len};
3475 size_t output_buffer_size = 0;
3476 uint8_t *output_buffer = NULL;
3477 size_t expected_capacity;
3478 size_t current_capacity;
3479 psa_key_policy_t policy;
3480 psa_status_t status;
3481 unsigned i;
3482
3483 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3484 {
3485 if( output_sizes[i] > output_buffer_size )
3486 output_buffer_size = output_sizes[i];
3487 if( output_sizes[i] == 0 )
3488 expected_outputs[i] = NULL;
3489 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003490 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003491 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3492
3493 psa_key_policy_init( &policy );
3494 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3495 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3496
3497 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3498 key_data->x,
3499 key_data->len ) == PSA_SUCCESS );
3500
3501 /* Extraction phase. */
3502 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3503 salt->x, salt->len,
3504 label->x, label->len,
3505 requested_capacity ) == PSA_SUCCESS );
3506 TEST_ASSERT( psa_get_generator_capacity( &generator,
3507 &current_capacity ) ==
3508 PSA_SUCCESS );
3509 TEST_ASSERT( current_capacity == requested_capacity );
3510 expected_capacity = requested_capacity;
3511
3512 /* Expansion phase. */
3513 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3514 {
3515 /* Read some bytes. */
3516 status = psa_generator_read( &generator,
3517 output_buffer, output_sizes[i] );
3518 if( expected_capacity == 0 && output_sizes[i] == 0 )
3519 {
3520 /* Reading 0 bytes when 0 bytes are available can go either way. */
3521 TEST_ASSERT( status == PSA_SUCCESS ||
3522 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3523 continue;
3524 }
3525 else if( expected_capacity == 0 ||
3526 output_sizes[i] > expected_capacity )
3527 {
3528 /* Capacity exceeded. */
3529 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3530 expected_capacity = 0;
3531 continue;
3532 }
3533 /* Success. Check the read data. */
3534 TEST_ASSERT( status == PSA_SUCCESS );
3535 if( output_sizes[i] != 0 )
3536 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3537 output_sizes[i] ) == 0 );
3538 /* Check the generator status. */
3539 expected_capacity -= output_sizes[i];
3540 TEST_ASSERT( psa_get_generator_capacity( &generator,
3541 &current_capacity ) ==
3542 PSA_SUCCESS );
3543 TEST_ASSERT( expected_capacity == current_capacity );
3544 }
3545 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3546
3547exit:
3548 mbedtls_free( output_buffer );
3549 psa_generator_abort( &generator );
3550 psa_destroy_key( slot );
3551 mbedtls_psa_crypto_free( );
3552}
3553/* END_CASE */
3554
3555/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003556void derive_full( int alg_arg,
3557 data_t *key_data,
3558 data_t *salt,
3559 data_t *label,
3560 int requested_capacity_arg )
3561{
3562 psa_key_slot_t slot = 1;
3563 psa_algorithm_t alg = alg_arg;
3564 size_t requested_capacity = requested_capacity_arg;
3565 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3566 unsigned char output_buffer[16];
3567 size_t expected_capacity = requested_capacity;
3568 size_t current_capacity;
3569 psa_key_policy_t policy;
3570
3571 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3572
3573 psa_key_policy_init( &policy );
3574 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3575 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3576
3577 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3578 key_data->x,
3579 key_data->len ) == PSA_SUCCESS );
3580
3581 /* Extraction phase. */
3582 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3583 salt->x, salt->len,
3584 label->x, label->len,
3585 requested_capacity ) == PSA_SUCCESS );
3586 TEST_ASSERT( psa_get_generator_capacity( &generator,
3587 &current_capacity ) ==
3588 PSA_SUCCESS );
3589 TEST_ASSERT( current_capacity == expected_capacity );
3590
3591 /* Expansion phase. */
3592 while( current_capacity > 0 )
3593 {
3594 size_t read_size = sizeof( output_buffer );
3595 if( read_size > current_capacity )
3596 read_size = current_capacity;
3597 TEST_ASSERT( psa_generator_read( &generator,
3598 output_buffer,
3599 read_size ) == PSA_SUCCESS );
3600 expected_capacity -= read_size;
3601 TEST_ASSERT( psa_get_generator_capacity( &generator,
3602 &current_capacity ) ==
3603 PSA_SUCCESS );
3604 TEST_ASSERT( current_capacity == expected_capacity );
3605 }
3606
3607 /* Check that the generator refuses to go over capacity. */
3608 TEST_ASSERT( psa_generator_read( &generator,
3609 output_buffer,
3610 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3611
3612 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3613
3614exit:
3615 psa_generator_abort( &generator );
3616 psa_destroy_key( slot );
3617 mbedtls_psa_crypto_free( );
3618}
3619/* END_CASE */
3620
3621/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003622void derive_key_exercise( int alg_arg,
3623 data_t *key_data,
3624 data_t *salt,
3625 data_t *label,
3626 int derived_type_arg,
3627 int derived_bits_arg,
3628 int derived_usage_arg,
3629 int derived_alg_arg )
3630{
3631 psa_key_slot_t base_key = 1;
3632 psa_key_slot_t derived_key = 2;
3633 psa_algorithm_t alg = alg_arg;
3634 psa_key_type_t derived_type = derived_type_arg;
3635 size_t derived_bits = derived_bits_arg;
3636 psa_key_usage_t derived_usage = derived_usage_arg;
3637 psa_algorithm_t derived_alg = derived_alg_arg;
3638 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3639 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3640 psa_key_policy_t policy;
3641 psa_key_type_t got_type;
3642 size_t got_bits;
3643
3644 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3645
3646 psa_key_policy_init( &policy );
3647 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3648 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3649 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3650 key_data->x,
3651 key_data->len ) == PSA_SUCCESS );
3652
3653 /* Derive a key. */
3654 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3655 salt->x, salt->len,
3656 label->x, label->len,
3657 capacity ) == PSA_SUCCESS );
3658 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3659 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3660 TEST_ASSERT( psa_generator_import_key( derived_key,
3661 derived_type,
3662 derived_bits,
3663 &generator ) == PSA_SUCCESS );
3664
3665 /* Test the key information */
3666 TEST_ASSERT( psa_get_key_information( derived_key,
3667 &got_type,
3668 &got_bits ) == PSA_SUCCESS );
3669 TEST_ASSERT( got_type == derived_type );
3670 TEST_ASSERT( got_bits == derived_bits );
3671
3672 /* Exercise the derived key. */
3673 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3674 goto exit;
3675
3676exit:
3677 psa_generator_abort( &generator );
3678 psa_destroy_key( base_key );
3679 psa_destroy_key( derived_key );
3680 mbedtls_psa_crypto_free( );
3681}
3682/* END_CASE */
3683
3684/* BEGIN_CASE */
3685void derive_key_export( int alg_arg,
3686 data_t *key_data,
3687 data_t *salt,
3688 data_t *label,
3689 int bytes1_arg,
3690 int bytes2_arg )
3691{
3692 psa_key_slot_t base_key = 1;
3693 psa_key_slot_t derived_key = 2;
3694 psa_algorithm_t alg = alg_arg;
3695 size_t bytes1 = bytes1_arg;
3696 size_t bytes2 = bytes2_arg;
3697 size_t capacity = bytes1 + bytes2;
3698 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003699 uint8_t *output_buffer = NULL;
3700 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003701 psa_key_policy_t policy;
3702 size_t length;
3703
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003704 ASSERT_ALLOC( output_buffer, capacity );
3705 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003706 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3707
3708 psa_key_policy_init( &policy );
3709 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3710 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3711 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3712 key_data->x,
3713 key_data->len ) == PSA_SUCCESS );
3714
3715 /* Derive some material and output it. */
3716 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3717 salt->x, salt->len,
3718 label->x, label->len,
3719 capacity ) == PSA_SUCCESS );
3720 TEST_ASSERT( psa_generator_read( &generator,
3721 output_buffer,
3722 capacity ) == PSA_SUCCESS );
3723 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3724
3725 /* Derive the same output again, but this time store it in key objects. */
3726 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3727 salt->x, salt->len,
3728 label->x, label->len,
3729 capacity ) == PSA_SUCCESS );
3730 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3731 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3732 TEST_ASSERT( psa_generator_import_key( derived_key,
3733 PSA_KEY_TYPE_RAW_DATA,
3734 PSA_BYTES_TO_BITS( bytes1 ),
3735 &generator ) == PSA_SUCCESS );
3736 TEST_ASSERT( psa_export_key( derived_key,
3737 export_buffer, bytes1,
3738 &length ) == PSA_SUCCESS );
3739 TEST_ASSERT( length == bytes1 );
3740 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3741 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3742 TEST_ASSERT( psa_generator_import_key( derived_key,
3743 PSA_KEY_TYPE_RAW_DATA,
3744 PSA_BYTES_TO_BITS( bytes2 ),
3745 &generator ) == PSA_SUCCESS );
3746 TEST_ASSERT( psa_export_key( derived_key,
3747 export_buffer + bytes1, bytes2,
3748 &length ) == PSA_SUCCESS );
3749 TEST_ASSERT( length == bytes2 );
3750
3751 /* Compare the outputs from the two runs. */
3752 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3753
3754exit:
3755 mbedtls_free( output_buffer );
3756 mbedtls_free( export_buffer );
3757 psa_generator_abort( &generator );
3758 psa_destroy_key( base_key );
3759 psa_destroy_key( derived_key );
3760 mbedtls_psa_crypto_free( );
3761}
3762/* END_CASE */
3763
3764/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003765void key_agreement_setup( int alg_arg,
3766 int our_key_type_arg, data_t *our_key_data,
3767 data_t *peer_key_data,
3768 int expected_status_arg )
3769{
3770 psa_key_slot_t our_key = 1;
3771 psa_algorithm_t alg = alg_arg;
3772 psa_key_type_t our_key_type = our_key_type_arg;
3773 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3774 psa_key_policy_t policy;
3775
3776 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3777
3778 psa_key_policy_init( &policy );
3779 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3780 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3781 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3782 our_key_data->x,
3783 our_key_data->len ) == PSA_SUCCESS );
3784
3785 TEST_ASSERT( psa_key_agreement( &generator,
3786 our_key,
3787 peer_key_data->x, peer_key_data->len,
3788 alg ) == expected_status_arg );
3789
3790exit:
3791 psa_generator_abort( &generator );
3792 psa_destroy_key( our_key );
3793 mbedtls_psa_crypto_free( );
3794}
3795/* END_CASE */
3796
3797/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003798void key_agreement_capacity( int alg_arg,
3799 int our_key_type_arg, data_t *our_key_data,
3800 data_t *peer_key_data,
3801 int expected_capacity_arg )
3802{
3803 psa_key_slot_t our_key = 1;
3804 psa_algorithm_t alg = alg_arg;
3805 psa_key_type_t our_key_type = our_key_type_arg;
3806 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3807 psa_key_policy_t policy;
3808 size_t actual_capacity;
3809
3810 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3811
3812 psa_key_policy_init( &policy );
3813 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3814 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3815 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3816 our_key_data->x,
3817 our_key_data->len ) == PSA_SUCCESS );
3818
3819 TEST_ASSERT( psa_key_agreement( &generator,
3820 our_key,
3821 peer_key_data->x, peer_key_data->len,
3822 alg ) == PSA_SUCCESS );
3823
3824 TEST_ASSERT( psa_get_generator_capacity(
3825 &generator, &actual_capacity ) == PSA_SUCCESS );
3826 TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg );
3827
3828exit:
3829 psa_generator_abort( &generator );
3830 psa_destroy_key( our_key );
3831 mbedtls_psa_crypto_free( );
3832}
3833/* END_CASE */
3834
3835/* BEGIN_CASE */
3836void key_agreement_output( int alg_arg,
3837 int our_key_type_arg, data_t *our_key_data,
3838 data_t *peer_key_data,
3839 data_t *expected_output )
3840{
3841 psa_key_slot_t our_key = 1;
3842 psa_algorithm_t alg = alg_arg;
3843 psa_key_type_t our_key_type = our_key_type_arg;
3844 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3845 psa_key_policy_t policy;
3846 uint8_t *actual_output = mbedtls_calloc( 1, expected_output->len );
3847
3848 TEST_ASSERT( actual_output != NULL );
3849
3850 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3851
3852 psa_key_policy_init( &policy );
3853 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3854 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3855 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3856 our_key_data->x,
3857 our_key_data->len ) == PSA_SUCCESS );
3858
3859 TEST_ASSERT( psa_key_agreement( &generator,
3860 our_key,
3861 peer_key_data->x, peer_key_data->len,
3862 alg ) == PSA_SUCCESS );
3863
3864 TEST_ASSERT( psa_generator_read( &generator,
3865 actual_output,
3866 expected_output->len ) == PSA_SUCCESS );
3867 TEST_ASSERT( memcmp( actual_output, expected_output->x,
3868 expected_output->len ) == 0 );
3869
3870exit:
3871 psa_generator_abort( &generator );
3872 psa_destroy_key( our_key );
3873 mbedtls_psa_crypto_free( );
3874 mbedtls_free( actual_output );
3875}
3876/* END_CASE */
3877
3878/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003879void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003880{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003881 size_t bytes = bytes_arg;
3882 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003883 unsigned char *output = NULL;
3884 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003885 size_t i;
3886 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003887
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003888 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3889 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003890 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003891
3892 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3893
Gilles Peskinea50d7392018-06-21 10:22:13 +02003894 /* Run several times, to ensure that every output byte will be
3895 * nonzero at least once with overwhelming probability
3896 * (2^(-8*number_of_runs)). */
3897 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003898 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003899 if( bytes != 0 )
3900 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003901 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3902
3903 /* Check that no more than bytes have been overwritten */
3904 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3905
3906 for( i = 0; i < bytes; i++ )
3907 {
3908 if( output[i] != 0 )
3909 ++changed[i];
3910 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003911 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003912
3913 /* Check that every byte was changed to nonzero at least once. This
3914 * validates that psa_generate_random is overwriting every byte of
3915 * the output buffer. */
3916 for( i = 0; i < bytes; i++ )
3917 {
3918 TEST_ASSERT( changed[i] != 0 );
3919 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003920
3921exit:
3922 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003923 mbedtls_free( output );
3924 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003925}
3926/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003927
3928/* BEGIN_CASE */
3929void generate_key( int type_arg,
3930 int bits_arg,
3931 int usage_arg,
3932 int alg_arg,
3933 int expected_status_arg )
3934{
3935 int slot = 1;
3936 psa_key_type_t type = type_arg;
3937 psa_key_usage_t usage = usage_arg;
3938 size_t bits = bits_arg;
3939 psa_algorithm_t alg = alg_arg;
3940 psa_status_t expected_status = expected_status_arg;
3941 psa_key_type_t got_type;
3942 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003943 psa_status_t expected_info_status =
3944 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3945 psa_key_policy_t policy;
3946
3947 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3948
3949 psa_key_policy_init( &policy );
3950 psa_key_policy_set_usage( &policy, usage, alg );
3951 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3952
3953 /* Generate a key */
3954 TEST_ASSERT( psa_generate_key( slot, type, bits,
3955 NULL, 0 ) == expected_status );
3956
3957 /* Test the key information */
3958 TEST_ASSERT( psa_get_key_information( slot,
3959 &got_type,
3960 &got_bits ) == expected_info_status );
3961 if( expected_info_status != PSA_SUCCESS )
3962 goto exit;
3963 TEST_ASSERT( got_type == type );
3964 TEST_ASSERT( got_bits == bits );
3965
Gilles Peskine818ca122018-06-20 18:16:48 +02003966 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003967 if( ! exercise_key( slot, usage, alg ) )
3968 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003969
3970exit:
3971 psa_destroy_key( slot );
3972 mbedtls_psa_crypto_free( );
3973}
3974/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003975
3976/* BEGIN_CASE */
3977void validate_module_init_generate_random( )
3978{
3979 psa_status_t status;
3980 uint8_t random[10] = { 0 };
3981 status = psa_generate_random( random, sizeof( random ) );
3982 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3983}
3984/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003985
3986/* BEGIN_CASE */
3987void validate_module_init_key_based( )
3988{
3989 psa_status_t status;
3990 uint8_t data[10] = { 0 };
3991 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3992 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3993}
3994/* END_CASE */