blob: 59020f7638fc9d79cdfe4e0e5d6f7e09211c1693 [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 Peskine10df3412018-10-25 22:35:43 +0200805static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
806 psa_algorithm_t alg )
807{
808 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
809 {
810 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
811 PSA_KEY_USAGE_VERIFY :
812 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
813 }
814 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
815 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
816 {
817 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
818 PSA_KEY_USAGE_ENCRYPT :
819 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
820 }
821 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
822 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
823 {
824 return( PSA_KEY_USAGE_DERIVE );
825 }
826 else
827 {
828 return( 0 );
829 }
830
831}
Gilles Peskinee59236f2018-01-27 23:32:46 +0100832/* END_HEADER */
833
834/* BEGIN_DEPENDENCIES
835 * depends_on:MBEDTLS_PSA_CRYPTO_C
836 * END_DEPENDENCIES
837 */
838
839/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200840void static_checks( )
841{
842 size_t max_truncated_mac_size =
843 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
844
845 /* Check that the length for a truncated MAC always fits in the algorithm
846 * encoding. The shifted mask is the maximum truncated value. The
847 * untruncated algorithm may be one byte larger. */
848 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
849}
850/* END_CASE */
851
852/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200853void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100854{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100856 int i;
857 for( i = 0; i <= 1; i++ )
858 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 status = psa_crypto_init( );
860 TEST_ASSERT( status == PSA_SUCCESS );
861 status = psa_crypto_init( );
862 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100863 mbedtls_psa_crypto_free( );
864 }
865}
866/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867
868/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200869void fill_slots( int max_arg )
870{
871 /* Fill all the slots until we run out of memory or out of slots,
872 * or until some limit specified in the test data for the sake of
873 * implementations with an essentially unlimited number of slots.
874 * This test assumes that available slots are numbered from 1. */
875
876 psa_key_slot_t slot;
877 psa_key_slot_t max = 0;
878 psa_key_policy_t policy;
879 uint8_t exported[sizeof( max )];
880 size_t exported_size;
881 psa_status_t status;
882
883 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
884
885 psa_key_policy_init( &policy );
886 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
887
888 for( max = 1; max <= (size_t) max_arg; max++ )
889 {
890 status = psa_set_key_policy( max, &policy );
891 /* Stop filling slots if we run out of memory or out of
892 * available slots. */
893 TEST_ASSERT( status == PSA_SUCCESS ||
894 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
895 status == PSA_ERROR_INVALID_ARGUMENT );
896 if( status != PSA_SUCCESS )
897 break;
898 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
899 (uint8_t*) &max, sizeof( max ) );
900 /* Since psa_set_key_policy succeeded, we know that the slot
901 * number is valid. But we may legitimately run out of memory. */
902 TEST_ASSERT( status == PSA_SUCCESS ||
903 status == PSA_ERROR_INSUFFICIENT_MEMORY );
904 if( status != PSA_SUCCESS )
905 break;
906 }
907 /* `max` is now the first slot number that wasn't filled. */
908 max -= 1;
909
910 for( slot = 1; slot <= max; slot++ )
911 {
912 TEST_ASSERT( psa_export_key( slot,
913 exported, sizeof( exported ),
914 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200915 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200916 }
917
918exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200919 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200920 mbedtls_psa_crypto_free( );
921}
922/* END_CASE */
923
924/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200925void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926{
927 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200928 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100929 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100930
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100931 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300932 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100933 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
934
Gilles Peskine4abf7412018-06-18 16:35:34 +0200935 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200936 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937 if( status == PSA_SUCCESS )
938 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
939
940exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 mbedtls_psa_crypto_free( );
942}
943/* END_CASE */
944
945/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200946void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
947{
948 int slot = 1;
949 size_t bits = bits_arg;
950 psa_status_t expected_status = expected_status_arg;
951 psa_status_t status;
952 psa_key_type_t type =
953 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
954 size_t buffer_size = /* Slight overapproximations */
955 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200956 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200957 unsigned char *p;
958 int ret;
959 size_t length;
960
961 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200962 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200963
964 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
965 bits, keypair ) ) >= 0 );
966 length = ret;
967
968 /* Try importing the key */
969 status = psa_import_key( slot, type, p, length );
970 TEST_ASSERT( status == expected_status );
971 if( status == PSA_SUCCESS )
972 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
973
974exit:
975 mbedtls_free( buffer );
976 mbedtls_psa_crypto_free( );
977}
978/* END_CASE */
979
980/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300981void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300982 int type_arg,
983 int alg_arg,
984 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 int expected_bits,
986 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200987 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 int canonical_input )
989{
990 int slot = 1;
991 int slot2 = slot + 1;
992 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200993 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200994 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 unsigned char *exported = NULL;
997 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100999 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 size_t reexported_length;
1001 psa_key_type_t got_type;
1002 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001003 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001006 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001007 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001008 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001010 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1012
mohammad1603a97cb8c2018-03-28 03:46:26 -07001013 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001014 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001015 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1016
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 /* Import the key */
1018 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001019 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020
1021 /* Test the key information */
1022 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001023 &got_type,
1024 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001025 TEST_ASSERT( got_type == type );
1026 TEST_ASSERT( got_bits == (size_t) expected_bits );
1027
1028 /* Export the key */
1029 status = psa_export_key( slot,
1030 exported, export_size,
1031 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001032 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001033
1034 /* The exported length must be set by psa_export_key() to a value between 0
1035 * and export_size. On errors, the exported length must be 0. */
1036 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1037 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1038 TEST_ASSERT( exported_length <= export_size );
1039
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001040 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001041 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001043 {
1044 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001046 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001047
Gilles Peskine8f609232018-08-11 01:24:55 +02001048 if( ! exercise_export_key( slot, usage_arg ) )
1049 goto exit;
1050
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001051 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001052 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053 else
1054 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001055 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1056
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001058 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001059 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001061 reexported,
1062 export_size,
1063 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001064 ASSERT_COMPARE( exported, exported_length,
1065 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001067 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068
1069destroy:
1070 /* Destroy the key */
1071 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1072 TEST_ASSERT( psa_get_key_information(
1073 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1074
1075exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001076 mbedtls_free( exported );
1077 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001078 mbedtls_psa_crypto_free( );
1079}
1080/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001081
Moran Pekerf709f4a2018-06-06 17:26:04 +03001082/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001083void import_key_nonempty_slot( )
1084{
1085 int slot = 1;
1086 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1087 psa_status_t status;
1088 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1089 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1090
1091 /* Import the key */
1092 TEST_ASSERT( psa_import_key( slot, type,
1093 data, sizeof( data ) ) == PSA_SUCCESS );
1094
1095 /* Import the key again */
1096 status = psa_import_key( slot, type, data, sizeof( data ) );
1097 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1098
1099exit:
1100 mbedtls_psa_crypto_free( );
1101}
1102/* END_CASE */
1103
1104/* BEGIN_CASE */
1105void export_invalid_slot( int slot, int expected_export_status_arg )
1106{
1107 psa_status_t status;
1108 unsigned char *exported = NULL;
1109 size_t export_size = 0;
1110 size_t exported_length = INVALID_EXPORT_LENGTH;
1111 psa_status_t expected_export_status = expected_export_status_arg;
1112
1113 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1114
1115 /* Export the key */
1116 status = psa_export_key( slot,
1117 exported, export_size,
1118 &exported_length );
1119 TEST_ASSERT( status == expected_export_status );
1120
1121exit:
1122 mbedtls_psa_crypto_free( );
1123}
1124/* END_CASE */
1125
1126/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001127void export_with_no_key_activity( )
1128{
1129 int slot = 1;
1130 psa_algorithm_t alg = PSA_ALG_CTR;
1131 psa_status_t status;
1132 psa_key_policy_t policy;
1133 unsigned char *exported = NULL;
1134 size_t export_size = 0;
1135 size_t exported_length = INVALID_EXPORT_LENGTH;
1136
1137 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1138
1139 psa_key_policy_init( &policy );
1140 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1141 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1142
1143 /* Export the key */
1144 status = psa_export_key( slot,
1145 exported, export_size,
1146 &exported_length );
1147 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1148
1149exit:
1150 mbedtls_psa_crypto_free( );
1151}
1152/* END_CASE */
1153
1154/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001155void cipher_with_no_key_activity( )
1156{
1157 int slot = 1;
1158 psa_status_t status;
1159 psa_key_policy_t policy;
1160 psa_cipher_operation_t operation;
1161 int exercise_alg = PSA_ALG_CTR;
1162
1163 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1164
1165 psa_key_policy_init( &policy );
1166 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
1167 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1168
1169 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1170 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1171
1172exit:
1173 psa_cipher_abort( &operation );
1174 mbedtls_psa_crypto_free( );
1175}
1176/* END_CASE */
1177
1178/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001179void export_after_import_failure( data_t *data, int type_arg,
1180 int expected_import_status_arg )
1181{
1182 int slot = 1;
1183 psa_key_type_t type = type_arg;
1184 psa_status_t status;
1185 unsigned char *exported = NULL;
1186 size_t export_size = 0;
1187 psa_status_t expected_import_status = expected_import_status_arg;
1188 size_t exported_length = INVALID_EXPORT_LENGTH;
1189
1190 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1191
1192 /* Import the key - expect failure */
1193 status = psa_import_key( slot, type,
1194 data->x, data->len );
1195 TEST_ASSERT( status == expected_import_status );
1196
1197 /* Export the key */
1198 status = psa_export_key( slot,
1199 exported, export_size,
1200 &exported_length );
1201 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1202
1203exit:
1204 mbedtls_psa_crypto_free( );
1205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001209void cipher_after_import_failure( data_t *data, int type_arg,
1210 int expected_import_status_arg )
1211{
1212 int slot = 1;
1213 psa_cipher_operation_t operation;
1214 psa_key_type_t type = type_arg;
1215 psa_status_t status;
1216 psa_status_t expected_import_status = expected_import_status_arg;
1217 int exercise_alg = PSA_ALG_CTR;
1218
1219 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1220
1221 /* Import the key - expect failure */
1222 status = psa_import_key( slot, type,
1223 data->x, data->len );
1224 TEST_ASSERT( status == expected_import_status );
1225
1226 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1227 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1228
1229exit:
1230 psa_cipher_abort( &operation );
1231 mbedtls_psa_crypto_free( );
1232}
1233/* END_CASE */
1234
1235/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001236void export_after_destroy_key( data_t *data, int type_arg )
1237{
1238 int slot = 1;
1239 psa_key_type_t type = type_arg;
1240 psa_status_t status;
1241 psa_key_policy_t policy;
1242 psa_algorithm_t alg = PSA_ALG_CTR;
1243 unsigned char *exported = NULL;
1244 size_t export_size = 0;
1245 size_t exported_length = INVALID_EXPORT_LENGTH;
1246
1247 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1248
1249 psa_key_policy_init( &policy );
1250 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1251 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1252 export_size = (ptrdiff_t) data->len;
1253 ASSERT_ALLOC( exported, export_size );
1254
1255 /* Import the key */
1256 TEST_ASSERT( psa_import_key( slot, type,
1257 data->x, data->len ) == PSA_SUCCESS );
1258
1259 TEST_ASSERT( psa_export_key( slot, exported, export_size,
1260 &exported_length ) == PSA_SUCCESS );
1261
1262 /* Destroy the key */
1263 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1264
1265 /* Export the key */
1266 status = psa_export_key( slot, exported, export_size,
1267 &exported_length );
1268 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1269
1270exit:
1271 mbedtls_free( exported );
1272 mbedtls_psa_crypto_free( );
1273}
1274/* END_CASE */
1275
1276/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001277void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001278 int type_arg,
1279 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001280 int export_size_delta,
1281 int expected_export_status_arg,
1282 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001283{
1284 int slot = 1;
1285 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001286 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001287 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001288 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001289 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001290 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001291 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001292 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001293
Moran Pekerf709f4a2018-06-06 17:26:04 +03001294 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1295
1296 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001297 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001298 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1299
1300 /* Import the key */
1301 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001302 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001303
Gilles Peskine49c25912018-10-29 15:15:31 +01001304 /* Export the public key */
1305 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001306 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001307 exported, export_size,
1308 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001309 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001310 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001311 {
1312 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1313 size_t bits;
1314 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1315 PSA_SUCCESS );
1316 TEST_ASSERT( expected_public_key->len <=
1317 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001318 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1319 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001320 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001321
1322exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001323 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001324 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001325 mbedtls_psa_crypto_free( );
1326}
1327/* END_CASE */
1328
Gilles Peskine20035e32018-02-03 22:44:14 +01001329/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001330void import_and_exercise_key( data_t *data,
1331 int type_arg,
1332 int bits_arg,
1333 int alg_arg )
1334{
1335 int slot = 1;
1336 psa_key_type_t type = type_arg;
1337 size_t bits = bits_arg;
1338 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001339 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001340 psa_key_policy_t policy;
1341 psa_key_type_t got_type;
1342 size_t got_bits;
1343 psa_status_t status;
1344
1345 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1346
1347 psa_key_policy_init( &policy );
1348 psa_key_policy_set_usage( &policy, usage, alg );
1349 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1350
1351 /* Import the key */
1352 status = psa_import_key( slot, type, data->x, data->len );
1353 TEST_ASSERT( status == PSA_SUCCESS );
1354
1355 /* Test the key information */
1356 TEST_ASSERT( psa_get_key_information( slot,
1357 &got_type,
1358 &got_bits ) == PSA_SUCCESS );
1359 TEST_ASSERT( got_type == type );
1360 TEST_ASSERT( got_bits == bits );
1361
1362 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001363 if( ! exercise_key( slot, usage, alg ) )
1364 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001365
1366exit:
1367 psa_destroy_key( slot );
1368 mbedtls_psa_crypto_free( );
1369}
1370/* END_CASE */
1371
1372/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001373void key_policy( int usage_arg, int alg_arg )
1374{
1375 int key_slot = 1;
1376 psa_algorithm_t alg = alg_arg;
1377 psa_key_usage_t usage = usage_arg;
1378 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1379 unsigned char key[32] = {0};
1380 psa_key_policy_t policy_set;
1381 psa_key_policy_t policy_get;
1382
1383 memset( key, 0x2a, sizeof( key ) );
1384
1385 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1386
1387 psa_key_policy_init( &policy_set );
1388 psa_key_policy_init( &policy_get );
1389
1390 psa_key_policy_set_usage( &policy_set, usage, alg );
1391
1392 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1393 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1394 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1395
1396 TEST_ASSERT( psa_import_key( key_slot, key_type,
1397 key, sizeof( key ) ) == PSA_SUCCESS );
1398
1399 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1400
1401 TEST_ASSERT( policy_get.usage == policy_set.usage );
1402 TEST_ASSERT( policy_get.alg == policy_set.alg );
1403
1404exit:
1405 psa_destroy_key( key_slot );
1406 mbedtls_psa_crypto_free( );
1407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001411void mac_key_policy( int policy_usage,
1412 int policy_alg,
1413 int key_type,
1414 data_t *key_data,
1415 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001416{
1417 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001418 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001419 psa_mac_operation_t operation;
1420 psa_status_t status;
1421 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001422
1423 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1424
1425 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001426 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001427 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1428
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001429 TEST_ASSERT( psa_import_key( key_slot, key_type,
1430 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001431
Gilles Peskine89167cb2018-07-08 20:12:23 +02001432 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001433 if( policy_alg == exercise_alg &&
1434 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1435 TEST_ASSERT( status == PSA_SUCCESS );
1436 else
1437 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1438 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001439
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001441 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001442 if( policy_alg == exercise_alg &&
1443 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001444 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001445 else
1446 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1447
1448exit:
1449 psa_mac_abort( &operation );
1450 psa_destroy_key( key_slot );
1451 mbedtls_psa_crypto_free( );
1452}
1453/* END_CASE */
1454
1455/* BEGIN_CASE */
1456void cipher_key_policy( int policy_usage,
1457 int policy_alg,
1458 int key_type,
1459 data_t *key_data,
1460 int exercise_alg )
1461{
1462 int key_slot = 1;
1463 psa_key_policy_t policy;
1464 psa_cipher_operation_t operation;
1465 psa_status_t status;
1466
1467 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1468
1469 psa_key_policy_init( &policy );
1470 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1471 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1472
1473 TEST_ASSERT( psa_import_key( key_slot, key_type,
1474 key_data->x, key_data->len ) == PSA_SUCCESS );
1475
Gilles Peskinefe119512018-07-08 21:39:34 +02001476 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001477 if( policy_alg == exercise_alg &&
1478 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1479 TEST_ASSERT( status == PSA_SUCCESS );
1480 else
1481 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1482 psa_cipher_abort( &operation );
1483
Gilles Peskinefe119512018-07-08 21:39:34 +02001484 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001485 if( policy_alg == exercise_alg &&
1486 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1487 TEST_ASSERT( status == PSA_SUCCESS );
1488 else
1489 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1490
1491exit:
1492 psa_cipher_abort( &operation );
1493 psa_destroy_key( key_slot );
1494 mbedtls_psa_crypto_free( );
1495}
1496/* END_CASE */
1497
1498/* BEGIN_CASE */
1499void aead_key_policy( int policy_usage,
1500 int policy_alg,
1501 int key_type,
1502 data_t *key_data,
1503 int nonce_length_arg,
1504 int tag_length_arg,
1505 int exercise_alg )
1506{
1507 int key_slot = 1;
1508 psa_key_policy_t policy;
1509 psa_status_t status;
1510 unsigned char nonce[16] = {0};
1511 size_t nonce_length = nonce_length_arg;
1512 unsigned char tag[16];
1513 size_t tag_length = tag_length_arg;
1514 size_t output_length;
1515
1516 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1517 TEST_ASSERT( tag_length <= sizeof( tag ) );
1518
1519 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1520
1521 psa_key_policy_init( &policy );
1522 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1523 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1524
1525 TEST_ASSERT( psa_import_key( key_slot, key_type,
1526 key_data->x, key_data->len ) == PSA_SUCCESS );
1527
1528 status = psa_aead_encrypt( key_slot, exercise_alg,
1529 nonce, nonce_length,
1530 NULL, 0,
1531 NULL, 0,
1532 tag, tag_length,
1533 &output_length );
1534 if( policy_alg == exercise_alg &&
1535 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1536 TEST_ASSERT( status == PSA_SUCCESS );
1537 else
1538 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1539
1540 memset( tag, 0, sizeof( tag ) );
1541 status = psa_aead_decrypt( key_slot, exercise_alg,
1542 nonce, nonce_length,
1543 NULL, 0,
1544 tag, tag_length,
1545 NULL, 0,
1546 &output_length );
1547 if( policy_alg == exercise_alg &&
1548 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1549 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1550 else
1551 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1552
1553exit:
1554 psa_destroy_key( key_slot );
1555 mbedtls_psa_crypto_free( );
1556}
1557/* END_CASE */
1558
1559/* BEGIN_CASE */
1560void asymmetric_encryption_key_policy( int policy_usage,
1561 int policy_alg,
1562 int key_type,
1563 data_t *key_data,
1564 int exercise_alg )
1565{
1566 int key_slot = 1;
1567 psa_key_policy_t policy;
1568 psa_status_t status;
1569 size_t key_bits;
1570 size_t buffer_length;
1571 unsigned char *buffer = NULL;
1572 size_t output_length;
1573
1574 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1575
1576 psa_key_policy_init( &policy );
1577 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1578 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1579
1580 TEST_ASSERT( psa_import_key( key_slot, key_type,
1581 key_data->x, key_data->len ) == PSA_SUCCESS );
1582
1583 TEST_ASSERT( psa_get_key_information( key_slot,
1584 NULL,
1585 &key_bits ) == PSA_SUCCESS );
1586 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1587 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001588 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001589
1590 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1591 NULL, 0,
1592 NULL, 0,
1593 buffer, buffer_length,
1594 &output_length );
1595 if( policy_alg == exercise_alg &&
1596 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1597 TEST_ASSERT( status == PSA_SUCCESS );
1598 else
1599 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1600
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001601 if( buffer_length != 0 )
1602 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1604 buffer, buffer_length,
1605 NULL, 0,
1606 buffer, buffer_length,
1607 &output_length );
1608 if( policy_alg == exercise_alg &&
1609 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1610 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1611 else
1612 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1613
1614exit:
1615 psa_destroy_key( key_slot );
1616 mbedtls_psa_crypto_free( );
1617 mbedtls_free( buffer );
1618}
1619/* END_CASE */
1620
1621/* BEGIN_CASE */
1622void asymmetric_signature_key_policy( int policy_usage,
1623 int policy_alg,
1624 int key_type,
1625 data_t *key_data,
1626 int exercise_alg )
1627{
1628 int key_slot = 1;
1629 psa_key_policy_t policy;
1630 psa_status_t status;
1631 unsigned char payload[16] = {1};
1632 size_t payload_length = sizeof( payload );
1633 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1634 size_t signature_length;
1635
1636 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1637
1638 psa_key_policy_init( &policy );
1639 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1640 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1641
1642 TEST_ASSERT( psa_import_key( key_slot, key_type,
1643 key_data->x, key_data->len ) == PSA_SUCCESS );
1644
1645 status = psa_asymmetric_sign( key_slot, exercise_alg,
1646 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647 signature, sizeof( signature ),
1648 &signature_length );
1649 if( policy_alg == exercise_alg &&
1650 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1651 TEST_ASSERT( status == PSA_SUCCESS );
1652 else
1653 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1654
1655 memset( signature, 0, sizeof( signature ) );
1656 status = psa_asymmetric_verify( key_slot, exercise_alg,
1657 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658 signature, sizeof( signature ) );
1659 if( policy_alg == exercise_alg &&
1660 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1661 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1662 else
1663 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001664
1665exit:
1666 psa_destroy_key( key_slot );
1667 mbedtls_psa_crypto_free( );
1668}
1669/* END_CASE */
1670
1671/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001672void derive_key_policy( int policy_usage,
1673 int policy_alg,
1674 int key_type,
1675 data_t *key_data,
1676 int exercise_alg )
1677{
1678 int key_slot = 1;
1679 psa_key_policy_t policy;
1680 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1681 psa_status_t status;
1682
1683 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1684
1685 psa_key_policy_init( &policy );
1686 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1687 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1688
1689 TEST_ASSERT( psa_import_key( key_slot, key_type,
1690 key_data->x, key_data->len ) == PSA_SUCCESS );
1691
1692 status = psa_key_derivation( &generator, key_slot,
1693 exercise_alg,
1694 NULL, 0,
1695 NULL, 0,
1696 1 );
1697 if( policy_alg == exercise_alg &&
1698 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1699 TEST_ASSERT( status == PSA_SUCCESS );
1700 else
1701 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1702
1703exit:
Gilles Peskine1d7c0822018-10-08 19:05:22 +02001704 mbedtls_free( public_key );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001705 psa_generator_abort( &generator );
1706 psa_destroy_key( key_slot );
1707 mbedtls_psa_crypto_free( );
1708}
1709/* END_CASE */
1710
1711/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001712void agreement_key_policy( int policy_usage,
1713 int policy_alg,
1714 int key_type_arg,
1715 data_t *key_data,
1716 int exercise_alg )
1717{
1718 int key_slot = 1;
1719 psa_key_policy_t policy;
1720 psa_key_type_t key_type = key_type_arg;
1721 psa_key_type_t public_key_type;
1722 size_t key_bits;
1723 uint8_t *public_key = NULL;
1724 size_t public_key_length;
1725 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1726 psa_status_t status;
1727
1728 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1729
1730 psa_key_policy_init( &policy );
1731 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1732 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1733
1734 TEST_ASSERT( psa_import_key( key_slot, key_type,
1735 key_data->x, key_data->len ) == PSA_SUCCESS );
1736
1737 /* We need two keys to exercise key agreement. Exercise the
1738 * private key against its own public key. */
1739 TEST_ASSERT( psa_get_key_information( key_slot,
1740 &key_type,
1741 &key_bits ) == PSA_SUCCESS );
1742 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( key_type );
1743 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
Gilles Peskinefc411f12018-10-25 22:34:48 +02001744 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001745 TEST_ASSERT( public_key != NULL );
1746 TEST_ASSERT( psa_export_public_key( key_slot,
1747 public_key, public_key_length,
1748 &public_key_length ) == PSA_SUCCESS );
1749
1750 status = psa_key_agreement( &generator, key_slot,
1751 public_key, public_key_length,
1752 exercise_alg );
1753 if( policy_alg == exercise_alg &&
1754 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1755 TEST_ASSERT( status == PSA_SUCCESS );
1756 else
1757 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1758
1759exit:
1760 psa_generator_abort( &generator );
1761 psa_destroy_key( key_slot );
1762 mbedtls_psa_crypto_free( );
1763}
1764/* END_CASE */
1765
1766/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001767void key_lifetime( int lifetime_arg )
1768{
1769 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001770 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001771 unsigned char key[32] = {0};
1772 psa_key_lifetime_t lifetime_set = lifetime_arg;
1773 psa_key_lifetime_t lifetime_get;
1774
1775 memset( key, 0x2a, sizeof( key ) );
1776
1777 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1778
1779 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1780 lifetime_set ) == PSA_SUCCESS );
1781
1782 TEST_ASSERT( psa_import_key( key_slot, key_type,
1783 key, sizeof( key ) ) == PSA_SUCCESS );
1784
1785 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1786 &lifetime_get ) == PSA_SUCCESS );
1787
1788 TEST_ASSERT( lifetime_get == lifetime_set );
1789
1790exit:
1791 psa_destroy_key( key_slot );
1792 mbedtls_psa_crypto_free( );
1793}
1794/* END_CASE */
1795
1796/* BEGIN_CASE */
1797void key_lifetime_set_fail( int key_slot_arg,
1798 int lifetime_arg,
1799 int expected_status_arg )
1800{
1801 psa_key_slot_t key_slot = key_slot_arg;
1802 psa_key_lifetime_t lifetime_set = lifetime_arg;
1803 psa_status_t actual_status;
1804 psa_status_t expected_status = expected_status_arg;
1805
1806 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1807
1808 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1809
1810 if( actual_status == PSA_SUCCESS )
1811 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1812
1813 TEST_ASSERT( expected_status == actual_status );
1814
1815exit:
1816 psa_destroy_key( key_slot );
1817 mbedtls_psa_crypto_free( );
1818}
1819/* END_CASE */
1820
1821/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001822void hash_setup( int alg_arg,
1823 int expected_status_arg )
1824{
1825 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001826 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001827 psa_hash_operation_t operation;
1828 psa_status_t status;
1829
1830 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1831
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001832 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001833 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001834 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001835
1836exit:
1837 mbedtls_psa_crypto_free( );
1838}
1839/* END_CASE */
1840
1841/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001842void hash_bad_order( )
1843{
1844 unsigned char input[] = "";
1845 /* SHA-256 hash of an empty string */
1846 unsigned char hash[] = {
1847 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1848 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1849 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1850 size_t hash_len;
1851 psa_hash_operation_t operation;
1852
1853 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1854
1855 /* psa_hash_update without calling psa_hash_setup beforehand */
1856 memset( &operation, 0, sizeof( operation ) );
1857 TEST_ASSERT( psa_hash_update( &operation,
1858 input, sizeof( input ) ) ==
1859 PSA_ERROR_INVALID_ARGUMENT );
1860
1861 /* psa_hash_verify without calling psa_hash_setup beforehand */
1862 memset( &operation, 0, sizeof( operation ) );
1863 TEST_ASSERT( psa_hash_verify( &operation,
1864 hash, sizeof( hash ) ) ==
1865 PSA_ERROR_INVALID_ARGUMENT );
1866
1867 /* psa_hash_finish without calling psa_hash_setup beforehand */
1868 memset( &operation, 0, sizeof( operation ) );
1869 TEST_ASSERT( psa_hash_finish( &operation,
1870 hash, sizeof( hash ), &hash_len ) ==
1871 PSA_ERROR_INVALID_ARGUMENT );
1872
1873exit:
1874 mbedtls_psa_crypto_free( );
1875}
1876/* END_CASE */
1877
itayzafrir27e69452018-11-01 14:26:34 +02001878/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1879void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001880{
1881 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001882 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1883 * appended to it */
1884 unsigned char hash[] = {
1885 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1886 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1887 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001888 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001889 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001890
1891 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1892
itayzafrir27e69452018-11-01 14:26:34 +02001893 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001894 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1895 TEST_ASSERT( psa_hash_verify( &operation,
1896 hash, expected_size - 1 ) ==
1897 PSA_ERROR_INVALID_SIGNATURE );
1898
itayzafrir27e69452018-11-01 14:26:34 +02001899 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001900 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001901 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001902 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001903 PSA_ERROR_INVALID_SIGNATURE );
1904
itayzafrir27e69452018-11-01 14:26:34 +02001905 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001906 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001907 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001908 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001909 PSA_ERROR_INVALID_SIGNATURE );
1910
itayzafrirec93d302018-10-18 18:01:10 +03001911exit:
1912 mbedtls_psa_crypto_free( );
1913}
1914/* END_CASE */
1915
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001916/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1917void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001918{
1919 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001920 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001921 size_t expected_size = PSA_HASH_SIZE( alg );
1922 psa_hash_operation_t operation;
1923 size_t hash_len;
1924
1925 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1926
itayzafrir58028322018-10-25 10:22:01 +03001927 /* psa_hash_finish with a smaller hash buffer than expected */
1928 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1929 TEST_ASSERT( psa_hash_finish( &operation,
1930 hash, expected_size - 1,
1931 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1932
1933exit:
1934 mbedtls_psa_crypto_free( );
1935}
1936/* END_CASE */
1937
1938/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001939void mac_setup( int key_type_arg,
1940 data_t *key,
1941 int alg_arg,
1942 int expected_status_arg )
1943{
1944 int key_slot = 1;
1945 psa_key_type_t key_type = key_type_arg;
1946 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001947 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948 psa_mac_operation_t operation;
1949 psa_key_policy_t policy;
1950 psa_status_t status;
1951
1952 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1953
1954 psa_key_policy_init( &policy );
1955 psa_key_policy_set_usage( &policy,
1956 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1957 alg );
1958 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1959
1960 TEST_ASSERT( psa_import_key( key_slot, key_type,
1961 key->x, key->len ) == PSA_SUCCESS );
1962
Gilles Peskine89167cb2018-07-08 20:12:23 +02001963 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001964 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001965 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001966
1967exit:
1968 psa_destroy_key( key_slot );
1969 mbedtls_psa_crypto_free( );
1970}
1971/* END_CASE */
1972
1973/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001974void mac_sign( int key_type_arg,
1975 data_t *key,
1976 int alg_arg,
1977 data_t *input,
1978 data_t *expected_mac )
1979{
1980 int key_slot = 1;
1981 psa_key_type_t key_type = key_type_arg;
1982 psa_algorithm_t alg = alg_arg;
1983 psa_mac_operation_t operation;
1984 psa_key_policy_t policy;
1985 /* Leave a little extra room in the output buffer. At the end of the
1986 * test, we'll check that the implementation didn't overwrite onto
1987 * this extra room. */
1988 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1989 size_t mac_buffer_size =
1990 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1991 size_t mac_length = 0;
1992
1993 memset( actual_mac, '+', sizeof( actual_mac ) );
1994 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1995 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1996
1997 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1998
1999 psa_key_policy_init( &policy );
2000 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
2001 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2002
2003 TEST_ASSERT( psa_import_key( key_slot, key_type,
2004 key->x, key->len ) == PSA_SUCCESS );
2005
2006 /* Calculate the MAC. */
2007 TEST_ASSERT( psa_mac_sign_setup( &operation,
2008 key_slot, alg ) == PSA_SUCCESS );
2009 TEST_ASSERT( psa_mac_update( &operation,
2010 input->x, input->len ) == PSA_SUCCESS );
2011 TEST_ASSERT( psa_mac_sign_finish( &operation,
2012 actual_mac, mac_buffer_size,
2013 &mac_length ) == PSA_SUCCESS );
2014
2015 /* Compare with the expected value. */
2016 TEST_ASSERT( mac_length == expected_mac->len );
2017 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
2018
2019 /* Verify that the end of the buffer is untouched. */
2020 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2021 sizeof( actual_mac ) - mac_length ) );
2022
2023exit:
2024 psa_destroy_key( key_slot );
2025 mbedtls_psa_crypto_free( );
2026}
2027/* END_CASE */
2028
2029/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002030void mac_verify( int key_type_arg,
2031 data_t *key,
2032 int alg_arg,
2033 data_t *input,
2034 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035{
2036 int key_slot = 1;
2037 psa_key_type_t key_type = key_type_arg;
2038 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002039 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002040 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002041
Gilles Peskine69c12672018-06-28 00:07:19 +02002042 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2043
Gilles Peskine8c9def32018-02-08 10:02:12 +01002044 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002048 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2049 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002050
2051 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2052
mohammad16036df908f2018-04-02 08:34:15 -07002053 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002054 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07002055 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2056
Gilles Peskine8c9def32018-02-08 10:02:12 +01002057 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002058 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002059
Gilles Peskine89167cb2018-07-08 20:12:23 +02002060 TEST_ASSERT( psa_mac_verify_setup( &operation,
2061 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002062 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
2063 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002064 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02002065 TEST_ASSERT( psa_mac_verify_finish( &operation,
2066 expected_mac->x,
2067 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002068
2069exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070 psa_destroy_key( key_slot );
2071 mbedtls_psa_crypto_free( );
2072}
2073/* END_CASE */
2074
2075/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002076void cipher_setup( int key_type_arg,
2077 data_t *key,
2078 int alg_arg,
2079 int expected_status_arg )
2080{
2081 int key_slot = 1;
2082 psa_key_type_t key_type = key_type_arg;
2083 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002084 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002085 psa_cipher_operation_t operation;
2086 psa_key_policy_t policy;
2087 psa_status_t status;
2088
2089 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2090
2091 psa_key_policy_init( &policy );
2092 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2093 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2094
2095 TEST_ASSERT( psa_import_key( key_slot, key_type,
2096 key->x, key->len ) == PSA_SUCCESS );
2097
Gilles Peskinefe119512018-07-08 21:39:34 +02002098 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002099 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002100 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002101
2102exit:
2103 psa_destroy_key( key_slot );
2104 mbedtls_psa_crypto_free( );
2105}
2106/* END_CASE */
2107
2108/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002109void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002110 data_t *key,
2111 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002112 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113{
2114 int key_slot = 1;
2115 psa_status_t status;
2116 psa_key_type_t key_type = key_type_arg;
2117 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002118 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002119 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002120 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002121 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002122 size_t output_buffer_size = 0;
2123 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002124 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002125 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002126 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002127
Gilles Peskine50e586b2018-06-08 14:28:46 +02002128 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002129 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002130 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002131 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2132 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2133 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002134
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002135 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2136 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002137
2138 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2139
Moran Pekered346952018-07-05 15:22:45 +03002140 psa_key_policy_init( &policy );
2141 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2142 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2143
Gilles Peskine50e586b2018-06-08 14:28:46 +02002144 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002145 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146
Gilles Peskinefe119512018-07-08 21:39:34 +02002147 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2148 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002149
Gilles Peskinefe119512018-07-08 21:39:34 +02002150 TEST_ASSERT( psa_cipher_set_iv( &operation,
2151 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002152 output_buffer_size = (size_t) input->len +
2153 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002154 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155
Gilles Peskine4abf7412018-06-18 16:35:34 +02002156 TEST_ASSERT( psa_cipher_update( &operation,
2157 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002158 output, output_buffer_size,
2159 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002160 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002161 status = psa_cipher_finish( &operation,
2162 output + function_output_length,
2163 output_buffer_size,
2164 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002165 total_output_length += function_output_length;
2166
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002167 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002168 if( expected_status == PSA_SUCCESS )
2169 {
2170 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002171 ASSERT_COMPARE( expected_output->x, expected_output->len,
2172 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002173 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002174
Gilles Peskine50e586b2018-06-08 14:28:46 +02002175exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002176 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002177 psa_destroy_key( key_slot );
2178 mbedtls_psa_crypto_free( );
2179}
2180/* END_CASE */
2181
2182/* BEGIN_CASE */
2183void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002184 data_t *key,
2185 data_t *input,
2186 int first_part_size,
2187 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002188{
2189 int key_slot = 1;
2190 psa_key_type_t key_type = key_type_arg;
2191 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002192 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002193 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002194 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195 size_t output_buffer_size = 0;
2196 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002197 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002198 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002199 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002200
Gilles Peskine50e586b2018-06-08 14:28:46 +02002201 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002203 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002204 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2205 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2206 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002207
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002208 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2209 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210
2211 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2212
Moran Pekered346952018-07-05 15:22:45 +03002213 psa_key_policy_init( &policy );
2214 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2215 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2216
Gilles Peskine50e586b2018-06-08 14:28:46 +02002217 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002218 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002219
Gilles Peskinefe119512018-07-08 21:39:34 +02002220 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2221 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002222
Gilles Peskinefe119512018-07-08 21:39:34 +02002223 TEST_ASSERT( psa_cipher_set_iv( &operation,
2224 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002225 output_buffer_size = (size_t) input->len +
2226 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002227 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002228
Gilles Peskine4abf7412018-06-18 16:35:34 +02002229 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002230 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002231 output, output_buffer_size,
2232 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002233 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002234 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002235 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002236 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002237 output, output_buffer_size,
2238 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002239 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002240 TEST_ASSERT( psa_cipher_finish( &operation,
2241 output + function_output_length,
2242 output_buffer_size,
2243 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002244 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2246
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002247 ASSERT_COMPARE( expected_output->x, expected_output->len,
2248 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002249
2250exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002251 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002252 psa_destroy_key( key_slot );
2253 mbedtls_psa_crypto_free( );
2254}
2255/* END_CASE */
2256
2257/* BEGIN_CASE */
2258void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002259 data_t *key,
2260 data_t *input,
2261 int first_part_size,
2262 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002263{
2264 int key_slot = 1;
2265
2266 psa_key_type_t key_type = key_type_arg;
2267 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002268 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002269 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002270 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271 size_t output_buffer_size = 0;
2272 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002273 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002275 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002276
Gilles Peskine50e586b2018-06-08 14:28:46 +02002277 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002278 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002279 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002280 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2281 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2282 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002283
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002284 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2285 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002286
2287 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2288
Moran Pekered346952018-07-05 15:22:45 +03002289 psa_key_policy_init( &policy );
2290 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2291 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2292
Gilles Peskine50e586b2018-06-08 14:28:46 +02002293 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002294 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002295
Gilles Peskinefe119512018-07-08 21:39:34 +02002296 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2297 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002298
Gilles Peskinefe119512018-07-08 21:39:34 +02002299 TEST_ASSERT( psa_cipher_set_iv( &operation,
2300 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002301
mohammad16033d91abe2018-07-03 13:15:54 +03002302 output_buffer_size = (size_t) input->len +
2303 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002304 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002305
Gilles Peskine4abf7412018-06-18 16:35:34 +02002306 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2307 TEST_ASSERT( psa_cipher_update( &operation,
2308 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002309 output, output_buffer_size,
2310 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002311 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002312 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002313 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002314 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002315 output, output_buffer_size,
2316 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002317 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002318 TEST_ASSERT( psa_cipher_finish( &operation,
2319 output + function_output_length,
2320 output_buffer_size,
2321 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002322 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2324
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002325 ASSERT_COMPARE( expected_output->x, expected_output->len,
2326 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002327
2328exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002329 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002330 psa_destroy_key( key_slot );
2331 mbedtls_psa_crypto_free( );
2332}
2333/* END_CASE */
2334
Gilles Peskine50e586b2018-06-08 14:28:46 +02002335/* BEGIN_CASE */
2336void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002337 data_t *key,
2338 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002339 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002340{
2341 int key_slot = 1;
2342 psa_status_t status;
2343 psa_key_type_t key_type = key_type_arg;
2344 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002345 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002347 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002348 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002349 size_t output_buffer_size = 0;
2350 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002351 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002352 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002353 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002354
Gilles Peskine50e586b2018-06-08 14:28:46 +02002355 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002357 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002358 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2359 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2360 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002361
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002362 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2363 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002364
2365 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2366
Moran Pekered346952018-07-05 15:22:45 +03002367 psa_key_policy_init( &policy );
2368 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2369 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2370
Gilles Peskine50e586b2018-06-08 14:28:46 +02002371 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002372 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002373
Gilles Peskinefe119512018-07-08 21:39:34 +02002374 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2375 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002376
Gilles Peskinefe119512018-07-08 21:39:34 +02002377 TEST_ASSERT( psa_cipher_set_iv( &operation,
2378 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002379
mohammad16033d91abe2018-07-03 13:15:54 +03002380 output_buffer_size = (size_t) input->len +
2381 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002382 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002383
Gilles Peskine4abf7412018-06-18 16:35:34 +02002384 TEST_ASSERT( psa_cipher_update( &operation,
2385 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386 output, output_buffer_size,
2387 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002388 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002389 status = psa_cipher_finish( &operation,
2390 output + function_output_length,
2391 output_buffer_size,
2392 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002393 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002394 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395
2396 if( expected_status == PSA_SUCCESS )
2397 {
2398 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002399 ASSERT_COMPARE( expected_output->x, expected_output->len,
2400 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002401 }
2402
Gilles Peskine50e586b2018-06-08 14:28:46 +02002403exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002404 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405 psa_destroy_key( key_slot );
2406 mbedtls_psa_crypto_free( );
2407}
2408/* END_CASE */
2409
Gilles Peskine50e586b2018-06-08 14:28:46 +02002410/* BEGIN_CASE */
2411void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002412 data_t *key,
2413 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002414{
2415 int key_slot = 1;
2416 psa_key_type_t key_type = key_type_arg;
2417 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002418 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002419 size_t iv_size = 16;
2420 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002421 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002422 size_t output1_size = 0;
2423 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002424 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002425 size_t output2_size = 0;
2426 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002427 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002428 psa_cipher_operation_t operation1;
2429 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002430 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002431
mohammad1603d7d7ba52018-03-12 18:51:53 +02002432 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002433 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002434 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2435 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002436
mohammad1603d7d7ba52018-03-12 18:51:53 +02002437 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2438
Moran Pekered346952018-07-05 15:22:45 +03002439 psa_key_policy_init( &policy );
2440 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2441 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2442
mohammad1603d7d7ba52018-03-12 18:51:53 +02002443 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002444 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002445
Gilles Peskinefe119512018-07-08 21:39:34 +02002446 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2447 key_slot, alg ) == PSA_SUCCESS );
2448 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2449 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002450
Gilles Peskinefe119512018-07-08 21:39:34 +02002451 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2452 iv, iv_size,
2453 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002454 output1_size = (size_t) input->len +
2455 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002456 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002457
Gilles Peskine4abf7412018-06-18 16:35:34 +02002458 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002459 output1, output1_size,
2460 &output1_length ) == PSA_SUCCESS );
2461 TEST_ASSERT( psa_cipher_finish( &operation1,
2462 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002463 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002464
Gilles Peskine048b7f02018-06-08 14:20:49 +02002465 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002466
2467 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2468
2469 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002470 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002471
Gilles Peskinefe119512018-07-08 21:39:34 +02002472 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2473 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002474 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2475 output2, output2_size,
2476 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002477 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002478 TEST_ASSERT( psa_cipher_finish( &operation2,
2479 output2 + output2_length,
2480 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002481 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002482
Gilles Peskine048b7f02018-06-08 14:20:49 +02002483 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002484
Janos Follath25c4fa82018-07-06 16:23:25 +01002485 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002486
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002487 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002488
2489exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002490 mbedtls_free( output1 );
2491 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002492 psa_destroy_key( key_slot );
2493 mbedtls_psa_crypto_free( );
2494}
2495/* END_CASE */
2496
2497/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002498void cipher_verify_output_multipart( int alg_arg,
2499 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002500 data_t *key,
2501 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002502 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002503{
2504 int key_slot = 1;
2505 psa_key_type_t key_type = key_type_arg;
2506 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002507 unsigned char iv[16] = {0};
2508 size_t iv_size = 16;
2509 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002510 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002511 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002512 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002513 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002514 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002515 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002516 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002517 psa_cipher_operation_t operation1;
2518 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002519 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002520
Moran Pekerded84402018-06-06 16:36:50 +03002521 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002522 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002523 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2524 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002525
Moran Pekerded84402018-06-06 16:36:50 +03002526 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2527
Moran Pekered346952018-07-05 15:22:45 +03002528 psa_key_policy_init( &policy );
2529 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2530 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2531
Moran Pekerded84402018-06-06 16:36:50 +03002532 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002533 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002534
Gilles Peskinefe119512018-07-08 21:39:34 +02002535 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2536 key_slot, alg ) == PSA_SUCCESS );
2537 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2538 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002539
Gilles Peskinefe119512018-07-08 21:39:34 +02002540 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2541 iv, iv_size,
2542 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002543 output1_buffer_size = (size_t) input->len +
2544 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002545 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002546
Gilles Peskine4abf7412018-06-18 16:35:34 +02002547 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002548
itayzafrir3e02b3b2018-06-12 17:06:52 +03002549 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002550 output1, output1_buffer_size,
2551 &function_output_length ) == PSA_SUCCESS );
2552 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002553
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002554 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002555 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002556 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002557 output1, output1_buffer_size,
2558 &function_output_length ) == PSA_SUCCESS );
2559 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002560
Gilles Peskine048b7f02018-06-08 14:20:49 +02002561 TEST_ASSERT( psa_cipher_finish( &operation1,
2562 output1 + output1_length,
2563 output1_buffer_size - output1_length,
2564 &function_output_length ) == PSA_SUCCESS );
2565 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002566
2567 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2568
Gilles Peskine048b7f02018-06-08 14:20:49 +02002569 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002570 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002571
Gilles Peskinefe119512018-07-08 21:39:34 +02002572 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2573 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002574
2575 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002576 output2, output2_buffer_size,
2577 &function_output_length ) == PSA_SUCCESS );
2578 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002579
Gilles Peskine048b7f02018-06-08 14:20:49 +02002580 TEST_ASSERT( psa_cipher_update( &operation2,
2581 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002582 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002583 output2, output2_buffer_size,
2584 &function_output_length ) == PSA_SUCCESS );
2585 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002586
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002587 TEST_ASSERT( psa_cipher_finish( &operation2,
2588 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002589 output2_buffer_size - output2_length,
2590 &function_output_length ) == PSA_SUCCESS );
2591 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002592
Janos Follath25c4fa82018-07-06 16:23:25 +01002593 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002594
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002595 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002596
2597exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002598 mbedtls_free( output1 );
2599 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002600 psa_destroy_key( key_slot );
2601 mbedtls_psa_crypto_free( );
2602}
2603/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002604
Gilles Peskine20035e32018-02-03 22:44:14 +01002605/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002606void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002607 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002608 data_t *nonce,
2609 data_t *additional_data,
2610 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002611 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002612{
2613 int slot = 1;
2614 psa_key_type_t key_type = key_type_arg;
2615 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002616 unsigned char *output_data = NULL;
2617 size_t output_size = 0;
2618 size_t output_length = 0;
2619 unsigned char *output_data2 = NULL;
2620 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002621 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002622 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002623 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002624
Gilles Peskinea1cac842018-06-11 19:33:02 +02002625 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002626 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002627 TEST_ASSERT( nonce != NULL );
2628 TEST_ASSERT( additional_data != NULL );
2629 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2630 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2631 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2632 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2633
Gilles Peskine4abf7412018-06-18 16:35:34 +02002634 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002635 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002636
2637 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2638
2639 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002640 psa_key_policy_set_usage( &policy,
2641 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2642 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002643 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2644
2645 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002646 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647
2648 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002649 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002650 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002651 additional_data->len,
2652 input_data->x, input_data->len,
2653 output_data, output_size,
2654 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002655
2656 if( PSA_SUCCESS == expected_result )
2657 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002658 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002659
2660 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002661 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002662 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002663 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002664 output_data, output_length,
2665 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002666 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002667
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002668 ASSERT_COMPARE( input_data->x, input_data->len,
2669 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002670 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002671
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672exit:
2673 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002674 mbedtls_free( output_data );
2675 mbedtls_free( output_data2 );
2676 mbedtls_psa_crypto_free( );
2677}
2678/* END_CASE */
2679
2680/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002681void aead_encrypt( int key_type_arg, data_t *key_data,
2682 int alg_arg,
2683 data_t *nonce,
2684 data_t *additional_data,
2685 data_t *input_data,
2686 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002687{
2688 int slot = 1;
2689 psa_key_type_t key_type = key_type_arg;
2690 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002691 unsigned char *output_data = NULL;
2692 size_t output_size = 0;
2693 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002694 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002695 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696
Gilles Peskinea1cac842018-06-11 19:33:02 +02002697 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002698 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002699 TEST_ASSERT( additional_data != NULL );
2700 TEST_ASSERT( nonce != NULL );
2701 TEST_ASSERT( expected_result != NULL );
2702 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2703 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2704 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2705 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2706 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2707
Gilles Peskine4abf7412018-06-18 16:35:34 +02002708 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002709 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002710
Gilles Peskinea1cac842018-06-11 19:33:02 +02002711 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2712
2713 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002714 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002715 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2716
2717 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002718 key_data->x,
2719 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002720
2721 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002722 nonce->x, nonce->len,
2723 additional_data->x, additional_data->len,
2724 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002725 output_data, output_size,
2726 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002727
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002728 ASSERT_COMPARE( expected_result->x, expected_result->len,
2729 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002730
Gilles Peskinea1cac842018-06-11 19:33:02 +02002731exit:
2732 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002733 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002734 mbedtls_psa_crypto_free( );
2735}
2736/* END_CASE */
2737
2738/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002739void aead_decrypt( int key_type_arg, data_t *key_data,
2740 int alg_arg,
2741 data_t *nonce,
2742 data_t *additional_data,
2743 data_t *input_data,
2744 data_t *expected_data,
2745 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002746{
2747 int slot = 1;
2748 psa_key_type_t key_type = key_type_arg;
2749 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002750 unsigned char *output_data = NULL;
2751 size_t output_size = 0;
2752 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002754 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002755 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002756
Gilles Peskinea1cac842018-06-11 19:33:02 +02002757 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002758 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002759 TEST_ASSERT( additional_data != NULL );
2760 TEST_ASSERT( nonce != NULL );
2761 TEST_ASSERT( expected_data != NULL );
2762 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2763 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2764 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2765 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2766 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2767
Gilles Peskine4abf7412018-06-18 16:35:34 +02002768 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002769 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002770
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2772
2773 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002774 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002775 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2776
2777 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002778 key_data->x,
2779 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002780
2781 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002782 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002783 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002784 additional_data->len,
2785 input_data->x, input_data->len,
2786 output_data, output_size,
2787 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002788
Gilles Peskine2d277862018-06-18 15:41:12 +02002789 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002790 ASSERT_COMPARE( expected_data->x, expected_data->len,
2791 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002792
Gilles Peskinea1cac842018-06-11 19:33:02 +02002793exit:
2794 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002795 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002796 mbedtls_psa_crypto_free( );
2797}
2798/* END_CASE */
2799
2800/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002801void signature_size( int type_arg,
2802 int bits,
2803 int alg_arg,
2804 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002805{
2806 psa_key_type_t type = type_arg;
2807 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002808 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002809 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2810exit:
2811 ;
2812}
2813/* END_CASE */
2814
2815/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002816void sign_deterministic( int key_type_arg, data_t *key_data,
2817 int alg_arg, data_t *input_data,
2818 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002819{
2820 int slot = 1;
2821 psa_key_type_t key_type = key_type_arg;
2822 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002823 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002824 unsigned char *signature = NULL;
2825 size_t signature_size;
2826 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002827 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002828
Gilles Peskine20035e32018-02-03 22:44:14 +01002829 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002830 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002831 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002832 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2833 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2834 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002835
2836 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2837
mohammad1603a97cb8c2018-03-28 03:46:26 -07002838 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002839 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002840 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2841
Gilles Peskine20035e32018-02-03 22:44:14 +01002842 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002843 key_data->x,
2844 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002845 TEST_ASSERT( psa_get_key_information( slot,
2846 NULL,
2847 &key_bits ) == PSA_SUCCESS );
2848
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002849 /* Allocate a buffer which has the size advertized by the
2850 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002851 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2852 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002853 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002854 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002855 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002856
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002857 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002858 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002859 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002860 signature, signature_size,
2861 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002862 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002863 ASSERT_COMPARE( output_data->x, output_data->len,
2864 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002865
2866exit:
2867 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002868 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002869 mbedtls_psa_crypto_free( );
2870}
2871/* END_CASE */
2872
2873/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002874void sign_fail( int key_type_arg, data_t *key_data,
2875 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002876 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002877{
2878 int slot = 1;
2879 psa_key_type_t key_type = key_type_arg;
2880 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002881 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002882 psa_status_t actual_status;
2883 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002884 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002885 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002886 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002887
Gilles Peskine20035e32018-02-03 22:44:14 +01002888 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002889 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2891 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2892
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002893 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002894
2895 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2896
mohammad1603a97cb8c2018-03-28 03:46:26 -07002897 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002898 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002899 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2900
Gilles Peskine20035e32018-02-03 22:44:14 +01002901 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002902 key_data->x,
2903 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002904
2905 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002906 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002907 signature, signature_size,
2908 &signature_length );
2909 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002910 /* The value of *signature_length is unspecified on error, but
2911 * whatever it is, it should be less than signature_size, so that
2912 * if the caller tries to read *signature_length bytes without
2913 * checking the error code then they don't overflow a buffer. */
2914 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002915
2916exit:
2917 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002918 mbedtls_free( signature );
2919 mbedtls_psa_crypto_free( );
2920}
2921/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002922
2923/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002924void sign_verify( int key_type_arg, data_t *key_data,
2925 int alg_arg, data_t *input_data )
2926{
2927 int slot = 1;
2928 psa_key_type_t key_type = key_type_arg;
2929 psa_algorithm_t alg = alg_arg;
2930 size_t key_bits;
2931 unsigned char *signature = NULL;
2932 size_t signature_size;
2933 size_t signature_length = 0xdeadbeef;
2934 psa_key_policy_t policy;
2935
2936 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2937
2938 psa_key_policy_init( &policy );
2939 psa_key_policy_set_usage( &policy,
2940 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2941 alg );
2942 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2943
2944 TEST_ASSERT( psa_import_key( slot, key_type,
2945 key_data->x,
2946 key_data->len ) == PSA_SUCCESS );
2947 TEST_ASSERT( psa_get_key_information( slot,
2948 NULL,
2949 &key_bits ) == PSA_SUCCESS );
2950
2951 /* Allocate a buffer which has the size advertized by the
2952 * library. */
2953 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2954 key_bits, alg );
2955 TEST_ASSERT( signature_size != 0 );
2956 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002957 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002958
2959 /* Perform the signature. */
2960 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2961 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002962 signature, signature_size,
2963 &signature_length ) == PSA_SUCCESS );
2964 /* Check that the signature length looks sensible. */
2965 TEST_ASSERT( signature_length <= signature_size );
2966 TEST_ASSERT( signature_length > 0 );
2967
2968 /* Use the library to verify that the signature is correct. */
2969 TEST_ASSERT( psa_asymmetric_verify(
2970 slot, alg,
2971 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002972 signature, signature_length ) == PSA_SUCCESS );
2973
2974 if( input_data->len != 0 )
2975 {
2976 /* Flip a bit in the input and verify that the signature is now
2977 * detected as invalid. Flip a bit at the beginning, not at the end,
2978 * because ECDSA may ignore the last few bits of the input. */
2979 input_data->x[0] ^= 1;
2980 TEST_ASSERT( psa_asymmetric_verify(
2981 slot, alg,
2982 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002983 signature,
2984 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2985 }
2986
2987exit:
2988 psa_destroy_key( slot );
2989 mbedtls_free( signature );
2990 mbedtls_psa_crypto_free( );
2991}
2992/* END_CASE */
2993
2994/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002995void asymmetric_verify( int key_type_arg, data_t *key_data,
2996 int alg_arg, data_t *hash_data,
2997 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002998{
2999 int slot = 1;
3000 psa_key_type_t key_type = key_type_arg;
3001 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003002 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03003003
Gilles Peskine69c12672018-06-28 00:07:19 +02003004 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3005
itayzafrir5c753392018-05-08 11:18:38 +03003006 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003007 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003008 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003009 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3010 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3011 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003012
3013 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3014
3015 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003016 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03003017 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3018
3019 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003020 key_data->x,
3021 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003022
3023 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003024 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003025 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003026 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003027exit:
3028 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03003029 mbedtls_psa_crypto_free( );
3030}
3031/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032
3033/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003034void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3035 int alg_arg, data_t *hash_data,
3036 data_t *signature_data,
3037 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003038{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003039 int slot = 1;
3040 psa_key_type_t key_type = key_type_arg;
3041 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042 psa_status_t actual_status;
3043 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003044 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003045
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003049 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3050 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3051 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003052
3053 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3054
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003055 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003056 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003057 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3058
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003059 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003060 key_data->x,
3061 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062
3063 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003064 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003065 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003066 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068 TEST_ASSERT( actual_status == expected_status );
3069
3070exit:
3071 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003072 mbedtls_psa_crypto_free( );
3073}
3074/* END_CASE */
3075
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003076/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003077void asymmetric_encrypt( int key_type_arg,
3078 data_t *key_data,
3079 int alg_arg,
3080 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003081 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003082 int expected_output_length_arg,
3083 int expected_status_arg )
3084{
3085 int slot = 1;
3086 psa_key_type_t key_type = key_type_arg;
3087 psa_algorithm_t alg = alg_arg;
3088 size_t expected_output_length = expected_output_length_arg;
3089 size_t key_bits;
3090 unsigned char *output = NULL;
3091 size_t output_size;
3092 size_t output_length = ~0;
3093 psa_status_t actual_status;
3094 psa_status_t expected_status = expected_status_arg;
3095 psa_key_policy_t policy;
3096
3097 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3098
3099 /* Import the key */
3100 psa_key_policy_init( &policy );
3101 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
3102 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3103 TEST_ASSERT( psa_import_key( slot, key_type,
3104 key_data->x,
3105 key_data->len ) == PSA_SUCCESS );
3106
3107 /* Determine the maximum output length */
3108 TEST_ASSERT( psa_get_key_information( slot,
3109 NULL,
3110 &key_bits ) == PSA_SUCCESS );
3111 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003112 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003113
3114 /* Encrypt the input */
3115 actual_status = psa_asymmetric_encrypt( slot, alg,
3116 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003117 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003118 output, output_size,
3119 &output_length );
3120 TEST_ASSERT( actual_status == expected_status );
3121 TEST_ASSERT( output_length == expected_output_length );
3122
Gilles Peskine68428122018-06-30 18:42:41 +02003123 /* If the label is empty, the test framework puts a non-null pointer
3124 * in label->x. Test that a null pointer works as well. */
3125 if( label->len == 0 )
3126 {
3127 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003128 if( output_size != 0 )
3129 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003130 actual_status = psa_asymmetric_encrypt( slot, alg,
3131 input_data->x, input_data->len,
3132 NULL, label->len,
3133 output, output_size,
3134 &output_length );
3135 TEST_ASSERT( actual_status == expected_status );
3136 TEST_ASSERT( output_length == expected_output_length );
3137 }
3138
Gilles Peskine656896e2018-06-29 19:12:28 +02003139exit:
3140 psa_destroy_key( slot );
3141 mbedtls_free( output );
3142 mbedtls_psa_crypto_free( );
3143}
3144/* END_CASE */
3145
3146/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003147void asymmetric_encrypt_decrypt( int key_type_arg,
3148 data_t *key_data,
3149 int alg_arg,
3150 data_t *input_data,
3151 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152{
3153 int slot = 1;
3154 psa_key_type_t key_type = key_type_arg;
3155 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003156 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003157 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003158 size_t output_size;
3159 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003160 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003161 size_t output2_size;
3162 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003163 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003165 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003167 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3168 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3169
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3171
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003172 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003173 psa_key_policy_set_usage( &policy,
3174 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003175 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003176 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3177
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003179 key_data->x,
3180 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003182
3183 /* Determine the maximum ciphertext length */
3184 TEST_ASSERT( psa_get_key_information( slot,
3185 NULL,
3186 &key_bits ) == PSA_SUCCESS );
3187 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003188 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003189 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003190 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003191
Gilles Peskineeebd7382018-06-08 18:11:54 +02003192 /* We test encryption by checking that encrypt-then-decrypt gives back
3193 * the original plaintext because of the non-optional random
3194 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02003195 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003196 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003197 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003198 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003199 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003200 /* We don't know what ciphertext length to expect, but check that
3201 * it looks sensible. */
3202 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003203
Gilles Peskine2d277862018-06-18 15:41:12 +02003204 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003205 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003206 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003207 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003208 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003209 ASSERT_COMPARE( input_data->x, input_data->len,
3210 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
3212exit:
3213 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003214 mbedtls_free( output );
3215 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217}
3218/* END_CASE */
3219
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003220/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003221void asymmetric_decrypt( int key_type_arg,
3222 data_t *key_data,
3223 int alg_arg,
3224 data_t *input_data,
3225 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003226 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003227{
3228 int slot = 1;
3229 psa_key_type_t key_type = key_type_arg;
3230 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003231 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003232 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003233 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003234 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003239 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3240 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3241 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3242
Gilles Peskine4abf7412018-06-18 16:35:34 +02003243 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003244 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003245
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003246 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3247
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003248 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003249 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003250 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3251
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003253 key_data->x,
3254 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003256 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003257 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003258 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003259 output,
3260 output_size,
3261 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003262 ASSERT_COMPARE( expected_data->x, expected_data->len,
3263 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003264
Gilles Peskine68428122018-06-30 18:42:41 +02003265 /* If the label is empty, the test framework puts a non-null pointer
3266 * in label->x. Test that a null pointer works as well. */
3267 if( label->len == 0 )
3268 {
3269 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003270 if( output_size != 0 )
3271 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003272 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3273 input_data->x, input_data->len,
3274 NULL, label->len,
3275 output,
3276 output_size,
3277 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003278 ASSERT_COMPARE( expected_data->x, expected_data->len,
3279 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003280 }
3281
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282exit:
3283 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003284 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286}
3287/* END_CASE */
3288
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003290void asymmetric_decrypt_fail( int key_type_arg,
3291 data_t *key_data,
3292 int alg_arg,
3293 data_t *input_data,
3294 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003295 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003297 int slot = 1;
3298 psa_key_type_t key_type = key_type_arg;
3299 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003301 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003302 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003303 psa_status_t actual_status;
3304 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003305 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003307 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003309 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3310 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3311
Gilles Peskine4abf7412018-06-18 16:35:34 +02003312 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003313 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003314
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3316
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003317 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003318 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003319 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3320
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003321 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003322 key_data->x,
3323 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003324
Gilles Peskine2d277862018-06-18 15:41:12 +02003325 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003326 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003327 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003328 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003329 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003330 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003331 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332
Gilles Peskine68428122018-06-30 18:42:41 +02003333 /* If the label is empty, the test framework puts a non-null pointer
3334 * in label->x. Test that a null pointer works as well. */
3335 if( label->len == 0 )
3336 {
3337 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003338 if( output_size != 0 )
3339 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003340 actual_status = psa_asymmetric_decrypt( slot, alg,
3341 input_data->x, input_data->len,
3342 NULL, label->len,
3343 output, output_size,
3344 &output_length );
3345 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003346 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003347 }
3348
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003349exit:
3350 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003351 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003352 mbedtls_psa_crypto_free( );
3353}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003354/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003355
3356/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003357void derive_setup( int key_type_arg,
3358 data_t *key_data,
3359 int alg_arg,
3360 data_t *salt,
3361 data_t *label,
3362 int requested_capacity_arg,
3363 int expected_status_arg )
3364{
3365 psa_key_slot_t slot = 1;
3366 size_t key_type = key_type_arg;
3367 psa_algorithm_t alg = alg_arg;
3368 size_t requested_capacity = requested_capacity_arg;
3369 psa_status_t expected_status = expected_status_arg;
3370 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3371 psa_key_policy_t policy;
3372
3373 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3374
3375 psa_key_policy_init( &policy );
3376 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3377 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3378
3379 TEST_ASSERT( psa_import_key( slot, key_type,
3380 key_data->x,
3381 key_data->len ) == PSA_SUCCESS );
3382
3383 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3384 salt->x, salt->len,
3385 label->x, label->len,
3386 requested_capacity ) == expected_status );
3387
3388exit:
3389 psa_generator_abort( &generator );
3390 psa_destroy_key( slot );
3391 mbedtls_psa_crypto_free( );
3392}
3393/* END_CASE */
3394
3395/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003396void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003397{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003398 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003399 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003400 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003401 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003402 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003403 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003404 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3405 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3406 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003407 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003408
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003409 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3410
3411 psa_key_policy_init( &policy );
3412 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3413 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3414
3415 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003416 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003417 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003418
3419 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003420 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003421 NULL, 0,
3422 NULL, 0,
3423 capacity ) == PSA_SUCCESS );
3424
3425 /* state of generator shouldn't allow additional generation */
3426 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3427 NULL, 0,
3428 NULL, 0,
3429 capacity ) == PSA_ERROR_BAD_STATE );
3430
3431 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3432 == PSA_SUCCESS );
3433
3434 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3435 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3436
3437
3438exit:
3439 psa_generator_abort( &generator );
3440 psa_destroy_key( base_key );
3441 mbedtls_psa_crypto_free( );
3442}
3443/* END_CASE */
3444
3445
3446/* BEGIN_CASE */
3447void test_derive_invalid_generator_tests( )
3448{
3449 uint8_t output_buffer[16];
3450 size_t buffer_size = 16;
3451 size_t capacity = 0;
3452 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3453
Nir Sonnenschein50789302018-10-31 12:16:38 +02003454 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003455 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003456
3457 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003458 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003459
Nir Sonnenschein50789302018-10-31 12:16:38 +02003460 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003461
Nir Sonnenschein50789302018-10-31 12:16:38 +02003462 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003463 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003464
Nir Sonnenschein50789302018-10-31 12:16:38 +02003465 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003466 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003467
3468exit:
3469 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003470}
3471/* END_CASE */
3472
3473/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003474void derive_output( int alg_arg,
3475 data_t *key_data,
3476 data_t *salt,
3477 data_t *label,
3478 int requested_capacity_arg,
3479 data_t *expected_output1,
3480 data_t *expected_output2 )
3481{
3482 psa_key_slot_t slot = 1;
3483 psa_algorithm_t alg = alg_arg;
3484 size_t requested_capacity = requested_capacity_arg;
3485 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3486 uint8_t *expected_outputs[2] =
3487 {expected_output1->x, expected_output2->x};
3488 size_t output_sizes[2] =
3489 {expected_output1->len, expected_output2->len};
3490 size_t output_buffer_size = 0;
3491 uint8_t *output_buffer = NULL;
3492 size_t expected_capacity;
3493 size_t current_capacity;
3494 psa_key_policy_t policy;
3495 psa_status_t status;
3496 unsigned i;
3497
3498 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3499 {
3500 if( output_sizes[i] > output_buffer_size )
3501 output_buffer_size = output_sizes[i];
3502 if( output_sizes[i] == 0 )
3503 expected_outputs[i] = NULL;
3504 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003505 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003506 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3507
3508 psa_key_policy_init( &policy );
3509 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3510 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3511
3512 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3513 key_data->x,
3514 key_data->len ) == PSA_SUCCESS );
3515
3516 /* Extraction phase. */
3517 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3518 salt->x, salt->len,
3519 label->x, label->len,
3520 requested_capacity ) == PSA_SUCCESS );
3521 TEST_ASSERT( psa_get_generator_capacity( &generator,
3522 &current_capacity ) ==
3523 PSA_SUCCESS );
3524 TEST_ASSERT( current_capacity == requested_capacity );
3525 expected_capacity = requested_capacity;
3526
3527 /* Expansion phase. */
3528 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3529 {
3530 /* Read some bytes. */
3531 status = psa_generator_read( &generator,
3532 output_buffer, output_sizes[i] );
3533 if( expected_capacity == 0 && output_sizes[i] == 0 )
3534 {
3535 /* Reading 0 bytes when 0 bytes are available can go either way. */
3536 TEST_ASSERT( status == PSA_SUCCESS ||
3537 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3538 continue;
3539 }
3540 else if( expected_capacity == 0 ||
3541 output_sizes[i] > expected_capacity )
3542 {
3543 /* Capacity exceeded. */
3544 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3545 expected_capacity = 0;
3546 continue;
3547 }
3548 /* Success. Check the read data. */
3549 TEST_ASSERT( status == PSA_SUCCESS );
3550 if( output_sizes[i] != 0 )
3551 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3552 output_sizes[i] ) == 0 );
3553 /* Check the generator status. */
3554 expected_capacity -= output_sizes[i];
3555 TEST_ASSERT( psa_get_generator_capacity( &generator,
3556 &current_capacity ) ==
3557 PSA_SUCCESS );
3558 TEST_ASSERT( expected_capacity == current_capacity );
3559 }
3560 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3561
3562exit:
3563 mbedtls_free( output_buffer );
3564 psa_generator_abort( &generator );
3565 psa_destroy_key( slot );
3566 mbedtls_psa_crypto_free( );
3567}
3568/* END_CASE */
3569
3570/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003571void derive_full( int alg_arg,
3572 data_t *key_data,
3573 data_t *salt,
3574 data_t *label,
3575 int requested_capacity_arg )
3576{
3577 psa_key_slot_t slot = 1;
3578 psa_algorithm_t alg = alg_arg;
3579 size_t requested_capacity = requested_capacity_arg;
3580 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3581 unsigned char output_buffer[16];
3582 size_t expected_capacity = requested_capacity;
3583 size_t current_capacity;
3584 psa_key_policy_t policy;
3585
3586 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3587
3588 psa_key_policy_init( &policy );
3589 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3590 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3591
3592 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3593 key_data->x,
3594 key_data->len ) == PSA_SUCCESS );
3595
3596 /* Extraction phase. */
3597 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3598 salt->x, salt->len,
3599 label->x, label->len,
3600 requested_capacity ) == PSA_SUCCESS );
3601 TEST_ASSERT( psa_get_generator_capacity( &generator,
3602 &current_capacity ) ==
3603 PSA_SUCCESS );
3604 TEST_ASSERT( current_capacity == expected_capacity );
3605
3606 /* Expansion phase. */
3607 while( current_capacity > 0 )
3608 {
3609 size_t read_size = sizeof( output_buffer );
3610 if( read_size > current_capacity )
3611 read_size = current_capacity;
3612 TEST_ASSERT( psa_generator_read( &generator,
3613 output_buffer,
3614 read_size ) == PSA_SUCCESS );
3615 expected_capacity -= read_size;
3616 TEST_ASSERT( psa_get_generator_capacity( &generator,
3617 &current_capacity ) ==
3618 PSA_SUCCESS );
3619 TEST_ASSERT( current_capacity == expected_capacity );
3620 }
3621
3622 /* Check that the generator refuses to go over capacity. */
3623 TEST_ASSERT( psa_generator_read( &generator,
3624 output_buffer,
3625 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3626
3627 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3628
3629exit:
3630 psa_generator_abort( &generator );
3631 psa_destroy_key( slot );
3632 mbedtls_psa_crypto_free( );
3633}
3634/* END_CASE */
3635
3636/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003637void derive_key_exercise( int alg_arg,
3638 data_t *key_data,
3639 data_t *salt,
3640 data_t *label,
3641 int derived_type_arg,
3642 int derived_bits_arg,
3643 int derived_usage_arg,
3644 int derived_alg_arg )
3645{
3646 psa_key_slot_t base_key = 1;
3647 psa_key_slot_t derived_key = 2;
3648 psa_algorithm_t alg = alg_arg;
3649 psa_key_type_t derived_type = derived_type_arg;
3650 size_t derived_bits = derived_bits_arg;
3651 psa_key_usage_t derived_usage = derived_usage_arg;
3652 psa_algorithm_t derived_alg = derived_alg_arg;
3653 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3654 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3655 psa_key_policy_t policy;
3656 psa_key_type_t got_type;
3657 size_t got_bits;
3658
3659 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3660
3661 psa_key_policy_init( &policy );
3662 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3663 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3664 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3665 key_data->x,
3666 key_data->len ) == PSA_SUCCESS );
3667
3668 /* Derive a key. */
3669 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3670 salt->x, salt->len,
3671 label->x, label->len,
3672 capacity ) == PSA_SUCCESS );
3673 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3674 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3675 TEST_ASSERT( psa_generator_import_key( derived_key,
3676 derived_type,
3677 derived_bits,
3678 &generator ) == PSA_SUCCESS );
3679
3680 /* Test the key information */
3681 TEST_ASSERT( psa_get_key_information( derived_key,
3682 &got_type,
3683 &got_bits ) == PSA_SUCCESS );
3684 TEST_ASSERT( got_type == derived_type );
3685 TEST_ASSERT( got_bits == derived_bits );
3686
3687 /* Exercise the derived key. */
3688 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3689 goto exit;
3690
3691exit:
3692 psa_generator_abort( &generator );
3693 psa_destroy_key( base_key );
3694 psa_destroy_key( derived_key );
3695 mbedtls_psa_crypto_free( );
3696}
3697/* END_CASE */
3698
3699/* BEGIN_CASE */
3700void derive_key_export( int alg_arg,
3701 data_t *key_data,
3702 data_t *salt,
3703 data_t *label,
3704 int bytes1_arg,
3705 int bytes2_arg )
3706{
3707 psa_key_slot_t base_key = 1;
3708 psa_key_slot_t derived_key = 2;
3709 psa_algorithm_t alg = alg_arg;
3710 size_t bytes1 = bytes1_arg;
3711 size_t bytes2 = bytes2_arg;
3712 size_t capacity = bytes1 + bytes2;
3713 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003714 uint8_t *output_buffer = NULL;
3715 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003716 psa_key_policy_t policy;
3717 size_t length;
3718
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003719 ASSERT_ALLOC( output_buffer, capacity );
3720 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003721 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3722
3723 psa_key_policy_init( &policy );
3724 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3725 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3726 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3727 key_data->x,
3728 key_data->len ) == PSA_SUCCESS );
3729
3730 /* Derive some material and output it. */
3731 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3732 salt->x, salt->len,
3733 label->x, label->len,
3734 capacity ) == PSA_SUCCESS );
3735 TEST_ASSERT( psa_generator_read( &generator,
3736 output_buffer,
3737 capacity ) == PSA_SUCCESS );
3738 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3739
3740 /* Derive the same output again, but this time store it in key objects. */
3741 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3742 salt->x, salt->len,
3743 label->x, label->len,
3744 capacity ) == PSA_SUCCESS );
3745 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3746 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3747 TEST_ASSERT( psa_generator_import_key( derived_key,
3748 PSA_KEY_TYPE_RAW_DATA,
3749 PSA_BYTES_TO_BITS( bytes1 ),
3750 &generator ) == PSA_SUCCESS );
3751 TEST_ASSERT( psa_export_key( derived_key,
3752 export_buffer, bytes1,
3753 &length ) == PSA_SUCCESS );
3754 TEST_ASSERT( length == bytes1 );
3755 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3756 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3757 TEST_ASSERT( psa_generator_import_key( derived_key,
3758 PSA_KEY_TYPE_RAW_DATA,
3759 PSA_BYTES_TO_BITS( bytes2 ),
3760 &generator ) == PSA_SUCCESS );
3761 TEST_ASSERT( psa_export_key( derived_key,
3762 export_buffer + bytes1, bytes2,
3763 &length ) == PSA_SUCCESS );
3764 TEST_ASSERT( length == bytes2 );
3765
3766 /* Compare the outputs from the two runs. */
3767 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3768
3769exit:
3770 mbedtls_free( output_buffer );
3771 mbedtls_free( export_buffer );
3772 psa_generator_abort( &generator );
3773 psa_destroy_key( base_key );
3774 psa_destroy_key( derived_key );
3775 mbedtls_psa_crypto_free( );
3776}
3777/* END_CASE */
3778
3779/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003780void key_agreement_setup( int alg_arg,
3781 int our_key_type_arg, data_t *our_key_data,
3782 data_t *peer_key_data,
3783 int expected_status_arg )
3784{
3785 psa_key_slot_t our_key = 1;
3786 psa_algorithm_t alg = alg_arg;
3787 psa_key_type_t our_key_type = our_key_type_arg;
3788 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3789 psa_key_policy_t policy;
3790
3791 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3792
3793 psa_key_policy_init( &policy );
3794 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3795 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3796 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3797 our_key_data->x,
3798 our_key_data->len ) == PSA_SUCCESS );
3799
3800 TEST_ASSERT( psa_key_agreement( &generator,
3801 our_key,
3802 peer_key_data->x, peer_key_data->len,
3803 alg ) == expected_status_arg );
3804
3805exit:
3806 psa_generator_abort( &generator );
3807 psa_destroy_key( our_key );
3808 mbedtls_psa_crypto_free( );
3809}
3810/* END_CASE */
3811
3812/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003813void key_agreement_capacity( int alg_arg,
3814 int our_key_type_arg, data_t *our_key_data,
3815 data_t *peer_key_data,
3816 int expected_capacity_arg )
3817{
3818 psa_key_slot_t our_key = 1;
3819 psa_algorithm_t alg = alg_arg;
3820 psa_key_type_t our_key_type = our_key_type_arg;
3821 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3822 psa_key_policy_t policy;
3823 size_t actual_capacity;
3824
3825 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3826
3827 psa_key_policy_init( &policy );
3828 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3829 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3830 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3831 our_key_data->x,
3832 our_key_data->len ) == PSA_SUCCESS );
3833
3834 TEST_ASSERT( psa_key_agreement( &generator,
3835 our_key,
3836 peer_key_data->x, peer_key_data->len,
3837 alg ) == PSA_SUCCESS );
3838
3839 TEST_ASSERT( psa_get_generator_capacity(
3840 &generator, &actual_capacity ) == PSA_SUCCESS );
3841 TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg );
3842
3843exit:
3844 psa_generator_abort( &generator );
3845 psa_destroy_key( our_key );
3846 mbedtls_psa_crypto_free( );
3847}
3848/* END_CASE */
3849
3850/* BEGIN_CASE */
3851void key_agreement_output( int alg_arg,
3852 int our_key_type_arg, data_t *our_key_data,
3853 data_t *peer_key_data,
3854 data_t *expected_output )
3855{
3856 psa_key_slot_t our_key = 1;
3857 psa_algorithm_t alg = alg_arg;
3858 psa_key_type_t our_key_type = our_key_type_arg;
3859 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3860 psa_key_policy_t policy;
3861 uint8_t *actual_output = mbedtls_calloc( 1, expected_output->len );
3862
3863 TEST_ASSERT( actual_output != NULL );
3864
3865 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3866
3867 psa_key_policy_init( &policy );
3868 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3869 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3870 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3871 our_key_data->x,
3872 our_key_data->len ) == PSA_SUCCESS );
3873
3874 TEST_ASSERT( psa_key_agreement( &generator,
3875 our_key,
3876 peer_key_data->x, peer_key_data->len,
3877 alg ) == PSA_SUCCESS );
3878
3879 TEST_ASSERT( psa_generator_read( &generator,
3880 actual_output,
3881 expected_output->len ) == PSA_SUCCESS );
3882 TEST_ASSERT( memcmp( actual_output, expected_output->x,
3883 expected_output->len ) == 0 );
3884
3885exit:
3886 psa_generator_abort( &generator );
3887 psa_destroy_key( our_key );
3888 mbedtls_psa_crypto_free( );
3889 mbedtls_free( actual_output );
3890}
3891/* END_CASE */
3892
3893/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003894void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003895{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003896 size_t bytes = bytes_arg;
3897 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003898 unsigned char *output = NULL;
3899 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003900 size_t i;
3901 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003902
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003903 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3904 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003905 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003906
3907 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3908
Gilles Peskinea50d7392018-06-21 10:22:13 +02003909 /* Run several times, to ensure that every output byte will be
3910 * nonzero at least once with overwhelming probability
3911 * (2^(-8*number_of_runs)). */
3912 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003913 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003914 if( bytes != 0 )
3915 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003916 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3917
3918 /* Check that no more than bytes have been overwritten */
3919 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3920
3921 for( i = 0; i < bytes; i++ )
3922 {
3923 if( output[i] != 0 )
3924 ++changed[i];
3925 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003926 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003927
3928 /* Check that every byte was changed to nonzero at least once. This
3929 * validates that psa_generate_random is overwriting every byte of
3930 * the output buffer. */
3931 for( i = 0; i < bytes; i++ )
3932 {
3933 TEST_ASSERT( changed[i] != 0 );
3934 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003935
3936exit:
3937 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003938 mbedtls_free( output );
3939 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003940}
3941/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003942
3943/* BEGIN_CASE */
3944void generate_key( int type_arg,
3945 int bits_arg,
3946 int usage_arg,
3947 int alg_arg,
3948 int expected_status_arg )
3949{
3950 int slot = 1;
3951 psa_key_type_t type = type_arg;
3952 psa_key_usage_t usage = usage_arg;
3953 size_t bits = bits_arg;
3954 psa_algorithm_t alg = alg_arg;
3955 psa_status_t expected_status = expected_status_arg;
3956 psa_key_type_t got_type;
3957 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003958 psa_status_t expected_info_status =
3959 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3960 psa_key_policy_t policy;
3961
3962 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3963
3964 psa_key_policy_init( &policy );
3965 psa_key_policy_set_usage( &policy, usage, alg );
3966 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3967
3968 /* Generate a key */
3969 TEST_ASSERT( psa_generate_key( slot, type, bits,
3970 NULL, 0 ) == expected_status );
3971
3972 /* Test the key information */
3973 TEST_ASSERT( psa_get_key_information( slot,
3974 &got_type,
3975 &got_bits ) == expected_info_status );
3976 if( expected_info_status != PSA_SUCCESS )
3977 goto exit;
3978 TEST_ASSERT( got_type == type );
3979 TEST_ASSERT( got_bits == bits );
3980
Gilles Peskine818ca122018-06-20 18:16:48 +02003981 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003982 if( ! exercise_key( slot, usage, alg ) )
3983 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003984
3985exit:
3986 psa_destroy_key( slot );
3987 mbedtls_psa_crypto_free( );
3988}
3989/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003990
3991/* BEGIN_CASE */
3992void validate_module_init_generate_random( )
3993{
3994 psa_status_t status;
3995 uint8_t random[10] = { 0 };
3996 status = psa_generate_random( random, sizeof( random ) );
3997 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3998}
3999/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03004000
4001/* BEGIN_CASE */
4002void validate_module_init_key_based( )
4003{
4004 psa_status_t status;
4005 uint8_t data[10] = { 0 };
4006 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
4007 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
4008}
4009/* END_CASE */