blob: 001869e3e240996c57bfffc7abd700e4a93919c4 [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 Peskine5b802a32018-10-29 19:21:41 +0100567 /* Just the secret value */
568 TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) );
569 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200571#endif /* MBEDTLS_ECP_C */
572
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200573 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
574 {
575 uint8_t *p = exported;
576 uint8_t *end = exported + exported_length;
577 size_t len;
578 mbedtls_asn1_buf alg;
579 mbedtls_asn1_buf params;
580 mbedtls_asn1_bitstring bitstring;
581 /* SubjectPublicKeyInfo ::= SEQUENCE {
582 * algorithm AlgorithmIdentifier,
583 * subjectPublicKey BIT STRING }
584 * AlgorithmIdentifier ::= SEQUENCE {
585 * algorithm OBJECT IDENTIFIER,
586 * parameters ANY DEFINED BY algorithm OPTIONAL }
587 */
588 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
589 MBEDTLS_ASN1_SEQUENCE |
590 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
591 TEST_ASSERT( p + len == end );
592 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
593 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
594 goto exit;
595 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
596 TEST_ASSERT( p == end );
597 p = bitstring.p;
598#if defined(MBEDTLS_RSA_C)
599 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
600 {
601 /* RSAPublicKey ::= SEQUENCE {
602 * modulus INTEGER, -- n
603 * publicExponent INTEGER } -- e
604 */
605 TEST_ASSERT( bitstring.unused_bits == 0 );
606 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
607 MBEDTLS_ASN1_SEQUENCE |
608 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
609 TEST_ASSERT( p + len == end );
610 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
611 goto exit;
612 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
613 goto exit;
614 TEST_ASSERT( p == end );
615 }
616 else
617#endif /* MBEDTLS_RSA_C */
618#if defined(MBEDTLS_ECP_C)
619 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
620 {
621 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200622 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 * -- then x_P as an n-bit string, big endian;
624 * -- then y_P as a n-bit string, big endian,
625 * -- where n is the order of the curve.
626 */
627 TEST_ASSERT( bitstring.unused_bits == 0 );
628 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
629 TEST_ASSERT( p[0] == 4 );
630 }
631 else
632#endif /* MBEDTLS_ECP_C */
633 {
634 char message[40];
635 mbedtls_snprintf( message, sizeof( message ),
636 "No sanity check for public key type=0x%08lx",
637 (unsigned long) type );
638 test_fail( message, __LINE__, __FILE__ );
639 return( 0 );
640 }
641 }
642 else
643
644 {
645 /* No sanity checks for other types */
646 }
647
648 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200649
650exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200651 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200652}
653
654static int exercise_export_key( psa_key_slot_t slot,
655 psa_key_usage_t usage )
656{
657 psa_key_type_t type;
658 size_t bits;
659 uint8_t *exported = NULL;
660 size_t exported_size = 0;
661 size_t exported_length = 0;
662 int ok = 0;
663
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200664 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
665
666 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
667 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200668 {
669 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
670 PSA_ERROR_NOT_PERMITTED );
671 return( 1 );
672 }
673
Gilles Peskined14664a2018-08-10 19:07:32 +0200674 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200675 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200676
677 TEST_ASSERT( psa_export_key( slot,
678 exported, exported_size,
679 &exported_length ) == PSA_SUCCESS );
680 ok = exported_key_sanity_check( type, bits, exported, exported_length );
681
682exit:
683 mbedtls_free( exported );
684 return( ok );
685}
686
687static int exercise_export_public_key( psa_key_slot_t slot )
688{
689 psa_key_type_t type;
690 psa_key_type_t public_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
697 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
698 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
699 {
700 TEST_ASSERT( psa_export_public_key( slot,
701 NULL, 0, &exported_length ) ==
702 PSA_ERROR_INVALID_ARGUMENT );
703 return( 1 );
704 }
705
706 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
707 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_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_public_key( slot,
711 exported, exported_size,
712 &exported_length ) == PSA_SUCCESS );
713 ok = exported_key_sanity_check( public_type, bits,
714 exported, exported_length );
715
716exit:
717 mbedtls_free( exported );
718 return( ok );
719}
720
Gilles Peskine02b75072018-07-01 22:31:34 +0200721static int exercise_key( psa_key_slot_t slot,
722 psa_key_usage_t usage,
723 psa_algorithm_t alg )
724{
725 int ok;
726 if( alg == 0 )
727 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
728 else if( PSA_ALG_IS_MAC( alg ) )
729 ok = exercise_mac_key( slot, usage, alg );
730 else if( PSA_ALG_IS_CIPHER( alg ) )
731 ok = exercise_cipher_key( slot, usage, alg );
732 else if( PSA_ALG_IS_AEAD( alg ) )
733 ok = exercise_aead_key( slot, usage, alg );
734 else if( PSA_ALG_IS_SIGN( alg ) )
735 ok = exercise_signature_key( slot, usage, alg );
736 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
737 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200738 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
739 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200740 else
741 {
742 char message[40];
743 mbedtls_snprintf( message, sizeof( message ),
744 "No code to exercise alg=0x%08lx",
745 (unsigned long) alg );
746 test_fail( message, __LINE__, __FILE__ );
747 ok = 0;
748 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200749
750 ok = ok && exercise_export_key( slot, usage );
751 ok = ok && exercise_export_public_key( slot );
752
Gilles Peskine02b75072018-07-01 22:31:34 +0200753 return( ok );
754}
755
Gilles Peskinee59236f2018-01-27 23:32:46 +0100756/* END_HEADER */
757
758/* BEGIN_DEPENDENCIES
759 * depends_on:MBEDTLS_PSA_CRYPTO_C
760 * END_DEPENDENCIES
761 */
762
763/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200764void static_checks( )
765{
766 size_t max_truncated_mac_size =
767 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
768
769 /* Check that the length for a truncated MAC always fits in the algorithm
770 * encoding. The shifted mask is the maximum truncated value. The
771 * untruncated algorithm may be one byte larger. */
772 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
773}
774/* END_CASE */
775
776/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200777void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100778{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100780 int i;
781 for( i = 0; i <= 1; i++ )
782 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 status = psa_crypto_init( );
784 TEST_ASSERT( status == PSA_SUCCESS );
785 status = psa_crypto_init( );
786 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100787 mbedtls_psa_crypto_free( );
788 }
789}
790/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791
792/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200793void fill_slots( int max_arg )
794{
795 /* Fill all the slots until we run out of memory or out of slots,
796 * or until some limit specified in the test data for the sake of
797 * implementations with an essentially unlimited number of slots.
798 * This test assumes that available slots are numbered from 1. */
799
800 psa_key_slot_t slot;
801 psa_key_slot_t max = 0;
802 psa_key_policy_t policy;
803 uint8_t exported[sizeof( max )];
804 size_t exported_size;
805 psa_status_t status;
806
807 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
808
809 psa_key_policy_init( &policy );
810 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
811
812 for( max = 1; max <= (size_t) max_arg; max++ )
813 {
814 status = psa_set_key_policy( max, &policy );
815 /* Stop filling slots if we run out of memory or out of
816 * available slots. */
817 TEST_ASSERT( status == PSA_SUCCESS ||
818 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
819 status == PSA_ERROR_INVALID_ARGUMENT );
820 if( status != PSA_SUCCESS )
821 break;
822 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
823 (uint8_t*) &max, sizeof( max ) );
824 /* Since psa_set_key_policy succeeded, we know that the slot
825 * number is valid. But we may legitimately run out of memory. */
826 TEST_ASSERT( status == PSA_SUCCESS ||
827 status == PSA_ERROR_INSUFFICIENT_MEMORY );
828 if( status != PSA_SUCCESS )
829 break;
830 }
831 /* `max` is now the first slot number that wasn't filled. */
832 max -= 1;
833
834 for( slot = 1; slot <= max; slot++ )
835 {
836 TEST_ASSERT( psa_export_key( slot,
837 exported, sizeof( exported ),
838 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200839 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200840 }
841
842exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200843 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200844 mbedtls_psa_crypto_free( );
845}
846/* END_CASE */
847
848/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200849void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850{
851 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200852 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100854
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100857 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
858
Gilles Peskine4abf7412018-06-18 16:35:34 +0200859 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200860 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861 if( status == PSA_SUCCESS )
862 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
863
864exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865 mbedtls_psa_crypto_free( );
866}
867/* END_CASE */
868
869/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200870void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
871{
872 int slot = 1;
873 size_t bits = bits_arg;
874 psa_status_t expected_status = expected_status_arg;
875 psa_status_t status;
876 psa_key_type_t type =
877 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
878 size_t buffer_size = /* Slight overapproximations */
879 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200880 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200881 unsigned char *p;
882 int ret;
883 size_t length;
884
885 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200886 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200887
888 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
889 bits, keypair ) ) >= 0 );
890 length = ret;
891
892 /* Try importing the key */
893 status = psa_import_key( slot, type, p, length );
894 TEST_ASSERT( status == expected_status );
895 if( status == PSA_SUCCESS )
896 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
897
898exit:
899 mbedtls_free( buffer );
900 mbedtls_psa_crypto_free( );
901}
902/* END_CASE */
903
904/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300905void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300906 int type_arg,
907 int alg_arg,
908 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100909 int expected_bits,
910 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200911 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100912 int canonical_input )
913{
914 int slot = 1;
915 int slot2 = slot + 1;
916 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200917 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200918 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100919 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100920 unsigned char *exported = NULL;
921 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100922 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100923 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100924 size_t reexported_length;
925 psa_key_type_t got_type;
926 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200927 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100929 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300930 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300931 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200932 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100933 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200934 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100935 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
936
mohammad1603a97cb8c2018-03-28 03:46:26 -0700937 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200938 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700939 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
940
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 /* Import the key */
942 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200943 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944
945 /* Test the key information */
946 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200947 &got_type,
948 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949 TEST_ASSERT( got_type == type );
950 TEST_ASSERT( got_bits == (size_t) expected_bits );
951
952 /* Export the key */
953 status = psa_export_key( slot,
954 exported, export_size,
955 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200956 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100957
958 /* The exported length must be set by psa_export_key() to a value between 0
959 * and export_size. On errors, the exported length must be 0. */
960 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
961 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
962 TEST_ASSERT( exported_length <= export_size );
963
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200964 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200965 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200967 {
968 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200970 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971
Gilles Peskine8f609232018-08-11 01:24:55 +0200972 if( ! exercise_export_key( slot, usage_arg ) )
973 goto exit;
974
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200976 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 else
978 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700979 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
980
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200982 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +0200983 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200985 reexported,
986 export_size,
987 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200988 ASSERT_COMPARE( exported, exported_length,
989 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100991 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
993destroy:
994 /* Destroy the key */
995 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
996 TEST_ASSERT( psa_get_key_information(
997 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
998
999exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001000 mbedtls_free( exported );
1001 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 mbedtls_psa_crypto_free( );
1003}
1004/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001005
Moran Pekerf709f4a2018-06-06 17:26:04 +03001006/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001007void import_key_nonempty_slot( )
1008{
1009 int slot = 1;
1010 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1011 psa_status_t status;
1012 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1013 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1014
1015 /* Import the key */
1016 TEST_ASSERT( psa_import_key( slot, type,
1017 data, sizeof( data ) ) == PSA_SUCCESS );
1018
1019 /* Import the key again */
1020 status = psa_import_key( slot, type, data, sizeof( data ) );
1021 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1022
1023exit:
1024 mbedtls_psa_crypto_free( );
1025}
1026/* END_CASE */
1027
1028/* BEGIN_CASE */
1029void export_invalid_slot( int slot, int expected_export_status_arg )
1030{
1031 psa_status_t status;
1032 unsigned char *exported = NULL;
1033 size_t export_size = 0;
1034 size_t exported_length = INVALID_EXPORT_LENGTH;
1035 psa_status_t expected_export_status = expected_export_status_arg;
1036
1037 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1038
1039 /* Export the key */
1040 status = psa_export_key( slot,
1041 exported, export_size,
1042 &exported_length );
1043 TEST_ASSERT( status == expected_export_status );
1044
1045exit:
1046 mbedtls_psa_crypto_free( );
1047}
1048/* END_CASE */
1049
1050/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001051void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001052 int type_arg,
1053 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001054 int export_size_delta,
1055 int expected_export_status_arg,
1056 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001057{
1058 int slot = 1;
1059 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001060 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001061 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001062 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001063 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001064 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001065 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001066 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001067
Moran Pekerf709f4a2018-06-06 17:26:04 +03001068 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1069
1070 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001071 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001072 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1073
1074 /* Import the key */
1075 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001076 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001077
Gilles Peskine49c25912018-10-29 15:15:31 +01001078 /* Export the public key */
1079 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001080 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001081 exported, export_size,
1082 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001083 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001084 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001085 {
1086 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1087 size_t bits;
1088 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1089 PSA_SUCCESS );
1090 TEST_ASSERT( expected_public_key->len <=
1091 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001092 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1093 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001094 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001095
1096exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001097 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001098 psa_destroy_key( slot );
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 */
itayzafrirf86548d2018-11-01 10:44:32 +02001571void hash_bad_order( )
1572{
1573 unsigned char input[] = "";
1574 /* SHA-256 hash of an empty string */
1575 unsigned char hash[] = {
1576 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1577 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1578 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1579 size_t hash_len;
1580 psa_hash_operation_t operation;
1581
1582 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1583
1584 /* psa_hash_update without calling psa_hash_setup beforehand */
1585 memset( &operation, 0, sizeof( operation ) );
1586 TEST_ASSERT( psa_hash_update( &operation,
1587 input, sizeof( input ) ) ==
1588 PSA_ERROR_INVALID_ARGUMENT );
1589
1590 /* psa_hash_verify without calling psa_hash_setup beforehand */
1591 memset( &operation, 0, sizeof( operation ) );
1592 TEST_ASSERT( psa_hash_verify( &operation,
1593 hash, sizeof( hash ) ) ==
1594 PSA_ERROR_INVALID_ARGUMENT );
1595
1596 /* psa_hash_finish without calling psa_hash_setup beforehand */
1597 memset( &operation, 0, sizeof( operation ) );
1598 TEST_ASSERT( psa_hash_finish( &operation,
1599 hash, sizeof( hash ), &hash_len ) ==
1600 PSA_ERROR_INVALID_ARGUMENT );
1601
1602exit:
1603 mbedtls_psa_crypto_free( );
1604}
1605/* END_CASE */
1606
itayzafrir27e69452018-11-01 14:26:34 +02001607/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1608void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001609{
1610 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001611 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1612 * appended to it */
1613 unsigned char hash[] = {
1614 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1615 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1616 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001617 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001618 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001619
1620 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1621
itayzafrir27e69452018-11-01 14:26:34 +02001622 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001623 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1624 TEST_ASSERT( psa_hash_verify( &operation,
1625 hash, expected_size - 1 ) ==
1626 PSA_ERROR_INVALID_SIGNATURE );
1627
itayzafrir27e69452018-11-01 14:26:34 +02001628 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001629 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001630 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001631 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001632 PSA_ERROR_INVALID_SIGNATURE );
1633
itayzafrir27e69452018-11-01 14:26:34 +02001634 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001635 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001636 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001637 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001638 PSA_ERROR_INVALID_SIGNATURE );
1639
itayzafrirec93d302018-10-18 18:01:10 +03001640exit:
1641 mbedtls_psa_crypto_free( );
1642}
1643/* END_CASE */
1644
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001645/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1646void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001647{
1648 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001649 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001650 size_t expected_size = PSA_HASH_SIZE( alg );
1651 psa_hash_operation_t operation;
1652 size_t hash_len;
1653
1654 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1655
itayzafrir58028322018-10-25 10:22:01 +03001656 /* psa_hash_finish with a smaller hash buffer than expected */
1657 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1658 TEST_ASSERT( psa_hash_finish( &operation,
1659 hash, expected_size - 1,
1660 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1661
1662exit:
1663 mbedtls_psa_crypto_free( );
1664}
1665/* END_CASE */
1666
1667/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001668void mac_setup( int key_type_arg,
1669 data_t *key,
1670 int alg_arg,
1671 int expected_status_arg )
1672{
1673 int key_slot = 1;
1674 psa_key_type_t key_type = key_type_arg;
1675 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001676 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001677 psa_mac_operation_t operation;
1678 psa_key_policy_t policy;
1679 psa_status_t status;
1680
1681 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1682
1683 psa_key_policy_init( &policy );
1684 psa_key_policy_set_usage( &policy,
1685 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1686 alg );
1687 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1688
1689 TEST_ASSERT( psa_import_key( key_slot, key_type,
1690 key->x, key->len ) == PSA_SUCCESS );
1691
Gilles Peskine89167cb2018-07-08 20:12:23 +02001692 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001693 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001694 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001695
1696exit:
1697 psa_destroy_key( key_slot );
1698 mbedtls_psa_crypto_free( );
1699}
1700/* END_CASE */
1701
1702/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001703void mac_sign( int key_type_arg,
1704 data_t *key,
1705 int alg_arg,
1706 data_t *input,
1707 data_t *expected_mac )
1708{
1709 int key_slot = 1;
1710 psa_key_type_t key_type = key_type_arg;
1711 psa_algorithm_t alg = alg_arg;
1712 psa_mac_operation_t operation;
1713 psa_key_policy_t policy;
1714 /* Leave a little extra room in the output buffer. At the end of the
1715 * test, we'll check that the implementation didn't overwrite onto
1716 * this extra room. */
1717 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1718 size_t mac_buffer_size =
1719 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1720 size_t mac_length = 0;
1721
1722 memset( actual_mac, '+', sizeof( actual_mac ) );
1723 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1724 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1725
1726 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1727
1728 psa_key_policy_init( &policy );
1729 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1730 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1731
1732 TEST_ASSERT( psa_import_key( key_slot, key_type,
1733 key->x, key->len ) == PSA_SUCCESS );
1734
1735 /* Calculate the MAC. */
1736 TEST_ASSERT( psa_mac_sign_setup( &operation,
1737 key_slot, alg ) == PSA_SUCCESS );
1738 TEST_ASSERT( psa_mac_update( &operation,
1739 input->x, input->len ) == PSA_SUCCESS );
1740 TEST_ASSERT( psa_mac_sign_finish( &operation,
1741 actual_mac, mac_buffer_size,
1742 &mac_length ) == PSA_SUCCESS );
1743
1744 /* Compare with the expected value. */
1745 TEST_ASSERT( mac_length == expected_mac->len );
1746 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1747
1748 /* Verify that the end of the buffer is untouched. */
1749 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1750 sizeof( actual_mac ) - mac_length ) );
1751
1752exit:
1753 psa_destroy_key( key_slot );
1754 mbedtls_psa_crypto_free( );
1755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001759void mac_verify( int key_type_arg,
1760 data_t *key,
1761 int alg_arg,
1762 data_t *input,
1763 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764{
1765 int key_slot = 1;
1766 psa_key_type_t key_type = key_type_arg;
1767 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001768 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001769 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001770
Gilles Peskine69c12672018-06-28 00:07:19 +02001771 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1772
Gilles Peskine8c9def32018-02-08 10:02:12 +01001773 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001774 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001775 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001776 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001777 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1778 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001779
1780 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1781
mohammad16036df908f2018-04-02 08:34:15 -07001782 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001783 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001784 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1785
Gilles Peskine8c9def32018-02-08 10:02:12 +01001786 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001787 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001788
Gilles Peskine89167cb2018-07-08 20:12:23 +02001789 TEST_ASSERT( psa_mac_verify_setup( &operation,
1790 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001791 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1792 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001793 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001794 TEST_ASSERT( psa_mac_verify_finish( &operation,
1795 expected_mac->x,
1796 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001797
1798exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001799 psa_destroy_key( key_slot );
1800 mbedtls_psa_crypto_free( );
1801}
1802/* END_CASE */
1803
1804/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001805void cipher_setup( int key_type_arg,
1806 data_t *key,
1807 int alg_arg,
1808 int expected_status_arg )
1809{
1810 int key_slot = 1;
1811 psa_key_type_t key_type = key_type_arg;
1812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001813 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001814 psa_cipher_operation_t operation;
1815 psa_key_policy_t policy;
1816 psa_status_t status;
1817
1818 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1819
1820 psa_key_policy_init( &policy );
1821 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1822 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1823
1824 TEST_ASSERT( psa_import_key( key_slot, key_type,
1825 key->x, key->len ) == PSA_SUCCESS );
1826
Gilles Peskinefe119512018-07-08 21:39:34 +02001827 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001828 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001829 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001830
1831exit:
1832 psa_destroy_key( key_slot );
1833 mbedtls_psa_crypto_free( );
1834}
1835/* END_CASE */
1836
1837/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001838void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001839 data_t *key,
1840 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001841 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001842{
1843 int key_slot = 1;
1844 psa_status_t status;
1845 psa_key_type_t key_type = key_type_arg;
1846 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001847 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001848 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001849 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001850 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001851 size_t output_buffer_size = 0;
1852 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001853 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001854 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001855 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001856
Gilles Peskine50e586b2018-06-08 14:28:46 +02001857 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001858 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001859 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001860 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1861 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1862 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001863
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001864 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1865 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001866
1867 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1868
Moran Pekered346952018-07-05 15:22:45 +03001869 psa_key_policy_init( &policy );
1870 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1871 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1872
Gilles Peskine50e586b2018-06-08 14:28:46 +02001873 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001874 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001875
Gilles Peskinefe119512018-07-08 21:39:34 +02001876 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1877 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001878
Gilles Peskinefe119512018-07-08 21:39:34 +02001879 TEST_ASSERT( psa_cipher_set_iv( &operation,
1880 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001881 output_buffer_size = (size_t) input->len +
1882 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001883 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001884
Gilles Peskine4abf7412018-06-18 16:35:34 +02001885 TEST_ASSERT( psa_cipher_update( &operation,
1886 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001887 output, output_buffer_size,
1888 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001889 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001890 status = psa_cipher_finish( &operation,
1891 output + function_output_length,
1892 output_buffer_size,
1893 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001894 total_output_length += function_output_length;
1895
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001896 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001897 if( expected_status == PSA_SUCCESS )
1898 {
1899 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001900 ASSERT_COMPARE( expected_output->x, expected_output->len,
1901 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001902 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001903
Gilles Peskine50e586b2018-06-08 14:28:46 +02001904exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001905 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001906 psa_destroy_key( key_slot );
1907 mbedtls_psa_crypto_free( );
1908}
1909/* END_CASE */
1910
1911/* BEGIN_CASE */
1912void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001913 data_t *key,
1914 data_t *input,
1915 int first_part_size,
1916 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001917{
1918 int key_slot = 1;
1919 psa_key_type_t key_type = key_type_arg;
1920 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001921 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001922 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001923 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001924 size_t output_buffer_size = 0;
1925 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001926 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001927 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001928 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001929
Gilles Peskine50e586b2018-06-08 14:28:46 +02001930 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001931 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001932 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001933 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1934 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1935 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001936
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001937 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1938 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001939
1940 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1941
Moran Pekered346952018-07-05 15:22:45 +03001942 psa_key_policy_init( &policy );
1943 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1944 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1945
Gilles Peskine50e586b2018-06-08 14:28:46 +02001946 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001947 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001948
Gilles Peskinefe119512018-07-08 21:39:34 +02001949 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1950 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001951
Gilles Peskinefe119512018-07-08 21:39:34 +02001952 TEST_ASSERT( psa_cipher_set_iv( &operation,
1953 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001954 output_buffer_size = (size_t) input->len +
1955 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001956 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001957
Gilles Peskine4abf7412018-06-18 16:35:34 +02001958 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001959 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001960 output, output_buffer_size,
1961 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001962 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001963 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001964 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001965 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001966 output, output_buffer_size,
1967 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001968 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001969 TEST_ASSERT( psa_cipher_finish( &operation,
1970 output + function_output_length,
1971 output_buffer_size,
1972 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001973 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001974 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1975
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001976 ASSERT_COMPARE( expected_output->x, expected_output->len,
1977 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001978
1979exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001980 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001981 psa_destroy_key( key_slot );
1982 mbedtls_psa_crypto_free( );
1983}
1984/* END_CASE */
1985
1986/* BEGIN_CASE */
1987void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001988 data_t *key,
1989 data_t *input,
1990 int first_part_size,
1991 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001992{
1993 int key_slot = 1;
1994
1995 psa_key_type_t key_type = key_type_arg;
1996 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001997 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001998 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001999 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002000 size_t output_buffer_size = 0;
2001 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002002 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002003 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002004 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002005
Gilles Peskine50e586b2018-06-08 14:28:46 +02002006 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002007 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002008 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002009 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2010 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2011 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002012
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002013 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2014 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002015
2016 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2017
Moran Pekered346952018-07-05 15:22:45 +03002018 psa_key_policy_init( &policy );
2019 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2020 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2021
Gilles Peskine50e586b2018-06-08 14:28:46 +02002022 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002023 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002024
Gilles Peskinefe119512018-07-08 21:39:34 +02002025 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2026 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002027
Gilles Peskinefe119512018-07-08 21:39:34 +02002028 TEST_ASSERT( psa_cipher_set_iv( &operation,
2029 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002030
mohammad16033d91abe2018-07-03 13:15:54 +03002031 output_buffer_size = (size_t) input->len +
2032 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002033 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034
Gilles Peskine4abf7412018-06-18 16:35:34 +02002035 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2036 TEST_ASSERT( psa_cipher_update( &operation,
2037 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002038 output, output_buffer_size,
2039 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002040 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002041 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002042 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002043 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002044 output, output_buffer_size,
2045 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002046 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002047 TEST_ASSERT( psa_cipher_finish( &operation,
2048 output + function_output_length,
2049 output_buffer_size,
2050 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002051 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002052 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2053
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002054 ASSERT_COMPARE( expected_output->x, expected_output->len,
2055 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002056
2057exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002058 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002059 psa_destroy_key( key_slot );
2060 mbedtls_psa_crypto_free( );
2061}
2062/* END_CASE */
2063
Gilles Peskine50e586b2018-06-08 14:28:46 +02002064/* BEGIN_CASE */
2065void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002066 data_t *key,
2067 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002068 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002069{
2070 int key_slot = 1;
2071 psa_status_t status;
2072 psa_key_type_t key_type = key_type_arg;
2073 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002074 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002075 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002076 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002077 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002078 size_t output_buffer_size = 0;
2079 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002080 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002081 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002082 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002083
Gilles Peskine50e586b2018-06-08 14:28:46 +02002084 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002085 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002086 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002087 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2088 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2089 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002090
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002091 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2092 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002093
2094 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2095
Moran Pekered346952018-07-05 15:22:45 +03002096 psa_key_policy_init( &policy );
2097 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2098 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2099
Gilles Peskine50e586b2018-06-08 14:28:46 +02002100 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002101 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002102
Gilles Peskinefe119512018-07-08 21:39:34 +02002103 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2104 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002105
Gilles Peskinefe119512018-07-08 21:39:34 +02002106 TEST_ASSERT( psa_cipher_set_iv( &operation,
2107 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002108
mohammad16033d91abe2018-07-03 13:15:54 +03002109 output_buffer_size = (size_t) input->len +
2110 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002111 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002112
Gilles Peskine4abf7412018-06-18 16:35:34 +02002113 TEST_ASSERT( psa_cipher_update( &operation,
2114 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002115 output, output_buffer_size,
2116 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002117 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002118 status = psa_cipher_finish( &operation,
2119 output + function_output_length,
2120 output_buffer_size,
2121 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002122 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002123 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002124
2125 if( expected_status == PSA_SUCCESS )
2126 {
2127 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002128 ASSERT_COMPARE( expected_output->x, expected_output->len,
2129 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002130 }
2131
Gilles Peskine50e586b2018-06-08 14:28:46 +02002132exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002133 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002134 psa_destroy_key( key_slot );
2135 mbedtls_psa_crypto_free( );
2136}
2137/* END_CASE */
2138
Gilles Peskine50e586b2018-06-08 14:28:46 +02002139/* BEGIN_CASE */
2140void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002141 data_t *key,
2142 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002143{
2144 int key_slot = 1;
2145 psa_key_type_t key_type = key_type_arg;
2146 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002147 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002148 size_t iv_size = 16;
2149 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002150 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002151 size_t output1_size = 0;
2152 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002153 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002154 size_t output2_size = 0;
2155 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002156 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002157 psa_cipher_operation_t operation1;
2158 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002159 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002160
mohammad1603d7d7ba52018-03-12 18:51:53 +02002161 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002162 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002163 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2164 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002165
mohammad1603d7d7ba52018-03-12 18:51:53 +02002166 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2167
Moran Pekered346952018-07-05 15:22:45 +03002168 psa_key_policy_init( &policy );
2169 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2170 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2171
mohammad1603d7d7ba52018-03-12 18:51:53 +02002172 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002173 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002174
Gilles Peskinefe119512018-07-08 21:39:34 +02002175 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2176 key_slot, alg ) == PSA_SUCCESS );
2177 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2178 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002179
Gilles Peskinefe119512018-07-08 21:39:34 +02002180 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2181 iv, iv_size,
2182 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002183 output1_size = (size_t) input->len +
2184 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002185 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002186
Gilles Peskine4abf7412018-06-18 16:35:34 +02002187 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002188 output1, output1_size,
2189 &output1_length ) == PSA_SUCCESS );
2190 TEST_ASSERT( psa_cipher_finish( &operation1,
2191 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002192 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002193
Gilles Peskine048b7f02018-06-08 14:20:49 +02002194 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002195
2196 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2197
2198 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002199 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002200
Gilles Peskinefe119512018-07-08 21:39:34 +02002201 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2202 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002203 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2204 output2, output2_size,
2205 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002206 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002207 TEST_ASSERT( psa_cipher_finish( &operation2,
2208 output2 + output2_length,
2209 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002210 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002211
Gilles Peskine048b7f02018-06-08 14:20:49 +02002212 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002213
Janos Follath25c4fa82018-07-06 16:23:25 +01002214 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002215
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002216 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002217
2218exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002219 mbedtls_free( output1 );
2220 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002221 psa_destroy_key( key_slot );
2222 mbedtls_psa_crypto_free( );
2223}
2224/* END_CASE */
2225
2226/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227void cipher_verify_output_multipart( int alg_arg,
2228 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002229 data_t *key,
2230 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002231 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002232{
2233 int key_slot = 1;
2234 psa_key_type_t key_type = key_type_arg;
2235 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002236 unsigned char iv[16] = {0};
2237 size_t iv_size = 16;
2238 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002239 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002240 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002241 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002242 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002243 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002244 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002245 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002246 psa_cipher_operation_t operation1;
2247 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002248 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002249
Moran Pekerded84402018-06-06 16:36:50 +03002250 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002251 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002252 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2253 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002254
Moran Pekerded84402018-06-06 16:36:50 +03002255 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2256
Moran Pekered346952018-07-05 15:22:45 +03002257 psa_key_policy_init( &policy );
2258 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2259 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2260
Moran Pekerded84402018-06-06 16:36:50 +03002261 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002262 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002263
Gilles Peskinefe119512018-07-08 21:39:34 +02002264 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2265 key_slot, alg ) == PSA_SUCCESS );
2266 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2267 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002268
Gilles Peskinefe119512018-07-08 21:39:34 +02002269 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2270 iv, iv_size,
2271 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002272 output1_buffer_size = (size_t) input->len +
2273 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002274 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002275
Gilles Peskine4abf7412018-06-18 16:35:34 +02002276 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002277
itayzafrir3e02b3b2018-06-12 17:06:52 +03002278 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002279 output1, output1_buffer_size,
2280 &function_output_length ) == PSA_SUCCESS );
2281 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002282
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002283 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002284 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002285 input->len - 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 Peskine048b7f02018-06-08 14:20:49 +02002290 TEST_ASSERT( psa_cipher_finish( &operation1,
2291 output1 + output1_length,
2292 output1_buffer_size - output1_length,
2293 &function_output_length ) == PSA_SUCCESS );
2294 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002295
2296 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2297
Gilles Peskine048b7f02018-06-08 14:20:49 +02002298 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002299 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002300
Gilles Peskinefe119512018-07-08 21:39:34 +02002301 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2302 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002303
2304 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002305 output2, output2_buffer_size,
2306 &function_output_length ) == PSA_SUCCESS );
2307 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002308
Gilles Peskine048b7f02018-06-08 14:20:49 +02002309 TEST_ASSERT( psa_cipher_update( &operation2,
2310 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002311 output1_length - 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 Peskine4ca9c3f2018-06-06 18:44:09 +02002316 TEST_ASSERT( psa_cipher_finish( &operation2,
2317 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002318 output2_buffer_size - output2_length,
2319 &function_output_length ) == PSA_SUCCESS );
2320 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002321
Janos Follath25c4fa82018-07-06 16:23:25 +01002322 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002323
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002324 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002325
2326exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002327 mbedtls_free( output1 );
2328 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002329 psa_destroy_key( key_slot );
2330 mbedtls_psa_crypto_free( );
2331}
2332/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002333
Gilles Peskine20035e32018-02-03 22:44:14 +01002334/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002335void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002336 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002337 data_t *nonce,
2338 data_t *additional_data,
2339 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002340 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002341{
2342 int slot = 1;
2343 psa_key_type_t key_type = key_type_arg;
2344 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002345 unsigned char *output_data = NULL;
2346 size_t output_size = 0;
2347 size_t output_length = 0;
2348 unsigned char *output_data2 = NULL;
2349 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002350 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002351 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002352 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002353
Gilles Peskinea1cac842018-06-11 19:33:02 +02002354 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002355 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002356 TEST_ASSERT( nonce != NULL );
2357 TEST_ASSERT( additional_data != NULL );
2358 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2359 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2360 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2361 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2362
Gilles Peskine4abf7412018-06-18 16:35:34 +02002363 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002364 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002365
2366 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2367
2368 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002369 psa_key_policy_set_usage( &policy,
2370 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2371 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002372 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2373
2374 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002375 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002376
2377 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002378 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002379 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002380 additional_data->len,
2381 input_data->x, input_data->len,
2382 output_data, output_size,
2383 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002384
2385 if( PSA_SUCCESS == expected_result )
2386 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002387 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002388
2389 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002390 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002391 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002392 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002393 output_data, output_length,
2394 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002395 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002396
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002397 ASSERT_COMPARE( input_data->x, input_data->len,
2398 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002399 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002400
Gilles Peskinea1cac842018-06-11 19:33:02 +02002401exit:
2402 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002403 mbedtls_free( output_data );
2404 mbedtls_free( output_data2 );
2405 mbedtls_psa_crypto_free( );
2406}
2407/* END_CASE */
2408
2409/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002410void aead_encrypt( int key_type_arg, data_t *key_data,
2411 int alg_arg,
2412 data_t *nonce,
2413 data_t *additional_data,
2414 data_t *input_data,
2415 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002416{
2417 int slot = 1;
2418 psa_key_type_t key_type = key_type_arg;
2419 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002420 unsigned char *output_data = NULL;
2421 size_t output_size = 0;
2422 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002423 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002424 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002425
Gilles Peskinea1cac842018-06-11 19:33:02 +02002426 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002427 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002428 TEST_ASSERT( additional_data != NULL );
2429 TEST_ASSERT( nonce != NULL );
2430 TEST_ASSERT( expected_result != NULL );
2431 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2432 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2433 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2434 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2435 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2436
Gilles Peskine4abf7412018-06-18 16:35:34 +02002437 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002438 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002439
Gilles Peskinea1cac842018-06-11 19:33:02 +02002440 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2441
2442 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002443 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002444 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2445
2446 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002447 key_data->x,
2448 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002449
2450 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002451 nonce->x, nonce->len,
2452 additional_data->x, additional_data->len,
2453 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002454 output_data, output_size,
2455 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002456
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002457 ASSERT_COMPARE( expected_result->x, expected_result->len,
2458 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002459
Gilles Peskinea1cac842018-06-11 19:33:02 +02002460exit:
2461 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002462 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002463 mbedtls_psa_crypto_free( );
2464}
2465/* END_CASE */
2466
2467/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002468void aead_decrypt( int key_type_arg, data_t *key_data,
2469 int alg_arg,
2470 data_t *nonce,
2471 data_t *additional_data,
2472 data_t *input_data,
2473 data_t *expected_data,
2474 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002475{
2476 int slot = 1;
2477 psa_key_type_t key_type = key_type_arg;
2478 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002479 unsigned char *output_data = NULL;
2480 size_t output_size = 0;
2481 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002482 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002483 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002484 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002485
Gilles Peskinea1cac842018-06-11 19:33:02 +02002486 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002487 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002488 TEST_ASSERT( additional_data != NULL );
2489 TEST_ASSERT( nonce != NULL );
2490 TEST_ASSERT( expected_data != NULL );
2491 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2492 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2493 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2494 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2495 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2496
Gilles Peskine4abf7412018-06-18 16:35:34 +02002497 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002498 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002499
Gilles Peskinea1cac842018-06-11 19:33:02 +02002500 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2501
2502 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002503 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002504 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2505
2506 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002507 key_data->x,
2508 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002509
2510 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002511 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002512 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002513 additional_data->len,
2514 input_data->x, input_data->len,
2515 output_data, output_size,
2516 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002517
Gilles Peskine2d277862018-06-18 15:41:12 +02002518 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002519 ASSERT_COMPARE( expected_data->x, expected_data->len,
2520 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002521
Gilles Peskinea1cac842018-06-11 19:33:02 +02002522exit:
2523 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002524 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002525 mbedtls_psa_crypto_free( );
2526}
2527/* END_CASE */
2528
2529/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002530void signature_size( int type_arg,
2531 int bits,
2532 int alg_arg,
2533 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002534{
2535 psa_key_type_t type = type_arg;
2536 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002537 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002538 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2539exit:
2540 ;
2541}
2542/* END_CASE */
2543
2544/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002545void sign_deterministic( int key_type_arg, data_t *key_data,
2546 int alg_arg, data_t *input_data,
2547 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002548{
2549 int slot = 1;
2550 psa_key_type_t key_type = key_type_arg;
2551 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002552 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002553 unsigned char *signature = NULL;
2554 size_t signature_size;
2555 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002556 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002557
Gilles Peskine20035e32018-02-03 22:44:14 +01002558 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002559 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002560 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002561 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2562 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2563 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002564
2565 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2566
mohammad1603a97cb8c2018-03-28 03:46:26 -07002567 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002568 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002569 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2570
Gilles Peskine20035e32018-02-03 22:44:14 +01002571 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002572 key_data->x,
2573 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002574 TEST_ASSERT( psa_get_key_information( slot,
2575 NULL,
2576 &key_bits ) == PSA_SUCCESS );
2577
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002578 /* Allocate a buffer which has the size advertized by the
2579 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002580 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2581 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002582 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002583 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002584 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002585
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002586 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002587 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002588 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002589 signature, signature_size,
2590 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002591 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002592 ASSERT_COMPARE( output_data->x, output_data->len,
2593 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002594
2595exit:
2596 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002597 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002598 mbedtls_psa_crypto_free( );
2599}
2600/* END_CASE */
2601
2602/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002603void sign_fail( int key_type_arg, data_t *key_data,
2604 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002605 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002606{
2607 int slot = 1;
2608 psa_key_type_t key_type = key_type_arg;
2609 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002610 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002611 psa_status_t actual_status;
2612 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002613 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002614 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002615 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002616
Gilles Peskine20035e32018-02-03 22:44:14 +01002617 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002618 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002619 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2620 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2621
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002622 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002623
2624 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2625
mohammad1603a97cb8c2018-03-28 03:46:26 -07002626 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002627 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002628 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2629
Gilles Peskine20035e32018-02-03 22:44:14 +01002630 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002631 key_data->x,
2632 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002633
2634 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002635 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002636 signature, signature_size,
2637 &signature_length );
2638 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002639 /* The value of *signature_length is unspecified on error, but
2640 * whatever it is, it should be less than signature_size, so that
2641 * if the caller tries to read *signature_length bytes without
2642 * checking the error code then they don't overflow a buffer. */
2643 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002644
2645exit:
2646 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002647 mbedtls_free( signature );
2648 mbedtls_psa_crypto_free( );
2649}
2650/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002651
2652/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002653void sign_verify( int key_type_arg, data_t *key_data,
2654 int alg_arg, data_t *input_data )
2655{
2656 int slot = 1;
2657 psa_key_type_t key_type = key_type_arg;
2658 psa_algorithm_t alg = alg_arg;
2659 size_t key_bits;
2660 unsigned char *signature = NULL;
2661 size_t signature_size;
2662 size_t signature_length = 0xdeadbeef;
2663 psa_key_policy_t policy;
2664
2665 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2666
2667 psa_key_policy_init( &policy );
2668 psa_key_policy_set_usage( &policy,
2669 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2670 alg );
2671 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2672
2673 TEST_ASSERT( psa_import_key( slot, key_type,
2674 key_data->x,
2675 key_data->len ) == PSA_SUCCESS );
2676 TEST_ASSERT( psa_get_key_information( slot,
2677 NULL,
2678 &key_bits ) == PSA_SUCCESS );
2679
2680 /* Allocate a buffer which has the size advertized by the
2681 * library. */
2682 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2683 key_bits, alg );
2684 TEST_ASSERT( signature_size != 0 );
2685 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002686 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002687
2688 /* Perform the signature. */
2689 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2690 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002691 signature, signature_size,
2692 &signature_length ) == PSA_SUCCESS );
2693 /* Check that the signature length looks sensible. */
2694 TEST_ASSERT( signature_length <= signature_size );
2695 TEST_ASSERT( signature_length > 0 );
2696
2697 /* Use the library to verify that the signature is correct. */
2698 TEST_ASSERT( psa_asymmetric_verify(
2699 slot, alg,
2700 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002701 signature, signature_length ) == PSA_SUCCESS );
2702
2703 if( input_data->len != 0 )
2704 {
2705 /* Flip a bit in the input and verify that the signature is now
2706 * detected as invalid. Flip a bit at the beginning, not at the end,
2707 * because ECDSA may ignore the last few bits of the input. */
2708 input_data->x[0] ^= 1;
2709 TEST_ASSERT( psa_asymmetric_verify(
2710 slot, alg,
2711 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002712 signature,
2713 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2714 }
2715
2716exit:
2717 psa_destroy_key( slot );
2718 mbedtls_free( signature );
2719 mbedtls_psa_crypto_free( );
2720}
2721/* END_CASE */
2722
2723/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002724void asymmetric_verify( int key_type_arg, data_t *key_data,
2725 int alg_arg, data_t *hash_data,
2726 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002727{
2728 int slot = 1;
2729 psa_key_type_t key_type = key_type_arg;
2730 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002731 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002732
Gilles Peskine69c12672018-06-28 00:07:19 +02002733 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2734
itayzafrir5c753392018-05-08 11:18:38 +03002735 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002736 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002737 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002738 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2739 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2740 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002741
2742 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2743
2744 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002745 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002746 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2747
2748 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002749 key_data->x,
2750 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002751
2752 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002753 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002754 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002755 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002756exit:
2757 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002758 mbedtls_psa_crypto_free( );
2759}
2760/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002761
2762/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002763void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2764 int alg_arg, data_t *hash_data,
2765 data_t *signature_data,
2766 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002767{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002768 int slot = 1;
2769 psa_key_type_t key_type = key_type_arg;
2770 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002771 psa_status_t actual_status;
2772 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002773 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002774
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002775 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002776 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002777 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002778 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2779 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2780 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002781
2782 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2783
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002784 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002785 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002786 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2787
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002788 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002789 key_data->x,
2790 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002791
2792 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002793 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002794 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002795 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002796
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002797 TEST_ASSERT( actual_status == expected_status );
2798
2799exit:
2800 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002801 mbedtls_psa_crypto_free( );
2802}
2803/* END_CASE */
2804
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002805/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002806void asymmetric_encrypt( int key_type_arg,
2807 data_t *key_data,
2808 int alg_arg,
2809 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002810 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002811 int expected_output_length_arg,
2812 int expected_status_arg )
2813{
2814 int slot = 1;
2815 psa_key_type_t key_type = key_type_arg;
2816 psa_algorithm_t alg = alg_arg;
2817 size_t expected_output_length = expected_output_length_arg;
2818 size_t key_bits;
2819 unsigned char *output = NULL;
2820 size_t output_size;
2821 size_t output_length = ~0;
2822 psa_status_t actual_status;
2823 psa_status_t expected_status = expected_status_arg;
2824 psa_key_policy_t policy;
2825
2826 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2827
2828 /* Import the key */
2829 psa_key_policy_init( &policy );
2830 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2831 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2832 TEST_ASSERT( psa_import_key( slot, key_type,
2833 key_data->x,
2834 key_data->len ) == PSA_SUCCESS );
2835
2836 /* Determine the maximum output length */
2837 TEST_ASSERT( psa_get_key_information( slot,
2838 NULL,
2839 &key_bits ) == PSA_SUCCESS );
2840 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002841 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002842
2843 /* Encrypt the input */
2844 actual_status = psa_asymmetric_encrypt( slot, alg,
2845 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002846 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002847 output, output_size,
2848 &output_length );
2849 TEST_ASSERT( actual_status == expected_status );
2850 TEST_ASSERT( output_length == expected_output_length );
2851
Gilles Peskine68428122018-06-30 18:42:41 +02002852 /* If the label is empty, the test framework puts a non-null pointer
2853 * in label->x. Test that a null pointer works as well. */
2854 if( label->len == 0 )
2855 {
2856 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002857 if( output_size != 0 )
2858 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002859 actual_status = psa_asymmetric_encrypt( slot, alg,
2860 input_data->x, input_data->len,
2861 NULL, label->len,
2862 output, output_size,
2863 &output_length );
2864 TEST_ASSERT( actual_status == expected_status );
2865 TEST_ASSERT( output_length == expected_output_length );
2866 }
2867
Gilles Peskine656896e2018-06-29 19:12:28 +02002868exit:
2869 psa_destroy_key( slot );
2870 mbedtls_free( output );
2871 mbedtls_psa_crypto_free( );
2872}
2873/* END_CASE */
2874
2875/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002876void asymmetric_encrypt_decrypt( int key_type_arg,
2877 data_t *key_data,
2878 int alg_arg,
2879 data_t *input_data,
2880 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002881{
2882 int slot = 1;
2883 psa_key_type_t key_type = key_type_arg;
2884 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002885 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002886 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002887 size_t output_size;
2888 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002889 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002890 size_t output2_size;
2891 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002892 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002894 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002895 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002896 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2897 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2898
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002899 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2900
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002901 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002902 psa_key_policy_set_usage( &policy,
2903 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002904 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002905 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2906
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002907 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002908 key_data->x,
2909 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002910
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002911
2912 /* Determine the maximum ciphertext length */
2913 TEST_ASSERT( psa_get_key_information( slot,
2914 NULL,
2915 &key_bits ) == PSA_SUCCESS );
2916 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002917 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002918 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002919 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002920
Gilles Peskineeebd7382018-06-08 18:11:54 +02002921 /* We test encryption by checking that encrypt-then-decrypt gives back
2922 * the original plaintext because of the non-optional random
2923 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002924 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002925 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002926 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002927 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002928 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002929 /* We don't know what ciphertext length to expect, but check that
2930 * it looks sensible. */
2931 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002932
Gilles Peskine2d277862018-06-18 15:41:12 +02002933 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002934 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002935 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002936 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002937 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002938 ASSERT_COMPARE( input_data->x, input_data->len,
2939 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002940
2941exit:
2942 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002943 mbedtls_free( output );
2944 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002945 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002946}
2947/* END_CASE */
2948
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002949/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002950void asymmetric_decrypt( int key_type_arg,
2951 data_t *key_data,
2952 int alg_arg,
2953 data_t *input_data,
2954 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002955 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002956{
2957 int slot = 1;
2958 psa_key_type_t key_type = key_type_arg;
2959 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002960 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002961 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002962 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002963 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002966 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002967 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002968 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2969 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2970 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2971
Gilles Peskine4abf7412018-06-18 16:35:34 +02002972 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002973 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002974
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002975 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2976
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002977 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002978 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002979 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2980
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002981 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002982 key_data->x,
2983 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002984
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002985 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002986 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002987 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002988 output,
2989 output_size,
2990 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002991 ASSERT_COMPARE( expected_data->x, expected_data->len,
2992 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002993
Gilles Peskine68428122018-06-30 18:42:41 +02002994 /* If the label is empty, the test framework puts a non-null pointer
2995 * in label->x. Test that a null pointer works as well. */
2996 if( label->len == 0 )
2997 {
2998 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002999 if( output_size != 0 )
3000 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003001 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3002 input_data->x, input_data->len,
3003 NULL, label->len,
3004 output,
3005 output_size,
3006 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003007 ASSERT_COMPARE( expected_data->x, expected_data->len,
3008 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003009 }
3010
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003011exit:
3012 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003013 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003014 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003015}
3016/* END_CASE */
3017
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003018/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003019void asymmetric_decrypt_fail( int key_type_arg,
3020 data_t *key_data,
3021 int alg_arg,
3022 data_t *input_data,
3023 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003024 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003026 int slot = 1;
3027 psa_key_type_t key_type = key_type_arg;
3028 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003029 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003030 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003031 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032 psa_status_t actual_status;
3033 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003034 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003035
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003036 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003037 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003038 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3039 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3040
Gilles Peskine4abf7412018-06-18 16:35:34 +02003041 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003042 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003043
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003044 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3045
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003046 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003047 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003048 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3049
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003051 key_data->x,
3052 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003053
Gilles Peskine2d277862018-06-18 15:41:12 +02003054 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003055 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003056 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003057 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003058 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003059 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003060 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061
Gilles Peskine68428122018-06-30 18:42:41 +02003062 /* If the label is empty, the test framework puts a non-null pointer
3063 * in label->x. Test that a null pointer works as well. */
3064 if( label->len == 0 )
3065 {
3066 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003067 if( output_size != 0 )
3068 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003069 actual_status = psa_asymmetric_decrypt( slot, alg,
3070 input_data->x, input_data->len,
3071 NULL, label->len,
3072 output, output_size,
3073 &output_length );
3074 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003075 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003076 }
3077
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078exit:
3079 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003080 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003081 mbedtls_psa_crypto_free( );
3082}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003083/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003084
3085/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003086void derive_setup( int key_type_arg,
3087 data_t *key_data,
3088 int alg_arg,
3089 data_t *salt,
3090 data_t *label,
3091 int requested_capacity_arg,
3092 int expected_status_arg )
3093{
3094 psa_key_slot_t slot = 1;
3095 size_t key_type = key_type_arg;
3096 psa_algorithm_t alg = alg_arg;
3097 size_t requested_capacity = requested_capacity_arg;
3098 psa_status_t expected_status = expected_status_arg;
3099 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3100 psa_key_policy_t policy;
3101
3102 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3103
3104 psa_key_policy_init( &policy );
3105 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3106 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3107
3108 TEST_ASSERT( psa_import_key( slot, key_type,
3109 key_data->x,
3110 key_data->len ) == PSA_SUCCESS );
3111
3112 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3113 salt->x, salt->len,
3114 label->x, label->len,
3115 requested_capacity ) == expected_status );
3116
3117exit:
3118 psa_generator_abort( &generator );
3119 psa_destroy_key( slot );
3120 mbedtls_psa_crypto_free( );
3121}
3122/* END_CASE */
3123
3124/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003125void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003126{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003127 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003128 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003129 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003130 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003131 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003132 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003133 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3134 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3135 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003136 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003137
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003138 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3139
3140 psa_key_policy_init( &policy );
3141 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3142 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3143
3144 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003145 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003146 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003147
3148 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003149 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003150 NULL, 0,
3151 NULL, 0,
3152 capacity ) == PSA_SUCCESS );
3153
3154 /* state of generator shouldn't allow additional generation */
3155 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3156 NULL, 0,
3157 NULL, 0,
3158 capacity ) == PSA_ERROR_BAD_STATE );
3159
3160 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3161 == PSA_SUCCESS );
3162
3163 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3164 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3165
3166
3167exit:
3168 psa_generator_abort( &generator );
3169 psa_destroy_key( base_key );
3170 mbedtls_psa_crypto_free( );
3171}
3172/* END_CASE */
3173
3174
3175/* BEGIN_CASE */
3176void test_derive_invalid_generator_tests( )
3177{
3178 uint8_t output_buffer[16];
3179 size_t buffer_size = 16;
3180 size_t capacity = 0;
3181 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3182
Nir Sonnenschein50789302018-10-31 12:16:38 +02003183 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003184 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003185
3186 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003187 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003188
Nir Sonnenschein50789302018-10-31 12:16:38 +02003189 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003190
Nir Sonnenschein50789302018-10-31 12:16:38 +02003191 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003192 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003193
Nir Sonnenschein50789302018-10-31 12:16:38 +02003194 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003195 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003196
3197exit:
3198 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003199}
3200/* END_CASE */
3201
3202/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003203void derive_output( int alg_arg,
3204 data_t *key_data,
3205 data_t *salt,
3206 data_t *label,
3207 int requested_capacity_arg,
3208 data_t *expected_output1,
3209 data_t *expected_output2 )
3210{
3211 psa_key_slot_t slot = 1;
3212 psa_algorithm_t alg = alg_arg;
3213 size_t requested_capacity = requested_capacity_arg;
3214 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3215 uint8_t *expected_outputs[2] =
3216 {expected_output1->x, expected_output2->x};
3217 size_t output_sizes[2] =
3218 {expected_output1->len, expected_output2->len};
3219 size_t output_buffer_size = 0;
3220 uint8_t *output_buffer = NULL;
3221 size_t expected_capacity;
3222 size_t current_capacity;
3223 psa_key_policy_t policy;
3224 psa_status_t status;
3225 unsigned i;
3226
3227 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3228 {
3229 if( output_sizes[i] > output_buffer_size )
3230 output_buffer_size = output_sizes[i];
3231 if( output_sizes[i] == 0 )
3232 expected_outputs[i] = NULL;
3233 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003234 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003235 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3236
3237 psa_key_policy_init( &policy );
3238 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3239 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3240
3241 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3242 key_data->x,
3243 key_data->len ) == PSA_SUCCESS );
3244
3245 /* Extraction phase. */
3246 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3247 salt->x, salt->len,
3248 label->x, label->len,
3249 requested_capacity ) == PSA_SUCCESS );
3250 TEST_ASSERT( psa_get_generator_capacity( &generator,
3251 &current_capacity ) ==
3252 PSA_SUCCESS );
3253 TEST_ASSERT( current_capacity == requested_capacity );
3254 expected_capacity = requested_capacity;
3255
3256 /* Expansion phase. */
3257 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3258 {
3259 /* Read some bytes. */
3260 status = psa_generator_read( &generator,
3261 output_buffer, output_sizes[i] );
3262 if( expected_capacity == 0 && output_sizes[i] == 0 )
3263 {
3264 /* Reading 0 bytes when 0 bytes are available can go either way. */
3265 TEST_ASSERT( status == PSA_SUCCESS ||
3266 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3267 continue;
3268 }
3269 else if( expected_capacity == 0 ||
3270 output_sizes[i] > expected_capacity )
3271 {
3272 /* Capacity exceeded. */
3273 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3274 expected_capacity = 0;
3275 continue;
3276 }
3277 /* Success. Check the read data. */
3278 TEST_ASSERT( status == PSA_SUCCESS );
3279 if( output_sizes[i] != 0 )
3280 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3281 output_sizes[i] ) == 0 );
3282 /* Check the generator status. */
3283 expected_capacity -= output_sizes[i];
3284 TEST_ASSERT( psa_get_generator_capacity( &generator,
3285 &current_capacity ) ==
3286 PSA_SUCCESS );
3287 TEST_ASSERT( expected_capacity == current_capacity );
3288 }
3289 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3290
3291exit:
3292 mbedtls_free( output_buffer );
3293 psa_generator_abort( &generator );
3294 psa_destroy_key( slot );
3295 mbedtls_psa_crypto_free( );
3296}
3297/* END_CASE */
3298
3299/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003300void derive_full( int alg_arg,
3301 data_t *key_data,
3302 data_t *salt,
3303 data_t *label,
3304 int requested_capacity_arg )
3305{
3306 psa_key_slot_t slot = 1;
3307 psa_algorithm_t alg = alg_arg;
3308 size_t requested_capacity = requested_capacity_arg;
3309 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3310 unsigned char output_buffer[16];
3311 size_t expected_capacity = requested_capacity;
3312 size_t current_capacity;
3313 psa_key_policy_t policy;
3314
3315 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3316
3317 psa_key_policy_init( &policy );
3318 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3319 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3320
3321 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3322 key_data->x,
3323 key_data->len ) == PSA_SUCCESS );
3324
3325 /* Extraction phase. */
3326 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3327 salt->x, salt->len,
3328 label->x, label->len,
3329 requested_capacity ) == PSA_SUCCESS );
3330 TEST_ASSERT( psa_get_generator_capacity( &generator,
3331 &current_capacity ) ==
3332 PSA_SUCCESS );
3333 TEST_ASSERT( current_capacity == expected_capacity );
3334
3335 /* Expansion phase. */
3336 while( current_capacity > 0 )
3337 {
3338 size_t read_size = sizeof( output_buffer );
3339 if( read_size > current_capacity )
3340 read_size = current_capacity;
3341 TEST_ASSERT( psa_generator_read( &generator,
3342 output_buffer,
3343 read_size ) == PSA_SUCCESS );
3344 expected_capacity -= read_size;
3345 TEST_ASSERT( psa_get_generator_capacity( &generator,
3346 &current_capacity ) ==
3347 PSA_SUCCESS );
3348 TEST_ASSERT( current_capacity == expected_capacity );
3349 }
3350
3351 /* Check that the generator refuses to go over capacity. */
3352 TEST_ASSERT( psa_generator_read( &generator,
3353 output_buffer,
3354 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3355
3356 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3357
3358exit:
3359 psa_generator_abort( &generator );
3360 psa_destroy_key( slot );
3361 mbedtls_psa_crypto_free( );
3362}
3363/* END_CASE */
3364
3365/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003366void derive_key_exercise( int alg_arg,
3367 data_t *key_data,
3368 data_t *salt,
3369 data_t *label,
3370 int derived_type_arg,
3371 int derived_bits_arg,
3372 int derived_usage_arg,
3373 int derived_alg_arg )
3374{
3375 psa_key_slot_t base_key = 1;
3376 psa_key_slot_t derived_key = 2;
3377 psa_algorithm_t alg = alg_arg;
3378 psa_key_type_t derived_type = derived_type_arg;
3379 size_t derived_bits = derived_bits_arg;
3380 psa_key_usage_t derived_usage = derived_usage_arg;
3381 psa_algorithm_t derived_alg = derived_alg_arg;
3382 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3383 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3384 psa_key_policy_t policy;
3385 psa_key_type_t got_type;
3386 size_t got_bits;
3387
3388 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3389
3390 psa_key_policy_init( &policy );
3391 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3392 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3393 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3394 key_data->x,
3395 key_data->len ) == PSA_SUCCESS );
3396
3397 /* Derive a key. */
3398 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3399 salt->x, salt->len,
3400 label->x, label->len,
3401 capacity ) == PSA_SUCCESS );
3402 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3403 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3404 TEST_ASSERT( psa_generator_import_key( derived_key,
3405 derived_type,
3406 derived_bits,
3407 &generator ) == PSA_SUCCESS );
3408
3409 /* Test the key information */
3410 TEST_ASSERT( psa_get_key_information( derived_key,
3411 &got_type,
3412 &got_bits ) == PSA_SUCCESS );
3413 TEST_ASSERT( got_type == derived_type );
3414 TEST_ASSERT( got_bits == derived_bits );
3415
3416 /* Exercise the derived key. */
3417 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3418 goto exit;
3419
3420exit:
3421 psa_generator_abort( &generator );
3422 psa_destroy_key( base_key );
3423 psa_destroy_key( derived_key );
3424 mbedtls_psa_crypto_free( );
3425}
3426/* END_CASE */
3427
3428/* BEGIN_CASE */
3429void derive_key_export( int alg_arg,
3430 data_t *key_data,
3431 data_t *salt,
3432 data_t *label,
3433 int bytes1_arg,
3434 int bytes2_arg )
3435{
3436 psa_key_slot_t base_key = 1;
3437 psa_key_slot_t derived_key = 2;
3438 psa_algorithm_t alg = alg_arg;
3439 size_t bytes1 = bytes1_arg;
3440 size_t bytes2 = bytes2_arg;
3441 size_t capacity = bytes1 + bytes2;
3442 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003443 uint8_t *output_buffer = NULL;
3444 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003445 psa_key_policy_t policy;
3446 size_t length;
3447
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003448 ASSERT_ALLOC( output_buffer, capacity );
3449 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003450 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3451
3452 psa_key_policy_init( &policy );
3453 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3454 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3455 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3456 key_data->x,
3457 key_data->len ) == PSA_SUCCESS );
3458
3459 /* Derive some material and output it. */
3460 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3461 salt->x, salt->len,
3462 label->x, label->len,
3463 capacity ) == PSA_SUCCESS );
3464 TEST_ASSERT( psa_generator_read( &generator,
3465 output_buffer,
3466 capacity ) == PSA_SUCCESS );
3467 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3468
3469 /* Derive the same output again, but this time store it in key objects. */
3470 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3471 salt->x, salt->len,
3472 label->x, label->len,
3473 capacity ) == PSA_SUCCESS );
3474 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3475 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3476 TEST_ASSERT( psa_generator_import_key( derived_key,
3477 PSA_KEY_TYPE_RAW_DATA,
3478 PSA_BYTES_TO_BITS( bytes1 ),
3479 &generator ) == PSA_SUCCESS );
3480 TEST_ASSERT( psa_export_key( derived_key,
3481 export_buffer, bytes1,
3482 &length ) == PSA_SUCCESS );
3483 TEST_ASSERT( length == bytes1 );
3484 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3485 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3486 TEST_ASSERT( psa_generator_import_key( derived_key,
3487 PSA_KEY_TYPE_RAW_DATA,
3488 PSA_BYTES_TO_BITS( bytes2 ),
3489 &generator ) == PSA_SUCCESS );
3490 TEST_ASSERT( psa_export_key( derived_key,
3491 export_buffer + bytes1, bytes2,
3492 &length ) == PSA_SUCCESS );
3493 TEST_ASSERT( length == bytes2 );
3494
3495 /* Compare the outputs from the two runs. */
3496 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3497
3498exit:
3499 mbedtls_free( output_buffer );
3500 mbedtls_free( export_buffer );
3501 psa_generator_abort( &generator );
3502 psa_destroy_key( base_key );
3503 psa_destroy_key( derived_key );
3504 mbedtls_psa_crypto_free( );
3505}
3506/* END_CASE */
3507
3508/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003509void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003510{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003511 size_t bytes = bytes_arg;
3512 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003513 unsigned char *output = NULL;
3514 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003515 size_t i;
3516 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003517
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003518 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3519 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003520 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003521
3522 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3523
Gilles Peskinea50d7392018-06-21 10:22:13 +02003524 /* Run several times, to ensure that every output byte will be
3525 * nonzero at least once with overwhelming probability
3526 * (2^(-8*number_of_runs)). */
3527 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003528 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003529 if( bytes != 0 )
3530 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003531 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3532
3533 /* Check that no more than bytes have been overwritten */
3534 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3535
3536 for( i = 0; i < bytes; i++ )
3537 {
3538 if( output[i] != 0 )
3539 ++changed[i];
3540 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003541 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003542
3543 /* Check that every byte was changed to nonzero at least once. This
3544 * validates that psa_generate_random is overwriting every byte of
3545 * the output buffer. */
3546 for( i = 0; i < bytes; i++ )
3547 {
3548 TEST_ASSERT( changed[i] != 0 );
3549 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003550
3551exit:
3552 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003553 mbedtls_free( output );
3554 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003555}
3556/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003557
3558/* BEGIN_CASE */
3559void generate_key( int type_arg,
3560 int bits_arg,
3561 int usage_arg,
3562 int alg_arg,
3563 int expected_status_arg )
3564{
3565 int slot = 1;
3566 psa_key_type_t type = type_arg;
3567 psa_key_usage_t usage = usage_arg;
3568 size_t bits = bits_arg;
3569 psa_algorithm_t alg = alg_arg;
3570 psa_status_t expected_status = expected_status_arg;
3571 psa_key_type_t got_type;
3572 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003573 psa_status_t expected_info_status =
3574 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3575 psa_key_policy_t policy;
3576
3577 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3578
3579 psa_key_policy_init( &policy );
3580 psa_key_policy_set_usage( &policy, usage, alg );
3581 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3582
3583 /* Generate a key */
3584 TEST_ASSERT( psa_generate_key( slot, type, bits,
3585 NULL, 0 ) == expected_status );
3586
3587 /* Test the key information */
3588 TEST_ASSERT( psa_get_key_information( slot,
3589 &got_type,
3590 &got_bits ) == expected_info_status );
3591 if( expected_info_status != PSA_SUCCESS )
3592 goto exit;
3593 TEST_ASSERT( got_type == type );
3594 TEST_ASSERT( got_bits == bits );
3595
Gilles Peskine818ca122018-06-20 18:16:48 +02003596 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003597 if( ! exercise_key( slot, usage, alg ) )
3598 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003599
3600exit:
3601 psa_destroy_key( slot );
3602 mbedtls_psa_crypto_free( );
3603}
3604/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003605
3606/* BEGIN_CASE */
3607void validate_module_init_generate_random( )
3608{
3609 psa_status_t status;
3610 uint8_t random[10] = { 0 };
3611 status = psa_generate_random( random, sizeof( random ) );
3612 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3613}
3614/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003615
3616/* BEGIN_CASE */
3617void validate_module_init_key_based( )
3618{
3619 psa_status_t status;
3620 uint8_t data[10] = { 0 };
3621 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3622 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3623}
3624/* END_CASE */