blob: 139a62f64f35923a471f10f662be5c91cec851e2 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskine96ee5c72018-07-12 17:24:54 +020014#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
15
itayzafrir3e02b3b2018-06-12 17:06:52 +030016#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020017#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#else
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskine818ca122018-06-20 18:16:48 +0200131static int exercise_mac_key( psa_key_slot_t key,
132 psa_key_usage_t usage,
133 psa_algorithm_t alg )
134{
135 psa_mac_operation_t operation;
136 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200137 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200138 size_t mac_length = sizeof( mac );
139
140 if( usage & PSA_KEY_USAGE_SIGN )
141 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200142 TEST_ASSERT( psa_mac_sign_setup( &operation,
143 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200144 TEST_ASSERT( psa_mac_update( &operation,
145 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200146 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200147 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200149 }
150
151 if( usage & PSA_KEY_USAGE_VERIFY )
152 {
153 psa_status_t verify_status =
154 ( usage & PSA_KEY_USAGE_SIGN ?
155 PSA_SUCCESS :
156 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200157 TEST_ASSERT( psa_mac_verify_setup( &operation,
158 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200159 TEST_ASSERT( psa_mac_update( &operation,
160 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200161 TEST_ASSERT( psa_mac_verify_finish( &operation,
162 mac,
163 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200164 }
165
166 return( 1 );
167
168exit:
169 psa_mac_abort( &operation );
170 return( 0 );
171}
172
173static int exercise_cipher_key( psa_key_slot_t key,
174 psa_key_usage_t usage,
175 psa_algorithm_t alg )
176{
177 psa_cipher_operation_t operation;
178 unsigned char iv[16] = {0};
179 size_t iv_length = sizeof( iv );
180 const unsigned char plaintext[16] = "Hello, world...";
181 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
182 size_t ciphertext_length = sizeof( ciphertext );
183 unsigned char decrypted[sizeof( ciphertext )];
184 size_t part_length;
185
186 if( usage & PSA_KEY_USAGE_ENCRYPT )
187 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200188 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
189 key, alg ) == PSA_SUCCESS );
190 TEST_ASSERT( psa_cipher_generate_iv( &operation,
191 iv, sizeof( iv ),
192 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200193 TEST_ASSERT( psa_cipher_update( &operation,
194 plaintext, sizeof( plaintext ),
195 ciphertext, sizeof( ciphertext ),
196 &ciphertext_length ) == PSA_SUCCESS );
197 TEST_ASSERT( psa_cipher_finish( &operation,
198 ciphertext + ciphertext_length,
199 sizeof( ciphertext ) - ciphertext_length,
200 &part_length ) == PSA_SUCCESS );
201 ciphertext_length += part_length;
202 }
203
204 if( usage & PSA_KEY_USAGE_DECRYPT )
205 {
206 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700207 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200208 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
209 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 size_t bits;
211 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
212 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
213 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200214 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
215 key, alg ) == PSA_SUCCESS );
216 TEST_ASSERT( psa_cipher_set_iv( &operation,
217 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200218 TEST_ASSERT( psa_cipher_update( &operation,
219 ciphertext, ciphertext_length,
220 decrypted, sizeof( decrypted ),
221 &part_length ) == PSA_SUCCESS );
222 status = psa_cipher_finish( &operation,
223 decrypted + part_length,
224 sizeof( decrypted ) - part_length,
225 &part_length );
226 /* For a stream cipher, all inputs are valid. For a block cipher,
227 * if the input is some aribtrary data rather than an actual
228 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700229 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700230 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200231 TEST_ASSERT( status == PSA_SUCCESS );
232 else
233 TEST_ASSERT( status == PSA_SUCCESS ||
234 status == PSA_ERROR_INVALID_PADDING );
235 }
236
237 return( 1 );
238
239exit:
240 psa_cipher_abort( &operation );
241 return( 0 );
242}
243
244static int exercise_aead_key( psa_key_slot_t key,
245 psa_key_usage_t usage,
246 psa_algorithm_t alg )
247{
248 unsigned char nonce[16] = {0};
249 size_t nonce_length = sizeof( nonce );
250 unsigned char plaintext[16] = "Hello, world...";
251 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
252 size_t ciphertext_length = sizeof( ciphertext );
253 size_t plaintext_length = sizeof( ciphertext );
254
255 if( usage & PSA_KEY_USAGE_ENCRYPT )
256 {
257 TEST_ASSERT( psa_aead_encrypt( key, alg,
258 nonce, nonce_length,
259 NULL, 0,
260 plaintext, sizeof( plaintext ),
261 ciphertext, sizeof( ciphertext ),
262 &ciphertext_length ) == PSA_SUCCESS );
263 }
264
265 if( usage & PSA_KEY_USAGE_DECRYPT )
266 {
267 psa_status_t verify_status =
268 ( usage & PSA_KEY_USAGE_ENCRYPT ?
269 PSA_SUCCESS :
270 PSA_ERROR_INVALID_SIGNATURE );
271 TEST_ASSERT( psa_aead_decrypt( key, alg,
272 nonce, nonce_length,
273 NULL, 0,
274 ciphertext, ciphertext_length,
275 plaintext, sizeof( plaintext ),
276 &plaintext_length ) == verify_status );
277 }
278
279 return( 1 );
280
281exit:
282 return( 0 );
283}
284
285static int exercise_signature_key( psa_key_slot_t key,
286 psa_key_usage_t usage,
287 psa_algorithm_t alg )
288{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200289 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
290 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200291 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 size_t signature_length = sizeof( signature );
293
294 if( usage & PSA_KEY_USAGE_SIGN )
295 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200296 /* Some algorithms require the payload to have the size of
297 * the hash encoded in the algorithm. Use this input size
298 * even for algorithms that allow other input sizes. */
299 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
300 if( hash_alg != 0 )
301 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200302 TEST_ASSERT( psa_asymmetric_sign( key, alg,
303 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 signature, sizeof( signature ),
305 &signature_length ) == PSA_SUCCESS );
306 }
307
308 if( usage & PSA_KEY_USAGE_VERIFY )
309 {
310 psa_status_t verify_status =
311 ( usage & PSA_KEY_USAGE_SIGN ?
312 PSA_SUCCESS :
313 PSA_ERROR_INVALID_SIGNATURE );
314 TEST_ASSERT( psa_asymmetric_verify( key, alg,
315 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 signature, signature_length ) ==
317 verify_status );
318 }
319
320 return( 1 );
321
322exit:
323 return( 0 );
324}
325
326static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
327 psa_key_usage_t usage,
328 psa_algorithm_t alg )
329{
330 unsigned char plaintext[256] = "Hello, world...";
331 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
332 size_t ciphertext_length = sizeof( ciphertext );
333 size_t plaintext_length = 16;
334
335 if( usage & PSA_KEY_USAGE_ENCRYPT )
336 {
337 TEST_ASSERT(
338 psa_asymmetric_encrypt( key, alg,
339 plaintext, plaintext_length,
340 NULL, 0,
341 ciphertext, sizeof( ciphertext ),
342 &ciphertext_length ) == PSA_SUCCESS );
343 }
344
345 if( usage & PSA_KEY_USAGE_DECRYPT )
346 {
347 psa_status_t status =
348 psa_asymmetric_decrypt( key, alg,
349 ciphertext, ciphertext_length,
350 NULL, 0,
351 plaintext, sizeof( plaintext ),
352 &plaintext_length );
353 TEST_ASSERT( status == PSA_SUCCESS ||
354 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
355 ( status == PSA_ERROR_INVALID_ARGUMENT ||
356 status == PSA_ERROR_INVALID_PADDING ) ) );
357 }
358
359 return( 1 );
360
361exit:
362 return( 0 );
363}
Gilles Peskine02b75072018-07-01 22:31:34 +0200364
Gilles Peskineea0fb492018-07-12 17:17:20 +0200365static int exercise_key_derivation_key( psa_key_slot_t key,
366 psa_key_usage_t usage,
367 psa_algorithm_t alg )
368{
369 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
370 unsigned char label[16] = "This is a label.";
371 size_t label_length = sizeof( label );
372 unsigned char seed[16] = "abcdefghijklmnop";
373 size_t seed_length = sizeof( seed );
374 unsigned char output[1];
375
376 if( usage & PSA_KEY_USAGE_DERIVE )
377 {
378 TEST_ASSERT( psa_key_derivation( &generator,
379 key, alg,
380 label, label_length,
381 seed, seed_length,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_read( &generator,
384 output,
385 sizeof( output ) ) == PSA_SUCCESS );
386 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
387 }
388
389 return( 1 );
390
391exit:
392 return( 0 );
393}
394
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200395static int is_oid_of_key_type( psa_key_type_t type,
396 const uint8_t *oid, size_t oid_length )
397{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200398 const uint8_t *expected_oid = NULL;
399 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200400#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200401 if( PSA_KEY_TYPE_IS_RSA( type ) )
402 {
403 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
404 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
405 }
406 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200407#endif /* MBEDTLS_RSA_C */
408#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200409 if( PSA_KEY_TYPE_IS_ECC( type ) )
410 {
411 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
412 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
413 }
414 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200415#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200416 {
417 char message[40];
418 mbedtls_snprintf( message, sizeof( message ),
419 "OID not known for key type=0x%08lx",
420 (unsigned long) type );
421 test_fail( message, __LINE__, __FILE__ );
422 return( 0 );
423 }
424
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200425 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200426 return( 1 );
427
428exit:
429 return( 0 );
430}
431
432static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
433 size_t min_bits, size_t max_bits,
434 int must_be_odd )
435{
436 size_t len;
437 size_t actual_bits;
438 unsigned char msb;
439 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
440 MBEDTLS_ASN1_INTEGER ) == 0 );
441 /* Tolerate a slight departure from DER encoding:
442 * - 0 may be represented by an empty string or a 1-byte string.
443 * - The sign bit may be used as a value bit. */
444 if( ( len == 1 && ( *p )[0] == 0 ) ||
445 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
446 {
447 ++( *p );
448 --len;
449 }
450 if( min_bits == 0 && len == 0 )
451 return( 1 );
452 msb = ( *p )[0];
453 TEST_ASSERT( msb != 0 );
454 actual_bits = 8 * ( len - 1 );
455 while( msb != 0 )
456 {
457 msb >>= 1;
458 ++actual_bits;
459 }
460 TEST_ASSERT( actual_bits >= min_bits );
461 TEST_ASSERT( actual_bits <= max_bits );
462 if( must_be_odd )
463 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
464 *p += len;
465 return( 1 );
466exit:
467 return( 0 );
468}
469
470static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
471 size_t *len,
472 unsigned char n, unsigned char tag )
473{
474 int ret;
475 ret = mbedtls_asn1_get_tag( p, end, len,
476 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
477 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
478 if( ret != 0 )
479 return( ret );
480 end = *p + *len;
481 ret = mbedtls_asn1_get_tag( p, end, len, tag );
482 if( ret != 0 )
483 return( ret );
484 if( *p + *len != end )
485 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
486 return( 0 );
487}
488
489static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
490 uint8_t *exported, size_t exported_length )
491{
492 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200493 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200494 else
495 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200496
497#if defined(MBEDTLS_DES_C)
498 if( type == PSA_KEY_TYPE_DES )
499 {
500 /* Check the parity bits. */
501 unsigned i;
502 for( i = 0; i < bits / 8; i++ )
503 {
504 unsigned bit_count = 0;
505 unsigned m;
506 for( m = 1; m <= 0x100; m <<= 1 )
507 {
508 if( exported[i] & m )
509 ++bit_count;
510 }
511 TEST_ASSERT( bit_count % 2 != 0 );
512 }
513 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200514 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200515#endif
516
517#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
518 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
519 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200520 uint8_t *p = exported;
521 uint8_t *end = exported + exported_length;
522 size_t len;
523 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200524 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200525 * modulus INTEGER, -- n
526 * publicExponent INTEGER, -- e
527 * privateExponent INTEGER, -- d
528 * prime1 INTEGER, -- p
529 * prime2 INTEGER, -- q
530 * exponent1 INTEGER, -- d mod (p-1)
531 * exponent2 INTEGER, -- d mod (q-1)
532 * coefficient INTEGER, -- (inverse of q) mod p
533 * }
534 */
535 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
536 MBEDTLS_ASN1_SEQUENCE |
537 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
538 TEST_ASSERT( p + len == end );
539 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
540 goto exit;
541 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
542 goto exit;
543 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
544 goto exit;
545 /* Require d to be at least half the size of n. */
546 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
547 goto exit;
548 /* Require p and q to be at most half the size of n, rounded up. */
549 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
550 goto exit;
551 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
552 goto exit;
553 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
554 goto exit;
555 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
556 goto exit;
557 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
558 goto exit;
559 TEST_ASSERT( p == end );
560 }
561 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200562#endif /* MBEDTLS_RSA_C */
563
564#if defined(MBEDTLS_ECP_C)
565 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
566 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200567 uint8_t *p = exported;
568 uint8_t *end = exported + exported_length;
569 size_t len;
570 int version;
571 /* ECPrivateKey ::= SEQUENCE {
572 * version INTEGER, -- must be 1
573 * privateKey OCTET STRING,
574 * -- `ceiling(log_{256}(n))`-byte string, big endian,
575 * -- where n is the order of the curve.
576 * parameters ECParameters {{ NamedCurve }}, -- mandatory
577 * publicKey BIT STRING -- mandatory
578 * }
579 */
580 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
581 MBEDTLS_ASN1_SEQUENCE |
582 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
583 TEST_ASSERT( p + len == end );
584 TEST_ASSERT( mbedtls_asn1_get_int( &p, end, &version ) == 0 );
585 TEST_ASSERT( version == 1 );
586 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
587 MBEDTLS_ASN1_OCTET_STRING ) == 0 );
588 /* Bug in Mbed TLS: the length of the octet string depends on the value */
589 // TEST_ASSERT( len == PSA_BITS_TO_BYTES( bits ) );
590 p += len;
591 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 0,
592 MBEDTLS_ASN1_OID ) == 0 );
593 p += len;
Gilles Peskinec6290c02018-08-13 17:24:59 +0200594 /* publicKey: ECPoint in uncompressed representation (as below) */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200595 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 1,
596 MBEDTLS_ASN1_BIT_STRING ) == 0 );
597 TEST_ASSERT( p + len == end );
598 TEST_ASSERT( p[0] == 0 ); /* 0 unused bits in the bit string */
599 ++p;
600 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
601 TEST_ASSERT( p[0] == 4 );
602 }
603 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200604#endif /* MBEDTLS_ECP_C */
605
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200606 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
607 {
608 uint8_t *p = exported;
609 uint8_t *end = exported + exported_length;
610 size_t len;
611 mbedtls_asn1_buf alg;
612 mbedtls_asn1_buf params;
613 mbedtls_asn1_bitstring bitstring;
614 /* SubjectPublicKeyInfo ::= SEQUENCE {
615 * algorithm AlgorithmIdentifier,
616 * subjectPublicKey BIT STRING }
617 * AlgorithmIdentifier ::= SEQUENCE {
618 * algorithm OBJECT IDENTIFIER,
619 * parameters ANY DEFINED BY algorithm OPTIONAL }
620 */
621 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
622 MBEDTLS_ASN1_SEQUENCE |
623 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
624 TEST_ASSERT( p + len == end );
625 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
626 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
627 goto exit;
628 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
629 TEST_ASSERT( p == end );
630 p = bitstring.p;
631#if defined(MBEDTLS_RSA_C)
632 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
633 {
634 /* RSAPublicKey ::= SEQUENCE {
635 * modulus INTEGER, -- n
636 * publicExponent INTEGER } -- e
637 */
638 TEST_ASSERT( bitstring.unused_bits == 0 );
639 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
640 MBEDTLS_ASN1_SEQUENCE |
641 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
642 TEST_ASSERT( p + len == end );
643 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
644 goto exit;
645 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
646 goto exit;
647 TEST_ASSERT( p == end );
648 }
649 else
650#endif /* MBEDTLS_RSA_C */
651#if defined(MBEDTLS_ECP_C)
652 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
653 {
654 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200655 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200656 * -- then x_P as an n-bit string, big endian;
657 * -- then y_P as a n-bit string, big endian,
658 * -- where n is the order of the curve.
659 */
660 TEST_ASSERT( bitstring.unused_bits == 0 );
661 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
662 TEST_ASSERT( p[0] == 4 );
663 }
664 else
665#endif /* MBEDTLS_ECP_C */
666 {
667 char message[40];
668 mbedtls_snprintf( message, sizeof( message ),
669 "No sanity check for public key type=0x%08lx",
670 (unsigned long) type );
671 test_fail( message, __LINE__, __FILE__ );
672 return( 0 );
673 }
674 }
675 else
676
677 {
678 /* No sanity checks for other types */
679 }
680
681 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200682
683exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200684 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200685}
686
687static int exercise_export_key( psa_key_slot_t slot,
688 psa_key_usage_t usage )
689{
690 psa_key_type_t type;
691 size_t bits;
692 uint8_t *exported = NULL;
693 size_t exported_size = 0;
694 size_t exported_length = 0;
695 int ok = 0;
696
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200697 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
698
699 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
700 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200701 {
702 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
703 PSA_ERROR_NOT_PERMITTED );
704 return( 1 );
705 }
706
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200708 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200709
710 TEST_ASSERT( psa_export_key( slot,
711 exported, exported_size,
712 &exported_length ) == PSA_SUCCESS );
713 ok = exported_key_sanity_check( type, bits, exported, exported_length );
714
715exit:
716 mbedtls_free( exported );
717 return( ok );
718}
719
720static int exercise_export_public_key( psa_key_slot_t slot )
721{
722 psa_key_type_t type;
723 psa_key_type_t public_type;
724 size_t bits;
725 uint8_t *exported = NULL;
726 size_t exported_size = 0;
727 size_t exported_length = 0;
728 int ok = 0;
729
730 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
731 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
732 {
733 TEST_ASSERT( psa_export_public_key( slot,
734 NULL, 0, &exported_length ) ==
735 PSA_ERROR_INVALID_ARGUMENT );
736 return( 1 );
737 }
738
739 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
740 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200741 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200742
743 TEST_ASSERT( psa_export_public_key( slot,
744 exported, exported_size,
745 &exported_length ) == PSA_SUCCESS );
746 ok = exported_key_sanity_check( public_type, bits,
747 exported, exported_length );
748
749exit:
750 mbedtls_free( exported );
751 return( ok );
752}
753
Gilles Peskine02b75072018-07-01 22:31:34 +0200754static int exercise_key( psa_key_slot_t slot,
755 psa_key_usage_t usage,
756 psa_algorithm_t alg )
757{
758 int ok;
759 if( alg == 0 )
760 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
761 else if( PSA_ALG_IS_MAC( alg ) )
762 ok = exercise_mac_key( slot, usage, alg );
763 else if( PSA_ALG_IS_CIPHER( alg ) )
764 ok = exercise_cipher_key( slot, usage, alg );
765 else if( PSA_ALG_IS_AEAD( alg ) )
766 ok = exercise_aead_key( slot, usage, alg );
767 else if( PSA_ALG_IS_SIGN( alg ) )
768 ok = exercise_signature_key( slot, usage, alg );
769 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
770 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200771 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
772 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 else
774 {
775 char message[40];
776 mbedtls_snprintf( message, sizeof( message ),
777 "No code to exercise alg=0x%08lx",
778 (unsigned long) alg );
779 test_fail( message, __LINE__, __FILE__ );
780 ok = 0;
781 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200782
783 ok = ok && exercise_export_key( slot, usage );
784 ok = ok && exercise_export_public_key( slot );
785
Gilles Peskine02b75072018-07-01 22:31:34 +0200786 return( ok );
787}
788
Gilles Peskinee59236f2018-01-27 23:32:46 +0100789/* END_HEADER */
790
791/* BEGIN_DEPENDENCIES
792 * depends_on:MBEDTLS_PSA_CRYPTO_C
793 * END_DEPENDENCIES
794 */
795
796/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200797void static_checks( )
798{
799 size_t max_truncated_mac_size =
800 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
801
802 /* Check that the length for a truncated MAC always fits in the algorithm
803 * encoding. The shifted mask is the maximum truncated value. The
804 * untruncated algorithm may be one byte larger. */
805 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
806}
807/* END_CASE */
808
809/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200810void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100811{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100813 int i;
814 for( i = 0; i <= 1; i++ )
815 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 status = psa_crypto_init( );
817 TEST_ASSERT( status == PSA_SUCCESS );
818 status = psa_crypto_init( );
819 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100820 mbedtls_psa_crypto_free( );
821 }
822}
823/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824
825/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200826void fill_slots( int max_arg )
827{
828 /* Fill all the slots until we run out of memory or out of slots,
829 * or until some limit specified in the test data for the sake of
830 * implementations with an essentially unlimited number of slots.
831 * This test assumes that available slots are numbered from 1. */
832
833 psa_key_slot_t slot;
834 psa_key_slot_t max = 0;
835 psa_key_policy_t policy;
836 uint8_t exported[sizeof( max )];
837 size_t exported_size;
838 psa_status_t status;
839
840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
841
842 psa_key_policy_init( &policy );
843 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
844
845 for( max = 1; max <= (size_t) max_arg; max++ )
846 {
847 status = psa_set_key_policy( max, &policy );
848 /* Stop filling slots if we run out of memory or out of
849 * available slots. */
850 TEST_ASSERT( status == PSA_SUCCESS ||
851 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
852 status == PSA_ERROR_INVALID_ARGUMENT );
853 if( status != PSA_SUCCESS )
854 break;
855 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
856 (uint8_t*) &max, sizeof( max ) );
857 /* Since psa_set_key_policy succeeded, we know that the slot
858 * number is valid. But we may legitimately run out of memory. */
859 TEST_ASSERT( status == PSA_SUCCESS ||
860 status == PSA_ERROR_INSUFFICIENT_MEMORY );
861 if( status != PSA_SUCCESS )
862 break;
863 }
864 /* `max` is now the first slot number that wasn't filled. */
865 max -= 1;
866
867 for( slot = 1; slot <= max; slot++ )
868 {
869 TEST_ASSERT( psa_export_key( slot,
870 exported, sizeof( exported ),
871 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200872 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200873 }
874
875exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200876 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200877 mbedtls_psa_crypto_free( );
878}
879/* END_CASE */
880
881/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200882void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883{
884 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200885 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
891
Gilles Peskine4abf7412018-06-18 16:35:34 +0200892 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200893 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894 if( status == PSA_SUCCESS )
895 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
896
897exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898 mbedtls_psa_crypto_free( );
899}
900/* END_CASE */
901
902/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200903void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
904{
905 int slot = 1;
906 size_t bits = bits_arg;
907 psa_status_t expected_status = expected_status_arg;
908 psa_status_t status;
909 psa_key_type_t type =
910 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
911 size_t buffer_size = /* Slight overapproximations */
912 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200913 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200914 unsigned char *p;
915 int ret;
916 size_t length;
917
918 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200919 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200920
921 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
922 bits, keypair ) ) >= 0 );
923 length = ret;
924
925 /* Try importing the key */
926 status = psa_import_key( slot, type, p, length );
927 TEST_ASSERT( status == expected_status );
928 if( status == PSA_SUCCESS )
929 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
930
931exit:
932 mbedtls_free( buffer );
933 mbedtls_psa_crypto_free( );
934}
935/* END_CASE */
936
937/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300938void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300939 int type_arg,
940 int alg_arg,
941 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 int expected_bits,
943 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200944 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945 int canonical_input )
946{
947 int slot = 1;
948 int slot2 = slot + 1;
949 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200950 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200951 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 unsigned char *exported = NULL;
954 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100956 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957 size_t reexported_length;
958 psa_key_type_t got_type;
959 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200960 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300963 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300964 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200965 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200967 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
969
mohammad1603a97cb8c2018-03-28 03:46:26 -0700970 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200971 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700972 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
973
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 /* Import the key */
975 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200976 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977
978 /* Test the key information */
979 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200980 &got_type,
981 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982 TEST_ASSERT( got_type == type );
983 TEST_ASSERT( got_bits == (size_t) expected_bits );
984
985 /* Export the key */
986 status = psa_export_key( slot,
987 exported, export_size,
988 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200989 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100990
991 /* The exported length must be set by psa_export_key() to a value between 0
992 * and export_size. On errors, the exported length must be 0. */
993 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
994 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
995 TEST_ASSERT( exported_length <= export_size );
996
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200997 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200998 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001000 {
1001 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001003 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004
Gilles Peskine8f609232018-08-11 01:24:55 +02001005 if( ! exercise_export_key( slot, usage_arg ) )
1006 goto exit;
1007
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001009 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 else
1011 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001012 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1013
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001015 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001016 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001018 reexported,
1019 export_size,
1020 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001021 ASSERT_COMPARE( exported, exported_length,
1022 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001024 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001025
1026destroy:
1027 /* Destroy the key */
1028 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1029 TEST_ASSERT( psa_get_key_information(
1030 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1031
1032exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001033 mbedtls_free( exported );
1034 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035 mbedtls_psa_crypto_free( );
1036}
1037/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001038
Moran Pekerf709f4a2018-06-06 17:26:04 +03001039/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001040void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001041 int type_arg,
1042 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001043 int export_size_delta,
1044 int expected_export_status_arg,
1045 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001046{
1047 int slot = 1;
1048 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001049 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001050 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001053 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001054 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001055 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001056
Moran Pekerf709f4a2018-06-06 17:26:04 +03001057 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1058
1059 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001060 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001061 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1062
1063 /* Import the key */
1064 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001065 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001066
Gilles Peskine49c25912018-10-29 15:15:31 +01001067 /* Export the public key */
1068 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001069 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001070 exported, export_size,
1071 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001072 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001073 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001074 {
1075 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1076 size_t bits;
1077 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1078 PSA_SUCCESS );
1079 TEST_ASSERT( expected_public_key->len <=
1080 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001081 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1082 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001083 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001084
1085exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001086 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001087 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001088 mbedtls_psa_crypto_free( );
1089}
1090/* END_CASE */
1091
Gilles Peskine20035e32018-02-03 22:44:14 +01001092/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001093void import_and_exercise_key( data_t *data,
1094 int type_arg,
1095 int bits_arg,
1096 int alg_arg )
1097{
1098 int slot = 1;
1099 psa_key_type_t type = type_arg;
1100 size_t bits = bits_arg;
1101 psa_algorithm_t alg = alg_arg;
1102 psa_key_usage_t usage =
1103 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1104 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1105 PSA_KEY_USAGE_VERIFY :
1106 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1107 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1108 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1109 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1110 PSA_KEY_USAGE_ENCRYPT :
1111 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001112 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001113 0 );
1114 psa_key_policy_t policy;
1115 psa_key_type_t got_type;
1116 size_t got_bits;
1117 psa_status_t status;
1118
1119 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1120
1121 psa_key_policy_init( &policy );
1122 psa_key_policy_set_usage( &policy, usage, alg );
1123 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1124
1125 /* Import the key */
1126 status = psa_import_key( slot, type, data->x, data->len );
1127 TEST_ASSERT( status == PSA_SUCCESS );
1128
1129 /* Test the key information */
1130 TEST_ASSERT( psa_get_key_information( slot,
1131 &got_type,
1132 &got_bits ) == PSA_SUCCESS );
1133 TEST_ASSERT( got_type == type );
1134 TEST_ASSERT( got_bits == bits );
1135
1136 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001137 if( ! exercise_key( slot, usage, alg ) )
1138 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001139
1140exit:
1141 psa_destroy_key( slot );
1142 mbedtls_psa_crypto_free( );
1143}
1144/* END_CASE */
1145
1146/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001147void key_policy( int usage_arg, int alg_arg )
1148{
1149 int key_slot = 1;
1150 psa_algorithm_t alg = alg_arg;
1151 psa_key_usage_t usage = usage_arg;
1152 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1153 unsigned char key[32] = {0};
1154 psa_key_policy_t policy_set;
1155 psa_key_policy_t policy_get;
1156
1157 memset( key, 0x2a, sizeof( key ) );
1158
1159 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1160
1161 psa_key_policy_init( &policy_set );
1162 psa_key_policy_init( &policy_get );
1163
1164 psa_key_policy_set_usage( &policy_set, usage, alg );
1165
1166 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1167 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1168 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1169
1170 TEST_ASSERT( psa_import_key( key_slot, key_type,
1171 key, sizeof( key ) ) == PSA_SUCCESS );
1172
1173 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1174
1175 TEST_ASSERT( policy_get.usage == policy_set.usage );
1176 TEST_ASSERT( policy_get.alg == policy_set.alg );
1177
1178exit:
1179 psa_destroy_key( key_slot );
1180 mbedtls_psa_crypto_free( );
1181}
1182/* END_CASE */
1183
1184/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001185void mac_key_policy( int policy_usage,
1186 int policy_alg,
1187 int key_type,
1188 data_t *key_data,
1189 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001190{
1191 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001192 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001193 psa_mac_operation_t operation;
1194 psa_status_t status;
1195 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001196
1197 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1198
1199 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001200 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001201 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1202
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001203 TEST_ASSERT( psa_import_key( key_slot, key_type,
1204 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001205
Gilles Peskine89167cb2018-07-08 20:12:23 +02001206 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207 if( policy_alg == exercise_alg &&
1208 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1209 TEST_ASSERT( status == PSA_SUCCESS );
1210 else
1211 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1212 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001213
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001215 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001216 if( policy_alg == exercise_alg &&
1217 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001218 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219 else
1220 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1221
1222exit:
1223 psa_mac_abort( &operation );
1224 psa_destroy_key( key_slot );
1225 mbedtls_psa_crypto_free( );
1226}
1227/* END_CASE */
1228
1229/* BEGIN_CASE */
1230void cipher_key_policy( int policy_usage,
1231 int policy_alg,
1232 int key_type,
1233 data_t *key_data,
1234 int exercise_alg )
1235{
1236 int key_slot = 1;
1237 psa_key_policy_t policy;
1238 psa_cipher_operation_t operation;
1239 psa_status_t status;
1240
1241 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1242
1243 psa_key_policy_init( &policy );
1244 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1245 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1246
1247 TEST_ASSERT( psa_import_key( key_slot, key_type,
1248 key_data->x, key_data->len ) == PSA_SUCCESS );
1249
Gilles Peskinefe119512018-07-08 21:39:34 +02001250 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001251 if( policy_alg == exercise_alg &&
1252 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1253 TEST_ASSERT( status == PSA_SUCCESS );
1254 else
1255 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1256 psa_cipher_abort( &operation );
1257
Gilles Peskinefe119512018-07-08 21:39:34 +02001258 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259 if( policy_alg == exercise_alg &&
1260 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1261 TEST_ASSERT( status == PSA_SUCCESS );
1262 else
1263 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1264
1265exit:
1266 psa_cipher_abort( &operation );
1267 psa_destroy_key( key_slot );
1268 mbedtls_psa_crypto_free( );
1269}
1270/* END_CASE */
1271
1272/* BEGIN_CASE */
1273void aead_key_policy( int policy_usage,
1274 int policy_alg,
1275 int key_type,
1276 data_t *key_data,
1277 int nonce_length_arg,
1278 int tag_length_arg,
1279 int exercise_alg )
1280{
1281 int key_slot = 1;
1282 psa_key_policy_t policy;
1283 psa_status_t status;
1284 unsigned char nonce[16] = {0};
1285 size_t nonce_length = nonce_length_arg;
1286 unsigned char tag[16];
1287 size_t tag_length = tag_length_arg;
1288 size_t output_length;
1289
1290 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1291 TEST_ASSERT( tag_length <= sizeof( tag ) );
1292
1293 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1294
1295 psa_key_policy_init( &policy );
1296 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1297 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1298
1299 TEST_ASSERT( psa_import_key( key_slot, key_type,
1300 key_data->x, key_data->len ) == PSA_SUCCESS );
1301
1302 status = psa_aead_encrypt( key_slot, exercise_alg,
1303 nonce, nonce_length,
1304 NULL, 0,
1305 NULL, 0,
1306 tag, tag_length,
1307 &output_length );
1308 if( policy_alg == exercise_alg &&
1309 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1310 TEST_ASSERT( status == PSA_SUCCESS );
1311 else
1312 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1313
1314 memset( tag, 0, sizeof( tag ) );
1315 status = psa_aead_decrypt( key_slot, exercise_alg,
1316 nonce, nonce_length,
1317 NULL, 0,
1318 tag, tag_length,
1319 NULL, 0,
1320 &output_length );
1321 if( policy_alg == exercise_alg &&
1322 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1323 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1324 else
1325 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1326
1327exit:
1328 psa_destroy_key( key_slot );
1329 mbedtls_psa_crypto_free( );
1330}
1331/* END_CASE */
1332
1333/* BEGIN_CASE */
1334void asymmetric_encryption_key_policy( int policy_usage,
1335 int policy_alg,
1336 int key_type,
1337 data_t *key_data,
1338 int exercise_alg )
1339{
1340 int key_slot = 1;
1341 psa_key_policy_t policy;
1342 psa_status_t status;
1343 size_t key_bits;
1344 size_t buffer_length;
1345 unsigned char *buffer = NULL;
1346 size_t output_length;
1347
1348 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1349
1350 psa_key_policy_init( &policy );
1351 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1352 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1353
1354 TEST_ASSERT( psa_import_key( key_slot, key_type,
1355 key_data->x, key_data->len ) == PSA_SUCCESS );
1356
1357 TEST_ASSERT( psa_get_key_information( key_slot,
1358 NULL,
1359 &key_bits ) == PSA_SUCCESS );
1360 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1361 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001362 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001363
1364 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1365 NULL, 0,
1366 NULL, 0,
1367 buffer, buffer_length,
1368 &output_length );
1369 if( policy_alg == exercise_alg &&
1370 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1371 TEST_ASSERT( status == PSA_SUCCESS );
1372 else
1373 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1374
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001375 if( buffer_length != 0 )
1376 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001377 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1378 buffer, buffer_length,
1379 NULL, 0,
1380 buffer, buffer_length,
1381 &output_length );
1382 if( policy_alg == exercise_alg &&
1383 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1384 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1385 else
1386 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1387
1388exit:
1389 psa_destroy_key( key_slot );
1390 mbedtls_psa_crypto_free( );
1391 mbedtls_free( buffer );
1392}
1393/* END_CASE */
1394
1395/* BEGIN_CASE */
1396void asymmetric_signature_key_policy( int policy_usage,
1397 int policy_alg,
1398 int key_type,
1399 data_t *key_data,
1400 int exercise_alg )
1401{
1402 int key_slot = 1;
1403 psa_key_policy_t policy;
1404 psa_status_t status;
1405 unsigned char payload[16] = {1};
1406 size_t payload_length = sizeof( payload );
1407 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1408 size_t signature_length;
1409
1410 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1411
1412 psa_key_policy_init( &policy );
1413 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1414 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1415
1416 TEST_ASSERT( psa_import_key( key_slot, key_type,
1417 key_data->x, key_data->len ) == PSA_SUCCESS );
1418
1419 status = psa_asymmetric_sign( key_slot, exercise_alg,
1420 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001421 signature, sizeof( signature ),
1422 &signature_length );
1423 if( policy_alg == exercise_alg &&
1424 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1425 TEST_ASSERT( status == PSA_SUCCESS );
1426 else
1427 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1428
1429 memset( signature, 0, sizeof( signature ) );
1430 status = psa_asymmetric_verify( key_slot, exercise_alg,
1431 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001432 signature, sizeof( signature ) );
1433 if( policy_alg == exercise_alg &&
1434 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1435 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1436 else
1437 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001438
1439exit:
1440 psa_destroy_key( key_slot );
1441 mbedtls_psa_crypto_free( );
1442}
1443/* END_CASE */
1444
1445/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001446void derive_key_policy( int policy_usage,
1447 int policy_alg,
1448 int key_type,
1449 data_t *key_data,
1450 int exercise_alg )
1451{
1452 int key_slot = 1;
1453 psa_key_policy_t policy;
1454 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1455 psa_status_t status;
1456
1457 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1458
1459 psa_key_policy_init( &policy );
1460 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1461 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1462
1463 TEST_ASSERT( psa_import_key( key_slot, key_type,
1464 key_data->x, key_data->len ) == PSA_SUCCESS );
1465
1466 status = psa_key_derivation( &generator, key_slot,
1467 exercise_alg,
1468 NULL, 0,
1469 NULL, 0,
1470 1 );
1471 if( policy_alg == exercise_alg &&
1472 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1473 TEST_ASSERT( status == PSA_SUCCESS );
1474 else
1475 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1476
1477exit:
1478 psa_generator_abort( &generator );
1479 psa_destroy_key( key_slot );
1480 mbedtls_psa_crypto_free( );
1481}
1482/* END_CASE */
1483
1484/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001485void key_lifetime( int lifetime_arg )
1486{
1487 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001488 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001489 unsigned char key[32] = {0};
1490 psa_key_lifetime_t lifetime_set = lifetime_arg;
1491 psa_key_lifetime_t lifetime_get;
1492
1493 memset( key, 0x2a, sizeof( key ) );
1494
1495 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1496
1497 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1498 lifetime_set ) == PSA_SUCCESS );
1499
1500 TEST_ASSERT( psa_import_key( key_slot, key_type,
1501 key, sizeof( key ) ) == PSA_SUCCESS );
1502
1503 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1504 &lifetime_get ) == PSA_SUCCESS );
1505
1506 TEST_ASSERT( lifetime_get == lifetime_set );
1507
1508exit:
1509 psa_destroy_key( key_slot );
1510 mbedtls_psa_crypto_free( );
1511}
1512/* END_CASE */
1513
1514/* BEGIN_CASE */
1515void key_lifetime_set_fail( int key_slot_arg,
1516 int lifetime_arg,
1517 int expected_status_arg )
1518{
1519 psa_key_slot_t key_slot = key_slot_arg;
1520 psa_key_lifetime_t lifetime_set = lifetime_arg;
1521 psa_status_t actual_status;
1522 psa_status_t expected_status = expected_status_arg;
1523
1524 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1525
1526 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1527
1528 if( actual_status == PSA_SUCCESS )
1529 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1530
1531 TEST_ASSERT( expected_status == actual_status );
1532
1533exit:
1534 psa_destroy_key( key_slot );
1535 mbedtls_psa_crypto_free( );
1536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001540void hash_setup( int alg_arg,
1541 int expected_status_arg )
1542{
1543 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001544 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001545 psa_hash_operation_t operation;
1546 psa_status_t status;
1547
1548 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1549
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001550 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001551 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001552 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001553
1554exit:
1555 mbedtls_psa_crypto_free( );
1556}
1557/* END_CASE */
1558
1559/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001560void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001561{
1562 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001563 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001564 size_t actual_hash_length;
1565 psa_hash_operation_t operation;
1566
Gilles Peskine69c12672018-06-28 00:07:19 +02001567 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1568 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1569
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001570 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001571 TEST_ASSERT( expected_hash != NULL );
1572 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1573 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001574
1575 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1576
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001577 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001579 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001580 TEST_ASSERT( psa_hash_finish( &operation,
1581 actual_hash, sizeof( actual_hash ),
1582 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001583 ASSERT_COMPARE( expected_hash->x, expected_hash->len,
1584 actual_hash, actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585
1586exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001587 mbedtls_psa_crypto_free( );
1588}
1589/* END_CASE */
1590
1591/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001592void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593{
1594 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001595 psa_hash_operation_t operation;
1596
Gilles Peskine69c12672018-06-28 00:07:19 +02001597 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1598 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1599
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001600 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001601 TEST_ASSERT( expected_hash != NULL );
1602 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1603 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001604
1605 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1606
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001607 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001608 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001609 input->x,
1610 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001611 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001612 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001613 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001614
1615exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001616 mbedtls_psa_crypto_free( );
1617}
1618/* END_CASE */
1619
1620/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001621void mac_setup( int key_type_arg,
1622 data_t *key,
1623 int alg_arg,
1624 int expected_status_arg )
1625{
1626 int key_slot = 1;
1627 psa_key_type_t key_type = key_type_arg;
1628 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001629 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001630 psa_mac_operation_t operation;
1631 psa_key_policy_t policy;
1632 psa_status_t status;
1633
1634 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1635
1636 psa_key_policy_init( &policy );
1637 psa_key_policy_set_usage( &policy,
1638 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1639 alg );
1640 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1641
1642 TEST_ASSERT( psa_import_key( key_slot, key_type,
1643 key->x, key->len ) == PSA_SUCCESS );
1644
Gilles Peskine89167cb2018-07-08 20:12:23 +02001645 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001646 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001647 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001648
1649exit:
1650 psa_destroy_key( key_slot );
1651 mbedtls_psa_crypto_free( );
1652}
1653/* END_CASE */
1654
1655/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001656void mac_sign( int key_type_arg,
1657 data_t *key,
1658 int alg_arg,
1659 data_t *input,
1660 data_t *expected_mac )
1661{
1662 int key_slot = 1;
1663 psa_key_type_t key_type = key_type_arg;
1664 psa_algorithm_t alg = alg_arg;
1665 psa_mac_operation_t operation;
1666 psa_key_policy_t policy;
1667 /* Leave a little extra room in the output buffer. At the end of the
1668 * test, we'll check that the implementation didn't overwrite onto
1669 * this extra room. */
1670 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1671 size_t mac_buffer_size =
1672 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1673 size_t mac_length = 0;
1674
1675 memset( actual_mac, '+', sizeof( actual_mac ) );
1676 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1677 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1678
1679 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1680
1681 psa_key_policy_init( &policy );
1682 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1683 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1684
1685 TEST_ASSERT( psa_import_key( key_slot, key_type,
1686 key->x, key->len ) == PSA_SUCCESS );
1687
1688 /* Calculate the MAC. */
1689 TEST_ASSERT( psa_mac_sign_setup( &operation,
1690 key_slot, alg ) == PSA_SUCCESS );
1691 TEST_ASSERT( psa_mac_update( &operation,
1692 input->x, input->len ) == PSA_SUCCESS );
1693 TEST_ASSERT( psa_mac_sign_finish( &operation,
1694 actual_mac, mac_buffer_size,
1695 &mac_length ) == PSA_SUCCESS );
1696
1697 /* Compare with the expected value. */
1698 TEST_ASSERT( mac_length == expected_mac->len );
1699 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1700
1701 /* Verify that the end of the buffer is untouched. */
1702 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1703 sizeof( actual_mac ) - mac_length ) );
1704
1705exit:
1706 psa_destroy_key( key_slot );
1707 mbedtls_psa_crypto_free( );
1708}
1709/* END_CASE */
1710
1711/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001712void mac_verify( int key_type_arg,
1713 data_t *key,
1714 int alg_arg,
1715 data_t *input,
1716 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001717{
1718 int key_slot = 1;
1719 psa_key_type_t key_type = key_type_arg;
1720 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001721 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001722 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001723
Gilles Peskine69c12672018-06-28 00:07:19 +02001724 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1725
Gilles Peskine8c9def32018-02-08 10:02:12 +01001726 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001727 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001728 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001729 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001730 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1731 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001732
1733 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1734
mohammad16036df908f2018-04-02 08:34:15 -07001735 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001736 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001737 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1738
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001740 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001741
Gilles Peskine89167cb2018-07-08 20:12:23 +02001742 TEST_ASSERT( psa_mac_verify_setup( &operation,
1743 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1745 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001746 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001747 TEST_ASSERT( psa_mac_verify_finish( &operation,
1748 expected_mac->x,
1749 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750
1751exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752 psa_destroy_key( key_slot );
1753 mbedtls_psa_crypto_free( );
1754}
1755/* END_CASE */
1756
1757/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001758void cipher_setup( int key_type_arg,
1759 data_t *key,
1760 int alg_arg,
1761 int expected_status_arg )
1762{
1763 int key_slot = 1;
1764 psa_key_type_t key_type = key_type_arg;
1765 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001766 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001767 psa_cipher_operation_t operation;
1768 psa_key_policy_t policy;
1769 psa_status_t status;
1770
1771 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1772
1773 psa_key_policy_init( &policy );
1774 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1775 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1776
1777 TEST_ASSERT( psa_import_key( key_slot, key_type,
1778 key->x, key->len ) == PSA_SUCCESS );
1779
Gilles Peskinefe119512018-07-08 21:39:34 +02001780 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001781 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001782 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001783
1784exit:
1785 psa_destroy_key( key_slot );
1786 mbedtls_psa_crypto_free( );
1787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001791void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001792 data_t *key,
1793 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001794 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001795{
1796 int key_slot = 1;
1797 psa_status_t status;
1798 psa_key_type_t key_type = key_type_arg;
1799 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001800 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001801 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001802 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001803 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001804 size_t output_buffer_size = 0;
1805 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001806 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001807 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001808 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001809
Gilles Peskine50e586b2018-06-08 14:28:46 +02001810 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001811 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001812 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001813 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1814 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1815 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001816
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001817 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1818 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001819
1820 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1821
Moran Pekered346952018-07-05 15:22:45 +03001822 psa_key_policy_init( &policy );
1823 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1824 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1825
Gilles Peskine50e586b2018-06-08 14:28:46 +02001826 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001827 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001828
Gilles Peskinefe119512018-07-08 21:39:34 +02001829 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1830 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001831
Gilles Peskinefe119512018-07-08 21:39:34 +02001832 TEST_ASSERT( psa_cipher_set_iv( &operation,
1833 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001834 output_buffer_size = (size_t) input->len +
1835 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001836 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001837
Gilles Peskine4abf7412018-06-18 16:35:34 +02001838 TEST_ASSERT( psa_cipher_update( &operation,
1839 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001840 output, output_buffer_size,
1841 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001842 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001843 status = psa_cipher_finish( &operation,
1844 output + function_output_length,
1845 output_buffer_size,
1846 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001847 total_output_length += function_output_length;
1848
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001849 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001850 if( expected_status == PSA_SUCCESS )
1851 {
1852 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001853 ASSERT_COMPARE( expected_output->x, expected_output->len,
1854 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001855 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001856
Gilles Peskine50e586b2018-06-08 14:28:46 +02001857exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001858 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001859 psa_destroy_key( key_slot );
1860 mbedtls_psa_crypto_free( );
1861}
1862/* END_CASE */
1863
1864/* BEGIN_CASE */
1865void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001866 data_t *key,
1867 data_t *input,
1868 int first_part_size,
1869 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001870{
1871 int key_slot = 1;
1872 psa_key_type_t key_type = key_type_arg;
1873 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001874 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001875 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001876 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001877 size_t output_buffer_size = 0;
1878 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001879 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001880 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001881 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001882
Gilles Peskine50e586b2018-06-08 14:28:46 +02001883 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001884 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001885 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001886 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1887 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1888 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001889
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001890 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1891 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001892
1893 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1894
Moran Pekered346952018-07-05 15:22:45 +03001895 psa_key_policy_init( &policy );
1896 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1897 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1898
Gilles Peskine50e586b2018-06-08 14:28:46 +02001899 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001900 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001901
Gilles Peskinefe119512018-07-08 21:39:34 +02001902 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1903 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001904
Gilles Peskinefe119512018-07-08 21:39:34 +02001905 TEST_ASSERT( psa_cipher_set_iv( &operation,
1906 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001907 output_buffer_size = (size_t) input->len +
1908 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001909 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001910
Gilles Peskine4abf7412018-06-18 16:35:34 +02001911 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001912 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001913 output, output_buffer_size,
1914 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001915 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001916 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001917 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001918 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001919 output, output_buffer_size,
1920 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001921 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001922 TEST_ASSERT( psa_cipher_finish( &operation,
1923 output + function_output_length,
1924 output_buffer_size,
1925 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001926 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001927 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1928
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001929 ASSERT_COMPARE( expected_output->x, expected_output->len,
1930 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001931
1932exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001933 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001934 psa_destroy_key( key_slot );
1935 mbedtls_psa_crypto_free( );
1936}
1937/* END_CASE */
1938
1939/* BEGIN_CASE */
1940void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001941 data_t *key,
1942 data_t *input,
1943 int first_part_size,
1944 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001945{
1946 int key_slot = 1;
1947
1948 psa_key_type_t key_type = key_type_arg;
1949 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001950 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001951 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001952 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001953 size_t output_buffer_size = 0;
1954 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001955 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001956 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001957 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001958
Gilles Peskine50e586b2018-06-08 14:28:46 +02001959 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001960 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001961 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001962 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1963 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1964 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001965
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001966 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1967 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001968
1969 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1970
Moran Pekered346952018-07-05 15:22:45 +03001971 psa_key_policy_init( &policy );
1972 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1973 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1974
Gilles Peskine50e586b2018-06-08 14:28:46 +02001975 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001976 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001977
Gilles Peskinefe119512018-07-08 21:39:34 +02001978 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1979 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001980
Gilles Peskinefe119512018-07-08 21:39:34 +02001981 TEST_ASSERT( psa_cipher_set_iv( &operation,
1982 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001983
mohammad16033d91abe2018-07-03 13:15:54 +03001984 output_buffer_size = (size_t) input->len +
1985 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001986 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001987
Gilles Peskine4abf7412018-06-18 16:35:34 +02001988 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1989 TEST_ASSERT( psa_cipher_update( &operation,
1990 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001991 output, output_buffer_size,
1992 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001993 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001994 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001995 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001996 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001997 output, output_buffer_size,
1998 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001999 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002000 TEST_ASSERT( psa_cipher_finish( &operation,
2001 output + function_output_length,
2002 output_buffer_size,
2003 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002004 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002005 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2006
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002007 ASSERT_COMPARE( expected_output->x, expected_output->len,
2008 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002009
2010exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002011 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002012 psa_destroy_key( key_slot );
2013 mbedtls_psa_crypto_free( );
2014}
2015/* END_CASE */
2016
Gilles Peskine50e586b2018-06-08 14:28:46 +02002017/* BEGIN_CASE */
2018void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002019 data_t *key,
2020 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002021 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002022{
2023 int key_slot = 1;
2024 psa_status_t status;
2025 psa_key_type_t key_type = key_type_arg;
2026 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002027 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002028 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002029 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002030 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002031 size_t output_buffer_size = 0;
2032 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002033 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002035 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002036
Gilles Peskine50e586b2018-06-08 14:28:46 +02002037 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002038 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002039 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002040 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2041 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2042 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002043
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002044 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2045 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002046
2047 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2048
Moran Pekered346952018-07-05 15:22:45 +03002049 psa_key_policy_init( &policy );
2050 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2051 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2052
Gilles Peskine50e586b2018-06-08 14:28:46 +02002053 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002054 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002055
Gilles Peskinefe119512018-07-08 21:39:34 +02002056 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2057 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002058
Gilles Peskinefe119512018-07-08 21:39:34 +02002059 TEST_ASSERT( psa_cipher_set_iv( &operation,
2060 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002061
mohammad16033d91abe2018-07-03 13:15:54 +03002062 output_buffer_size = (size_t) input->len +
2063 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002064 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002065
Gilles Peskine4abf7412018-06-18 16:35:34 +02002066 TEST_ASSERT( psa_cipher_update( &operation,
2067 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002068 output, output_buffer_size,
2069 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002070 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002071 status = psa_cipher_finish( &operation,
2072 output + function_output_length,
2073 output_buffer_size,
2074 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002075 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002076 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002077
2078 if( expected_status == PSA_SUCCESS )
2079 {
2080 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002081 ASSERT_COMPARE( expected_output->x, expected_output->len,
2082 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002083 }
2084
Gilles Peskine50e586b2018-06-08 14:28:46 +02002085exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002086 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002087 psa_destroy_key( key_slot );
2088 mbedtls_psa_crypto_free( );
2089}
2090/* END_CASE */
2091
Gilles Peskine50e586b2018-06-08 14:28:46 +02002092/* BEGIN_CASE */
2093void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002094 data_t *key,
2095 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002096{
2097 int key_slot = 1;
2098 psa_key_type_t key_type = key_type_arg;
2099 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002100 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002101 size_t iv_size = 16;
2102 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002103 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002104 size_t output1_size = 0;
2105 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002106 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002107 size_t output2_size = 0;
2108 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002109 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002110 psa_cipher_operation_t operation1;
2111 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002112 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002113
mohammad1603d7d7ba52018-03-12 18:51:53 +02002114 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002115 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002116 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2117 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002118
mohammad1603d7d7ba52018-03-12 18:51:53 +02002119 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2120
Moran Pekered346952018-07-05 15:22:45 +03002121 psa_key_policy_init( &policy );
2122 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2123 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2124
mohammad1603d7d7ba52018-03-12 18:51:53 +02002125 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002126 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002127
Gilles Peskinefe119512018-07-08 21:39:34 +02002128 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2129 key_slot, alg ) == PSA_SUCCESS );
2130 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2131 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002132
Gilles Peskinefe119512018-07-08 21:39:34 +02002133 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2134 iv, iv_size,
2135 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002136 output1_size = (size_t) input->len +
2137 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002138 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002139
Gilles Peskine4abf7412018-06-18 16:35:34 +02002140 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002141 output1, output1_size,
2142 &output1_length ) == PSA_SUCCESS );
2143 TEST_ASSERT( psa_cipher_finish( &operation1,
2144 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002145 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002146
Gilles Peskine048b7f02018-06-08 14:20:49 +02002147 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002148
2149 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2150
2151 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002152 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002153
Gilles Peskinefe119512018-07-08 21:39:34 +02002154 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2155 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002156 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2157 output2, output2_size,
2158 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002159 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002160 TEST_ASSERT( psa_cipher_finish( &operation2,
2161 output2 + output2_length,
2162 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002163 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002164
Gilles Peskine048b7f02018-06-08 14:20:49 +02002165 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002166
Janos Follath25c4fa82018-07-06 16:23:25 +01002167 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002168
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002169 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002170
2171exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002172 mbedtls_free( output1 );
2173 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002174 psa_destroy_key( key_slot );
2175 mbedtls_psa_crypto_free( );
2176}
2177/* END_CASE */
2178
2179/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180void cipher_verify_output_multipart( int alg_arg,
2181 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002182 data_t *key,
2183 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002184 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002185{
2186 int key_slot = 1;
2187 psa_key_type_t key_type = key_type_arg;
2188 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002189 unsigned char iv[16] = {0};
2190 size_t iv_size = 16;
2191 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002192 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002193 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002194 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002195 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002196 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002197 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002198 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002199 psa_cipher_operation_t operation1;
2200 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002201 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002202
Moran Pekerded84402018-06-06 16:36:50 +03002203 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002204 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002205 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2206 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002207
Moran Pekerded84402018-06-06 16:36:50 +03002208 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2209
Moran Pekered346952018-07-05 15:22:45 +03002210 psa_key_policy_init( &policy );
2211 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2212 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2213
Moran Pekerded84402018-06-06 16:36:50 +03002214 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002215 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002216
Gilles Peskinefe119512018-07-08 21:39:34 +02002217 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2218 key_slot, alg ) == PSA_SUCCESS );
2219 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2220 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002221
Gilles Peskinefe119512018-07-08 21:39:34 +02002222 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2223 iv, iv_size,
2224 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002225 output1_buffer_size = (size_t) input->len +
2226 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002227 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002228
Gilles Peskine4abf7412018-06-18 16:35:34 +02002229 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002230
itayzafrir3e02b3b2018-06-12 17:06:52 +03002231 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002232 output1, output1_buffer_size,
2233 &function_output_length ) == PSA_SUCCESS );
2234 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002235
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002236 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002237 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002238 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002239 output1, output1_buffer_size,
2240 &function_output_length ) == PSA_SUCCESS );
2241 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002242
Gilles Peskine048b7f02018-06-08 14:20:49 +02002243 TEST_ASSERT( psa_cipher_finish( &operation1,
2244 output1 + output1_length,
2245 output1_buffer_size - output1_length,
2246 &function_output_length ) == PSA_SUCCESS );
2247 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002248
2249 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2250
Gilles Peskine048b7f02018-06-08 14:20:49 +02002251 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002252 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002253
Gilles Peskinefe119512018-07-08 21:39:34 +02002254 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2255 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002256
2257 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002258 output2, output2_buffer_size,
2259 &function_output_length ) == PSA_SUCCESS );
2260 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002261
Gilles Peskine048b7f02018-06-08 14:20:49 +02002262 TEST_ASSERT( psa_cipher_update( &operation2,
2263 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002264 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002265 output2, output2_buffer_size,
2266 &function_output_length ) == PSA_SUCCESS );
2267 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002268
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002269 TEST_ASSERT( psa_cipher_finish( &operation2,
2270 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002271 output2_buffer_size - output2_length,
2272 &function_output_length ) == PSA_SUCCESS );
2273 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002274
Janos Follath25c4fa82018-07-06 16:23:25 +01002275 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002276
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002277 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002278
2279exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002280 mbedtls_free( output1 );
2281 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002282 psa_destroy_key( key_slot );
2283 mbedtls_psa_crypto_free( );
2284}
2285/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002286
Gilles Peskine20035e32018-02-03 22:44:14 +01002287/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002288void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002289 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002290 data_t *nonce,
2291 data_t *additional_data,
2292 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002293 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002294{
2295 int slot = 1;
2296 psa_key_type_t key_type = key_type_arg;
2297 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002298 unsigned char *output_data = NULL;
2299 size_t output_size = 0;
2300 size_t output_length = 0;
2301 unsigned char *output_data2 = NULL;
2302 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002303 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002304 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002305 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002306
Gilles Peskinea1cac842018-06-11 19:33:02 +02002307 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002308 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002309 TEST_ASSERT( nonce != NULL );
2310 TEST_ASSERT( additional_data != NULL );
2311 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2312 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2313 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2314 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2315
Gilles Peskine4abf7412018-06-18 16:35:34 +02002316 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002317 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002318
2319 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2320
2321 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002322 psa_key_policy_set_usage( &policy,
2323 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2324 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002325 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2326
2327 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002328 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002329
2330 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002331 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002332 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002333 additional_data->len,
2334 input_data->x, input_data->len,
2335 output_data, output_size,
2336 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002337
2338 if( PSA_SUCCESS == expected_result )
2339 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002340 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002341
2342 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002343 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002344 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002345 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002346 output_data, output_length,
2347 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002348 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002349
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002350 ASSERT_COMPARE( input_data->x, input_data->len,
2351 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002352 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002353
Gilles Peskinea1cac842018-06-11 19:33:02 +02002354exit:
2355 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002356 mbedtls_free( output_data );
2357 mbedtls_free( output_data2 );
2358 mbedtls_psa_crypto_free( );
2359}
2360/* END_CASE */
2361
2362/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002363void aead_encrypt( int key_type_arg, data_t *key_data,
2364 int alg_arg,
2365 data_t *nonce,
2366 data_t *additional_data,
2367 data_t *input_data,
2368 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002369{
2370 int slot = 1;
2371 psa_key_type_t key_type = key_type_arg;
2372 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002373 unsigned char *output_data = NULL;
2374 size_t output_size = 0;
2375 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002376 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002377 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002378
Gilles Peskinea1cac842018-06-11 19:33:02 +02002379 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002380 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002381 TEST_ASSERT( additional_data != NULL );
2382 TEST_ASSERT( nonce != NULL );
2383 TEST_ASSERT( expected_result != NULL );
2384 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2385 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2386 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2387 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2388 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2389
Gilles Peskine4abf7412018-06-18 16:35:34 +02002390 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002391 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002392
Gilles Peskinea1cac842018-06-11 19:33:02 +02002393 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2394
2395 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002396 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002397 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2398
2399 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002400 key_data->x,
2401 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002402
2403 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002404 nonce->x, nonce->len,
2405 additional_data->x, additional_data->len,
2406 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002407 output_data, output_size,
2408 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002409
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002410 ASSERT_COMPARE( expected_result->x, expected_result->len,
2411 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002412
Gilles Peskinea1cac842018-06-11 19:33:02 +02002413exit:
2414 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002415 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002416 mbedtls_psa_crypto_free( );
2417}
2418/* END_CASE */
2419
2420/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002421void aead_decrypt( int key_type_arg, data_t *key_data,
2422 int alg_arg,
2423 data_t *nonce,
2424 data_t *additional_data,
2425 data_t *input_data,
2426 data_t *expected_data,
2427 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002428{
2429 int slot = 1;
2430 psa_key_type_t key_type = key_type_arg;
2431 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002432 unsigned char *output_data = NULL;
2433 size_t output_size = 0;
2434 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002435 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002436 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002437 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002438
Gilles Peskinea1cac842018-06-11 19:33:02 +02002439 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002440 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002441 TEST_ASSERT( additional_data != NULL );
2442 TEST_ASSERT( nonce != NULL );
2443 TEST_ASSERT( expected_data != NULL );
2444 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2445 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2446 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2447 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2448 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2449
Gilles Peskine4abf7412018-06-18 16:35:34 +02002450 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002451 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002452
Gilles Peskinea1cac842018-06-11 19:33:02 +02002453 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2454
2455 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002456 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002457 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2458
2459 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002460 key_data->x,
2461 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002462
2463 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002464 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002465 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002466 additional_data->len,
2467 input_data->x, input_data->len,
2468 output_data, output_size,
2469 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002470
Gilles Peskine2d277862018-06-18 15:41:12 +02002471 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002472 ASSERT_COMPARE( expected_data->x, expected_data->len,
2473 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002474
Gilles Peskinea1cac842018-06-11 19:33:02 +02002475exit:
2476 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002477 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002478 mbedtls_psa_crypto_free( );
2479}
2480/* END_CASE */
2481
2482/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002483void signature_size( int type_arg,
2484 int bits,
2485 int alg_arg,
2486 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002487{
2488 psa_key_type_t type = type_arg;
2489 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002490 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002491 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2492exit:
2493 ;
2494}
2495/* END_CASE */
2496
2497/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002498void sign_deterministic( int key_type_arg, data_t *key_data,
2499 int alg_arg, data_t *input_data,
2500 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002501{
2502 int slot = 1;
2503 psa_key_type_t key_type = key_type_arg;
2504 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002505 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002506 unsigned char *signature = NULL;
2507 size_t signature_size;
2508 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002509 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002510
Gilles Peskine20035e32018-02-03 22:44:14 +01002511 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002512 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002513 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002514 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2515 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2516 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002517
2518 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2519
mohammad1603a97cb8c2018-03-28 03:46:26 -07002520 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002521 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002522 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2523
Gilles Peskine20035e32018-02-03 22:44:14 +01002524 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002525 key_data->x,
2526 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002527 TEST_ASSERT( psa_get_key_information( slot,
2528 NULL,
2529 &key_bits ) == PSA_SUCCESS );
2530
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002531 /* Allocate a buffer which has the size advertized by the
2532 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002533 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2534 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002535 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002536 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002537 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002538
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002539 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002540 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002541 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002542 signature, signature_size,
2543 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002544 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002545 ASSERT_COMPARE( output_data->x, output_data->len,
2546 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002547
2548exit:
2549 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002550 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002551 mbedtls_psa_crypto_free( );
2552}
2553/* END_CASE */
2554
2555/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002556void sign_fail( int key_type_arg, data_t *key_data,
2557 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002558 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002559{
2560 int slot = 1;
2561 psa_key_type_t key_type = key_type_arg;
2562 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002563 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002564 psa_status_t actual_status;
2565 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002566 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002567 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002568 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002569
Gilles Peskine20035e32018-02-03 22:44:14 +01002570 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002571 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002572 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2573 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2574
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002575 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002576
2577 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2578
mohammad1603a97cb8c2018-03-28 03:46:26 -07002579 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002580 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002581 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2582
Gilles Peskine20035e32018-02-03 22:44:14 +01002583 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002584 key_data->x,
2585 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002586
2587 actual_status = 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 );
2591 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002592 /* The value of *signature_length is unspecified on error, but
2593 * whatever it is, it should be less than signature_size, so that
2594 * if the caller tries to read *signature_length bytes without
2595 * checking the error code then they don't overflow a buffer. */
2596 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002597
2598exit:
2599 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002600 mbedtls_free( signature );
2601 mbedtls_psa_crypto_free( );
2602}
2603/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002604
2605/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002606void sign_verify( int key_type_arg, data_t *key_data,
2607 int alg_arg, data_t *input_data )
2608{
2609 int slot = 1;
2610 psa_key_type_t key_type = key_type_arg;
2611 psa_algorithm_t alg = alg_arg;
2612 size_t key_bits;
2613 unsigned char *signature = NULL;
2614 size_t signature_size;
2615 size_t signature_length = 0xdeadbeef;
2616 psa_key_policy_t policy;
2617
2618 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2619
2620 psa_key_policy_init( &policy );
2621 psa_key_policy_set_usage( &policy,
2622 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2623 alg );
2624 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2625
2626 TEST_ASSERT( psa_import_key( slot, key_type,
2627 key_data->x,
2628 key_data->len ) == PSA_SUCCESS );
2629 TEST_ASSERT( psa_get_key_information( slot,
2630 NULL,
2631 &key_bits ) == PSA_SUCCESS );
2632
2633 /* Allocate a buffer which has the size advertized by the
2634 * library. */
2635 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2636 key_bits, alg );
2637 TEST_ASSERT( signature_size != 0 );
2638 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002639 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002640
2641 /* Perform the signature. */
2642 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2643 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002644 signature, signature_size,
2645 &signature_length ) == PSA_SUCCESS );
2646 /* Check that the signature length looks sensible. */
2647 TEST_ASSERT( signature_length <= signature_size );
2648 TEST_ASSERT( signature_length > 0 );
2649
2650 /* Use the library to verify that the signature is correct. */
2651 TEST_ASSERT( psa_asymmetric_verify(
2652 slot, alg,
2653 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002654 signature, signature_length ) == PSA_SUCCESS );
2655
2656 if( input_data->len != 0 )
2657 {
2658 /* Flip a bit in the input and verify that the signature is now
2659 * detected as invalid. Flip a bit at the beginning, not at the end,
2660 * because ECDSA may ignore the last few bits of the input. */
2661 input_data->x[0] ^= 1;
2662 TEST_ASSERT( psa_asymmetric_verify(
2663 slot, alg,
2664 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002665 signature,
2666 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2667 }
2668
2669exit:
2670 psa_destroy_key( slot );
2671 mbedtls_free( signature );
2672 mbedtls_psa_crypto_free( );
2673}
2674/* END_CASE */
2675
2676/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002677void asymmetric_verify( int key_type_arg, data_t *key_data,
2678 int alg_arg, data_t *hash_data,
2679 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002680{
2681 int slot = 1;
2682 psa_key_type_t key_type = key_type_arg;
2683 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002684 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002685
Gilles Peskine69c12672018-06-28 00:07:19 +02002686 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2687
itayzafrir5c753392018-05-08 11:18:38 +03002688 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002689 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002690 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002691 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2692 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2693 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002694
2695 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2696
2697 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002698 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002699 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2700
2701 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002702 key_data->x,
2703 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002704
2705 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002706 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002707 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002708 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002709exit:
2710 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002711 mbedtls_psa_crypto_free( );
2712}
2713/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714
2715/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002716void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2717 int alg_arg, data_t *hash_data,
2718 data_t *signature_data,
2719 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002720{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 int slot = 1;
2722 psa_key_type_t key_type = key_type_arg;
2723 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002724 psa_status_t actual_status;
2725 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002726 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002728 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002729 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002730 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002731 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2732 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2733 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002734
2735 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2736
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002737 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002738 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002739 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2740
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002741 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002742 key_data->x,
2743 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002744
2745 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002746 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002747 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002748 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002749
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002750 TEST_ASSERT( actual_status == expected_status );
2751
2752exit:
2753 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002754 mbedtls_psa_crypto_free( );
2755}
2756/* END_CASE */
2757
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002758/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002759void asymmetric_encrypt( int key_type_arg,
2760 data_t *key_data,
2761 int alg_arg,
2762 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002763 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002764 int expected_output_length_arg,
2765 int expected_status_arg )
2766{
2767 int slot = 1;
2768 psa_key_type_t key_type = key_type_arg;
2769 psa_algorithm_t alg = alg_arg;
2770 size_t expected_output_length = expected_output_length_arg;
2771 size_t key_bits;
2772 unsigned char *output = NULL;
2773 size_t output_size;
2774 size_t output_length = ~0;
2775 psa_status_t actual_status;
2776 psa_status_t expected_status = expected_status_arg;
2777 psa_key_policy_t policy;
2778
2779 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2780
2781 /* Import the key */
2782 psa_key_policy_init( &policy );
2783 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2784 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2785 TEST_ASSERT( psa_import_key( slot, key_type,
2786 key_data->x,
2787 key_data->len ) == PSA_SUCCESS );
2788
2789 /* Determine the maximum output length */
2790 TEST_ASSERT( psa_get_key_information( slot,
2791 NULL,
2792 &key_bits ) == PSA_SUCCESS );
2793 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002794 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002795
2796 /* Encrypt the input */
2797 actual_status = psa_asymmetric_encrypt( slot, alg,
2798 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002799 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002800 output, output_size,
2801 &output_length );
2802 TEST_ASSERT( actual_status == expected_status );
2803 TEST_ASSERT( output_length == expected_output_length );
2804
Gilles Peskine68428122018-06-30 18:42:41 +02002805 /* If the label is empty, the test framework puts a non-null pointer
2806 * in label->x. Test that a null pointer works as well. */
2807 if( label->len == 0 )
2808 {
2809 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002810 if( output_size != 0 )
2811 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002812 actual_status = psa_asymmetric_encrypt( slot, alg,
2813 input_data->x, input_data->len,
2814 NULL, label->len,
2815 output, output_size,
2816 &output_length );
2817 TEST_ASSERT( actual_status == expected_status );
2818 TEST_ASSERT( output_length == expected_output_length );
2819 }
2820
Gilles Peskine656896e2018-06-29 19:12:28 +02002821exit:
2822 psa_destroy_key( slot );
2823 mbedtls_free( output );
2824 mbedtls_psa_crypto_free( );
2825}
2826/* END_CASE */
2827
2828/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002829void asymmetric_encrypt_decrypt( int key_type_arg,
2830 data_t *key_data,
2831 int alg_arg,
2832 data_t *input_data,
2833 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002834{
2835 int slot = 1;
2836 psa_key_type_t key_type = key_type_arg;
2837 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002838 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002839 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002840 size_t output_size;
2841 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002842 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002843 size_t output2_size;
2844 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002845 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002846
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002847 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002848 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002849 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2850 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2851
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002852 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2853
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002854 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002855 psa_key_policy_set_usage( &policy,
2856 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002857 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002858 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2859
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002860 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002861 key_data->x,
2862 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002863
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002864
2865 /* Determine the maximum ciphertext length */
2866 TEST_ASSERT( psa_get_key_information( slot,
2867 NULL,
2868 &key_bits ) == PSA_SUCCESS );
2869 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002870 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002871 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002872 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002873
Gilles Peskineeebd7382018-06-08 18:11:54 +02002874 /* We test encryption by checking that encrypt-then-decrypt gives back
2875 * the original plaintext because of the non-optional random
2876 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002877 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002878 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002879 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002880 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002881 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002882 /* We don't know what ciphertext length to expect, but check that
2883 * it looks sensible. */
2884 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002885
Gilles Peskine2d277862018-06-18 15:41:12 +02002886 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002887 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002888 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002889 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002890 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002891 ASSERT_COMPARE( input_data->x, input_data->len,
2892 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893
2894exit:
2895 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002896 mbedtls_free( output );
2897 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002898 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002899}
2900/* END_CASE */
2901
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002902/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002903void asymmetric_decrypt( int key_type_arg,
2904 data_t *key_data,
2905 int alg_arg,
2906 data_t *input_data,
2907 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002908 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002909{
2910 int slot = 1;
2911 psa_key_type_t key_type = key_type_arg;
2912 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002913 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002914 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002915 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002916 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002917
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002918 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002919 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002920 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002921 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2922 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2923 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2924
Gilles Peskine4abf7412018-06-18 16:35:34 +02002925 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002926 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002927
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002928 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2929
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002930 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002931 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002932 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2933
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002934 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002935 key_data->x,
2936 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002937
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002938 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002939 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002940 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002941 output,
2942 output_size,
2943 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002944 ASSERT_COMPARE( expected_data->x, expected_data->len,
2945 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002946
Gilles Peskine68428122018-06-30 18:42:41 +02002947 /* If the label is empty, the test framework puts a non-null pointer
2948 * in label->x. Test that a null pointer works as well. */
2949 if( label->len == 0 )
2950 {
2951 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002952 if( output_size != 0 )
2953 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002954 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2955 input_data->x, input_data->len,
2956 NULL, label->len,
2957 output,
2958 output_size,
2959 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002960 ASSERT_COMPARE( expected_data->x, expected_data->len,
2961 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02002962 }
2963
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964exit:
2965 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002966 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002967 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002968}
2969/* END_CASE */
2970
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002971/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002972void asymmetric_decrypt_fail( int key_type_arg,
2973 data_t *key_data,
2974 int alg_arg,
2975 data_t *input_data,
2976 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002977 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979 int slot = 1;
2980 psa_key_type_t key_type = key_type_arg;
2981 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002983 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002984 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002985 psa_status_t actual_status;
2986 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002987 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002989 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002990 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002991 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2992 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2993
Gilles Peskine4abf7412018-06-18 16:35:34 +02002994 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002995 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002996
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002997 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2998
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002999 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003000 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003001 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3002
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003003 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003004 key_data->x,
3005 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003006
Gilles Peskine2d277862018-06-18 15:41:12 +02003007 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003008 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003009 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003010 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003011 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003012 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003013 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003014
Gilles Peskine68428122018-06-30 18:42:41 +02003015 /* If the label is empty, the test framework puts a non-null pointer
3016 * in label->x. Test that a null pointer works as well. */
3017 if( label->len == 0 )
3018 {
3019 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003020 if( output_size != 0 )
3021 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003022 actual_status = psa_asymmetric_decrypt( slot, alg,
3023 input_data->x, input_data->len,
3024 NULL, label->len,
3025 output, output_size,
3026 &output_length );
3027 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003028 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003029 }
3030
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003031exit:
3032 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003033 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003034 mbedtls_psa_crypto_free( );
3035}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003036/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003037
3038/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003039void derive_setup( int key_type_arg,
3040 data_t *key_data,
3041 int alg_arg,
3042 data_t *salt,
3043 data_t *label,
3044 int requested_capacity_arg,
3045 int expected_status_arg )
3046{
3047 psa_key_slot_t slot = 1;
3048 size_t key_type = key_type_arg;
3049 psa_algorithm_t alg = alg_arg;
3050 size_t requested_capacity = requested_capacity_arg;
3051 psa_status_t expected_status = expected_status_arg;
3052 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3053 psa_key_policy_t policy;
3054
3055 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3056
3057 psa_key_policy_init( &policy );
3058 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3059 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3060
3061 TEST_ASSERT( psa_import_key( slot, key_type,
3062 key_data->x,
3063 key_data->len ) == PSA_SUCCESS );
3064
3065 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3066 salt->x, salt->len,
3067 label->x, label->len,
3068 requested_capacity ) == expected_status );
3069
3070exit:
3071 psa_generator_abort( &generator );
3072 psa_destroy_key( slot );
3073 mbedtls_psa_crypto_free( );
3074}
3075/* END_CASE */
3076
3077/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003078void derive_output( int alg_arg,
3079 data_t *key_data,
3080 data_t *salt,
3081 data_t *label,
3082 int requested_capacity_arg,
3083 data_t *expected_output1,
3084 data_t *expected_output2 )
3085{
3086 psa_key_slot_t slot = 1;
3087 psa_algorithm_t alg = alg_arg;
3088 size_t requested_capacity = requested_capacity_arg;
3089 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3090 uint8_t *expected_outputs[2] =
3091 {expected_output1->x, expected_output2->x};
3092 size_t output_sizes[2] =
3093 {expected_output1->len, expected_output2->len};
3094 size_t output_buffer_size = 0;
3095 uint8_t *output_buffer = NULL;
3096 size_t expected_capacity;
3097 size_t current_capacity;
3098 psa_key_policy_t policy;
3099 psa_status_t status;
3100 unsigned i;
3101
3102 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3103 {
3104 if( output_sizes[i] > output_buffer_size )
3105 output_buffer_size = output_sizes[i];
3106 if( output_sizes[i] == 0 )
3107 expected_outputs[i] = NULL;
3108 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003109 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003110 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3111
3112 psa_key_policy_init( &policy );
3113 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3114 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3115
3116 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3117 key_data->x,
3118 key_data->len ) == PSA_SUCCESS );
3119
3120 /* Extraction phase. */
3121 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3122 salt->x, salt->len,
3123 label->x, label->len,
3124 requested_capacity ) == PSA_SUCCESS );
3125 TEST_ASSERT( psa_get_generator_capacity( &generator,
3126 &current_capacity ) ==
3127 PSA_SUCCESS );
3128 TEST_ASSERT( current_capacity == requested_capacity );
3129 expected_capacity = requested_capacity;
3130
3131 /* Expansion phase. */
3132 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3133 {
3134 /* Read some bytes. */
3135 status = psa_generator_read( &generator,
3136 output_buffer, output_sizes[i] );
3137 if( expected_capacity == 0 && output_sizes[i] == 0 )
3138 {
3139 /* Reading 0 bytes when 0 bytes are available can go either way. */
3140 TEST_ASSERT( status == PSA_SUCCESS ||
3141 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3142 continue;
3143 }
3144 else if( expected_capacity == 0 ||
3145 output_sizes[i] > expected_capacity )
3146 {
3147 /* Capacity exceeded. */
3148 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3149 expected_capacity = 0;
3150 continue;
3151 }
3152 /* Success. Check the read data. */
3153 TEST_ASSERT( status == PSA_SUCCESS );
3154 if( output_sizes[i] != 0 )
3155 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3156 output_sizes[i] ) == 0 );
3157 /* Check the generator status. */
3158 expected_capacity -= output_sizes[i];
3159 TEST_ASSERT( psa_get_generator_capacity( &generator,
3160 &current_capacity ) ==
3161 PSA_SUCCESS );
3162 TEST_ASSERT( expected_capacity == current_capacity );
3163 }
3164 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3165
3166exit:
3167 mbedtls_free( output_buffer );
3168 psa_generator_abort( &generator );
3169 psa_destroy_key( slot );
3170 mbedtls_psa_crypto_free( );
3171}
3172/* END_CASE */
3173
3174/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003175void derive_full( int alg_arg,
3176 data_t *key_data,
3177 data_t *salt,
3178 data_t *label,
3179 int requested_capacity_arg )
3180{
3181 psa_key_slot_t slot = 1;
3182 psa_algorithm_t alg = alg_arg;
3183 size_t requested_capacity = requested_capacity_arg;
3184 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3185 unsigned char output_buffer[16];
3186 size_t expected_capacity = requested_capacity;
3187 size_t current_capacity;
3188 psa_key_policy_t policy;
3189
3190 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3191
3192 psa_key_policy_init( &policy );
3193 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3194 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3195
3196 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3197 key_data->x,
3198 key_data->len ) == PSA_SUCCESS );
3199
3200 /* Extraction phase. */
3201 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3202 salt->x, salt->len,
3203 label->x, label->len,
3204 requested_capacity ) == PSA_SUCCESS );
3205 TEST_ASSERT( psa_get_generator_capacity( &generator,
3206 &current_capacity ) ==
3207 PSA_SUCCESS );
3208 TEST_ASSERT( current_capacity == expected_capacity );
3209
3210 /* Expansion phase. */
3211 while( current_capacity > 0 )
3212 {
3213 size_t read_size = sizeof( output_buffer );
3214 if( read_size > current_capacity )
3215 read_size = current_capacity;
3216 TEST_ASSERT( psa_generator_read( &generator,
3217 output_buffer,
3218 read_size ) == PSA_SUCCESS );
3219 expected_capacity -= read_size;
3220 TEST_ASSERT( psa_get_generator_capacity( &generator,
3221 &current_capacity ) ==
3222 PSA_SUCCESS );
3223 TEST_ASSERT( current_capacity == expected_capacity );
3224 }
3225
3226 /* Check that the generator refuses to go over capacity. */
3227 TEST_ASSERT( psa_generator_read( &generator,
3228 output_buffer,
3229 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3230
3231 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3232
3233exit:
3234 psa_generator_abort( &generator );
3235 psa_destroy_key( slot );
3236 mbedtls_psa_crypto_free( );
3237}
3238/* END_CASE */
3239
3240/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003241void derive_key_exercise( int alg_arg,
3242 data_t *key_data,
3243 data_t *salt,
3244 data_t *label,
3245 int derived_type_arg,
3246 int derived_bits_arg,
3247 int derived_usage_arg,
3248 int derived_alg_arg )
3249{
3250 psa_key_slot_t base_key = 1;
3251 psa_key_slot_t derived_key = 2;
3252 psa_algorithm_t alg = alg_arg;
3253 psa_key_type_t derived_type = derived_type_arg;
3254 size_t derived_bits = derived_bits_arg;
3255 psa_key_usage_t derived_usage = derived_usage_arg;
3256 psa_algorithm_t derived_alg = derived_alg_arg;
3257 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3258 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3259 psa_key_policy_t policy;
3260 psa_key_type_t got_type;
3261 size_t got_bits;
3262
3263 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3264
3265 psa_key_policy_init( &policy );
3266 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3267 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3268 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3269 key_data->x,
3270 key_data->len ) == PSA_SUCCESS );
3271
3272 /* Derive a key. */
3273 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3274 salt->x, salt->len,
3275 label->x, label->len,
3276 capacity ) == PSA_SUCCESS );
3277 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3278 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3279 TEST_ASSERT( psa_generator_import_key( derived_key,
3280 derived_type,
3281 derived_bits,
3282 &generator ) == PSA_SUCCESS );
3283
3284 /* Test the key information */
3285 TEST_ASSERT( psa_get_key_information( derived_key,
3286 &got_type,
3287 &got_bits ) == PSA_SUCCESS );
3288 TEST_ASSERT( got_type == derived_type );
3289 TEST_ASSERT( got_bits == derived_bits );
3290
3291 /* Exercise the derived key. */
3292 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3293 goto exit;
3294
3295exit:
3296 psa_generator_abort( &generator );
3297 psa_destroy_key( base_key );
3298 psa_destroy_key( derived_key );
3299 mbedtls_psa_crypto_free( );
3300}
3301/* END_CASE */
3302
3303/* BEGIN_CASE */
3304void derive_key_export( int alg_arg,
3305 data_t *key_data,
3306 data_t *salt,
3307 data_t *label,
3308 int bytes1_arg,
3309 int bytes2_arg )
3310{
3311 psa_key_slot_t base_key = 1;
3312 psa_key_slot_t derived_key = 2;
3313 psa_algorithm_t alg = alg_arg;
3314 size_t bytes1 = bytes1_arg;
3315 size_t bytes2 = bytes2_arg;
3316 size_t capacity = bytes1 + bytes2;
3317 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003318 uint8_t *output_buffer = NULL;
3319 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003320 psa_key_policy_t policy;
3321 size_t length;
3322
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003323 ASSERT_ALLOC( output_buffer, capacity );
3324 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003325 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3326
3327 psa_key_policy_init( &policy );
3328 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3329 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3330 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3331 key_data->x,
3332 key_data->len ) == PSA_SUCCESS );
3333
3334 /* Derive some material and output it. */
3335 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3336 salt->x, salt->len,
3337 label->x, label->len,
3338 capacity ) == PSA_SUCCESS );
3339 TEST_ASSERT( psa_generator_read( &generator,
3340 output_buffer,
3341 capacity ) == PSA_SUCCESS );
3342 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3343
3344 /* Derive the same output again, but this time store it in key objects. */
3345 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3346 salt->x, salt->len,
3347 label->x, label->len,
3348 capacity ) == PSA_SUCCESS );
3349 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3350 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3351 TEST_ASSERT( psa_generator_import_key( derived_key,
3352 PSA_KEY_TYPE_RAW_DATA,
3353 PSA_BYTES_TO_BITS( bytes1 ),
3354 &generator ) == PSA_SUCCESS );
3355 TEST_ASSERT( psa_export_key( derived_key,
3356 export_buffer, bytes1,
3357 &length ) == PSA_SUCCESS );
3358 TEST_ASSERT( length == bytes1 );
3359 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3360 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3361 TEST_ASSERT( psa_generator_import_key( derived_key,
3362 PSA_KEY_TYPE_RAW_DATA,
3363 PSA_BYTES_TO_BITS( bytes2 ),
3364 &generator ) == PSA_SUCCESS );
3365 TEST_ASSERT( psa_export_key( derived_key,
3366 export_buffer + bytes1, bytes2,
3367 &length ) == PSA_SUCCESS );
3368 TEST_ASSERT( length == bytes2 );
3369
3370 /* Compare the outputs from the two runs. */
3371 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3372
3373exit:
3374 mbedtls_free( output_buffer );
3375 mbedtls_free( export_buffer );
3376 psa_generator_abort( &generator );
3377 psa_destroy_key( base_key );
3378 psa_destroy_key( derived_key );
3379 mbedtls_psa_crypto_free( );
3380}
3381/* END_CASE */
3382
3383/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003384void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003385{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003386 size_t bytes = bytes_arg;
3387 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003388 unsigned char *output = NULL;
3389 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003390 size_t i;
3391 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003392
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003393 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3394 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003395 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003396
3397 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3398
Gilles Peskinea50d7392018-06-21 10:22:13 +02003399 /* Run several times, to ensure that every output byte will be
3400 * nonzero at least once with overwhelming probability
3401 * (2^(-8*number_of_runs)). */
3402 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003403 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003404 if( bytes != 0 )
3405 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003406 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3407
3408 /* Check that no more than bytes have been overwritten */
3409 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3410
3411 for( i = 0; i < bytes; i++ )
3412 {
3413 if( output[i] != 0 )
3414 ++changed[i];
3415 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003416 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003417
3418 /* Check that every byte was changed to nonzero at least once. This
3419 * validates that psa_generate_random is overwriting every byte of
3420 * the output buffer. */
3421 for( i = 0; i < bytes; i++ )
3422 {
3423 TEST_ASSERT( changed[i] != 0 );
3424 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003425
3426exit:
3427 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003428 mbedtls_free( output );
3429 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003430}
3431/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003432
3433/* BEGIN_CASE */
3434void generate_key( int type_arg,
3435 int bits_arg,
3436 int usage_arg,
3437 int alg_arg,
3438 int expected_status_arg )
3439{
3440 int slot = 1;
3441 psa_key_type_t type = type_arg;
3442 psa_key_usage_t usage = usage_arg;
3443 size_t bits = bits_arg;
3444 psa_algorithm_t alg = alg_arg;
3445 psa_status_t expected_status = expected_status_arg;
3446 psa_key_type_t got_type;
3447 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003448 psa_status_t expected_info_status =
3449 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3450 psa_key_policy_t policy;
3451
3452 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3453
3454 psa_key_policy_init( &policy );
3455 psa_key_policy_set_usage( &policy, usage, alg );
3456 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3457
3458 /* Generate a key */
3459 TEST_ASSERT( psa_generate_key( slot, type, bits,
3460 NULL, 0 ) == expected_status );
3461
3462 /* Test the key information */
3463 TEST_ASSERT( psa_get_key_information( slot,
3464 &got_type,
3465 &got_bits ) == expected_info_status );
3466 if( expected_info_status != PSA_SUCCESS )
3467 goto exit;
3468 TEST_ASSERT( got_type == type );
3469 TEST_ASSERT( got_bits == bits );
3470
Gilles Peskine818ca122018-06-20 18:16:48 +02003471 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003472 if( ! exercise_key( slot, usage, alg ) )
3473 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003474
3475exit:
3476 psa_destroy_key( slot );
3477 mbedtls_psa_crypto_free( );
3478}
3479/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003480
3481/* BEGIN_CASE */
3482void validate_module_init_generate_random( )
3483{
3484 psa_status_t status;
3485 uint8_t random[10] = { 0 };
3486 status = psa_generate_random( random, sizeof( random ) );
3487 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3488}
3489/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003490
3491/* BEGIN_CASE */
3492void validate_module_init_key_based( )
3493{
3494 psa_status_t status;
3495 uint8_t data[10] = { 0 };
3496 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3497 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3498}
3499/* END_CASE */