blob: 99e95562b7c31ee651f5f978f33409660c066912 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskine96ee5c72018-07-12 17:24:54 +020014#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
15
itayzafrir3e02b3b2018-06-12 17:06:52 +030016#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020017#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#else
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskine818ca122018-06-20 18:16:48 +0200131static int exercise_mac_key( psa_key_slot_t key,
132 psa_key_usage_t usage,
133 psa_algorithm_t alg )
134{
135 psa_mac_operation_t operation;
136 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200137 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200138 size_t mac_length = sizeof( mac );
139
140 if( usage & PSA_KEY_USAGE_SIGN )
141 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200142 TEST_ASSERT( psa_mac_sign_setup( &operation,
143 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200144 TEST_ASSERT( psa_mac_update( &operation,
145 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200146 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200147 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200149 }
150
151 if( usage & PSA_KEY_USAGE_VERIFY )
152 {
153 psa_status_t verify_status =
154 ( usage & PSA_KEY_USAGE_SIGN ?
155 PSA_SUCCESS :
156 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200157 TEST_ASSERT( psa_mac_verify_setup( &operation,
158 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200159 TEST_ASSERT( psa_mac_update( &operation,
160 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200161 TEST_ASSERT( psa_mac_verify_finish( &operation,
162 mac,
163 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200164 }
165
166 return( 1 );
167
168exit:
169 psa_mac_abort( &operation );
170 return( 0 );
171}
172
173static int exercise_cipher_key( psa_key_slot_t key,
174 psa_key_usage_t usage,
175 psa_algorithm_t alg )
176{
177 psa_cipher_operation_t operation;
178 unsigned char iv[16] = {0};
179 size_t iv_length = sizeof( iv );
180 const unsigned char plaintext[16] = "Hello, world...";
181 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
182 size_t ciphertext_length = sizeof( ciphertext );
183 unsigned char decrypted[sizeof( ciphertext )];
184 size_t part_length;
185
186 if( usage & PSA_KEY_USAGE_ENCRYPT )
187 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200188 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
189 key, alg ) == PSA_SUCCESS );
190 TEST_ASSERT( psa_cipher_generate_iv( &operation,
191 iv, sizeof( iv ),
192 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200193 TEST_ASSERT( psa_cipher_update( &operation,
194 plaintext, sizeof( plaintext ),
195 ciphertext, sizeof( ciphertext ),
196 &ciphertext_length ) == PSA_SUCCESS );
197 TEST_ASSERT( psa_cipher_finish( &operation,
198 ciphertext + ciphertext_length,
199 sizeof( ciphertext ) - ciphertext_length,
200 &part_length ) == PSA_SUCCESS );
201 ciphertext_length += part_length;
202 }
203
204 if( usage & PSA_KEY_USAGE_DECRYPT )
205 {
206 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700207 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200208 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
209 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 size_t bits;
211 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
212 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
213 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200214 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
215 key, alg ) == PSA_SUCCESS );
216 TEST_ASSERT( psa_cipher_set_iv( &operation,
217 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200218 TEST_ASSERT( psa_cipher_update( &operation,
219 ciphertext, ciphertext_length,
220 decrypted, sizeof( decrypted ),
221 &part_length ) == PSA_SUCCESS );
222 status = psa_cipher_finish( &operation,
223 decrypted + part_length,
224 sizeof( decrypted ) - part_length,
225 &part_length );
226 /* For a stream cipher, all inputs are valid. For a block cipher,
227 * if the input is some aribtrary data rather than an actual
228 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700229 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700230 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200231 TEST_ASSERT( status == PSA_SUCCESS );
232 else
233 TEST_ASSERT( status == PSA_SUCCESS ||
234 status == PSA_ERROR_INVALID_PADDING );
235 }
236
237 return( 1 );
238
239exit:
240 psa_cipher_abort( &operation );
241 return( 0 );
242}
243
244static int exercise_aead_key( psa_key_slot_t key,
245 psa_key_usage_t usage,
246 psa_algorithm_t alg )
247{
248 unsigned char nonce[16] = {0};
249 size_t nonce_length = sizeof( nonce );
250 unsigned char plaintext[16] = "Hello, world...";
251 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
252 size_t ciphertext_length = sizeof( ciphertext );
253 size_t plaintext_length = sizeof( ciphertext );
254
255 if( usage & PSA_KEY_USAGE_ENCRYPT )
256 {
257 TEST_ASSERT( psa_aead_encrypt( key, alg,
258 nonce, nonce_length,
259 NULL, 0,
260 plaintext, sizeof( plaintext ),
261 ciphertext, sizeof( ciphertext ),
262 &ciphertext_length ) == PSA_SUCCESS );
263 }
264
265 if( usage & PSA_KEY_USAGE_DECRYPT )
266 {
267 psa_status_t verify_status =
268 ( usage & PSA_KEY_USAGE_ENCRYPT ?
269 PSA_SUCCESS :
270 PSA_ERROR_INVALID_SIGNATURE );
271 TEST_ASSERT( psa_aead_decrypt( key, alg,
272 nonce, nonce_length,
273 NULL, 0,
274 ciphertext, ciphertext_length,
275 plaintext, sizeof( plaintext ),
276 &plaintext_length ) == verify_status );
277 }
278
279 return( 1 );
280
281exit:
282 return( 0 );
283}
284
285static int exercise_signature_key( psa_key_slot_t key,
286 psa_key_usage_t usage,
287 psa_algorithm_t alg )
288{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200289 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
290 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200291 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 size_t signature_length = sizeof( signature );
293
294 if( usage & PSA_KEY_USAGE_SIGN )
295 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200296 /* Some algorithms require the payload to have the size of
297 * the hash encoded in the algorithm. Use this input size
298 * even for algorithms that allow other input sizes. */
299 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
300 if( hash_alg != 0 )
301 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200302 TEST_ASSERT( psa_asymmetric_sign( key, alg,
303 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 signature, sizeof( signature ),
305 &signature_length ) == PSA_SUCCESS );
306 }
307
308 if( usage & PSA_KEY_USAGE_VERIFY )
309 {
310 psa_status_t verify_status =
311 ( usage & PSA_KEY_USAGE_SIGN ?
312 PSA_SUCCESS :
313 PSA_ERROR_INVALID_SIGNATURE );
314 TEST_ASSERT( psa_asymmetric_verify( key, alg,
315 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 signature, signature_length ) ==
317 verify_status );
318 }
319
320 return( 1 );
321
322exit:
323 return( 0 );
324}
325
326static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
327 psa_key_usage_t usage,
328 psa_algorithm_t alg )
329{
330 unsigned char plaintext[256] = "Hello, world...";
331 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
332 size_t ciphertext_length = sizeof( ciphertext );
333 size_t plaintext_length = 16;
334
335 if( usage & PSA_KEY_USAGE_ENCRYPT )
336 {
337 TEST_ASSERT(
338 psa_asymmetric_encrypt( key, alg,
339 plaintext, plaintext_length,
340 NULL, 0,
341 ciphertext, sizeof( ciphertext ),
342 &ciphertext_length ) == PSA_SUCCESS );
343 }
344
345 if( usage & PSA_KEY_USAGE_DECRYPT )
346 {
347 psa_status_t status =
348 psa_asymmetric_decrypt( key, alg,
349 ciphertext, ciphertext_length,
350 NULL, 0,
351 plaintext, sizeof( plaintext ),
352 &plaintext_length );
353 TEST_ASSERT( status == PSA_SUCCESS ||
354 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
355 ( status == PSA_ERROR_INVALID_ARGUMENT ||
356 status == PSA_ERROR_INVALID_PADDING ) ) );
357 }
358
359 return( 1 );
360
361exit:
362 return( 0 );
363}
Gilles Peskine02b75072018-07-01 22:31:34 +0200364
Gilles Peskineea0fb492018-07-12 17:17:20 +0200365static int exercise_key_derivation_key( psa_key_slot_t key,
366 psa_key_usage_t usage,
367 psa_algorithm_t alg )
368{
369 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
370 unsigned char label[16] = "This is a label.";
371 size_t label_length = sizeof( label );
372 unsigned char seed[16] = "abcdefghijklmnop";
373 size_t seed_length = sizeof( seed );
374 unsigned char output[1];
375
376 if( usage & PSA_KEY_USAGE_DERIVE )
377 {
378 TEST_ASSERT( psa_key_derivation( &generator,
379 key, alg,
380 label, label_length,
381 seed, seed_length,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_read( &generator,
384 output,
385 sizeof( output ) ) == PSA_SUCCESS );
386 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
387 }
388
389 return( 1 );
390
391exit:
392 return( 0 );
393}
394
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200395static int is_oid_of_key_type( psa_key_type_t type,
396 const uint8_t *oid, size_t oid_length )
397{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200398 const uint8_t *expected_oid = NULL;
399 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200400#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200401 if( PSA_KEY_TYPE_IS_RSA( type ) )
402 {
403 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
404 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
405 }
406 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200407#endif /* MBEDTLS_RSA_C */
408#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200409 if( PSA_KEY_TYPE_IS_ECC( type ) )
410 {
411 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
412 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
413 }
414 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200415#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200416 {
417 char message[40];
418 mbedtls_snprintf( message, sizeof( message ),
419 "OID not known for key type=0x%08lx",
420 (unsigned long) type );
421 test_fail( message, __LINE__, __FILE__ );
422 return( 0 );
423 }
424
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200425 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200426 return( 1 );
427
428exit:
429 return( 0 );
430}
431
432static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
433 size_t min_bits, size_t max_bits,
434 int must_be_odd )
435{
436 size_t len;
437 size_t actual_bits;
438 unsigned char msb;
439 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
440 MBEDTLS_ASN1_INTEGER ) == 0 );
441 /* Tolerate a slight departure from DER encoding:
442 * - 0 may be represented by an empty string or a 1-byte string.
443 * - The sign bit may be used as a value bit. */
444 if( ( len == 1 && ( *p )[0] == 0 ) ||
445 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
446 {
447 ++( *p );
448 --len;
449 }
450 if( min_bits == 0 && len == 0 )
451 return( 1 );
452 msb = ( *p )[0];
453 TEST_ASSERT( msb != 0 );
454 actual_bits = 8 * ( len - 1 );
455 while( msb != 0 )
456 {
457 msb >>= 1;
458 ++actual_bits;
459 }
460 TEST_ASSERT( actual_bits >= min_bits );
461 TEST_ASSERT( actual_bits <= max_bits );
462 if( must_be_odd )
463 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
464 *p += len;
465 return( 1 );
466exit:
467 return( 0 );
468}
469
470static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
471 size_t *len,
472 unsigned char n, unsigned char tag )
473{
474 int ret;
475 ret = mbedtls_asn1_get_tag( p, end, len,
476 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
477 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
478 if( ret != 0 )
479 return( ret );
480 end = *p + *len;
481 ret = mbedtls_asn1_get_tag( p, end, len, tag );
482 if( ret != 0 )
483 return( ret );
484 if( *p + *len != end )
485 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
486 return( 0 );
487}
488
489static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
490 uint8_t *exported, size_t exported_length )
491{
492 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200493 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200494 else
495 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200496
497#if defined(MBEDTLS_DES_C)
498 if( type == PSA_KEY_TYPE_DES )
499 {
500 /* Check the parity bits. */
501 unsigned i;
502 for( i = 0; i < bits / 8; i++ )
503 {
504 unsigned bit_count = 0;
505 unsigned m;
506 for( m = 1; m <= 0x100; m <<= 1 )
507 {
508 if( exported[i] & m )
509 ++bit_count;
510 }
511 TEST_ASSERT( bit_count % 2 != 0 );
512 }
513 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200514 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200515#endif
516
517#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
518 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
519 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200520 uint8_t *p = exported;
521 uint8_t *end = exported + exported_length;
522 size_t len;
523 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200524 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200525 * modulus INTEGER, -- n
526 * publicExponent INTEGER, -- e
527 * privateExponent INTEGER, -- d
528 * prime1 INTEGER, -- p
529 * prime2 INTEGER, -- q
530 * exponent1 INTEGER, -- d mod (p-1)
531 * exponent2 INTEGER, -- d mod (q-1)
532 * coefficient INTEGER, -- (inverse of q) mod p
533 * }
534 */
535 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
536 MBEDTLS_ASN1_SEQUENCE |
537 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
538 TEST_ASSERT( p + len == end );
539 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
540 goto exit;
541 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
542 goto exit;
543 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
544 goto exit;
545 /* Require d to be at least half the size of n. */
546 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
547 goto exit;
548 /* Require p and q to be at most half the size of n, rounded up. */
549 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
550 goto exit;
551 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
552 goto exit;
553 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
554 goto exit;
555 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
556 goto exit;
557 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
558 goto exit;
559 TEST_ASSERT( p == end );
560 }
561 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200562#endif /* MBEDTLS_RSA_C */
563
564#if defined(MBEDTLS_ECP_C)
565 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
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 int version;
571 /* ECPrivateKey ::= SEQUENCE {
572 * version INTEGER, -- must be 1
573 * privateKey OCTET STRING,
574 * -- `ceiling(log_{256}(n))`-byte string, big endian,
575 * -- where n is the order of the curve.
576 * parameters ECParameters {{ NamedCurve }}, -- mandatory
577 * publicKey BIT STRING -- mandatory
578 * }
579 */
580 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
581 MBEDTLS_ASN1_SEQUENCE |
582 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
583 TEST_ASSERT( p + len == end );
584 TEST_ASSERT( mbedtls_asn1_get_int( &p, end, &version ) == 0 );
585 TEST_ASSERT( version == 1 );
586 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
587 MBEDTLS_ASN1_OCTET_STRING ) == 0 );
588 /* Bug in Mbed TLS: the length of the octet string depends on the value */
589 // TEST_ASSERT( len == PSA_BITS_TO_BYTES( bits ) );
590 p += len;
591 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 0,
592 MBEDTLS_ASN1_OID ) == 0 );
593 p += len;
Gilles Peskinec6290c02018-08-13 17:24:59 +0200594 /* publicKey: ECPoint in uncompressed representation (as below) */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200595 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 1,
596 MBEDTLS_ASN1_BIT_STRING ) == 0 );
597 TEST_ASSERT( p + len == end );
598 TEST_ASSERT( p[0] == 0 ); /* 0 unused bits in the bit string */
599 ++p;
600 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
601 TEST_ASSERT( p[0] == 4 );
602 }
603 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200604#endif /* MBEDTLS_ECP_C */
605
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200606 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
607 {
608 uint8_t *p = exported;
609 uint8_t *end = exported + exported_length;
610 size_t len;
611 mbedtls_asn1_buf alg;
612 mbedtls_asn1_buf params;
613 mbedtls_asn1_bitstring bitstring;
614 /* SubjectPublicKeyInfo ::= SEQUENCE {
615 * algorithm AlgorithmIdentifier,
616 * subjectPublicKey BIT STRING }
617 * AlgorithmIdentifier ::= SEQUENCE {
618 * algorithm OBJECT IDENTIFIER,
619 * parameters ANY DEFINED BY algorithm OPTIONAL }
620 */
621 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
622 MBEDTLS_ASN1_SEQUENCE |
623 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
624 TEST_ASSERT( p + len == end );
625 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
626 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
627 goto exit;
628 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
629 TEST_ASSERT( p == end );
630 p = bitstring.p;
631#if defined(MBEDTLS_RSA_C)
632 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
633 {
634 /* RSAPublicKey ::= SEQUENCE {
635 * modulus INTEGER, -- n
636 * publicExponent INTEGER } -- e
637 */
638 TEST_ASSERT( bitstring.unused_bits == 0 );
639 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
640 MBEDTLS_ASN1_SEQUENCE |
641 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
642 TEST_ASSERT( p + len == end );
643 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
644 goto exit;
645 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
646 goto exit;
647 TEST_ASSERT( p == end );
648 }
649 else
650#endif /* MBEDTLS_RSA_C */
651#if defined(MBEDTLS_ECP_C)
652 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
653 {
654 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200655 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200656 * -- then x_P as an n-bit string, big endian;
657 * -- then y_P as a n-bit string, big endian,
658 * -- where n is the order of the curve.
659 */
660 TEST_ASSERT( bitstring.unused_bits == 0 );
661 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
662 TEST_ASSERT( p[0] == 4 );
663 }
664 else
665#endif /* MBEDTLS_ECP_C */
666 {
667 char message[40];
668 mbedtls_snprintf( message, sizeof( message ),
669 "No sanity check for public key type=0x%08lx",
670 (unsigned long) type );
671 test_fail( message, __LINE__, __FILE__ );
672 return( 0 );
673 }
674 }
675 else
676
677 {
678 /* No sanity checks for other types */
679 }
680
681 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200682
683exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200684 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200685}
686
687static int exercise_export_key( psa_key_slot_t slot,
688 psa_key_usage_t usage )
689{
690 psa_key_type_t type;
691 size_t bits;
692 uint8_t *exported = NULL;
693 size_t exported_size = 0;
694 size_t exported_length = 0;
695 int ok = 0;
696
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200697 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
698
699 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
700 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200701 {
702 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
703 PSA_ERROR_NOT_PERMITTED );
704 return( 1 );
705 }
706
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200708 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200709
710 TEST_ASSERT( psa_export_key( slot,
711 exported, exported_size,
712 &exported_length ) == PSA_SUCCESS );
713 ok = exported_key_sanity_check( type, bits, exported, exported_length );
714
715exit:
716 mbedtls_free( exported );
717 return( ok );
718}
719
720static int exercise_export_public_key( psa_key_slot_t slot )
721{
722 psa_key_type_t type;
723 psa_key_type_t public_type;
724 size_t bits;
725 uint8_t *exported = NULL;
726 size_t exported_size = 0;
727 size_t exported_length = 0;
728 int ok = 0;
729
730 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
731 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
732 {
733 TEST_ASSERT( psa_export_public_key( slot,
734 NULL, 0, &exported_length ) ==
735 PSA_ERROR_INVALID_ARGUMENT );
736 return( 1 );
737 }
738
739 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
740 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200741 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200742
743 TEST_ASSERT( psa_export_public_key( slot,
744 exported, exported_size,
745 &exported_length ) == PSA_SUCCESS );
746 ok = exported_key_sanity_check( public_type, bits,
747 exported, exported_length );
748
749exit:
750 mbedtls_free( exported );
751 return( ok );
752}
753
Gilles Peskine02b75072018-07-01 22:31:34 +0200754static int exercise_key( psa_key_slot_t slot,
755 psa_key_usage_t usage,
756 psa_algorithm_t alg )
757{
758 int ok;
759 if( alg == 0 )
760 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
761 else if( PSA_ALG_IS_MAC( alg ) )
762 ok = exercise_mac_key( slot, usage, alg );
763 else if( PSA_ALG_IS_CIPHER( alg ) )
764 ok = exercise_cipher_key( slot, usage, alg );
765 else if( PSA_ALG_IS_AEAD( alg ) )
766 ok = exercise_aead_key( slot, usage, alg );
767 else if( PSA_ALG_IS_SIGN( alg ) )
768 ok = exercise_signature_key( slot, usage, alg );
769 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
770 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200771 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
772 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 else
774 {
775 char message[40];
776 mbedtls_snprintf( message, sizeof( message ),
777 "No code to exercise alg=0x%08lx",
778 (unsigned long) alg );
779 test_fail( message, __LINE__, __FILE__ );
780 ok = 0;
781 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200782
783 ok = ok && exercise_export_key( slot, usage );
784 ok = ok && exercise_export_public_key( slot );
785
Gilles Peskine02b75072018-07-01 22:31:34 +0200786 return( ok );
787}
788
Gilles Peskinee59236f2018-01-27 23:32:46 +0100789/* END_HEADER */
790
791/* BEGIN_DEPENDENCIES
792 * depends_on:MBEDTLS_PSA_CRYPTO_C
793 * END_DEPENDENCIES
794 */
795
796/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200797void static_checks( )
798{
799 size_t max_truncated_mac_size =
800 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
801
802 /* Check that the length for a truncated MAC always fits in the algorithm
803 * encoding. The shifted mask is the maximum truncated value. The
804 * untruncated algorithm may be one byte larger. */
805 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
806}
807/* END_CASE */
808
809/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200810void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100811{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100813 int i;
814 for( i = 0; i <= 1; i++ )
815 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 status = psa_crypto_init( );
817 TEST_ASSERT( status == PSA_SUCCESS );
818 status = psa_crypto_init( );
819 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100820 mbedtls_psa_crypto_free( );
821 }
822}
823/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824
825/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200826void fill_slots( int max_arg )
827{
828 /* Fill all the slots until we run out of memory or out of slots,
829 * or until some limit specified in the test data for the sake of
830 * implementations with an essentially unlimited number of slots.
831 * This test assumes that available slots are numbered from 1. */
832
833 psa_key_slot_t slot;
834 psa_key_slot_t max = 0;
835 psa_key_policy_t policy;
836 uint8_t exported[sizeof( max )];
837 size_t exported_size;
838 psa_status_t status;
839
840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
841
842 psa_key_policy_init( &policy );
843 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
844
845 for( max = 1; max <= (size_t) max_arg; max++ )
846 {
847 status = psa_set_key_policy( max, &policy );
848 /* Stop filling slots if we run out of memory or out of
849 * available slots. */
850 TEST_ASSERT( status == PSA_SUCCESS ||
851 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
852 status == PSA_ERROR_INVALID_ARGUMENT );
853 if( status != PSA_SUCCESS )
854 break;
855 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
856 (uint8_t*) &max, sizeof( max ) );
857 /* Since psa_set_key_policy succeeded, we know that the slot
858 * number is valid. But we may legitimately run out of memory. */
859 TEST_ASSERT( status == PSA_SUCCESS ||
860 status == PSA_ERROR_INSUFFICIENT_MEMORY );
861 if( status != PSA_SUCCESS )
862 break;
863 }
864 /* `max` is now the first slot number that wasn't filled. */
865 max -= 1;
866
867 for( slot = 1; slot <= max; slot++ )
868 {
869 TEST_ASSERT( psa_export_key( slot,
870 exported, sizeof( exported ),
871 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200872 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200873 }
874
875exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200876 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200877 mbedtls_psa_crypto_free( );
878}
879/* END_CASE */
880
881/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200882void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883{
884 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200885 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
891
Gilles Peskine4abf7412018-06-18 16:35:34 +0200892 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200893 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894 if( status == PSA_SUCCESS )
895 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
896
897exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898 mbedtls_psa_crypto_free( );
899}
900/* END_CASE */
901
902/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200903void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
904{
905 int slot = 1;
906 size_t bits = bits_arg;
907 psa_status_t expected_status = expected_status_arg;
908 psa_status_t status;
909 psa_key_type_t type =
910 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
911 size_t buffer_size = /* Slight overapproximations */
912 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200913 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200914 unsigned char *p;
915 int ret;
916 size_t length;
917
918 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200919 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200920
921 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
922 bits, keypair ) ) >= 0 );
923 length = ret;
924
925 /* Try importing the key */
926 status = psa_import_key( slot, type, p, length );
927 TEST_ASSERT( status == expected_status );
928 if( status == PSA_SUCCESS )
929 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
930
931exit:
932 mbedtls_free( buffer );
933 mbedtls_psa_crypto_free( );
934}
935/* END_CASE */
936
937/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300938void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300939 int type_arg,
940 int alg_arg,
941 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 int expected_bits,
943 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200944 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945 int canonical_input )
946{
947 int slot = 1;
948 int slot2 = slot + 1;
949 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200950 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200951 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 unsigned char *exported = NULL;
954 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100956 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957 size_t reexported_length;
958 psa_key_type_t got_type;
959 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200960 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300963 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300964 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200965 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200967 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
969
mohammad1603a97cb8c2018-03-28 03:46:26 -0700970 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200971 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700972 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
973
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 /* Import the key */
975 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200976 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977
978 /* Test the key information */
979 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200980 &got_type,
981 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982 TEST_ASSERT( got_type == type );
983 TEST_ASSERT( got_bits == (size_t) expected_bits );
984
985 /* Export the key */
986 status = psa_export_key( slot,
987 exported, export_size,
988 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200989 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100990
991 /* The exported length must be set by psa_export_key() to a value between 0
992 * and export_size. On errors, the exported length must be 0. */
993 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
994 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
995 TEST_ASSERT( exported_length <= export_size );
996
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200997 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200998 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001000 {
1001 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001003 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004
Gilles Peskine8f609232018-08-11 01:24:55 +02001005 if( ! exercise_export_key( slot, usage_arg ) )
1006 goto exit;
1007
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001009 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 else
1011 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001012 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1013
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001015 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001016 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001018 reexported,
1019 export_size,
1020 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001021 ASSERT_COMPARE( exported, exported_length,
1022 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023 }
1024
1025destroy:
1026 /* Destroy the key */
1027 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1028 TEST_ASSERT( psa_get_key_information(
1029 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1030
1031exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001032 mbedtls_free( exported );
1033 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 mbedtls_psa_crypto_free( );
1035}
1036/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001037
Moran Pekerf709f4a2018-06-06 17:26:04 +03001038/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001039void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001040 int type_arg,
1041 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001042 int export_size_delta,
1043 int expected_export_status_arg,
1044 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045{
1046 int slot = 1;
1047 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001048 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001049 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001050 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001052 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001053 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001054 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001055
Moran Pekerf709f4a2018-06-06 17:26:04 +03001056 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1057
1058 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001059 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001060 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1061
1062 /* Import the key */
1063 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001064 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001065
Gilles Peskine49c25912018-10-29 15:15:31 +01001066 /* Export the public key */
1067 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001068 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001069 exported, export_size,
1070 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001071 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001072 if( status == PSA_SUCCESS )
1073 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1074 exported, exported_length );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001075
1076exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001077 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001078 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001079 mbedtls_psa_crypto_free( );
1080}
1081/* END_CASE */
1082
Gilles Peskine20035e32018-02-03 22:44:14 +01001083/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001084void import_and_exercise_key( data_t *data,
1085 int type_arg,
1086 int bits_arg,
1087 int alg_arg )
1088{
1089 int slot = 1;
1090 psa_key_type_t type = type_arg;
1091 size_t bits = bits_arg;
1092 psa_algorithm_t alg = alg_arg;
1093 psa_key_usage_t usage =
1094 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1095 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1096 PSA_KEY_USAGE_VERIFY :
1097 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1098 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1099 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1100 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1101 PSA_KEY_USAGE_ENCRYPT :
1102 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001103 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001104 0 );
1105 psa_key_policy_t policy;
1106 psa_key_type_t got_type;
1107 size_t got_bits;
1108 psa_status_t status;
1109
1110 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1111
1112 psa_key_policy_init( &policy );
1113 psa_key_policy_set_usage( &policy, usage, alg );
1114 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1115
1116 /* Import the key */
1117 status = psa_import_key( slot, type, data->x, data->len );
1118 TEST_ASSERT( status == PSA_SUCCESS );
1119
1120 /* Test the key information */
1121 TEST_ASSERT( psa_get_key_information( slot,
1122 &got_type,
1123 &got_bits ) == PSA_SUCCESS );
1124 TEST_ASSERT( got_type == type );
1125 TEST_ASSERT( got_bits == bits );
1126
1127 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001128 if( ! exercise_key( slot, usage, alg ) )
1129 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001130
1131exit:
1132 psa_destroy_key( slot );
1133 mbedtls_psa_crypto_free( );
1134}
1135/* END_CASE */
1136
1137/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001138void key_policy( int usage_arg, int alg_arg )
1139{
1140 int key_slot = 1;
1141 psa_algorithm_t alg = alg_arg;
1142 psa_key_usage_t usage = usage_arg;
1143 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1144 unsigned char key[32] = {0};
1145 psa_key_policy_t policy_set;
1146 psa_key_policy_t policy_get;
1147
1148 memset( key, 0x2a, sizeof( key ) );
1149
1150 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1151
1152 psa_key_policy_init( &policy_set );
1153 psa_key_policy_init( &policy_get );
1154
1155 psa_key_policy_set_usage( &policy_set, usage, alg );
1156
1157 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1158 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1159 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1160
1161 TEST_ASSERT( psa_import_key( key_slot, key_type,
1162 key, sizeof( key ) ) == PSA_SUCCESS );
1163
1164 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1165
1166 TEST_ASSERT( policy_get.usage == policy_set.usage );
1167 TEST_ASSERT( policy_get.alg == policy_set.alg );
1168
1169exit:
1170 psa_destroy_key( key_slot );
1171 mbedtls_psa_crypto_free( );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001176void mac_key_policy( int policy_usage,
1177 int policy_alg,
1178 int key_type,
1179 data_t *key_data,
1180 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001181{
1182 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001183 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001184 psa_mac_operation_t operation;
1185 psa_status_t status;
1186 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001187
1188 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1189
1190 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001192 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1193
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194 TEST_ASSERT( psa_import_key( key_slot, key_type,
1195 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001196
Gilles Peskine89167cb2018-07-08 20:12:23 +02001197 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198 if( policy_alg == exercise_alg &&
1199 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1200 TEST_ASSERT( status == PSA_SUCCESS );
1201 else
1202 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1203 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001204
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001205 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001206 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207 if( policy_alg == exercise_alg &&
1208 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001209 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 else
1211 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1212
1213exit:
1214 psa_mac_abort( &operation );
1215 psa_destroy_key( key_slot );
1216 mbedtls_psa_crypto_free( );
1217}
1218/* END_CASE */
1219
1220/* BEGIN_CASE */
1221void cipher_key_policy( int policy_usage,
1222 int policy_alg,
1223 int key_type,
1224 data_t *key_data,
1225 int exercise_alg )
1226{
1227 int key_slot = 1;
1228 psa_key_policy_t policy;
1229 psa_cipher_operation_t operation;
1230 psa_status_t status;
1231
1232 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1233
1234 psa_key_policy_init( &policy );
1235 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1236 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1237
1238 TEST_ASSERT( psa_import_key( key_slot, key_type,
1239 key_data->x, key_data->len ) == PSA_SUCCESS );
1240
Gilles Peskinefe119512018-07-08 21:39:34 +02001241 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001242 if( policy_alg == exercise_alg &&
1243 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1244 TEST_ASSERT( status == PSA_SUCCESS );
1245 else
1246 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1247 psa_cipher_abort( &operation );
1248
Gilles Peskinefe119512018-07-08 21:39:34 +02001249 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001250 if( policy_alg == exercise_alg &&
1251 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1252 TEST_ASSERT( status == PSA_SUCCESS );
1253 else
1254 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1255
1256exit:
1257 psa_cipher_abort( &operation );
1258 psa_destroy_key( key_slot );
1259 mbedtls_psa_crypto_free( );
1260}
1261/* END_CASE */
1262
1263/* BEGIN_CASE */
1264void aead_key_policy( int policy_usage,
1265 int policy_alg,
1266 int key_type,
1267 data_t *key_data,
1268 int nonce_length_arg,
1269 int tag_length_arg,
1270 int exercise_alg )
1271{
1272 int key_slot = 1;
1273 psa_key_policy_t policy;
1274 psa_status_t status;
1275 unsigned char nonce[16] = {0};
1276 size_t nonce_length = nonce_length_arg;
1277 unsigned char tag[16];
1278 size_t tag_length = tag_length_arg;
1279 size_t output_length;
1280
1281 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1282 TEST_ASSERT( tag_length <= sizeof( tag ) );
1283
1284 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1285
1286 psa_key_policy_init( &policy );
1287 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1288 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1289
1290 TEST_ASSERT( psa_import_key( key_slot, key_type,
1291 key_data->x, key_data->len ) == PSA_SUCCESS );
1292
1293 status = psa_aead_encrypt( key_slot, exercise_alg,
1294 nonce, nonce_length,
1295 NULL, 0,
1296 NULL, 0,
1297 tag, tag_length,
1298 &output_length );
1299 if( policy_alg == exercise_alg &&
1300 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1301 TEST_ASSERT( status == PSA_SUCCESS );
1302 else
1303 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1304
1305 memset( tag, 0, sizeof( tag ) );
1306 status = psa_aead_decrypt( key_slot, exercise_alg,
1307 nonce, nonce_length,
1308 NULL, 0,
1309 tag, tag_length,
1310 NULL, 0,
1311 &output_length );
1312 if( policy_alg == exercise_alg &&
1313 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1314 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1315 else
1316 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1317
1318exit:
1319 psa_destroy_key( key_slot );
1320 mbedtls_psa_crypto_free( );
1321}
1322/* END_CASE */
1323
1324/* BEGIN_CASE */
1325void asymmetric_encryption_key_policy( int policy_usage,
1326 int policy_alg,
1327 int key_type,
1328 data_t *key_data,
1329 int exercise_alg )
1330{
1331 int key_slot = 1;
1332 psa_key_policy_t policy;
1333 psa_status_t status;
1334 size_t key_bits;
1335 size_t buffer_length;
1336 unsigned char *buffer = NULL;
1337 size_t output_length;
1338
1339 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1340
1341 psa_key_policy_init( &policy );
1342 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1343 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1344
1345 TEST_ASSERT( psa_import_key( key_slot, key_type,
1346 key_data->x, key_data->len ) == PSA_SUCCESS );
1347
1348 TEST_ASSERT( psa_get_key_information( key_slot,
1349 NULL,
1350 &key_bits ) == PSA_SUCCESS );
1351 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1352 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001353 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
1355 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1356 NULL, 0,
1357 NULL, 0,
1358 buffer, buffer_length,
1359 &output_length );
1360 if( policy_alg == exercise_alg &&
1361 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1362 TEST_ASSERT( status == PSA_SUCCESS );
1363 else
1364 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1365
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001366 if( buffer_length != 0 )
1367 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001368 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1369 buffer, buffer_length,
1370 NULL, 0,
1371 buffer, buffer_length,
1372 &output_length );
1373 if( policy_alg == exercise_alg &&
1374 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1375 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1376 else
1377 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1378
1379exit:
1380 psa_destroy_key( key_slot );
1381 mbedtls_psa_crypto_free( );
1382 mbedtls_free( buffer );
1383}
1384/* END_CASE */
1385
1386/* BEGIN_CASE */
1387void asymmetric_signature_key_policy( int policy_usage,
1388 int policy_alg,
1389 int key_type,
1390 data_t *key_data,
1391 int exercise_alg )
1392{
1393 int key_slot = 1;
1394 psa_key_policy_t policy;
1395 psa_status_t status;
1396 unsigned char payload[16] = {1};
1397 size_t payload_length = sizeof( payload );
1398 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1399 size_t signature_length;
1400
1401 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1402
1403 psa_key_policy_init( &policy );
1404 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1405 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1406
1407 TEST_ASSERT( psa_import_key( key_slot, key_type,
1408 key_data->x, key_data->len ) == PSA_SUCCESS );
1409
1410 status = psa_asymmetric_sign( key_slot, exercise_alg,
1411 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001412 signature, sizeof( signature ),
1413 &signature_length );
1414 if( policy_alg == exercise_alg &&
1415 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1416 TEST_ASSERT( status == PSA_SUCCESS );
1417 else
1418 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1419
1420 memset( signature, 0, sizeof( signature ) );
1421 status = psa_asymmetric_verify( key_slot, exercise_alg,
1422 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001423 signature, sizeof( signature ) );
1424 if( policy_alg == exercise_alg &&
1425 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1426 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1427 else
1428 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001429
1430exit:
1431 psa_destroy_key( key_slot );
1432 mbedtls_psa_crypto_free( );
1433}
1434/* END_CASE */
1435
1436/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001437void derive_key_policy( int policy_usage,
1438 int policy_alg,
1439 int key_type,
1440 data_t *key_data,
1441 int exercise_alg )
1442{
1443 int key_slot = 1;
1444 psa_key_policy_t policy;
1445 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1446 psa_status_t status;
1447
1448 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1449
1450 psa_key_policy_init( &policy );
1451 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1452 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1453
1454 TEST_ASSERT( psa_import_key( key_slot, key_type,
1455 key_data->x, key_data->len ) == PSA_SUCCESS );
1456
1457 status = psa_key_derivation( &generator, key_slot,
1458 exercise_alg,
1459 NULL, 0,
1460 NULL, 0,
1461 1 );
1462 if( policy_alg == exercise_alg &&
1463 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1464 TEST_ASSERT( status == PSA_SUCCESS );
1465 else
1466 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1467
1468exit:
1469 psa_generator_abort( &generator );
1470 psa_destroy_key( key_slot );
1471 mbedtls_psa_crypto_free( );
1472}
1473/* END_CASE */
1474
1475/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001476void key_lifetime( int lifetime_arg )
1477{
1478 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001479 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001480 unsigned char key[32] = {0};
1481 psa_key_lifetime_t lifetime_set = lifetime_arg;
1482 psa_key_lifetime_t lifetime_get;
1483
1484 memset( key, 0x2a, sizeof( key ) );
1485
1486 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1487
1488 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1489 lifetime_set ) == PSA_SUCCESS );
1490
1491 TEST_ASSERT( psa_import_key( key_slot, key_type,
1492 key, sizeof( key ) ) == PSA_SUCCESS );
1493
1494 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1495 &lifetime_get ) == PSA_SUCCESS );
1496
1497 TEST_ASSERT( lifetime_get == lifetime_set );
1498
1499exit:
1500 psa_destroy_key( key_slot );
1501 mbedtls_psa_crypto_free( );
1502}
1503/* END_CASE */
1504
1505/* BEGIN_CASE */
1506void key_lifetime_set_fail( int key_slot_arg,
1507 int lifetime_arg,
1508 int expected_status_arg )
1509{
1510 psa_key_slot_t key_slot = key_slot_arg;
1511 psa_key_lifetime_t lifetime_set = lifetime_arg;
1512 psa_status_t actual_status;
1513 psa_status_t expected_status = expected_status_arg;
1514
1515 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1516
1517 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1518
1519 if( actual_status == PSA_SUCCESS )
1520 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1521
1522 TEST_ASSERT( expected_status == actual_status );
1523
1524exit:
1525 psa_destroy_key( key_slot );
1526 mbedtls_psa_crypto_free( );
1527}
1528/* END_CASE */
1529
1530/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001531void hash_setup( int alg_arg,
1532 int expected_status_arg )
1533{
1534 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001535 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001536 psa_hash_operation_t operation;
1537 psa_status_t status;
1538
1539 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1540
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001541 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001542 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001543 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001544
1545exit:
1546 mbedtls_psa_crypto_free( );
1547}
1548/* END_CASE */
1549
1550/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001551void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001552{
1553 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001554 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001555 size_t actual_hash_length;
1556 psa_hash_operation_t operation;
1557
Gilles Peskine69c12672018-06-28 00:07:19 +02001558 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1559 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1560
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001561 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001562 TEST_ASSERT( expected_hash != NULL );
1563 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1564 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001565
1566 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1567
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001568 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001569 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001570 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001571 TEST_ASSERT( psa_hash_finish( &operation,
1572 actual_hash, sizeof( actual_hash ),
1573 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001574 ASSERT_COMPARE( expected_hash->x, expected_hash->len,
1575 actual_hash, actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001576
1577exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 mbedtls_psa_crypto_free( );
1579}
1580/* END_CASE */
1581
1582/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001583void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001584{
1585 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586 psa_hash_operation_t operation;
1587
Gilles Peskine69c12672018-06-28 00:07:19 +02001588 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1589 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1590
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001591 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001592 TEST_ASSERT( expected_hash != NULL );
1593 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1594 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001595
1596 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1597
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001598 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001599 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001600 input->x,
1601 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001602 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001603 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001604 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001605
1606exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001607 mbedtls_psa_crypto_free( );
1608}
1609/* END_CASE */
1610
1611/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001612void mac_setup( int key_type_arg,
1613 data_t *key,
1614 int alg_arg,
1615 int expected_status_arg )
1616{
1617 int key_slot = 1;
1618 psa_key_type_t key_type = key_type_arg;
1619 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001620 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001621 psa_mac_operation_t operation;
1622 psa_key_policy_t policy;
1623 psa_status_t status;
1624
1625 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1626
1627 psa_key_policy_init( &policy );
1628 psa_key_policy_set_usage( &policy,
1629 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1630 alg );
1631 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1632
1633 TEST_ASSERT( psa_import_key( key_slot, key_type,
1634 key->x, key->len ) == PSA_SUCCESS );
1635
Gilles Peskine89167cb2018-07-08 20:12:23 +02001636 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001637 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001638 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001639
1640exit:
1641 psa_destroy_key( key_slot );
1642 mbedtls_psa_crypto_free( );
1643}
1644/* END_CASE */
1645
1646/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001647void mac_sign( int key_type_arg,
1648 data_t *key,
1649 int alg_arg,
1650 data_t *input,
1651 data_t *expected_mac )
1652{
1653 int key_slot = 1;
1654 psa_key_type_t key_type = key_type_arg;
1655 psa_algorithm_t alg = alg_arg;
1656 psa_mac_operation_t operation;
1657 psa_key_policy_t policy;
1658 /* Leave a little extra room in the output buffer. At the end of the
1659 * test, we'll check that the implementation didn't overwrite onto
1660 * this extra room. */
1661 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1662 size_t mac_buffer_size =
1663 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1664 size_t mac_length = 0;
1665
1666 memset( actual_mac, '+', sizeof( actual_mac ) );
1667 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1668 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1669
1670 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1671
1672 psa_key_policy_init( &policy );
1673 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1674 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1675
1676 TEST_ASSERT( psa_import_key( key_slot, key_type,
1677 key->x, key->len ) == PSA_SUCCESS );
1678
1679 /* Calculate the MAC. */
1680 TEST_ASSERT( psa_mac_sign_setup( &operation,
1681 key_slot, alg ) == PSA_SUCCESS );
1682 TEST_ASSERT( psa_mac_update( &operation,
1683 input->x, input->len ) == PSA_SUCCESS );
1684 TEST_ASSERT( psa_mac_sign_finish( &operation,
1685 actual_mac, mac_buffer_size,
1686 &mac_length ) == PSA_SUCCESS );
1687
1688 /* Compare with the expected value. */
1689 TEST_ASSERT( mac_length == expected_mac->len );
1690 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1691
1692 /* Verify that the end of the buffer is untouched. */
1693 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1694 sizeof( actual_mac ) - mac_length ) );
1695
1696exit:
1697 psa_destroy_key( key_slot );
1698 mbedtls_psa_crypto_free( );
1699}
1700/* END_CASE */
1701
1702/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001703void mac_verify( int key_type_arg,
1704 data_t *key,
1705 int alg_arg,
1706 data_t *input,
1707 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001708{
1709 int key_slot = 1;
1710 psa_key_type_t key_type = key_type_arg;
1711 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001712 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001713 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001714
Gilles Peskine69c12672018-06-28 00:07:19 +02001715 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1716
Gilles Peskine8c9def32018-02-08 10:02:12 +01001717 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001718 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001720 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001721 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1722 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001723
1724 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1725
mohammad16036df908f2018-04-02 08:34:15 -07001726 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001727 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001728 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1729
Gilles Peskine8c9def32018-02-08 10:02:12 +01001730 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001731 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001732
Gilles Peskine89167cb2018-07-08 20:12:23 +02001733 TEST_ASSERT( psa_mac_verify_setup( &operation,
1734 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001735 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1736 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001737 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001738 TEST_ASSERT( psa_mac_verify_finish( &operation,
1739 expected_mac->x,
1740 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001741
1742exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001743 psa_destroy_key( key_slot );
1744 mbedtls_psa_crypto_free( );
1745}
1746/* END_CASE */
1747
1748/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001749void cipher_setup( int key_type_arg,
1750 data_t *key,
1751 int alg_arg,
1752 int expected_status_arg )
1753{
1754 int key_slot = 1;
1755 psa_key_type_t key_type = key_type_arg;
1756 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001757 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001758 psa_cipher_operation_t operation;
1759 psa_key_policy_t policy;
1760 psa_status_t status;
1761
1762 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1763
1764 psa_key_policy_init( &policy );
1765 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1766 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1767
1768 TEST_ASSERT( psa_import_key( key_slot, key_type,
1769 key->x, key->len ) == PSA_SUCCESS );
1770
Gilles Peskinefe119512018-07-08 21:39:34 +02001771 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001772 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001773 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001774
1775exit:
1776 psa_destroy_key( key_slot );
1777 mbedtls_psa_crypto_free( );
1778}
1779/* END_CASE */
1780
1781/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001782void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001783 data_t *key,
1784 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001785 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001786{
1787 int key_slot = 1;
1788 psa_status_t status;
1789 psa_key_type_t key_type = key_type_arg;
1790 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001791 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001792 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001793 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001794 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001795 size_t output_buffer_size = 0;
1796 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001797 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001798 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001799 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001800
Gilles Peskine50e586b2018-06-08 14:28:46 +02001801 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001802 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001803 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001804 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1805 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1806 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001807
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001808 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1809 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001810
1811 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1812
Moran Pekered346952018-07-05 15:22:45 +03001813 psa_key_policy_init( &policy );
1814 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1815 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1816
Gilles Peskine50e586b2018-06-08 14:28:46 +02001817 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001818 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001819
Gilles Peskinefe119512018-07-08 21:39:34 +02001820 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1821 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001822
Gilles Peskinefe119512018-07-08 21:39:34 +02001823 TEST_ASSERT( psa_cipher_set_iv( &operation,
1824 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001825 output_buffer_size = (size_t) input->len +
1826 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001827 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001828
Gilles Peskine4abf7412018-06-18 16:35:34 +02001829 TEST_ASSERT( psa_cipher_update( &operation,
1830 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001831 output, output_buffer_size,
1832 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001833 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001834 status = psa_cipher_finish( &operation,
1835 output + function_output_length,
1836 output_buffer_size,
1837 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001838 total_output_length += function_output_length;
1839
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001840 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001841 if( expected_status == PSA_SUCCESS )
1842 {
1843 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001844 ASSERT_COMPARE( expected_output->x, expected_output->len,
1845 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001846 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001847
Gilles Peskine50e586b2018-06-08 14:28:46 +02001848exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001849 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001850 psa_destroy_key( key_slot );
1851 mbedtls_psa_crypto_free( );
1852}
1853/* END_CASE */
1854
1855/* BEGIN_CASE */
1856void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001857 data_t *key,
1858 data_t *input,
1859 int first_part_size,
1860 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001861{
1862 int key_slot = 1;
1863 psa_key_type_t key_type = key_type_arg;
1864 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001865 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001866 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001867 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001868 size_t output_buffer_size = 0;
1869 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001870 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001871 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001872 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001873
Gilles Peskine50e586b2018-06-08 14:28:46 +02001874 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001875 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001876 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001877 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1878 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1879 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001880
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001881 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1882 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001883
1884 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1885
Moran Pekered346952018-07-05 15:22:45 +03001886 psa_key_policy_init( &policy );
1887 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1888 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1889
Gilles Peskine50e586b2018-06-08 14:28:46 +02001890 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001891 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001892
Gilles Peskinefe119512018-07-08 21:39:34 +02001893 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1894 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001895
Gilles Peskinefe119512018-07-08 21:39:34 +02001896 TEST_ASSERT( psa_cipher_set_iv( &operation,
1897 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001898 output_buffer_size = (size_t) input->len +
1899 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001900 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001901
Gilles Peskine4abf7412018-06-18 16:35:34 +02001902 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001903 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001904 output, output_buffer_size,
1905 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001906 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001907 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001908 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001909 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001910 output, output_buffer_size,
1911 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001912 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001913 TEST_ASSERT( psa_cipher_finish( &operation,
1914 output + function_output_length,
1915 output_buffer_size,
1916 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001917 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001918 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1919
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001920 ASSERT_COMPARE( expected_output->x, expected_output->len,
1921 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001922
1923exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001924 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001925 psa_destroy_key( key_slot );
1926 mbedtls_psa_crypto_free( );
1927}
1928/* END_CASE */
1929
1930/* BEGIN_CASE */
1931void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001932 data_t *key,
1933 data_t *input,
1934 int first_part_size,
1935 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001936{
1937 int key_slot = 1;
1938
1939 psa_key_type_t key_type = key_type_arg;
1940 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001941 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001942 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001943 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001944 size_t output_buffer_size = 0;
1945 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001946 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001947 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001948 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001949
Gilles Peskine50e586b2018-06-08 14:28:46 +02001950 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001951 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001952 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001953 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1954 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1955 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001956
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001957 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1958 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001959
1960 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1961
Moran Pekered346952018-07-05 15:22:45 +03001962 psa_key_policy_init( &policy );
1963 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1964 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1965
Gilles Peskine50e586b2018-06-08 14:28:46 +02001966 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001967 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001968
Gilles Peskinefe119512018-07-08 21:39:34 +02001969 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1970 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001971
Gilles Peskinefe119512018-07-08 21:39:34 +02001972 TEST_ASSERT( psa_cipher_set_iv( &operation,
1973 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001974
mohammad16033d91abe2018-07-03 13:15:54 +03001975 output_buffer_size = (size_t) input->len +
1976 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001977 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001978
Gilles Peskine4abf7412018-06-18 16:35:34 +02001979 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1980 TEST_ASSERT( psa_cipher_update( &operation,
1981 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001982 output, output_buffer_size,
1983 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001984 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001985 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001986 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001987 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001988 output, output_buffer_size,
1989 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001990 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001991 TEST_ASSERT( psa_cipher_finish( &operation,
1992 output + function_output_length,
1993 output_buffer_size,
1994 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001995 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001996 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1997
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001998 ASSERT_COMPARE( expected_output->x, expected_output->len,
1999 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002000
2001exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002002 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002003 psa_destroy_key( key_slot );
2004 mbedtls_psa_crypto_free( );
2005}
2006/* END_CASE */
2007
Gilles Peskine50e586b2018-06-08 14:28:46 +02002008/* BEGIN_CASE */
2009void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002010 data_t *key,
2011 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002012 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002013{
2014 int key_slot = 1;
2015 psa_status_t status;
2016 psa_key_type_t key_type = key_type_arg;
2017 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002018 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002019 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002020 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002021 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002022 size_t output_buffer_size = 0;
2023 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002024 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002025 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002026 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002027
Gilles Peskine50e586b2018-06-08 14:28:46 +02002028 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002029 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002030 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002031 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2032 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2033 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002035 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2036 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002037
2038 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2039
Moran Pekered346952018-07-05 15:22:45 +03002040 psa_key_policy_init( &policy );
2041 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2042 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2043
Gilles Peskine50e586b2018-06-08 14:28:46 +02002044 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002045 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002046
Gilles Peskinefe119512018-07-08 21:39:34 +02002047 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2048 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002049
Gilles Peskinefe119512018-07-08 21:39:34 +02002050 TEST_ASSERT( psa_cipher_set_iv( &operation,
2051 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002052
mohammad16033d91abe2018-07-03 13:15:54 +03002053 output_buffer_size = (size_t) input->len +
2054 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002055 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002056
Gilles Peskine4abf7412018-06-18 16:35:34 +02002057 TEST_ASSERT( psa_cipher_update( &operation,
2058 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002059 output, output_buffer_size,
2060 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002061 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002062 status = psa_cipher_finish( &operation,
2063 output + function_output_length,
2064 output_buffer_size,
2065 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002066 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002067 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002068
2069 if( expected_status == PSA_SUCCESS )
2070 {
2071 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002072 ASSERT_COMPARE( expected_output->x, expected_output->len,
2073 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002074 }
2075
Gilles Peskine50e586b2018-06-08 14:28:46 +02002076exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002077 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002078 psa_destroy_key( key_slot );
2079 mbedtls_psa_crypto_free( );
2080}
2081/* END_CASE */
2082
Gilles Peskine50e586b2018-06-08 14:28:46 +02002083/* BEGIN_CASE */
2084void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002085 data_t *key,
2086 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002087{
2088 int key_slot = 1;
2089 psa_key_type_t key_type = key_type_arg;
2090 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002091 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002092 size_t iv_size = 16;
2093 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002094 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002095 size_t output1_size = 0;
2096 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002097 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002098 size_t output2_size = 0;
2099 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002100 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002101 psa_cipher_operation_t operation1;
2102 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002103 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002104
mohammad1603d7d7ba52018-03-12 18:51:53 +02002105 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002106 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002107 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2108 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002109
mohammad1603d7d7ba52018-03-12 18:51:53 +02002110 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2111
Moran Pekered346952018-07-05 15:22:45 +03002112 psa_key_policy_init( &policy );
2113 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2114 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2115
mohammad1603d7d7ba52018-03-12 18:51:53 +02002116 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002117 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002118
Gilles Peskinefe119512018-07-08 21:39:34 +02002119 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2120 key_slot, alg ) == PSA_SUCCESS );
2121 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2122 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002123
Gilles Peskinefe119512018-07-08 21:39:34 +02002124 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2125 iv, iv_size,
2126 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002127 output1_size = (size_t) input->len +
2128 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002129 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002130
Gilles Peskine4abf7412018-06-18 16:35:34 +02002131 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002132 output1, output1_size,
2133 &output1_length ) == PSA_SUCCESS );
2134 TEST_ASSERT( psa_cipher_finish( &operation1,
2135 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002136 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002137
Gilles Peskine048b7f02018-06-08 14:20:49 +02002138 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002139
2140 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2141
2142 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002143 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002144
Gilles Peskinefe119512018-07-08 21:39:34 +02002145 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2146 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002147 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2148 output2, output2_size,
2149 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002150 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002151 TEST_ASSERT( psa_cipher_finish( &operation2,
2152 output2 + output2_length,
2153 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002154 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002155
Gilles Peskine048b7f02018-06-08 14:20:49 +02002156 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002157
Janos Follath25c4fa82018-07-06 16:23:25 +01002158 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002159
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002160 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002161
2162exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002163 mbedtls_free( output1 );
2164 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002165 psa_destroy_key( key_slot );
2166 mbedtls_psa_crypto_free( );
2167}
2168/* END_CASE */
2169
2170/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002171void cipher_verify_output_multipart( int alg_arg,
2172 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002173 data_t *key,
2174 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002175 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002176{
2177 int key_slot = 1;
2178 psa_key_type_t key_type = key_type_arg;
2179 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002180 unsigned char iv[16] = {0};
2181 size_t iv_size = 16;
2182 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002183 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002184 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002185 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002186 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002187 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002188 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002189 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002190 psa_cipher_operation_t operation1;
2191 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002192 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002193
Moran Pekerded84402018-06-06 16:36:50 +03002194 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002195 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002196 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2197 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002198
Moran Pekerded84402018-06-06 16:36:50 +03002199 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2200
Moran Pekered346952018-07-05 15:22:45 +03002201 psa_key_policy_init( &policy );
2202 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2203 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2204
Moran Pekerded84402018-06-06 16:36:50 +03002205 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002206 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002207
Gilles Peskinefe119512018-07-08 21:39:34 +02002208 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2209 key_slot, alg ) == PSA_SUCCESS );
2210 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2211 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002212
Gilles Peskinefe119512018-07-08 21:39:34 +02002213 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2214 iv, iv_size,
2215 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002216 output1_buffer_size = (size_t) input->len +
2217 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002218 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002219
Gilles Peskine4abf7412018-06-18 16:35:34 +02002220 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002221
itayzafrir3e02b3b2018-06-12 17:06:52 +03002222 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002223 output1, output1_buffer_size,
2224 &function_output_length ) == PSA_SUCCESS );
2225 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002226
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002227 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002228 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002229 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002230 output1, output1_buffer_size,
2231 &function_output_length ) == PSA_SUCCESS );
2232 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002233
Gilles Peskine048b7f02018-06-08 14:20:49 +02002234 TEST_ASSERT( psa_cipher_finish( &operation1,
2235 output1 + output1_length,
2236 output1_buffer_size - output1_length,
2237 &function_output_length ) == PSA_SUCCESS );
2238 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002239
2240 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2241
Gilles Peskine048b7f02018-06-08 14:20:49 +02002242 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002243 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002244
Gilles Peskinefe119512018-07-08 21:39:34 +02002245 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2246 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002247
2248 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002249 output2, output2_buffer_size,
2250 &function_output_length ) == PSA_SUCCESS );
2251 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002252
Gilles Peskine048b7f02018-06-08 14:20:49 +02002253 TEST_ASSERT( psa_cipher_update( &operation2,
2254 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002255 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002256 output2, output2_buffer_size,
2257 &function_output_length ) == PSA_SUCCESS );
2258 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002259
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002260 TEST_ASSERT( psa_cipher_finish( &operation2,
2261 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002262 output2_buffer_size - output2_length,
2263 &function_output_length ) == PSA_SUCCESS );
2264 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002265
Janos Follath25c4fa82018-07-06 16:23:25 +01002266 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002267
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002268 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002269
2270exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002271 mbedtls_free( output1 );
2272 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002273 psa_destroy_key( key_slot );
2274 mbedtls_psa_crypto_free( );
2275}
2276/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002277
Gilles Peskine20035e32018-02-03 22:44:14 +01002278/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002279void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002280 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002281 data_t *nonce,
2282 data_t *additional_data,
2283 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002284 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002285{
2286 int slot = 1;
2287 psa_key_type_t key_type = key_type_arg;
2288 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002289 unsigned char *output_data = NULL;
2290 size_t output_size = 0;
2291 size_t output_length = 0;
2292 unsigned char *output_data2 = NULL;
2293 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002294 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002295 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002296 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002297
Gilles Peskinea1cac842018-06-11 19:33:02 +02002298 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002299 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002300 TEST_ASSERT( nonce != NULL );
2301 TEST_ASSERT( additional_data != NULL );
2302 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2303 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2304 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2305 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2306
Gilles Peskine4abf7412018-06-18 16:35:34 +02002307 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002308 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002309
2310 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2311
2312 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002313 psa_key_policy_set_usage( &policy,
2314 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2315 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002316 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2317
2318 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002319 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002320
2321 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002322 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002323 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002324 additional_data->len,
2325 input_data->x, input_data->len,
2326 output_data, output_size,
2327 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002328
2329 if( PSA_SUCCESS == expected_result )
2330 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002331 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002332
2333 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002334 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002335 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002336 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002337 output_data, output_length,
2338 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002339 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002340
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002341 ASSERT_COMPARE( input_data->x, input_data->len,
2342 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002343 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002344
Gilles Peskinea1cac842018-06-11 19:33:02 +02002345exit:
2346 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002347 mbedtls_free( output_data );
2348 mbedtls_free( output_data2 );
2349 mbedtls_psa_crypto_free( );
2350}
2351/* END_CASE */
2352
2353/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002354void aead_encrypt( int key_type_arg, data_t *key_data,
2355 int alg_arg,
2356 data_t *nonce,
2357 data_t *additional_data,
2358 data_t *input_data,
2359 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002360{
2361 int slot = 1;
2362 psa_key_type_t key_type = key_type_arg;
2363 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002364 unsigned char *output_data = NULL;
2365 size_t output_size = 0;
2366 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002367 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002368 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002369
Gilles Peskinea1cac842018-06-11 19:33:02 +02002370 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002371 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002372 TEST_ASSERT( additional_data != NULL );
2373 TEST_ASSERT( nonce != NULL );
2374 TEST_ASSERT( expected_result != NULL );
2375 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2376 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2377 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2378 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2379 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2380
Gilles Peskine4abf7412018-06-18 16:35:34 +02002381 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002382 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002383
Gilles Peskinea1cac842018-06-11 19:33:02 +02002384 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2385
2386 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002387 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002388 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2389
2390 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002391 key_data->x,
2392 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002393
2394 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002395 nonce->x, nonce->len,
2396 additional_data->x, additional_data->len,
2397 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002398 output_data, output_size,
2399 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002400
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002401 ASSERT_COMPARE( expected_result->x, expected_result->len,
2402 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002403
Gilles Peskinea1cac842018-06-11 19:33:02 +02002404exit:
2405 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002406 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002407 mbedtls_psa_crypto_free( );
2408}
2409/* END_CASE */
2410
2411/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002412void aead_decrypt( int key_type_arg, data_t *key_data,
2413 int alg_arg,
2414 data_t *nonce,
2415 data_t *additional_data,
2416 data_t *input_data,
2417 data_t *expected_data,
2418 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002419{
2420 int slot = 1;
2421 psa_key_type_t key_type = key_type_arg;
2422 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002423 unsigned char *output_data = NULL;
2424 size_t output_size = 0;
2425 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002426 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002427 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002428 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002429
Gilles Peskinea1cac842018-06-11 19:33:02 +02002430 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002431 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002432 TEST_ASSERT( additional_data != NULL );
2433 TEST_ASSERT( nonce != NULL );
2434 TEST_ASSERT( expected_data != NULL );
2435 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2436 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2437 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2438 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2439 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2440
Gilles Peskine4abf7412018-06-18 16:35:34 +02002441 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002442 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002443
Gilles Peskinea1cac842018-06-11 19:33:02 +02002444 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2445
2446 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002447 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002448 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2449
2450 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002451 key_data->x,
2452 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002453
2454 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002455 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002456 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002457 additional_data->len,
2458 input_data->x, input_data->len,
2459 output_data, output_size,
2460 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002461
Gilles Peskine2d277862018-06-18 15:41:12 +02002462 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002463 ASSERT_COMPARE( expected_data->x, expected_data->len,
2464 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002465
Gilles Peskinea1cac842018-06-11 19:33:02 +02002466exit:
2467 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002468 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002469 mbedtls_psa_crypto_free( );
2470}
2471/* END_CASE */
2472
2473/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002474void signature_size( int type_arg,
2475 int bits,
2476 int alg_arg,
2477 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002478{
2479 psa_key_type_t type = type_arg;
2480 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002481 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002482 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2483exit:
2484 ;
2485}
2486/* END_CASE */
2487
2488/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002489void sign_deterministic( int key_type_arg, data_t *key_data,
2490 int alg_arg, data_t *input_data,
2491 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002492{
2493 int slot = 1;
2494 psa_key_type_t key_type = key_type_arg;
2495 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002496 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002497 unsigned char *signature = NULL;
2498 size_t signature_size;
2499 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002500 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002501
Gilles Peskine20035e32018-02-03 22:44:14 +01002502 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002503 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002504 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002505 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2506 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2507 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002508
2509 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2510
mohammad1603a97cb8c2018-03-28 03:46:26 -07002511 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002512 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002513 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2514
Gilles Peskine20035e32018-02-03 22:44:14 +01002515 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002516 key_data->x,
2517 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002518 TEST_ASSERT( psa_get_key_information( slot,
2519 NULL,
2520 &key_bits ) == PSA_SUCCESS );
2521
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002522 /* Allocate a buffer which has the size advertized by the
2523 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002524 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2525 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002526 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002527 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002528 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002529
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002530 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002531 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002532 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002533 signature, signature_size,
2534 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002535 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002536 ASSERT_COMPARE( output_data->x, output_data->len,
2537 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002538
2539exit:
2540 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002541 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002542 mbedtls_psa_crypto_free( );
2543}
2544/* END_CASE */
2545
2546/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002547void sign_fail( int key_type_arg, data_t *key_data,
2548 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002549 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002550{
2551 int slot = 1;
2552 psa_key_type_t key_type = key_type_arg;
2553 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002554 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002555 psa_status_t actual_status;
2556 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002557 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002558 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002559 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002560
Gilles Peskine20035e32018-02-03 22:44:14 +01002561 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002562 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002563 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2564 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2565
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002566 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002567
2568 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2569
mohammad1603a97cb8c2018-03-28 03:46:26 -07002570 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002571 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002572 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2573
Gilles Peskine20035e32018-02-03 22:44:14 +01002574 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002575 key_data->x,
2576 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002577
2578 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002579 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002580 signature, signature_size,
2581 &signature_length );
2582 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002583 /* The value of *signature_length is unspecified on error, but
2584 * whatever it is, it should be less than signature_size, so that
2585 * if the caller tries to read *signature_length bytes without
2586 * checking the error code then they don't overflow a buffer. */
2587 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002588
2589exit:
2590 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002591 mbedtls_free( signature );
2592 mbedtls_psa_crypto_free( );
2593}
2594/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002595
2596/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002597void sign_verify( int key_type_arg, data_t *key_data,
2598 int alg_arg, data_t *input_data )
2599{
2600 int slot = 1;
2601 psa_key_type_t key_type = key_type_arg;
2602 psa_algorithm_t alg = alg_arg;
2603 size_t key_bits;
2604 unsigned char *signature = NULL;
2605 size_t signature_size;
2606 size_t signature_length = 0xdeadbeef;
2607 psa_key_policy_t policy;
2608
2609 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2610
2611 psa_key_policy_init( &policy );
2612 psa_key_policy_set_usage( &policy,
2613 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2614 alg );
2615 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2616
2617 TEST_ASSERT( psa_import_key( slot, key_type,
2618 key_data->x,
2619 key_data->len ) == PSA_SUCCESS );
2620 TEST_ASSERT( psa_get_key_information( slot,
2621 NULL,
2622 &key_bits ) == PSA_SUCCESS );
2623
2624 /* Allocate a buffer which has the size advertized by the
2625 * library. */
2626 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2627 key_bits, alg );
2628 TEST_ASSERT( signature_size != 0 );
2629 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002630 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002631
2632 /* Perform the signature. */
2633 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2634 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002635 signature, signature_size,
2636 &signature_length ) == PSA_SUCCESS );
2637 /* Check that the signature length looks sensible. */
2638 TEST_ASSERT( signature_length <= signature_size );
2639 TEST_ASSERT( signature_length > 0 );
2640
2641 /* Use the library to verify that the signature is correct. */
2642 TEST_ASSERT( psa_asymmetric_verify(
2643 slot, alg,
2644 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002645 signature, signature_length ) == PSA_SUCCESS );
2646
2647 if( input_data->len != 0 )
2648 {
2649 /* Flip a bit in the input and verify that the signature is now
2650 * detected as invalid. Flip a bit at the beginning, not at the end,
2651 * because ECDSA may ignore the last few bits of the input. */
2652 input_data->x[0] ^= 1;
2653 TEST_ASSERT( psa_asymmetric_verify(
2654 slot, alg,
2655 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002656 signature,
2657 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2658 }
2659
2660exit:
2661 psa_destroy_key( slot );
2662 mbedtls_free( signature );
2663 mbedtls_psa_crypto_free( );
2664}
2665/* END_CASE */
2666
2667/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002668void asymmetric_verify( int key_type_arg, data_t *key_data,
2669 int alg_arg, data_t *hash_data,
2670 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002671{
2672 int slot = 1;
2673 psa_key_type_t key_type = key_type_arg;
2674 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002675 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002676
Gilles Peskine69c12672018-06-28 00:07:19 +02002677 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2678
itayzafrir5c753392018-05-08 11:18:38 +03002679 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002680 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002681 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002682 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2683 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2684 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002685
2686 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2687
2688 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002689 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002690 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2691
2692 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002693 key_data->x,
2694 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002695
2696 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002697 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002698 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002699 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002700exit:
2701 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002702 mbedtls_psa_crypto_free( );
2703}
2704/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002705
2706/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002707void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2708 int alg_arg, data_t *hash_data,
2709 data_t *signature_data,
2710 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002711{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002712 int slot = 1;
2713 psa_key_type_t key_type = key_type_arg;
2714 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002715 psa_status_t actual_status;
2716 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002717 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002718
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002719 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002720 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002722 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2723 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2724 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725
2726 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2727
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002728 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002729 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002730 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2731
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002732 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002733 key_data->x,
2734 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002735
2736 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002737 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002738 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002739 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002740
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002741 TEST_ASSERT( actual_status == expected_status );
2742
2743exit:
2744 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002745 mbedtls_psa_crypto_free( );
2746}
2747/* END_CASE */
2748
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002749/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002750void asymmetric_encrypt( int key_type_arg,
2751 data_t *key_data,
2752 int alg_arg,
2753 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002754 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002755 int expected_output_length_arg,
2756 int expected_status_arg )
2757{
2758 int slot = 1;
2759 psa_key_type_t key_type = key_type_arg;
2760 psa_algorithm_t alg = alg_arg;
2761 size_t expected_output_length = expected_output_length_arg;
2762 size_t key_bits;
2763 unsigned char *output = NULL;
2764 size_t output_size;
2765 size_t output_length = ~0;
2766 psa_status_t actual_status;
2767 psa_status_t expected_status = expected_status_arg;
2768 psa_key_policy_t policy;
2769
2770 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2771
2772 /* Import the key */
2773 psa_key_policy_init( &policy );
2774 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2775 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2776 TEST_ASSERT( psa_import_key( slot, key_type,
2777 key_data->x,
2778 key_data->len ) == PSA_SUCCESS );
2779
2780 /* Determine the maximum output length */
2781 TEST_ASSERT( psa_get_key_information( slot,
2782 NULL,
2783 &key_bits ) == PSA_SUCCESS );
2784 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002785 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002786
2787 /* Encrypt the input */
2788 actual_status = psa_asymmetric_encrypt( slot, alg,
2789 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002790 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002791 output, output_size,
2792 &output_length );
2793 TEST_ASSERT( actual_status == expected_status );
2794 TEST_ASSERT( output_length == expected_output_length );
2795
Gilles Peskine68428122018-06-30 18:42:41 +02002796 /* If the label is empty, the test framework puts a non-null pointer
2797 * in label->x. Test that a null pointer works as well. */
2798 if( label->len == 0 )
2799 {
2800 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002801 if( output_size != 0 )
2802 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002803 actual_status = psa_asymmetric_encrypt( slot, alg,
2804 input_data->x, input_data->len,
2805 NULL, label->len,
2806 output, output_size,
2807 &output_length );
2808 TEST_ASSERT( actual_status == expected_status );
2809 TEST_ASSERT( output_length == expected_output_length );
2810 }
2811
Gilles Peskine656896e2018-06-29 19:12:28 +02002812exit:
2813 psa_destroy_key( slot );
2814 mbedtls_free( output );
2815 mbedtls_psa_crypto_free( );
2816}
2817/* END_CASE */
2818
2819/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002820void asymmetric_encrypt_decrypt( int key_type_arg,
2821 data_t *key_data,
2822 int alg_arg,
2823 data_t *input_data,
2824 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002825{
2826 int slot = 1;
2827 psa_key_type_t key_type = key_type_arg;
2828 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002829 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002830 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002831 size_t output_size;
2832 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002833 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002834 size_t output2_size;
2835 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002836 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002837
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002838 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002839 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002840 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2841 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2842
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002843 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2844
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002845 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002846 psa_key_policy_set_usage( &policy,
2847 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002848 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002849 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2850
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002851 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002852 key_data->x,
2853 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002854
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002855
2856 /* Determine the maximum ciphertext length */
2857 TEST_ASSERT( psa_get_key_information( slot,
2858 NULL,
2859 &key_bits ) == PSA_SUCCESS );
2860 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002861 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002862 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002863 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002864
Gilles Peskineeebd7382018-06-08 18:11:54 +02002865 /* We test encryption by checking that encrypt-then-decrypt gives back
2866 * the original plaintext because of the non-optional random
2867 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002868 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002869 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002870 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002871 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002872 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002873 /* We don't know what ciphertext length to expect, but check that
2874 * it looks sensible. */
2875 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002876
Gilles Peskine2d277862018-06-18 15:41:12 +02002877 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002878 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002879 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002880 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002881 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002882 ASSERT_COMPARE( input_data->x, input_data->len,
2883 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002884
2885exit:
2886 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002887 mbedtls_free( output );
2888 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002889 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002890}
2891/* END_CASE */
2892
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002894void asymmetric_decrypt( int key_type_arg,
2895 data_t *key_data,
2896 int alg_arg,
2897 data_t *input_data,
2898 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002899 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002900{
2901 int slot = 1;
2902 psa_key_type_t key_type = key_type_arg;
2903 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002904 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002905 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002906 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002907 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002908
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002909 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002910 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002911 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002912 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2913 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2914 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2915
Gilles Peskine4abf7412018-06-18 16:35:34 +02002916 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002917 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002918
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002919 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2920
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002921 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002922 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002923 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2924
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002925 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002926 key_data->x,
2927 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002928
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002929 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002930 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002931 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002932 output,
2933 output_size,
2934 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002935 ASSERT_COMPARE( expected_data->x, expected_data->len,
2936 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002937
Gilles Peskine68428122018-06-30 18:42:41 +02002938 /* If the label is empty, the test framework puts a non-null pointer
2939 * in label->x. Test that a null pointer works as well. */
2940 if( label->len == 0 )
2941 {
2942 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002943 if( output_size != 0 )
2944 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002945 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2946 input_data->x, input_data->len,
2947 NULL, label->len,
2948 output,
2949 output_size,
2950 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002951 ASSERT_COMPARE( expected_data->x, expected_data->len,
2952 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02002953 }
2954
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955exit:
2956 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002957 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002958 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002959}
2960/* END_CASE */
2961
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002963void asymmetric_decrypt_fail( int key_type_arg,
2964 data_t *key_data,
2965 int alg_arg,
2966 data_t *input_data,
2967 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002968 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002969{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002970 int slot = 1;
2971 psa_key_type_t key_type = key_type_arg;
2972 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002973 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002974 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002975 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002976 psa_status_t actual_status;
2977 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002978 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002980 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002981 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002982 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2983 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2984
Gilles Peskine4abf7412018-06-18 16:35:34 +02002985 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002986 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002987
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2989
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002990 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002991 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002992 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2993
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002994 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002995 key_data->x,
2996 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002997
Gilles Peskine2d277862018-06-18 15:41:12 +02002998 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002999 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003000 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003001 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003002 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003003 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003004 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003005
Gilles Peskine68428122018-06-30 18:42:41 +02003006 /* If the label is empty, the test framework puts a non-null pointer
3007 * in label->x. Test that a null pointer works as well. */
3008 if( label->len == 0 )
3009 {
3010 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003011 if( output_size != 0 )
3012 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003013 actual_status = psa_asymmetric_decrypt( slot, alg,
3014 input_data->x, input_data->len,
3015 NULL, label->len,
3016 output, output_size,
3017 &output_length );
3018 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003019 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003020 }
3021
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003022exit:
3023 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003024 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025 mbedtls_psa_crypto_free( );
3026}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003027/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003028
3029/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003030void derive_setup( int key_type_arg,
3031 data_t *key_data,
3032 int alg_arg,
3033 data_t *salt,
3034 data_t *label,
3035 int requested_capacity_arg,
3036 int expected_status_arg )
3037{
3038 psa_key_slot_t slot = 1;
3039 size_t key_type = key_type_arg;
3040 psa_algorithm_t alg = alg_arg;
3041 size_t requested_capacity = requested_capacity_arg;
3042 psa_status_t expected_status = expected_status_arg;
3043 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3044 psa_key_policy_t policy;
3045
3046 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3047
3048 psa_key_policy_init( &policy );
3049 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3050 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3051
3052 TEST_ASSERT( psa_import_key( slot, key_type,
3053 key_data->x,
3054 key_data->len ) == PSA_SUCCESS );
3055
3056 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3057 salt->x, salt->len,
3058 label->x, label->len,
3059 requested_capacity ) == expected_status );
3060
3061exit:
3062 psa_generator_abort( &generator );
3063 psa_destroy_key( slot );
3064 mbedtls_psa_crypto_free( );
3065}
3066/* END_CASE */
3067
3068/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003069void derive_output( int alg_arg,
3070 data_t *key_data,
3071 data_t *salt,
3072 data_t *label,
3073 int requested_capacity_arg,
3074 data_t *expected_output1,
3075 data_t *expected_output2 )
3076{
3077 psa_key_slot_t slot = 1;
3078 psa_algorithm_t alg = alg_arg;
3079 size_t requested_capacity = requested_capacity_arg;
3080 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3081 uint8_t *expected_outputs[2] =
3082 {expected_output1->x, expected_output2->x};
3083 size_t output_sizes[2] =
3084 {expected_output1->len, expected_output2->len};
3085 size_t output_buffer_size = 0;
3086 uint8_t *output_buffer = NULL;
3087 size_t expected_capacity;
3088 size_t current_capacity;
3089 psa_key_policy_t policy;
3090 psa_status_t status;
3091 unsigned i;
3092
3093 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3094 {
3095 if( output_sizes[i] > output_buffer_size )
3096 output_buffer_size = output_sizes[i];
3097 if( output_sizes[i] == 0 )
3098 expected_outputs[i] = NULL;
3099 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003100 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003101 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3102
3103 psa_key_policy_init( &policy );
3104 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3105 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3106
3107 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3108 key_data->x,
3109 key_data->len ) == PSA_SUCCESS );
3110
3111 /* Extraction phase. */
3112 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3113 salt->x, salt->len,
3114 label->x, label->len,
3115 requested_capacity ) == PSA_SUCCESS );
3116 TEST_ASSERT( psa_get_generator_capacity( &generator,
3117 &current_capacity ) ==
3118 PSA_SUCCESS );
3119 TEST_ASSERT( current_capacity == requested_capacity );
3120 expected_capacity = requested_capacity;
3121
3122 /* Expansion phase. */
3123 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3124 {
3125 /* Read some bytes. */
3126 status = psa_generator_read( &generator,
3127 output_buffer, output_sizes[i] );
3128 if( expected_capacity == 0 && output_sizes[i] == 0 )
3129 {
3130 /* Reading 0 bytes when 0 bytes are available can go either way. */
3131 TEST_ASSERT( status == PSA_SUCCESS ||
3132 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3133 continue;
3134 }
3135 else if( expected_capacity == 0 ||
3136 output_sizes[i] > expected_capacity )
3137 {
3138 /* Capacity exceeded. */
3139 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3140 expected_capacity = 0;
3141 continue;
3142 }
3143 /* Success. Check the read data. */
3144 TEST_ASSERT( status == PSA_SUCCESS );
3145 if( output_sizes[i] != 0 )
3146 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3147 output_sizes[i] ) == 0 );
3148 /* Check the generator status. */
3149 expected_capacity -= output_sizes[i];
3150 TEST_ASSERT( psa_get_generator_capacity( &generator,
3151 &current_capacity ) ==
3152 PSA_SUCCESS );
3153 TEST_ASSERT( expected_capacity == current_capacity );
3154 }
3155 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3156
3157exit:
3158 mbedtls_free( output_buffer );
3159 psa_generator_abort( &generator );
3160 psa_destroy_key( slot );
3161 mbedtls_psa_crypto_free( );
3162}
3163/* END_CASE */
3164
3165/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003166void derive_full( int alg_arg,
3167 data_t *key_data,
3168 data_t *salt,
3169 data_t *label,
3170 int requested_capacity_arg )
3171{
3172 psa_key_slot_t slot = 1;
3173 psa_algorithm_t alg = alg_arg;
3174 size_t requested_capacity = requested_capacity_arg;
3175 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3176 unsigned char output_buffer[16];
3177 size_t expected_capacity = requested_capacity;
3178 size_t current_capacity;
3179 psa_key_policy_t policy;
3180
3181 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3182
3183 psa_key_policy_init( &policy );
3184 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3185 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3186
3187 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3188 key_data->x,
3189 key_data->len ) == PSA_SUCCESS );
3190
3191 /* Extraction phase. */
3192 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3193 salt->x, salt->len,
3194 label->x, label->len,
3195 requested_capacity ) == PSA_SUCCESS );
3196 TEST_ASSERT( psa_get_generator_capacity( &generator,
3197 &current_capacity ) ==
3198 PSA_SUCCESS );
3199 TEST_ASSERT( current_capacity == expected_capacity );
3200
3201 /* Expansion phase. */
3202 while( current_capacity > 0 )
3203 {
3204 size_t read_size = sizeof( output_buffer );
3205 if( read_size > current_capacity )
3206 read_size = current_capacity;
3207 TEST_ASSERT( psa_generator_read( &generator,
3208 output_buffer,
3209 read_size ) == PSA_SUCCESS );
3210 expected_capacity -= read_size;
3211 TEST_ASSERT( psa_get_generator_capacity( &generator,
3212 &current_capacity ) ==
3213 PSA_SUCCESS );
3214 TEST_ASSERT( current_capacity == expected_capacity );
3215 }
3216
3217 /* Check that the generator refuses to go over capacity. */
3218 TEST_ASSERT( psa_generator_read( &generator,
3219 output_buffer,
3220 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3221
3222 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3223
3224exit:
3225 psa_generator_abort( &generator );
3226 psa_destroy_key( slot );
3227 mbedtls_psa_crypto_free( );
3228}
3229/* END_CASE */
3230
3231/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003232void derive_key_exercise( int alg_arg,
3233 data_t *key_data,
3234 data_t *salt,
3235 data_t *label,
3236 int derived_type_arg,
3237 int derived_bits_arg,
3238 int derived_usage_arg,
3239 int derived_alg_arg )
3240{
3241 psa_key_slot_t base_key = 1;
3242 psa_key_slot_t derived_key = 2;
3243 psa_algorithm_t alg = alg_arg;
3244 psa_key_type_t derived_type = derived_type_arg;
3245 size_t derived_bits = derived_bits_arg;
3246 psa_key_usage_t derived_usage = derived_usage_arg;
3247 psa_algorithm_t derived_alg = derived_alg_arg;
3248 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3249 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3250 psa_key_policy_t policy;
3251 psa_key_type_t got_type;
3252 size_t got_bits;
3253
3254 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3255
3256 psa_key_policy_init( &policy );
3257 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3258 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3259 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3260 key_data->x,
3261 key_data->len ) == PSA_SUCCESS );
3262
3263 /* Derive a key. */
3264 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3265 salt->x, salt->len,
3266 label->x, label->len,
3267 capacity ) == PSA_SUCCESS );
3268 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3269 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3270 TEST_ASSERT( psa_generator_import_key( derived_key,
3271 derived_type,
3272 derived_bits,
3273 &generator ) == PSA_SUCCESS );
3274
3275 /* Test the key information */
3276 TEST_ASSERT( psa_get_key_information( derived_key,
3277 &got_type,
3278 &got_bits ) == PSA_SUCCESS );
3279 TEST_ASSERT( got_type == derived_type );
3280 TEST_ASSERT( got_bits == derived_bits );
3281
3282 /* Exercise the derived key. */
3283 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3284 goto exit;
3285
3286exit:
3287 psa_generator_abort( &generator );
3288 psa_destroy_key( base_key );
3289 psa_destroy_key( derived_key );
3290 mbedtls_psa_crypto_free( );
3291}
3292/* END_CASE */
3293
3294/* BEGIN_CASE */
3295void derive_key_export( int alg_arg,
3296 data_t *key_data,
3297 data_t *salt,
3298 data_t *label,
3299 int bytes1_arg,
3300 int bytes2_arg )
3301{
3302 psa_key_slot_t base_key = 1;
3303 psa_key_slot_t derived_key = 2;
3304 psa_algorithm_t alg = alg_arg;
3305 size_t bytes1 = bytes1_arg;
3306 size_t bytes2 = bytes2_arg;
3307 size_t capacity = bytes1 + bytes2;
3308 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003309 uint8_t *output_buffer = NULL;
3310 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003311 psa_key_policy_t policy;
3312 size_t length;
3313
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003314 ASSERT_ALLOC( output_buffer, capacity );
3315 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003316 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3317
3318 psa_key_policy_init( &policy );
3319 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3320 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3321 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3322 key_data->x,
3323 key_data->len ) == PSA_SUCCESS );
3324
3325 /* Derive some material and output it. */
3326 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3327 salt->x, salt->len,
3328 label->x, label->len,
3329 capacity ) == PSA_SUCCESS );
3330 TEST_ASSERT( psa_generator_read( &generator,
3331 output_buffer,
3332 capacity ) == PSA_SUCCESS );
3333 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3334
3335 /* Derive the same output again, but this time store it in key objects. */
3336 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3337 salt->x, salt->len,
3338 label->x, label->len,
3339 capacity ) == PSA_SUCCESS );
3340 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3341 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3342 TEST_ASSERT( psa_generator_import_key( derived_key,
3343 PSA_KEY_TYPE_RAW_DATA,
3344 PSA_BYTES_TO_BITS( bytes1 ),
3345 &generator ) == PSA_SUCCESS );
3346 TEST_ASSERT( psa_export_key( derived_key,
3347 export_buffer, bytes1,
3348 &length ) == PSA_SUCCESS );
3349 TEST_ASSERT( length == bytes1 );
3350 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3351 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3352 TEST_ASSERT( psa_generator_import_key( derived_key,
3353 PSA_KEY_TYPE_RAW_DATA,
3354 PSA_BYTES_TO_BITS( bytes2 ),
3355 &generator ) == PSA_SUCCESS );
3356 TEST_ASSERT( psa_export_key( derived_key,
3357 export_buffer + bytes1, bytes2,
3358 &length ) == PSA_SUCCESS );
3359 TEST_ASSERT( length == bytes2 );
3360
3361 /* Compare the outputs from the two runs. */
3362 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3363
3364exit:
3365 mbedtls_free( output_buffer );
3366 mbedtls_free( export_buffer );
3367 psa_generator_abort( &generator );
3368 psa_destroy_key( base_key );
3369 psa_destroy_key( derived_key );
3370 mbedtls_psa_crypto_free( );
3371}
3372/* END_CASE */
3373
3374/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003375void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003376{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003377 size_t bytes = bytes_arg;
3378 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003379 unsigned char *output = NULL;
3380 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003381 size_t i;
3382 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003383
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003384 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3385 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003386 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003387
3388 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3389
Gilles Peskinea50d7392018-06-21 10:22:13 +02003390 /* Run several times, to ensure that every output byte will be
3391 * nonzero at least once with overwhelming probability
3392 * (2^(-8*number_of_runs)). */
3393 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003394 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003395 if( bytes != 0 )
3396 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003397 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3398
3399 /* Check that no more than bytes have been overwritten */
3400 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3401
3402 for( i = 0; i < bytes; i++ )
3403 {
3404 if( output[i] != 0 )
3405 ++changed[i];
3406 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003407 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003408
3409 /* Check that every byte was changed to nonzero at least once. This
3410 * validates that psa_generate_random is overwriting every byte of
3411 * the output buffer. */
3412 for( i = 0; i < bytes; i++ )
3413 {
3414 TEST_ASSERT( changed[i] != 0 );
3415 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003416
3417exit:
3418 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003419 mbedtls_free( output );
3420 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003421}
3422/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003423
3424/* BEGIN_CASE */
3425void generate_key( int type_arg,
3426 int bits_arg,
3427 int usage_arg,
3428 int alg_arg,
3429 int expected_status_arg )
3430{
3431 int slot = 1;
3432 psa_key_type_t type = type_arg;
3433 psa_key_usage_t usage = usage_arg;
3434 size_t bits = bits_arg;
3435 psa_algorithm_t alg = alg_arg;
3436 psa_status_t expected_status = expected_status_arg;
3437 psa_key_type_t got_type;
3438 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003439 psa_status_t expected_info_status =
3440 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3441 psa_key_policy_t policy;
3442
3443 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3444
3445 psa_key_policy_init( &policy );
3446 psa_key_policy_set_usage( &policy, usage, alg );
3447 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3448
3449 /* Generate a key */
3450 TEST_ASSERT( psa_generate_key( slot, type, bits,
3451 NULL, 0 ) == expected_status );
3452
3453 /* Test the key information */
3454 TEST_ASSERT( psa_get_key_information( slot,
3455 &got_type,
3456 &got_bits ) == expected_info_status );
3457 if( expected_info_status != PSA_SUCCESS )
3458 goto exit;
3459 TEST_ASSERT( got_type == type );
3460 TEST_ASSERT( got_bits == bits );
3461
Gilles Peskine818ca122018-06-20 18:16:48 +02003462 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003463 if( ! exercise_key( slot, usage, alg ) )
3464 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003465
3466exit:
3467 psa_destroy_key( slot );
3468 mbedtls_psa_crypto_free( );
3469}
3470/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003471
3472/* BEGIN_CASE */
3473void validate_module_init_generate_random( )
3474{
3475 psa_status_t status;
3476 uint8_t random[10] = { 0 };
3477 status = psa_generate_random( random, sizeof( random ) );
3478 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3479}
3480/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003481
3482/* BEGIN_CASE */
3483void validate_module_init_key_based( )
3484{
3485 psa_status_t status;
3486 uint8_t data[10] = { 0 };
3487 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3488 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3489}
3490/* END_CASE */