blob: 3db6df5b87e92e357d3ff375ee0437e94a23a1d5 [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,
1042 int expected_bits,
1043 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001044 int expected_export_status_arg )
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;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001053 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001054 psa_key_type_t got_type;
1055 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001056 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001057
Moran Pekerf709f4a2018-06-06 17:26:04 +03001058 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001059 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001060 export_size = (ptrdiff_t) data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001061 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001062
1063 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1064
1065 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001066 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001067 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1068
1069 /* Import the key */
1070 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001071 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001072
1073 /* Test the key information */
1074 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001075 &got_type,
1076 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001077 TEST_ASSERT( got_type == type );
1078 TEST_ASSERT( got_bits == (size_t) expected_bits );
1079
1080 /* Export the key */
1081 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001082 exported, export_size,
1083 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001084 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001085 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001086 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Jaeden Amero2a671e92018-06-27 17:47:40 +01001087 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001088 if( status != PSA_SUCCESS )
1089 goto destroy;
1090
Moran Pekerf709f4a2018-06-06 17:26:04 +03001091destroy:
1092 /* Destroy the key */
1093 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1094 TEST_ASSERT( psa_get_key_information(
1095 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1096
1097exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001098 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001099 mbedtls_psa_crypto_free( );
1100}
1101/* END_CASE */
1102
Gilles Peskine20035e32018-02-03 22:44:14 +01001103/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001104void import_and_exercise_key( data_t *data,
1105 int type_arg,
1106 int bits_arg,
1107 int alg_arg )
1108{
1109 int slot = 1;
1110 psa_key_type_t type = type_arg;
1111 size_t bits = bits_arg;
1112 psa_algorithm_t alg = alg_arg;
1113 psa_key_usage_t usage =
1114 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1115 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1116 PSA_KEY_USAGE_VERIFY :
1117 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1118 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1119 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1120 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1121 PSA_KEY_USAGE_ENCRYPT :
1122 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001123 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001124 0 );
1125 psa_key_policy_t policy;
1126 psa_key_type_t got_type;
1127 size_t got_bits;
1128 psa_status_t status;
1129
1130 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1131
1132 psa_key_policy_init( &policy );
1133 psa_key_policy_set_usage( &policy, usage, alg );
1134 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1135
1136 /* Import the key */
1137 status = psa_import_key( slot, type, data->x, data->len );
1138 TEST_ASSERT( status == PSA_SUCCESS );
1139
1140 /* Test the key information */
1141 TEST_ASSERT( psa_get_key_information( slot,
1142 &got_type,
1143 &got_bits ) == PSA_SUCCESS );
1144 TEST_ASSERT( got_type == type );
1145 TEST_ASSERT( got_bits == bits );
1146
1147 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001148 if( ! exercise_key( slot, usage, alg ) )
1149 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001150
1151exit:
1152 psa_destroy_key( slot );
1153 mbedtls_psa_crypto_free( );
1154}
1155/* END_CASE */
1156
1157/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001158void key_policy( int usage_arg, int alg_arg )
1159{
1160 int key_slot = 1;
1161 psa_algorithm_t alg = alg_arg;
1162 psa_key_usage_t usage = usage_arg;
1163 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1164 unsigned char key[32] = {0};
1165 psa_key_policy_t policy_set;
1166 psa_key_policy_t policy_get;
1167
1168 memset( key, 0x2a, sizeof( key ) );
1169
1170 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1171
1172 psa_key_policy_init( &policy_set );
1173 psa_key_policy_init( &policy_get );
1174
1175 psa_key_policy_set_usage( &policy_set, usage, alg );
1176
1177 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1178 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1179 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1180
1181 TEST_ASSERT( psa_import_key( key_slot, key_type,
1182 key, sizeof( key ) ) == PSA_SUCCESS );
1183
1184 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1185
1186 TEST_ASSERT( policy_get.usage == policy_set.usage );
1187 TEST_ASSERT( policy_get.alg == policy_set.alg );
1188
1189exit:
1190 psa_destroy_key( key_slot );
1191 mbedtls_psa_crypto_free( );
1192}
1193/* END_CASE */
1194
1195/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001196void mac_key_policy( int policy_usage,
1197 int policy_alg,
1198 int key_type,
1199 data_t *key_data,
1200 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001201{
1202 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001203 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204 psa_mac_operation_t operation;
1205 psa_status_t status;
1206 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001207
1208 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1209
1210 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001212 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1213
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214 TEST_ASSERT( psa_import_key( key_slot, key_type,
1215 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001216
Gilles Peskine89167cb2018-07-08 20:12:23 +02001217 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 if( policy_alg == exercise_alg &&
1219 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1220 TEST_ASSERT( status == PSA_SUCCESS );
1221 else
1222 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1223 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001224
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001226 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227 if( policy_alg == exercise_alg &&
1228 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001229 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 else
1231 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1232
1233exit:
1234 psa_mac_abort( &operation );
1235 psa_destroy_key( key_slot );
1236 mbedtls_psa_crypto_free( );
1237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE */
1241void cipher_key_policy( int policy_usage,
1242 int policy_alg,
1243 int key_type,
1244 data_t *key_data,
1245 int exercise_alg )
1246{
1247 int key_slot = 1;
1248 psa_key_policy_t policy;
1249 psa_cipher_operation_t operation;
1250 psa_status_t status;
1251
1252 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1253
1254 psa_key_policy_init( &policy );
1255 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1256 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1257
1258 TEST_ASSERT( psa_import_key( key_slot, key_type,
1259 key_data->x, key_data->len ) == PSA_SUCCESS );
1260
Gilles Peskinefe119512018-07-08 21:39:34 +02001261 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262 if( policy_alg == exercise_alg &&
1263 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1264 TEST_ASSERT( status == PSA_SUCCESS );
1265 else
1266 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1267 psa_cipher_abort( &operation );
1268
Gilles Peskinefe119512018-07-08 21:39:34 +02001269 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001270 if( policy_alg == exercise_alg &&
1271 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1272 TEST_ASSERT( status == PSA_SUCCESS );
1273 else
1274 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1275
1276exit:
1277 psa_cipher_abort( &operation );
1278 psa_destroy_key( key_slot );
1279 mbedtls_psa_crypto_free( );
1280}
1281/* END_CASE */
1282
1283/* BEGIN_CASE */
1284void aead_key_policy( int policy_usage,
1285 int policy_alg,
1286 int key_type,
1287 data_t *key_data,
1288 int nonce_length_arg,
1289 int tag_length_arg,
1290 int exercise_alg )
1291{
1292 int key_slot = 1;
1293 psa_key_policy_t policy;
1294 psa_status_t status;
1295 unsigned char nonce[16] = {0};
1296 size_t nonce_length = nonce_length_arg;
1297 unsigned char tag[16];
1298 size_t tag_length = tag_length_arg;
1299 size_t output_length;
1300
1301 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1302 TEST_ASSERT( tag_length <= sizeof( tag ) );
1303
1304 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1305
1306 psa_key_policy_init( &policy );
1307 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1308 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1309
1310 TEST_ASSERT( psa_import_key( key_slot, key_type,
1311 key_data->x, key_data->len ) == PSA_SUCCESS );
1312
1313 status = psa_aead_encrypt( key_slot, exercise_alg,
1314 nonce, nonce_length,
1315 NULL, 0,
1316 NULL, 0,
1317 tag, tag_length,
1318 &output_length );
1319 if( policy_alg == exercise_alg &&
1320 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1321 TEST_ASSERT( status == PSA_SUCCESS );
1322 else
1323 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1324
1325 memset( tag, 0, sizeof( tag ) );
1326 status = psa_aead_decrypt( key_slot, exercise_alg,
1327 nonce, nonce_length,
1328 NULL, 0,
1329 tag, tag_length,
1330 NULL, 0,
1331 &output_length );
1332 if( policy_alg == exercise_alg &&
1333 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1334 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1335 else
1336 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1337
1338exit:
1339 psa_destroy_key( key_slot );
1340 mbedtls_psa_crypto_free( );
1341}
1342/* END_CASE */
1343
1344/* BEGIN_CASE */
1345void asymmetric_encryption_key_policy( int policy_usage,
1346 int policy_alg,
1347 int key_type,
1348 data_t *key_data,
1349 int exercise_alg )
1350{
1351 int key_slot = 1;
1352 psa_key_policy_t policy;
1353 psa_status_t status;
1354 size_t key_bits;
1355 size_t buffer_length;
1356 unsigned char *buffer = NULL;
1357 size_t output_length;
1358
1359 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1360
1361 psa_key_policy_init( &policy );
1362 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1363 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1364
1365 TEST_ASSERT( psa_import_key( key_slot, key_type,
1366 key_data->x, key_data->len ) == PSA_SUCCESS );
1367
1368 TEST_ASSERT( psa_get_key_information( key_slot,
1369 NULL,
1370 &key_bits ) == PSA_SUCCESS );
1371 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1372 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001373 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001374
1375 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1376 NULL, 0,
1377 NULL, 0,
1378 buffer, buffer_length,
1379 &output_length );
1380 if( policy_alg == exercise_alg &&
1381 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1382 TEST_ASSERT( status == PSA_SUCCESS );
1383 else
1384 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1385
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001386 if( buffer_length != 0 )
1387 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001388 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1389 buffer, buffer_length,
1390 NULL, 0,
1391 buffer, buffer_length,
1392 &output_length );
1393 if( policy_alg == exercise_alg &&
1394 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1395 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1396 else
1397 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1398
1399exit:
1400 psa_destroy_key( key_slot );
1401 mbedtls_psa_crypto_free( );
1402 mbedtls_free( buffer );
1403}
1404/* END_CASE */
1405
1406/* BEGIN_CASE */
1407void asymmetric_signature_key_policy( int policy_usage,
1408 int policy_alg,
1409 int key_type,
1410 data_t *key_data,
1411 int exercise_alg )
1412{
1413 int key_slot = 1;
1414 psa_key_policy_t policy;
1415 psa_status_t status;
1416 unsigned char payload[16] = {1};
1417 size_t payload_length = sizeof( payload );
1418 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1419 size_t signature_length;
1420
1421 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1422
1423 psa_key_policy_init( &policy );
1424 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1425 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1426
1427 TEST_ASSERT( psa_import_key( key_slot, key_type,
1428 key_data->x, key_data->len ) == PSA_SUCCESS );
1429
1430 status = psa_asymmetric_sign( key_slot, exercise_alg,
1431 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001432 signature, sizeof( signature ),
1433 &signature_length );
1434 if( policy_alg == exercise_alg &&
1435 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1436 TEST_ASSERT( status == PSA_SUCCESS );
1437 else
1438 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1439
1440 memset( signature, 0, sizeof( signature ) );
1441 status = psa_asymmetric_verify( key_slot, exercise_alg,
1442 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001443 signature, sizeof( signature ) );
1444 if( policy_alg == exercise_alg &&
1445 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1446 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1447 else
1448 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001449
1450exit:
1451 psa_destroy_key( key_slot );
1452 mbedtls_psa_crypto_free( );
1453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001457void derive_key_policy( int policy_usage,
1458 int policy_alg,
1459 int key_type,
1460 data_t *key_data,
1461 int exercise_alg )
1462{
1463 int key_slot = 1;
1464 psa_key_policy_t policy;
1465 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1466 psa_status_t status;
1467
1468 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1469
1470 psa_key_policy_init( &policy );
1471 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1472 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1473
1474 TEST_ASSERT( psa_import_key( key_slot, key_type,
1475 key_data->x, key_data->len ) == PSA_SUCCESS );
1476
1477 status = psa_key_derivation( &generator, key_slot,
1478 exercise_alg,
1479 NULL, 0,
1480 NULL, 0,
1481 1 );
1482 if( policy_alg == exercise_alg &&
1483 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1484 TEST_ASSERT( status == PSA_SUCCESS );
1485 else
1486 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1487
1488exit:
1489 psa_generator_abort( &generator );
1490 psa_destroy_key( key_slot );
1491 mbedtls_psa_crypto_free( );
1492}
1493/* END_CASE */
1494
1495/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001496void key_lifetime( int lifetime_arg )
1497{
1498 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001499 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001500 unsigned char key[32] = {0};
1501 psa_key_lifetime_t lifetime_set = lifetime_arg;
1502 psa_key_lifetime_t lifetime_get;
1503
1504 memset( key, 0x2a, sizeof( key ) );
1505
1506 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1507
1508 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1509 lifetime_set ) == PSA_SUCCESS );
1510
1511 TEST_ASSERT( psa_import_key( key_slot, key_type,
1512 key, sizeof( key ) ) == PSA_SUCCESS );
1513
1514 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1515 &lifetime_get ) == PSA_SUCCESS );
1516
1517 TEST_ASSERT( lifetime_get == lifetime_set );
1518
1519exit:
1520 psa_destroy_key( key_slot );
1521 mbedtls_psa_crypto_free( );
1522}
1523/* END_CASE */
1524
1525/* BEGIN_CASE */
1526void key_lifetime_set_fail( int key_slot_arg,
1527 int lifetime_arg,
1528 int expected_status_arg )
1529{
1530 psa_key_slot_t key_slot = key_slot_arg;
1531 psa_key_lifetime_t lifetime_set = lifetime_arg;
1532 psa_status_t actual_status;
1533 psa_status_t expected_status = expected_status_arg;
1534
1535 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1536
1537 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1538
1539 if( actual_status == PSA_SUCCESS )
1540 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1541
1542 TEST_ASSERT( expected_status == actual_status );
1543
1544exit:
1545 psa_destroy_key( key_slot );
1546 mbedtls_psa_crypto_free( );
1547}
1548/* END_CASE */
1549
1550/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001551void hash_setup( int alg_arg,
1552 int expected_status_arg )
1553{
1554 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001555 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001556 psa_hash_operation_t operation;
1557 psa_status_t status;
1558
1559 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1560
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001561 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001562 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001563 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001564
1565exit:
1566 mbedtls_psa_crypto_free( );
1567}
1568/* END_CASE */
1569
1570/* BEGIN_CASE */
itayzafrir58028322018-10-25 10:22:01 +03001571void hash_verify_bad_paths( )
itayzafrirec93d302018-10-18 18:01:10 +03001572{
1573 psa_algorithm_t alg = PSA_ALG_SHA_256;
1574 unsigned char hash[PSA_HASH_MAX_SIZE] = { 0 };
1575 size_t expected_size = PSA_HASH_SIZE( alg );
1576 unsigned char input[] = "input";
1577 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001578
itayzafrir4271df92018-10-24 18:16:19 +03001579 /* SHA-256 hash digest of the string 'input' with 2 extra bytes appended at
1580 * the end */
1581 unsigned char extra_length_digest[] =
1582 {
1583 0x3a, 0x28, 0x92, 0x32, 0x39, 0x9a, 0x20, 0x75, 0x09, 0xf4, 0xfa, 0x9d,
1584 0x70, 0xfa, 0x6f, 0x68, 0x81, 0x7c, 0xe6, 0xa6, 0x6f, 0x21, 0x50, 0xff,
1585 0x08, 0x23, 0x36, 0x31, 0x1f, 0x4e, 0x55, 0xfe, 0xaa, 0xbb
1586 };
1587
itayzafrirec93d302018-10-18 18:01:10 +03001588 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1589
itayzafrirec93d302018-10-18 18:01:10 +03001590 /* psa_hash_verify without calling psa_hash_setup beforehand */
1591 memset( &operation, 0, sizeof( operation ) );
1592 TEST_ASSERT( psa_hash_verify( &operation,
1593 hash, expected_size ) ==
1594 PSA_ERROR_INVALID_ARGUMENT );
1595
itayzafrir69290f02018-10-24 13:50:54 +03001596 /* psa_hash_verify with a smaller hash digest than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001597 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1598 TEST_ASSERT( psa_hash_verify( &operation,
1599 hash, expected_size - 1 ) ==
1600 PSA_ERROR_INVALID_SIGNATURE );
1601
itayzafrir69290f02018-10-24 13:50:54 +03001602 /* psa_hash_verify with a non-matching hash digest */
itayzafrirec93d302018-10-18 18:01:10 +03001603 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1604 TEST_ASSERT( psa_hash_update( &operation,
1605 input, sizeof( input ) ) == PSA_SUCCESS );
1606 TEST_ASSERT( psa_hash_verify( &operation,
1607 hash, expected_size ) ==
1608 PSA_ERROR_INVALID_SIGNATURE );
1609
itayzafrir4271df92018-10-24 18:16:19 +03001610 /* psa_hash_verify with a hash digest longer than expected, where the first
1611 * 32 bytes match the expected digest but 2 extra bytes are appended at the
1612 * end of the digest */
1613 memset( hash, 0, sizeof( hash ) );
1614 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1615 TEST_ASSERT( psa_hash_update( &operation,
1616 input, sizeof( input ) ) == PSA_SUCCESS );
1617 TEST_ASSERT( psa_hash_verify( &operation,
1618 extra_length_digest,
1619 sizeof( extra_length_digest ) ) ==
1620 PSA_ERROR_INVALID_SIGNATURE );
1621
itayzafrirec93d302018-10-18 18:01:10 +03001622exit:
1623 mbedtls_psa_crypto_free( );
1624}
1625/* END_CASE */
1626
1627/* BEGIN_CASE */
itayzafrir58028322018-10-25 10:22:01 +03001628void hash_update_bad_paths( )
1629{
1630 unsigned char input[] = "input";
1631 psa_hash_operation_t operation;
1632
1633 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1634
1635 /* psa_hash_update without calling psa_hash_setup beforehand */
1636 memset( &operation, 0, sizeof( operation ) );
1637 TEST_ASSERT( psa_hash_update( &operation,
1638 input, sizeof( input ) ) ==
1639 PSA_ERROR_INVALID_ARGUMENT );
1640
1641exit:
1642 mbedtls_psa_crypto_free( );
1643}
1644/* END_CASE */
1645
1646/* BEGIN_CASE */
1647void hash_finish_bad_paths( )
1648{
1649 psa_algorithm_t alg = PSA_ALG_SHA_256;
1650 unsigned char hash[PSA_HASH_MAX_SIZE] = { 0 };
1651 size_t expected_size = PSA_HASH_SIZE( alg );
1652 psa_hash_operation_t operation;
1653 size_t hash_len;
1654
1655 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1656
1657 /* psa_hash_finish without calling psa_hash_setup beforehand */
1658 memset( &operation, 0, sizeof( operation ) );
1659 TEST_ASSERT( psa_hash_finish( &operation,
1660 hash, expected_size,
1661 &hash_len ) == PSA_ERROR_INVALID_ARGUMENT );
1662
1663 /* psa_hash_finish with a smaller hash buffer than expected */
1664 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1665 TEST_ASSERT( psa_hash_finish( &operation,
1666 hash, expected_size - 1,
1667 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1668
1669exit:
1670 mbedtls_psa_crypto_free( );
1671}
1672/* END_CASE */
1673
1674/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001675void mac_setup( int key_type_arg,
1676 data_t *key,
1677 int alg_arg,
1678 int expected_status_arg )
1679{
1680 int key_slot = 1;
1681 psa_key_type_t key_type = key_type_arg;
1682 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001683 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001684 psa_mac_operation_t operation;
1685 psa_key_policy_t policy;
1686 psa_status_t status;
1687
1688 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1689
1690 psa_key_policy_init( &policy );
1691 psa_key_policy_set_usage( &policy,
1692 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1693 alg );
1694 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1695
1696 TEST_ASSERT( psa_import_key( key_slot, key_type,
1697 key->x, key->len ) == PSA_SUCCESS );
1698
Gilles Peskine89167cb2018-07-08 20:12:23 +02001699 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001700 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001701 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001702
1703exit:
1704 psa_destroy_key( key_slot );
1705 mbedtls_psa_crypto_free( );
1706}
1707/* END_CASE */
1708
1709/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001710void mac_sign( int key_type_arg,
1711 data_t *key,
1712 int alg_arg,
1713 data_t *input,
1714 data_t *expected_mac )
1715{
1716 int key_slot = 1;
1717 psa_key_type_t key_type = key_type_arg;
1718 psa_algorithm_t alg = alg_arg;
1719 psa_mac_operation_t operation;
1720 psa_key_policy_t policy;
1721 /* Leave a little extra room in the output buffer. At the end of the
1722 * test, we'll check that the implementation didn't overwrite onto
1723 * this extra room. */
1724 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1725 size_t mac_buffer_size =
1726 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1727 size_t mac_length = 0;
1728
1729 memset( actual_mac, '+', sizeof( actual_mac ) );
1730 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1731 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1732
1733 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1734
1735 psa_key_policy_init( &policy );
1736 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1737 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1738
1739 TEST_ASSERT( psa_import_key( key_slot, key_type,
1740 key->x, key->len ) == PSA_SUCCESS );
1741
1742 /* Calculate the MAC. */
1743 TEST_ASSERT( psa_mac_sign_setup( &operation,
1744 key_slot, alg ) == PSA_SUCCESS );
1745 TEST_ASSERT( psa_mac_update( &operation,
1746 input->x, input->len ) == PSA_SUCCESS );
1747 TEST_ASSERT( psa_mac_sign_finish( &operation,
1748 actual_mac, mac_buffer_size,
1749 &mac_length ) == PSA_SUCCESS );
1750
1751 /* Compare with the expected value. */
1752 TEST_ASSERT( mac_length == expected_mac->len );
1753 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1754
1755 /* Verify that the end of the buffer is untouched. */
1756 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1757 sizeof( actual_mac ) - mac_length ) );
1758
1759exit:
1760 psa_destroy_key( key_slot );
1761 mbedtls_psa_crypto_free( );
1762}
1763/* END_CASE */
1764
1765/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001766void mac_verify( int key_type_arg,
1767 data_t *key,
1768 int alg_arg,
1769 data_t *input,
1770 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001771{
1772 int key_slot = 1;
1773 psa_key_type_t key_type = key_type_arg;
1774 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001775 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001776 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001777
Gilles Peskine69c12672018-06-28 00:07:19 +02001778 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1779
Gilles Peskine8c9def32018-02-08 10:02:12 +01001780 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001781 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001782 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001783 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001784 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1785 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001786
1787 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1788
mohammad16036df908f2018-04-02 08:34:15 -07001789 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001790 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001791 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1792
Gilles Peskine8c9def32018-02-08 10:02:12 +01001793 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001794 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001795
Gilles Peskine89167cb2018-07-08 20:12:23 +02001796 TEST_ASSERT( psa_mac_verify_setup( &operation,
1797 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001798 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1799 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001800 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001801 TEST_ASSERT( psa_mac_verify_finish( &operation,
1802 expected_mac->x,
1803 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001804
1805exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001806 psa_destroy_key( key_slot );
1807 mbedtls_psa_crypto_free( );
1808}
1809/* END_CASE */
1810
1811/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001812void cipher_setup( int key_type_arg,
1813 data_t *key,
1814 int alg_arg,
1815 int expected_status_arg )
1816{
1817 int key_slot = 1;
1818 psa_key_type_t key_type = key_type_arg;
1819 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001820 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001821 psa_cipher_operation_t operation;
1822 psa_key_policy_t policy;
1823 psa_status_t status;
1824
1825 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1826
1827 psa_key_policy_init( &policy );
1828 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1829 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1830
1831 TEST_ASSERT( psa_import_key( key_slot, key_type,
1832 key->x, key->len ) == PSA_SUCCESS );
1833
Gilles Peskinefe119512018-07-08 21:39:34 +02001834 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001835 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001836 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001837
1838exit:
1839 psa_destroy_key( key_slot );
1840 mbedtls_psa_crypto_free( );
1841}
1842/* END_CASE */
1843
1844/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001845void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001846 data_t *key,
1847 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001848 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001849{
1850 int key_slot = 1;
1851 psa_status_t status;
1852 psa_key_type_t key_type = key_type_arg;
1853 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001854 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001855 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001856 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001857 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001858 size_t output_buffer_size = 0;
1859 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001860 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001861 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001862 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001863
Gilles Peskine50e586b2018-06-08 14:28:46 +02001864 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001865 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001866 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001867 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1868 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1869 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001870
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001871 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1872 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001873
1874 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1875
Moran Pekered346952018-07-05 15:22:45 +03001876 psa_key_policy_init( &policy );
1877 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1878 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1879
Gilles Peskine50e586b2018-06-08 14:28:46 +02001880 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001881 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001882
Gilles Peskinefe119512018-07-08 21:39:34 +02001883 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1884 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001885
Gilles Peskinefe119512018-07-08 21:39:34 +02001886 TEST_ASSERT( psa_cipher_set_iv( &operation,
1887 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001888 output_buffer_size = (size_t) input->len +
1889 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001890 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001891
Gilles Peskine4abf7412018-06-18 16:35:34 +02001892 TEST_ASSERT( psa_cipher_update( &operation,
1893 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001894 output, output_buffer_size,
1895 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001896 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001897 status = psa_cipher_finish( &operation,
1898 output + function_output_length,
1899 output_buffer_size,
1900 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001901 total_output_length += function_output_length;
1902
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001903 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001904 if( expected_status == PSA_SUCCESS )
1905 {
1906 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001907 ASSERT_COMPARE( expected_output->x, expected_output->len,
1908 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001909 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001910
Gilles Peskine50e586b2018-06-08 14:28:46 +02001911exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001912 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001913 psa_destroy_key( key_slot );
1914 mbedtls_psa_crypto_free( );
1915}
1916/* END_CASE */
1917
1918/* BEGIN_CASE */
1919void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001920 data_t *key,
1921 data_t *input,
1922 int first_part_size,
1923 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001924{
1925 int key_slot = 1;
1926 psa_key_type_t key_type = key_type_arg;
1927 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001928 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001929 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001930 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001931 size_t output_buffer_size = 0;
1932 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001933 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001934 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001935 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001936
Gilles Peskine50e586b2018-06-08 14:28:46 +02001937 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001938 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001939 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001940 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1941 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1942 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001943
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001944 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1945 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001946
1947 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1948
Moran Pekered346952018-07-05 15:22:45 +03001949 psa_key_policy_init( &policy );
1950 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1951 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1952
Gilles Peskine50e586b2018-06-08 14:28:46 +02001953 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001954 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001955
Gilles Peskinefe119512018-07-08 21:39:34 +02001956 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1957 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001958
Gilles Peskinefe119512018-07-08 21:39:34 +02001959 TEST_ASSERT( psa_cipher_set_iv( &operation,
1960 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001961 output_buffer_size = (size_t) input->len +
1962 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001963 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001964
Gilles Peskine4abf7412018-06-18 16:35:34 +02001965 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001966 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001967 output, output_buffer_size,
1968 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001969 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001970 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001971 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001972 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001973 output, output_buffer_size,
1974 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001975 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001976 TEST_ASSERT( psa_cipher_finish( &operation,
1977 output + function_output_length,
1978 output_buffer_size,
1979 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001980 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001981 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1982
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001983 ASSERT_COMPARE( expected_output->x, expected_output->len,
1984 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001985
1986exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001987 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001988 psa_destroy_key( key_slot );
1989 mbedtls_psa_crypto_free( );
1990}
1991/* END_CASE */
1992
1993/* BEGIN_CASE */
1994void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001995 data_t *key,
1996 data_t *input,
1997 int first_part_size,
1998 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001999{
2000 int key_slot = 1;
2001
2002 psa_key_type_t key_type = key_type_arg;
2003 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002004 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002005 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002006 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002007 size_t output_buffer_size = 0;
2008 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002009 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002010 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002011 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002012
Gilles Peskine50e586b2018-06-08 14:28:46 +02002013 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002014 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002015 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002016 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2017 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2018 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002019
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002020 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2021 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002022
2023 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2024
Moran Pekered346952018-07-05 15:22:45 +03002025 psa_key_policy_init( &policy );
2026 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2027 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2028
Gilles Peskine50e586b2018-06-08 14:28:46 +02002029 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002030 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002031
Gilles Peskinefe119512018-07-08 21:39:34 +02002032 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2033 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034
Gilles Peskinefe119512018-07-08 21:39:34 +02002035 TEST_ASSERT( psa_cipher_set_iv( &operation,
2036 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002037
mohammad16033d91abe2018-07-03 13:15:54 +03002038 output_buffer_size = (size_t) input->len +
2039 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002040 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002041
Gilles Peskine4abf7412018-06-18 16:35:34 +02002042 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2043 TEST_ASSERT( psa_cipher_update( &operation,
2044 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002045 output, output_buffer_size,
2046 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002047 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002048 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002049 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002050 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002051 output, output_buffer_size,
2052 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002053 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002054 TEST_ASSERT( psa_cipher_finish( &operation,
2055 output + function_output_length,
2056 output_buffer_size,
2057 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002058 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002059 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2060
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002061 ASSERT_COMPARE( expected_output->x, expected_output->len,
2062 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002063
2064exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002065 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002066 psa_destroy_key( key_slot );
2067 mbedtls_psa_crypto_free( );
2068}
2069/* END_CASE */
2070
Gilles Peskine50e586b2018-06-08 14:28:46 +02002071/* BEGIN_CASE */
2072void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002073 data_t *key,
2074 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002075 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002076{
2077 int key_slot = 1;
2078 psa_status_t status;
2079 psa_key_type_t key_type = key_type_arg;
2080 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002081 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002082 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002083 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002084 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002085 size_t output_buffer_size = 0;
2086 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002087 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002088 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002089 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002090
Gilles Peskine50e586b2018-06-08 14:28:46 +02002091 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002092 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002093 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002094 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2095 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2096 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002097
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002098 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2099 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002100
2101 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2102
Moran Pekered346952018-07-05 15:22:45 +03002103 psa_key_policy_init( &policy );
2104 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2105 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2106
Gilles Peskine50e586b2018-06-08 14:28:46 +02002107 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002108 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002109
Gilles Peskinefe119512018-07-08 21:39:34 +02002110 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2111 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002112
Gilles Peskinefe119512018-07-08 21:39:34 +02002113 TEST_ASSERT( psa_cipher_set_iv( &operation,
2114 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002115
mohammad16033d91abe2018-07-03 13:15:54 +03002116 output_buffer_size = (size_t) input->len +
2117 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002118 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002119
Gilles Peskine4abf7412018-06-18 16:35:34 +02002120 TEST_ASSERT( psa_cipher_update( &operation,
2121 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002122 output, output_buffer_size,
2123 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002124 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002125 status = psa_cipher_finish( &operation,
2126 output + function_output_length,
2127 output_buffer_size,
2128 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002129 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002130 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002131
2132 if( expected_status == PSA_SUCCESS )
2133 {
2134 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002135 ASSERT_COMPARE( expected_output->x, expected_output->len,
2136 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002137 }
2138
Gilles Peskine50e586b2018-06-08 14:28:46 +02002139exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002140 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002141 psa_destroy_key( key_slot );
2142 mbedtls_psa_crypto_free( );
2143}
2144/* END_CASE */
2145
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146/* BEGIN_CASE */
2147void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002148 data_t *key,
2149 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002150{
2151 int key_slot = 1;
2152 psa_key_type_t key_type = key_type_arg;
2153 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002154 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002155 size_t iv_size = 16;
2156 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002157 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002158 size_t output1_size = 0;
2159 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002160 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002161 size_t output2_size = 0;
2162 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002163 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002164 psa_cipher_operation_t operation1;
2165 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002166 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002167
mohammad1603d7d7ba52018-03-12 18:51:53 +02002168 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002169 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002170 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2171 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002172
mohammad1603d7d7ba52018-03-12 18:51:53 +02002173 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2174
Moran Pekered346952018-07-05 15:22:45 +03002175 psa_key_policy_init( &policy );
2176 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2177 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2178
mohammad1603d7d7ba52018-03-12 18:51:53 +02002179 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002180 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002181
Gilles Peskinefe119512018-07-08 21:39:34 +02002182 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2183 key_slot, alg ) == PSA_SUCCESS );
2184 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2185 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002186
Gilles Peskinefe119512018-07-08 21:39:34 +02002187 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2188 iv, iv_size,
2189 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002190 output1_size = (size_t) input->len +
2191 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002192 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002193
Gilles Peskine4abf7412018-06-18 16:35:34 +02002194 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002195 output1, output1_size,
2196 &output1_length ) == PSA_SUCCESS );
2197 TEST_ASSERT( psa_cipher_finish( &operation1,
2198 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002199 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002200
Gilles Peskine048b7f02018-06-08 14:20:49 +02002201 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002202
2203 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2204
2205 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002206 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002207
Gilles Peskinefe119512018-07-08 21:39:34 +02002208 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2209 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002210 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2211 output2, output2_size,
2212 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002213 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002214 TEST_ASSERT( psa_cipher_finish( &operation2,
2215 output2 + output2_length,
2216 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002217 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002218
Gilles Peskine048b7f02018-06-08 14:20:49 +02002219 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002220
Janos Follath25c4fa82018-07-06 16:23:25 +01002221 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002222
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002223 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002224
2225exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002226 mbedtls_free( output1 );
2227 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002228 psa_destroy_key( key_slot );
2229 mbedtls_psa_crypto_free( );
2230}
2231/* END_CASE */
2232
2233/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002234void cipher_verify_output_multipart( int alg_arg,
2235 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002236 data_t *key,
2237 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002238 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002239{
2240 int key_slot = 1;
2241 psa_key_type_t key_type = key_type_arg;
2242 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002243 unsigned char iv[16] = {0};
2244 size_t iv_size = 16;
2245 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002246 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002247 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002248 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002249 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002250 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002251 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002252 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002253 psa_cipher_operation_t operation1;
2254 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002255 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002256
Moran Pekerded84402018-06-06 16:36:50 +03002257 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002258 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002259 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2260 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002261
Moran Pekerded84402018-06-06 16:36:50 +03002262 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2263
Moran Pekered346952018-07-05 15:22:45 +03002264 psa_key_policy_init( &policy );
2265 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2266 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2267
Moran Pekerded84402018-06-06 16:36:50 +03002268 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002269 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002270
Gilles Peskinefe119512018-07-08 21:39:34 +02002271 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2272 key_slot, alg ) == PSA_SUCCESS );
2273 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2274 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002275
Gilles Peskinefe119512018-07-08 21:39:34 +02002276 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2277 iv, iv_size,
2278 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002279 output1_buffer_size = (size_t) input->len +
2280 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002281 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002282
Gilles Peskine4abf7412018-06-18 16:35:34 +02002283 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002284
itayzafrir3e02b3b2018-06-12 17:06:52 +03002285 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002286 output1, output1_buffer_size,
2287 &function_output_length ) == PSA_SUCCESS );
2288 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002289
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002290 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002291 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002292 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002293 output1, output1_buffer_size,
2294 &function_output_length ) == PSA_SUCCESS );
2295 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002296
Gilles Peskine048b7f02018-06-08 14:20:49 +02002297 TEST_ASSERT( psa_cipher_finish( &operation1,
2298 output1 + output1_length,
2299 output1_buffer_size - output1_length,
2300 &function_output_length ) == PSA_SUCCESS );
2301 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002302
2303 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2304
Gilles Peskine048b7f02018-06-08 14:20:49 +02002305 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002306 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002307
Gilles Peskinefe119512018-07-08 21:39:34 +02002308 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2309 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002310
2311 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002312 output2, output2_buffer_size,
2313 &function_output_length ) == PSA_SUCCESS );
2314 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002315
Gilles Peskine048b7f02018-06-08 14:20:49 +02002316 TEST_ASSERT( psa_cipher_update( &operation2,
2317 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002318 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002319 output2, output2_buffer_size,
2320 &function_output_length ) == PSA_SUCCESS );
2321 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002322
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002323 TEST_ASSERT( psa_cipher_finish( &operation2,
2324 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002325 output2_buffer_size - output2_length,
2326 &function_output_length ) == PSA_SUCCESS );
2327 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002328
Janos Follath25c4fa82018-07-06 16:23:25 +01002329 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002330
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002331 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002332
2333exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002334 mbedtls_free( output1 );
2335 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002336 psa_destroy_key( key_slot );
2337 mbedtls_psa_crypto_free( );
2338}
2339/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002340
Gilles Peskine20035e32018-02-03 22:44:14 +01002341/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002342void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002343 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002344 data_t *nonce,
2345 data_t *additional_data,
2346 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002347 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002348{
2349 int slot = 1;
2350 psa_key_type_t key_type = key_type_arg;
2351 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002352 unsigned char *output_data = NULL;
2353 size_t output_size = 0;
2354 size_t output_length = 0;
2355 unsigned char *output_data2 = NULL;
2356 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002357 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002358 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002359 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002360
Gilles Peskinea1cac842018-06-11 19:33:02 +02002361 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002362 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002363 TEST_ASSERT( nonce != NULL );
2364 TEST_ASSERT( additional_data != NULL );
2365 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2366 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2367 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2368 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2369
Gilles Peskine4abf7412018-06-18 16:35:34 +02002370 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002371 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002372
2373 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2374
2375 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002376 psa_key_policy_set_usage( &policy,
2377 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2378 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002379 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2380
2381 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002382 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002383
2384 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002385 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002386 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002387 additional_data->len,
2388 input_data->x, input_data->len,
2389 output_data, output_size,
2390 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002391
2392 if( PSA_SUCCESS == expected_result )
2393 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002394 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002395
2396 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002397 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002398 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002399 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002400 output_data, output_length,
2401 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002402 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002403
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002404 ASSERT_COMPARE( input_data->x, input_data->len,
2405 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002406 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002407
Gilles Peskinea1cac842018-06-11 19:33:02 +02002408exit:
2409 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002410 mbedtls_free( output_data );
2411 mbedtls_free( output_data2 );
2412 mbedtls_psa_crypto_free( );
2413}
2414/* END_CASE */
2415
2416/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002417void aead_encrypt( int key_type_arg, data_t *key_data,
2418 int alg_arg,
2419 data_t *nonce,
2420 data_t *additional_data,
2421 data_t *input_data,
2422 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002423{
2424 int slot = 1;
2425 psa_key_type_t key_type = key_type_arg;
2426 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002427 unsigned char *output_data = NULL;
2428 size_t output_size = 0;
2429 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002430 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002431 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002432
Gilles Peskinea1cac842018-06-11 19:33:02 +02002433 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002434 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002435 TEST_ASSERT( additional_data != NULL );
2436 TEST_ASSERT( nonce != NULL );
2437 TEST_ASSERT( expected_result != NULL );
2438 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2439 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2440 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2441 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2442 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2443
Gilles Peskine4abf7412018-06-18 16:35:34 +02002444 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002445 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002446
Gilles Peskinea1cac842018-06-11 19:33:02 +02002447 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2448
2449 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002450 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002451 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2452
2453 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002454 key_data->x,
2455 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002456
2457 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002458 nonce->x, nonce->len,
2459 additional_data->x, additional_data->len,
2460 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002461 output_data, output_size,
2462 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002463
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002464 ASSERT_COMPARE( expected_result->x, expected_result->len,
2465 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002466
Gilles Peskinea1cac842018-06-11 19:33:02 +02002467exit:
2468 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002469 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002470 mbedtls_psa_crypto_free( );
2471}
2472/* END_CASE */
2473
2474/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002475void aead_decrypt( int key_type_arg, data_t *key_data,
2476 int alg_arg,
2477 data_t *nonce,
2478 data_t *additional_data,
2479 data_t *input_data,
2480 data_t *expected_data,
2481 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002482{
2483 int slot = 1;
2484 psa_key_type_t key_type = key_type_arg;
2485 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002486 unsigned char *output_data = NULL;
2487 size_t output_size = 0;
2488 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002489 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002490 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002491 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002492
Gilles Peskinea1cac842018-06-11 19:33:02 +02002493 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002494 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002495 TEST_ASSERT( additional_data != NULL );
2496 TEST_ASSERT( nonce != NULL );
2497 TEST_ASSERT( expected_data != NULL );
2498 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2499 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2500 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2501 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2502 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2503
Gilles Peskine4abf7412018-06-18 16:35:34 +02002504 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002505 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002506
Gilles Peskinea1cac842018-06-11 19:33:02 +02002507 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2508
2509 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002510 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002511 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2512
2513 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002514 key_data->x,
2515 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002516
2517 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002518 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002519 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002520 additional_data->len,
2521 input_data->x, input_data->len,
2522 output_data, output_size,
2523 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002524
Gilles Peskine2d277862018-06-18 15:41:12 +02002525 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002526 ASSERT_COMPARE( expected_data->x, expected_data->len,
2527 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002528
Gilles Peskinea1cac842018-06-11 19:33:02 +02002529exit:
2530 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002531 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002532 mbedtls_psa_crypto_free( );
2533}
2534/* END_CASE */
2535
2536/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002537void signature_size( int type_arg,
2538 int bits,
2539 int alg_arg,
2540 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002541{
2542 psa_key_type_t type = type_arg;
2543 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002544 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002545 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2546exit:
2547 ;
2548}
2549/* END_CASE */
2550
2551/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002552void sign_deterministic( int key_type_arg, data_t *key_data,
2553 int alg_arg, data_t *input_data,
2554 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002555{
2556 int slot = 1;
2557 psa_key_type_t key_type = key_type_arg;
2558 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002559 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002560 unsigned char *signature = NULL;
2561 size_t signature_size;
2562 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002563 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002564
Gilles Peskine20035e32018-02-03 22:44:14 +01002565 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002566 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002567 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002568 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2569 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2570 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002571
2572 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2573
mohammad1603a97cb8c2018-03-28 03:46:26 -07002574 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002575 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002576 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2577
Gilles Peskine20035e32018-02-03 22:44:14 +01002578 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002579 key_data->x,
2580 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002581 TEST_ASSERT( psa_get_key_information( slot,
2582 NULL,
2583 &key_bits ) == PSA_SUCCESS );
2584
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002585 /* Allocate a buffer which has the size advertized by the
2586 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002587 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2588 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002589 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002590 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002591 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002592
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002593 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002594 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002595 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002596 signature, signature_size,
2597 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002598 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002599 ASSERT_COMPARE( output_data->x, output_data->len,
2600 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002601
2602exit:
2603 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002604 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002605 mbedtls_psa_crypto_free( );
2606}
2607/* END_CASE */
2608
2609/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002610void sign_fail( int key_type_arg, data_t *key_data,
2611 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002612 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002613{
2614 int slot = 1;
2615 psa_key_type_t key_type = key_type_arg;
2616 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002617 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002618 psa_status_t actual_status;
2619 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002620 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002621 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002622 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002623
Gilles Peskine20035e32018-02-03 22:44:14 +01002624 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002625 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002626 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2627 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2628
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002629 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002630
2631 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2632
mohammad1603a97cb8c2018-03-28 03:46:26 -07002633 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002634 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002635 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2636
Gilles Peskine20035e32018-02-03 22:44:14 +01002637 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002638 key_data->x,
2639 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002640
2641 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002642 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002643 signature, signature_size,
2644 &signature_length );
2645 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002646 /* The value of *signature_length is unspecified on error, but
2647 * whatever it is, it should be less than signature_size, so that
2648 * if the caller tries to read *signature_length bytes without
2649 * checking the error code then they don't overflow a buffer. */
2650 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002651
2652exit:
2653 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002654 mbedtls_free( signature );
2655 mbedtls_psa_crypto_free( );
2656}
2657/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002658
2659/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002660void sign_verify( int key_type_arg, data_t *key_data,
2661 int alg_arg, data_t *input_data )
2662{
2663 int slot = 1;
2664 psa_key_type_t key_type = key_type_arg;
2665 psa_algorithm_t alg = alg_arg;
2666 size_t key_bits;
2667 unsigned char *signature = NULL;
2668 size_t signature_size;
2669 size_t signature_length = 0xdeadbeef;
2670 psa_key_policy_t policy;
2671
2672 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2673
2674 psa_key_policy_init( &policy );
2675 psa_key_policy_set_usage( &policy,
2676 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2677 alg );
2678 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2679
2680 TEST_ASSERT( psa_import_key( slot, key_type,
2681 key_data->x,
2682 key_data->len ) == PSA_SUCCESS );
2683 TEST_ASSERT( psa_get_key_information( slot,
2684 NULL,
2685 &key_bits ) == PSA_SUCCESS );
2686
2687 /* Allocate a buffer which has the size advertized by the
2688 * library. */
2689 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2690 key_bits, alg );
2691 TEST_ASSERT( signature_size != 0 );
2692 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002693 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002694
2695 /* Perform the signature. */
2696 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2697 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002698 signature, signature_size,
2699 &signature_length ) == PSA_SUCCESS );
2700 /* Check that the signature length looks sensible. */
2701 TEST_ASSERT( signature_length <= signature_size );
2702 TEST_ASSERT( signature_length > 0 );
2703
2704 /* Use the library to verify that the signature is correct. */
2705 TEST_ASSERT( psa_asymmetric_verify(
2706 slot, alg,
2707 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002708 signature, signature_length ) == PSA_SUCCESS );
2709
2710 if( input_data->len != 0 )
2711 {
2712 /* Flip a bit in the input and verify that the signature is now
2713 * detected as invalid. Flip a bit at the beginning, not at the end,
2714 * because ECDSA may ignore the last few bits of the input. */
2715 input_data->x[0] ^= 1;
2716 TEST_ASSERT( psa_asymmetric_verify(
2717 slot, alg,
2718 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002719 signature,
2720 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2721 }
2722
2723exit:
2724 psa_destroy_key( slot );
2725 mbedtls_free( signature );
2726 mbedtls_psa_crypto_free( );
2727}
2728/* END_CASE */
2729
2730/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002731void asymmetric_verify( int key_type_arg, data_t *key_data,
2732 int alg_arg, data_t *hash_data,
2733 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002734{
2735 int slot = 1;
2736 psa_key_type_t key_type = key_type_arg;
2737 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002738 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002739
Gilles Peskine69c12672018-06-28 00:07:19 +02002740 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2741
itayzafrir5c753392018-05-08 11:18:38 +03002742 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002743 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002744 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002745 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2746 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2747 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002748
2749 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2750
2751 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002752 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002753 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2754
2755 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002756 key_data->x,
2757 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002758
2759 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002760 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002761 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002762 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002763exit:
2764 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002765 mbedtls_psa_crypto_free( );
2766}
2767/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002768
2769/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002770void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2771 int alg_arg, data_t *hash_data,
2772 data_t *signature_data,
2773 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002774{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002775 int slot = 1;
2776 psa_key_type_t key_type = key_type_arg;
2777 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002778 psa_status_t actual_status;
2779 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002780 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002781
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002782 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002783 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002784 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002785 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2786 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2787 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002788
2789 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2790
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002791 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002792 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002793 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2794
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002795 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002796 key_data->x,
2797 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002798
2799 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002800 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002801 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002802 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002803
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002804 TEST_ASSERT( actual_status == expected_status );
2805
2806exit:
2807 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002808 mbedtls_psa_crypto_free( );
2809}
2810/* END_CASE */
2811
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002812/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002813void asymmetric_encrypt( int key_type_arg,
2814 data_t *key_data,
2815 int alg_arg,
2816 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002817 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002818 int expected_output_length_arg,
2819 int expected_status_arg )
2820{
2821 int slot = 1;
2822 psa_key_type_t key_type = key_type_arg;
2823 psa_algorithm_t alg = alg_arg;
2824 size_t expected_output_length = expected_output_length_arg;
2825 size_t key_bits;
2826 unsigned char *output = NULL;
2827 size_t output_size;
2828 size_t output_length = ~0;
2829 psa_status_t actual_status;
2830 psa_status_t expected_status = expected_status_arg;
2831 psa_key_policy_t policy;
2832
2833 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2834
2835 /* Import the key */
2836 psa_key_policy_init( &policy );
2837 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2838 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2839 TEST_ASSERT( psa_import_key( slot, key_type,
2840 key_data->x,
2841 key_data->len ) == PSA_SUCCESS );
2842
2843 /* Determine the maximum output length */
2844 TEST_ASSERT( psa_get_key_information( slot,
2845 NULL,
2846 &key_bits ) == PSA_SUCCESS );
2847 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002848 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002849
2850 /* Encrypt the input */
2851 actual_status = psa_asymmetric_encrypt( slot, alg,
2852 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002853 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002854 output, output_size,
2855 &output_length );
2856 TEST_ASSERT( actual_status == expected_status );
2857 TEST_ASSERT( output_length == expected_output_length );
2858
Gilles Peskine68428122018-06-30 18:42:41 +02002859 /* If the label is empty, the test framework puts a non-null pointer
2860 * in label->x. Test that a null pointer works as well. */
2861 if( label->len == 0 )
2862 {
2863 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002864 if( output_size != 0 )
2865 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002866 actual_status = psa_asymmetric_encrypt( slot, alg,
2867 input_data->x, input_data->len,
2868 NULL, label->len,
2869 output, output_size,
2870 &output_length );
2871 TEST_ASSERT( actual_status == expected_status );
2872 TEST_ASSERT( output_length == expected_output_length );
2873 }
2874
Gilles Peskine656896e2018-06-29 19:12:28 +02002875exit:
2876 psa_destroy_key( slot );
2877 mbedtls_free( output );
2878 mbedtls_psa_crypto_free( );
2879}
2880/* END_CASE */
2881
2882/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002883void asymmetric_encrypt_decrypt( int key_type_arg,
2884 data_t *key_data,
2885 int alg_arg,
2886 data_t *input_data,
2887 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002888{
2889 int slot = 1;
2890 psa_key_type_t key_type = key_type_arg;
2891 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002892 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002894 size_t output_size;
2895 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002896 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002897 size_t output2_size;
2898 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002899 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002900
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002901 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002902 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002903 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2904 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2905
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002906 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2907
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002908 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002909 psa_key_policy_set_usage( &policy,
2910 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002911 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002912 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2913
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002914 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002915 key_data->x,
2916 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002917
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002918
2919 /* Determine the maximum ciphertext length */
2920 TEST_ASSERT( psa_get_key_information( slot,
2921 NULL,
2922 &key_bits ) == PSA_SUCCESS );
2923 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002924 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002925 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002926 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002927
Gilles Peskineeebd7382018-06-08 18:11:54 +02002928 /* We test encryption by checking that encrypt-then-decrypt gives back
2929 * the original plaintext because of the non-optional random
2930 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002931 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002932 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002933 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002934 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002935 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002936 /* We don't know what ciphertext length to expect, but check that
2937 * it looks sensible. */
2938 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002939
Gilles Peskine2d277862018-06-18 15:41:12 +02002940 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002941 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002942 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002943 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002944 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002945 ASSERT_COMPARE( input_data->x, input_data->len,
2946 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947
2948exit:
2949 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002950 mbedtls_free( output );
2951 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002952 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002953}
2954/* END_CASE */
2955
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002956/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002957void asymmetric_decrypt( int key_type_arg,
2958 data_t *key_data,
2959 int alg_arg,
2960 data_t *input_data,
2961 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002962 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963{
2964 int slot = 1;
2965 psa_key_type_t key_type = key_type_arg;
2966 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002967 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002968 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002969 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002970 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002971
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002972 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002973 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002974 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002975 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2976 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2977 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2978
Gilles Peskine4abf7412018-06-18 16:35:34 +02002979 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002980 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002981
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2983
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002984 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002985 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002986 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2987
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002989 key_data->x,
2990 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002991
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002992 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002993 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002994 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002995 output,
2996 output_size,
2997 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002998 ASSERT_COMPARE( expected_data->x, expected_data->len,
2999 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003000
Gilles Peskine68428122018-06-30 18:42:41 +02003001 /* If the label is empty, the test framework puts a non-null pointer
3002 * in label->x. Test that a null pointer works as well. */
3003 if( label->len == 0 )
3004 {
3005 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003006 if( output_size != 0 )
3007 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003008 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3009 input_data->x, input_data->len,
3010 NULL, label->len,
3011 output,
3012 output_size,
3013 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003014 ASSERT_COMPARE( expected_data->x, expected_data->len,
3015 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003016 }
3017
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003018exit:
3019 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003020 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003021 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003022}
3023/* END_CASE */
3024
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003026void asymmetric_decrypt_fail( int key_type_arg,
3027 data_t *key_data,
3028 int alg_arg,
3029 data_t *input_data,
3030 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003031 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033 int slot = 1;
3034 psa_key_type_t key_type = key_type_arg;
3035 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003036 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003037 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003038 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003039 psa_status_t actual_status;
3040 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003041 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003043 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003044 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003045 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3046 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3047
Gilles Peskine4abf7412018-06-18 16:35:34 +02003048 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003049 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003050
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003051 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3052
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003053 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003054 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003055 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3056
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003057 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003058 key_data->x,
3059 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060
Gilles Peskine2d277862018-06-18 15:41:12 +02003061 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003062 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003063 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003064 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003065 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003066 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003067 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068
Gilles Peskine68428122018-06-30 18:42:41 +02003069 /* If the label is empty, the test framework puts a non-null pointer
3070 * in label->x. Test that a null pointer works as well. */
3071 if( label->len == 0 )
3072 {
3073 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003074 if( output_size != 0 )
3075 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003076 actual_status = psa_asymmetric_decrypt( slot, alg,
3077 input_data->x, input_data->len,
3078 NULL, label->len,
3079 output, output_size,
3080 &output_length );
3081 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003082 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003083 }
3084
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085exit:
3086 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003087 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003088 mbedtls_psa_crypto_free( );
3089}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003090/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003091
3092/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003093void derive_setup( int key_type_arg,
3094 data_t *key_data,
3095 int alg_arg,
3096 data_t *salt,
3097 data_t *label,
3098 int requested_capacity_arg,
3099 int expected_status_arg )
3100{
3101 psa_key_slot_t slot = 1;
3102 size_t key_type = key_type_arg;
3103 psa_algorithm_t alg = alg_arg;
3104 size_t requested_capacity = requested_capacity_arg;
3105 psa_status_t expected_status = expected_status_arg;
3106 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3107 psa_key_policy_t policy;
3108
3109 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3110
3111 psa_key_policy_init( &policy );
3112 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3113 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3114
3115 TEST_ASSERT( psa_import_key( slot, key_type,
3116 key_data->x,
3117 key_data->len ) == PSA_SUCCESS );
3118
3119 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3120 salt->x, salt->len,
3121 label->x, label->len,
3122 requested_capacity ) == expected_status );
3123
3124exit:
3125 psa_generator_abort( &generator );
3126 psa_destroy_key( slot );
3127 mbedtls_psa_crypto_free( );
3128}
3129/* END_CASE */
3130
3131/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003132void derive_output( int alg_arg,
3133 data_t *key_data,
3134 data_t *salt,
3135 data_t *label,
3136 int requested_capacity_arg,
3137 data_t *expected_output1,
3138 data_t *expected_output2 )
3139{
3140 psa_key_slot_t slot = 1;
3141 psa_algorithm_t alg = alg_arg;
3142 size_t requested_capacity = requested_capacity_arg;
3143 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3144 uint8_t *expected_outputs[2] =
3145 {expected_output1->x, expected_output2->x};
3146 size_t output_sizes[2] =
3147 {expected_output1->len, expected_output2->len};
3148 size_t output_buffer_size = 0;
3149 uint8_t *output_buffer = NULL;
3150 size_t expected_capacity;
3151 size_t current_capacity;
3152 psa_key_policy_t policy;
3153 psa_status_t status;
3154 unsigned i;
3155
3156 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3157 {
3158 if( output_sizes[i] > output_buffer_size )
3159 output_buffer_size = output_sizes[i];
3160 if( output_sizes[i] == 0 )
3161 expected_outputs[i] = NULL;
3162 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003163 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003164 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3165
3166 psa_key_policy_init( &policy );
3167 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3168 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3169
3170 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3171 key_data->x,
3172 key_data->len ) == PSA_SUCCESS );
3173
3174 /* Extraction phase. */
3175 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3176 salt->x, salt->len,
3177 label->x, label->len,
3178 requested_capacity ) == PSA_SUCCESS );
3179 TEST_ASSERT( psa_get_generator_capacity( &generator,
3180 &current_capacity ) ==
3181 PSA_SUCCESS );
3182 TEST_ASSERT( current_capacity == requested_capacity );
3183 expected_capacity = requested_capacity;
3184
3185 /* Expansion phase. */
3186 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3187 {
3188 /* Read some bytes. */
3189 status = psa_generator_read( &generator,
3190 output_buffer, output_sizes[i] );
3191 if( expected_capacity == 0 && output_sizes[i] == 0 )
3192 {
3193 /* Reading 0 bytes when 0 bytes are available can go either way. */
3194 TEST_ASSERT( status == PSA_SUCCESS ||
3195 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3196 continue;
3197 }
3198 else if( expected_capacity == 0 ||
3199 output_sizes[i] > expected_capacity )
3200 {
3201 /* Capacity exceeded. */
3202 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3203 expected_capacity = 0;
3204 continue;
3205 }
3206 /* Success. Check the read data. */
3207 TEST_ASSERT( status == PSA_SUCCESS );
3208 if( output_sizes[i] != 0 )
3209 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3210 output_sizes[i] ) == 0 );
3211 /* Check the generator status. */
3212 expected_capacity -= output_sizes[i];
3213 TEST_ASSERT( psa_get_generator_capacity( &generator,
3214 &current_capacity ) ==
3215 PSA_SUCCESS );
3216 TEST_ASSERT( expected_capacity == current_capacity );
3217 }
3218 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3219
3220exit:
3221 mbedtls_free( output_buffer );
3222 psa_generator_abort( &generator );
3223 psa_destroy_key( slot );
3224 mbedtls_psa_crypto_free( );
3225}
3226/* END_CASE */
3227
3228/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003229void derive_full( int alg_arg,
3230 data_t *key_data,
3231 data_t *salt,
3232 data_t *label,
3233 int requested_capacity_arg )
3234{
3235 psa_key_slot_t slot = 1;
3236 psa_algorithm_t alg = alg_arg;
3237 size_t requested_capacity = requested_capacity_arg;
3238 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3239 unsigned char output_buffer[16];
3240 size_t expected_capacity = requested_capacity;
3241 size_t current_capacity;
3242 psa_key_policy_t policy;
3243
3244 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3245
3246 psa_key_policy_init( &policy );
3247 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3248 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3249
3250 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3251 key_data->x,
3252 key_data->len ) == PSA_SUCCESS );
3253
3254 /* Extraction phase. */
3255 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3256 salt->x, salt->len,
3257 label->x, label->len,
3258 requested_capacity ) == PSA_SUCCESS );
3259 TEST_ASSERT( psa_get_generator_capacity( &generator,
3260 &current_capacity ) ==
3261 PSA_SUCCESS );
3262 TEST_ASSERT( current_capacity == expected_capacity );
3263
3264 /* Expansion phase. */
3265 while( current_capacity > 0 )
3266 {
3267 size_t read_size = sizeof( output_buffer );
3268 if( read_size > current_capacity )
3269 read_size = current_capacity;
3270 TEST_ASSERT( psa_generator_read( &generator,
3271 output_buffer,
3272 read_size ) == PSA_SUCCESS );
3273 expected_capacity -= read_size;
3274 TEST_ASSERT( psa_get_generator_capacity( &generator,
3275 &current_capacity ) ==
3276 PSA_SUCCESS );
3277 TEST_ASSERT( current_capacity == expected_capacity );
3278 }
3279
3280 /* Check that the generator refuses to go over capacity. */
3281 TEST_ASSERT( psa_generator_read( &generator,
3282 output_buffer,
3283 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3284
3285 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3286
3287exit:
3288 psa_generator_abort( &generator );
3289 psa_destroy_key( slot );
3290 mbedtls_psa_crypto_free( );
3291}
3292/* END_CASE */
3293
3294/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003295void derive_key_exercise( int alg_arg,
3296 data_t *key_data,
3297 data_t *salt,
3298 data_t *label,
3299 int derived_type_arg,
3300 int derived_bits_arg,
3301 int derived_usage_arg,
3302 int derived_alg_arg )
3303{
3304 psa_key_slot_t base_key = 1;
3305 psa_key_slot_t derived_key = 2;
3306 psa_algorithm_t alg = alg_arg;
3307 psa_key_type_t derived_type = derived_type_arg;
3308 size_t derived_bits = derived_bits_arg;
3309 psa_key_usage_t derived_usage = derived_usage_arg;
3310 psa_algorithm_t derived_alg = derived_alg_arg;
3311 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3312 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3313 psa_key_policy_t policy;
3314 psa_key_type_t got_type;
3315 size_t got_bits;
3316
3317 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3318
3319 psa_key_policy_init( &policy );
3320 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3321 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3322 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3323 key_data->x,
3324 key_data->len ) == PSA_SUCCESS );
3325
3326 /* Derive a key. */
3327 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3328 salt->x, salt->len,
3329 label->x, label->len,
3330 capacity ) == PSA_SUCCESS );
3331 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3332 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3333 TEST_ASSERT( psa_generator_import_key( derived_key,
3334 derived_type,
3335 derived_bits,
3336 &generator ) == PSA_SUCCESS );
3337
3338 /* Test the key information */
3339 TEST_ASSERT( psa_get_key_information( derived_key,
3340 &got_type,
3341 &got_bits ) == PSA_SUCCESS );
3342 TEST_ASSERT( got_type == derived_type );
3343 TEST_ASSERT( got_bits == derived_bits );
3344
3345 /* Exercise the derived key. */
3346 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3347 goto exit;
3348
3349exit:
3350 psa_generator_abort( &generator );
3351 psa_destroy_key( base_key );
3352 psa_destroy_key( derived_key );
3353 mbedtls_psa_crypto_free( );
3354}
3355/* END_CASE */
3356
3357/* BEGIN_CASE */
3358void derive_key_export( int alg_arg,
3359 data_t *key_data,
3360 data_t *salt,
3361 data_t *label,
3362 int bytes1_arg,
3363 int bytes2_arg )
3364{
3365 psa_key_slot_t base_key = 1;
3366 psa_key_slot_t derived_key = 2;
3367 psa_algorithm_t alg = alg_arg;
3368 size_t bytes1 = bytes1_arg;
3369 size_t bytes2 = bytes2_arg;
3370 size_t capacity = bytes1 + bytes2;
3371 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003372 uint8_t *output_buffer = NULL;
3373 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003374 psa_key_policy_t policy;
3375 size_t length;
3376
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003377 ASSERT_ALLOC( output_buffer, capacity );
3378 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003379 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3380
3381 psa_key_policy_init( &policy );
3382 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3383 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3384 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3385 key_data->x,
3386 key_data->len ) == PSA_SUCCESS );
3387
3388 /* Derive some material and output it. */
3389 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3390 salt->x, salt->len,
3391 label->x, label->len,
3392 capacity ) == PSA_SUCCESS );
3393 TEST_ASSERT( psa_generator_read( &generator,
3394 output_buffer,
3395 capacity ) == PSA_SUCCESS );
3396 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3397
3398 /* Derive the same output again, but this time store it in key objects. */
3399 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3400 salt->x, salt->len,
3401 label->x, label->len,
3402 capacity ) == PSA_SUCCESS );
3403 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3404 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3405 TEST_ASSERT( psa_generator_import_key( derived_key,
3406 PSA_KEY_TYPE_RAW_DATA,
3407 PSA_BYTES_TO_BITS( bytes1 ),
3408 &generator ) == PSA_SUCCESS );
3409 TEST_ASSERT( psa_export_key( derived_key,
3410 export_buffer, bytes1,
3411 &length ) == PSA_SUCCESS );
3412 TEST_ASSERT( length == bytes1 );
3413 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3414 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3415 TEST_ASSERT( psa_generator_import_key( derived_key,
3416 PSA_KEY_TYPE_RAW_DATA,
3417 PSA_BYTES_TO_BITS( bytes2 ),
3418 &generator ) == PSA_SUCCESS );
3419 TEST_ASSERT( psa_export_key( derived_key,
3420 export_buffer + bytes1, bytes2,
3421 &length ) == PSA_SUCCESS );
3422 TEST_ASSERT( length == bytes2 );
3423
3424 /* Compare the outputs from the two runs. */
3425 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3426
3427exit:
3428 mbedtls_free( output_buffer );
3429 mbedtls_free( export_buffer );
3430 psa_generator_abort( &generator );
3431 psa_destroy_key( base_key );
3432 psa_destroy_key( derived_key );
3433 mbedtls_psa_crypto_free( );
3434}
3435/* END_CASE */
3436
3437/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003438void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003439{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003440 size_t bytes = bytes_arg;
3441 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003442 unsigned char *output = NULL;
3443 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003444 size_t i;
3445 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003446
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003447 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3448 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003449 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003450
3451 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3452
Gilles Peskinea50d7392018-06-21 10:22:13 +02003453 /* Run several times, to ensure that every output byte will be
3454 * nonzero at least once with overwhelming probability
3455 * (2^(-8*number_of_runs)). */
3456 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003457 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003458 if( bytes != 0 )
3459 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003460 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3461
3462 /* Check that no more than bytes have been overwritten */
3463 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3464
3465 for( i = 0; i < bytes; i++ )
3466 {
3467 if( output[i] != 0 )
3468 ++changed[i];
3469 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003470 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003471
3472 /* Check that every byte was changed to nonzero at least once. This
3473 * validates that psa_generate_random is overwriting every byte of
3474 * the output buffer. */
3475 for( i = 0; i < bytes; i++ )
3476 {
3477 TEST_ASSERT( changed[i] != 0 );
3478 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003479
3480exit:
3481 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003482 mbedtls_free( output );
3483 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003484}
3485/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003486
3487/* BEGIN_CASE */
3488void generate_key( int type_arg,
3489 int bits_arg,
3490 int usage_arg,
3491 int alg_arg,
3492 int expected_status_arg )
3493{
3494 int slot = 1;
3495 psa_key_type_t type = type_arg;
3496 psa_key_usage_t usage = usage_arg;
3497 size_t bits = bits_arg;
3498 psa_algorithm_t alg = alg_arg;
3499 psa_status_t expected_status = expected_status_arg;
3500 psa_key_type_t got_type;
3501 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003502 psa_status_t expected_info_status =
3503 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3504 psa_key_policy_t policy;
3505
3506 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3507
3508 psa_key_policy_init( &policy );
3509 psa_key_policy_set_usage( &policy, usage, alg );
3510 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3511
3512 /* Generate a key */
3513 TEST_ASSERT( psa_generate_key( slot, type, bits,
3514 NULL, 0 ) == expected_status );
3515
3516 /* Test the key information */
3517 TEST_ASSERT( psa_get_key_information( slot,
3518 &got_type,
3519 &got_bits ) == expected_info_status );
3520 if( expected_info_status != PSA_SUCCESS )
3521 goto exit;
3522 TEST_ASSERT( got_type == type );
3523 TEST_ASSERT( got_bits == bits );
3524
Gilles Peskine818ca122018-06-20 18:16:48 +02003525 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003526 if( ! exercise_key( slot, usage, alg ) )
3527 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003528
3529exit:
3530 psa_destroy_key( slot );
3531 mbedtls_psa_crypto_free( );
3532}
3533/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003534
3535/* BEGIN_CASE */
3536void validate_module_init_generate_random( )
3537{
3538 psa_status_t status;
3539 uint8_t random[10] = { 0 };
3540 status = psa_generate_random( random, sizeof( random ) );
3541 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3542}
3543/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003544
3545/* BEGIN_CASE */
3546void validate_module_init_key_based( )
3547{
3548 psa_status_t status;
3549 uint8_t data[10] = { 0 };
3550 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3551 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3552}
3553/* END_CASE */