blob: 9f1945809319e6f1c489cbda898ac9ea215e1868 [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 Peskine2d277862018-06-18 15:41:12 +0200797void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100798{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100800 int i;
801 for( i = 0; i <= 1; i++ )
802 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 status = psa_crypto_init( );
804 TEST_ASSERT( status == PSA_SUCCESS );
805 status = psa_crypto_init( );
806 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100807 mbedtls_psa_crypto_free( );
808 }
809}
810/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811
812/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200813void fill_slots( int max_arg )
814{
815 /* Fill all the slots until we run out of memory or out of slots,
816 * or until some limit specified in the test data for the sake of
817 * implementations with an essentially unlimited number of slots.
818 * This test assumes that available slots are numbered from 1. */
819
820 psa_key_slot_t slot;
821 psa_key_slot_t max = 0;
822 psa_key_policy_t policy;
823 uint8_t exported[sizeof( max )];
824 size_t exported_size;
825 psa_status_t status;
826
827 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
828
829 psa_key_policy_init( &policy );
830 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
831
832 for( max = 1; max <= (size_t) max_arg; max++ )
833 {
834 status = psa_set_key_policy( max, &policy );
835 /* Stop filling slots if we run out of memory or out of
836 * available slots. */
837 TEST_ASSERT( status == PSA_SUCCESS ||
838 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
839 status == PSA_ERROR_INVALID_ARGUMENT );
840 if( status != PSA_SUCCESS )
841 break;
842 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
843 (uint8_t*) &max, sizeof( max ) );
844 /* Since psa_set_key_policy succeeded, we know that the slot
845 * number is valid. But we may legitimately run out of memory. */
846 TEST_ASSERT( status == PSA_SUCCESS ||
847 status == PSA_ERROR_INSUFFICIENT_MEMORY );
848 if( status != PSA_SUCCESS )
849 break;
850 }
851 /* `max` is now the first slot number that wasn't filled. */
852 max -= 1;
853
854 for( slot = 1; slot <= max; slot++ )
855 {
856 TEST_ASSERT( psa_export_key( slot,
857 exported, sizeof( exported ),
858 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200859 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200860 }
861
862exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200863 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200864 mbedtls_psa_crypto_free( );
865}
866/* END_CASE */
867
868/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200869void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870{
871 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200872 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300876 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
878
Gilles Peskine4abf7412018-06-18 16:35:34 +0200879 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200880 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881 if( status == PSA_SUCCESS )
882 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
883
884exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 mbedtls_psa_crypto_free( );
886}
887/* END_CASE */
888
889/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200890void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
891{
892 int slot = 1;
893 size_t bits = bits_arg;
894 psa_status_t expected_status = expected_status_arg;
895 psa_status_t status;
896 psa_key_type_t type =
897 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
898 size_t buffer_size = /* Slight overapproximations */
899 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200900 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200901 unsigned char *p;
902 int ret;
903 size_t length;
904
905 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200906 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200907
908 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
909 bits, keypair ) ) >= 0 );
910 length = ret;
911
912 /* Try importing the key */
913 status = psa_import_key( slot, type, p, length );
914 TEST_ASSERT( status == expected_status );
915 if( status == PSA_SUCCESS )
916 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
917
918exit:
919 mbedtls_free( buffer );
920 mbedtls_psa_crypto_free( );
921}
922/* END_CASE */
923
924/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300925void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300926 int type_arg,
927 int alg_arg,
928 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100929 int expected_bits,
930 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200931 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100932 int canonical_input )
933{
934 int slot = 1;
935 int slot2 = slot + 1;
936 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200937 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200938 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940 unsigned char *exported = NULL;
941 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100943 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944 size_t reexported_length;
945 psa_key_type_t got_type;
946 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200947 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300950 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300951 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200952 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200954 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
956
mohammad1603a97cb8c2018-03-28 03:46:26 -0700957 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200958 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700959 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
960
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961 /* Import the key */
962 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200963 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964
965 /* Test the key information */
966 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200967 &got_type,
968 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969 TEST_ASSERT( got_type == type );
970 TEST_ASSERT( got_bits == (size_t) expected_bits );
971
972 /* Export the key */
973 status = psa_export_key( slot,
974 exported, export_size,
975 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200976 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100977
978 /* The exported length must be set by psa_export_key() to a value between 0
979 * and export_size. On errors, the exported length must be 0. */
980 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
981 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
982 TEST_ASSERT( exported_length <= export_size );
983
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200984 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200985 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200987 {
988 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200990 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991
Gilles Peskine8f609232018-08-11 01:24:55 +0200992 if( ! exercise_export_key( slot, usage_arg ) )
993 goto exit;
994
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200996 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 else
998 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700999 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1000
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001002 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001003 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001005 reexported,
1006 export_size,
1007 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001008 ASSERT_COMPARE( exported, exported_length,
1009 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 }
1011
1012destroy:
1013 /* Destroy the key */
1014 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1015 TEST_ASSERT( psa_get_key_information(
1016 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1017
1018exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001019 mbedtls_free( exported );
1020 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001021 mbedtls_psa_crypto_free( );
1022}
1023/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001024
Moran Pekerf709f4a2018-06-06 17:26:04 +03001025/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001026void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001027 int type_arg,
1028 int alg_arg,
1029 int expected_bits,
1030 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001031 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001032{
1033 int slot = 1;
1034 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001035 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001036 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001037 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001038 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001039 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001040 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041 psa_key_type_t got_type;
1042 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001043 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001044
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001046 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001047 export_size = (ptrdiff_t) data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001048 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001049
1050 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1051
1052 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001053 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001054 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1055
1056 /* Import the key */
1057 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001058 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001059
1060 /* Test the key information */
1061 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001062 &got_type,
1063 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001064 TEST_ASSERT( got_type == type );
1065 TEST_ASSERT( got_bits == (size_t) expected_bits );
1066
1067 /* Export the key */
1068 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001069 exported, export_size,
1070 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001071 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001072 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001073 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Jaeden Amero2a671e92018-06-27 17:47:40 +01001074 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001075 if( status != PSA_SUCCESS )
1076 goto destroy;
1077
Moran Pekerf709f4a2018-06-06 17:26:04 +03001078destroy:
1079 /* Destroy the key */
1080 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1081 TEST_ASSERT( psa_get_key_information(
1082 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1083
1084exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001085 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001086 mbedtls_psa_crypto_free( );
1087}
1088/* END_CASE */
1089
Gilles Peskine20035e32018-02-03 22:44:14 +01001090/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001091void import_and_exercise_key( data_t *data,
1092 int type_arg,
1093 int bits_arg,
1094 int alg_arg )
1095{
1096 int slot = 1;
1097 psa_key_type_t type = type_arg;
1098 size_t bits = bits_arg;
1099 psa_algorithm_t alg = alg_arg;
1100 psa_key_usage_t usage =
1101 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1102 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1103 PSA_KEY_USAGE_VERIFY :
1104 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1105 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1106 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1107 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1108 PSA_KEY_USAGE_ENCRYPT :
1109 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001110 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001111 0 );
1112 psa_key_policy_t policy;
1113 psa_key_type_t got_type;
1114 size_t got_bits;
1115 psa_status_t status;
1116
1117 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1118
1119 psa_key_policy_init( &policy );
1120 psa_key_policy_set_usage( &policy, usage, alg );
1121 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1122
1123 /* Import the key */
1124 status = psa_import_key( slot, type, data->x, data->len );
1125 TEST_ASSERT( status == PSA_SUCCESS );
1126
1127 /* Test the key information */
1128 TEST_ASSERT( psa_get_key_information( slot,
1129 &got_type,
1130 &got_bits ) == PSA_SUCCESS );
1131 TEST_ASSERT( got_type == type );
1132 TEST_ASSERT( got_bits == bits );
1133
1134 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001135 if( ! exercise_key( slot, usage, alg ) )
1136 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001137
1138exit:
1139 psa_destroy_key( slot );
1140 mbedtls_psa_crypto_free( );
1141}
1142/* END_CASE */
1143
1144/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001145void key_policy( int usage_arg, int alg_arg )
1146{
1147 int key_slot = 1;
1148 psa_algorithm_t alg = alg_arg;
1149 psa_key_usage_t usage = usage_arg;
1150 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1151 unsigned char key[32] = {0};
1152 psa_key_policy_t policy_set;
1153 psa_key_policy_t policy_get;
1154
1155 memset( key, 0x2a, sizeof( key ) );
1156
1157 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1158
1159 psa_key_policy_init( &policy_set );
1160 psa_key_policy_init( &policy_get );
1161
1162 psa_key_policy_set_usage( &policy_set, usage, alg );
1163
1164 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1165 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1166 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1167
1168 TEST_ASSERT( psa_import_key( key_slot, key_type,
1169 key, sizeof( key ) ) == PSA_SUCCESS );
1170
1171 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1172
1173 TEST_ASSERT( policy_get.usage == policy_set.usage );
1174 TEST_ASSERT( policy_get.alg == policy_set.alg );
1175
1176exit:
1177 psa_destroy_key( key_slot );
1178 mbedtls_psa_crypto_free( );
1179}
1180/* END_CASE */
1181
1182/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001183void mac_key_policy( int policy_usage,
1184 int policy_alg,
1185 int key_type,
1186 data_t *key_data,
1187 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001188{
1189 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001190 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 psa_mac_operation_t operation;
1192 psa_status_t status;
1193 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001194
1195 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1196
1197 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001199 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1200
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201 TEST_ASSERT( psa_import_key( key_slot, key_type,
1202 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001203
Gilles Peskine89167cb2018-07-08 20:12:23 +02001204 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001205 if( policy_alg == exercise_alg &&
1206 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1207 TEST_ASSERT( status == PSA_SUCCESS );
1208 else
1209 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1210 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001211
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001213 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214 if( policy_alg == exercise_alg &&
1215 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001216 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001217 else
1218 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1219
1220exit:
1221 psa_mac_abort( &operation );
1222 psa_destroy_key( key_slot );
1223 mbedtls_psa_crypto_free( );
1224}
1225/* END_CASE */
1226
1227/* BEGIN_CASE */
1228void cipher_key_policy( int policy_usage,
1229 int policy_alg,
1230 int key_type,
1231 data_t *key_data,
1232 int exercise_alg )
1233{
1234 int key_slot = 1;
1235 psa_key_policy_t policy;
1236 psa_cipher_operation_t operation;
1237 psa_status_t status;
1238
1239 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1240
1241 psa_key_policy_init( &policy );
1242 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1243 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1244
1245 TEST_ASSERT( psa_import_key( key_slot, key_type,
1246 key_data->x, key_data->len ) == PSA_SUCCESS );
1247
Gilles Peskinefe119512018-07-08 21:39:34 +02001248 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 if( policy_alg == exercise_alg &&
1250 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1251 TEST_ASSERT( status == PSA_SUCCESS );
1252 else
1253 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1254 psa_cipher_abort( &operation );
1255
Gilles Peskinefe119512018-07-08 21:39:34 +02001256 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257 if( policy_alg == exercise_alg &&
1258 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1259 TEST_ASSERT( status == PSA_SUCCESS );
1260 else
1261 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1262
1263exit:
1264 psa_cipher_abort( &operation );
1265 psa_destroy_key( key_slot );
1266 mbedtls_psa_crypto_free( );
1267}
1268/* END_CASE */
1269
1270/* BEGIN_CASE */
1271void aead_key_policy( int policy_usage,
1272 int policy_alg,
1273 int key_type,
1274 data_t *key_data,
1275 int nonce_length_arg,
1276 int tag_length_arg,
1277 int exercise_alg )
1278{
1279 int key_slot = 1;
1280 psa_key_policy_t policy;
1281 psa_status_t status;
1282 unsigned char nonce[16] = {0};
1283 size_t nonce_length = nonce_length_arg;
1284 unsigned char tag[16];
1285 size_t tag_length = tag_length_arg;
1286 size_t output_length;
1287
1288 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1289 TEST_ASSERT( tag_length <= sizeof( tag ) );
1290
1291 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1292
1293 psa_key_policy_init( &policy );
1294 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1295 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1296
1297 TEST_ASSERT( psa_import_key( key_slot, key_type,
1298 key_data->x, key_data->len ) == PSA_SUCCESS );
1299
1300 status = psa_aead_encrypt( key_slot, exercise_alg,
1301 nonce, nonce_length,
1302 NULL, 0,
1303 NULL, 0,
1304 tag, tag_length,
1305 &output_length );
1306 if( policy_alg == exercise_alg &&
1307 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1308 TEST_ASSERT( status == PSA_SUCCESS );
1309 else
1310 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1311
1312 memset( tag, 0, sizeof( tag ) );
1313 status = psa_aead_decrypt( key_slot, exercise_alg,
1314 nonce, nonce_length,
1315 NULL, 0,
1316 tag, tag_length,
1317 NULL, 0,
1318 &output_length );
1319 if( policy_alg == exercise_alg &&
1320 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1321 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1322 else
1323 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1324
1325exit:
1326 psa_destroy_key( key_slot );
1327 mbedtls_psa_crypto_free( );
1328}
1329/* END_CASE */
1330
1331/* BEGIN_CASE */
1332void asymmetric_encryption_key_policy( int policy_usage,
1333 int policy_alg,
1334 int key_type,
1335 data_t *key_data,
1336 int exercise_alg )
1337{
1338 int key_slot = 1;
1339 psa_key_policy_t policy;
1340 psa_status_t status;
1341 size_t key_bits;
1342 size_t buffer_length;
1343 unsigned char *buffer = NULL;
1344 size_t output_length;
1345
1346 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1347
1348 psa_key_policy_init( &policy );
1349 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1350 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1351
1352 TEST_ASSERT( psa_import_key( key_slot, key_type,
1353 key_data->x, key_data->len ) == PSA_SUCCESS );
1354
1355 TEST_ASSERT( psa_get_key_information( key_slot,
1356 NULL,
1357 &key_bits ) == PSA_SUCCESS );
1358 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1359 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001360 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001361
1362 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1363 NULL, 0,
1364 NULL, 0,
1365 buffer, buffer_length,
1366 &output_length );
1367 if( policy_alg == exercise_alg &&
1368 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1369 TEST_ASSERT( status == PSA_SUCCESS );
1370 else
1371 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1372
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001373 if( buffer_length != 0 )
1374 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001375 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1376 buffer, buffer_length,
1377 NULL, 0,
1378 buffer, buffer_length,
1379 &output_length );
1380 if( policy_alg == exercise_alg &&
1381 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1382 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1383 else
1384 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1385
1386exit:
1387 psa_destroy_key( key_slot );
1388 mbedtls_psa_crypto_free( );
1389 mbedtls_free( buffer );
1390}
1391/* END_CASE */
1392
1393/* BEGIN_CASE */
1394void asymmetric_signature_key_policy( int policy_usage,
1395 int policy_alg,
1396 int key_type,
1397 data_t *key_data,
1398 int exercise_alg )
1399{
1400 int key_slot = 1;
1401 psa_key_policy_t policy;
1402 psa_status_t status;
1403 unsigned char payload[16] = {1};
1404 size_t payload_length = sizeof( payload );
1405 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1406 size_t signature_length;
1407
1408 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1409
1410 psa_key_policy_init( &policy );
1411 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1412 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1413
1414 TEST_ASSERT( psa_import_key( key_slot, key_type,
1415 key_data->x, key_data->len ) == PSA_SUCCESS );
1416
1417 status = psa_asymmetric_sign( key_slot, exercise_alg,
1418 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001419 signature, sizeof( signature ),
1420 &signature_length );
1421 if( policy_alg == exercise_alg &&
1422 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1423 TEST_ASSERT( status == PSA_SUCCESS );
1424 else
1425 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1426
1427 memset( signature, 0, sizeof( signature ) );
1428 status = psa_asymmetric_verify( key_slot, exercise_alg,
1429 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001430 signature, sizeof( signature ) );
1431 if( policy_alg == exercise_alg &&
1432 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1433 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1434 else
1435 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001436
1437exit:
1438 psa_destroy_key( key_slot );
1439 mbedtls_psa_crypto_free( );
1440}
1441/* END_CASE */
1442
1443/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001444void derive_key_policy( int policy_usage,
1445 int policy_alg,
1446 int key_type,
1447 data_t *key_data,
1448 int exercise_alg )
1449{
1450 int key_slot = 1;
1451 psa_key_policy_t policy;
1452 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1453 psa_status_t status;
1454
1455 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1456
1457 psa_key_policy_init( &policy );
1458 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1459 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1460
1461 TEST_ASSERT( psa_import_key( key_slot, key_type,
1462 key_data->x, key_data->len ) == PSA_SUCCESS );
1463
1464 status = psa_key_derivation( &generator, key_slot,
1465 exercise_alg,
1466 NULL, 0,
1467 NULL, 0,
1468 1 );
1469 if( policy_alg == exercise_alg &&
1470 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1471 TEST_ASSERT( status == PSA_SUCCESS );
1472 else
1473 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1474
1475exit:
1476 psa_generator_abort( &generator );
1477 psa_destroy_key( key_slot );
1478 mbedtls_psa_crypto_free( );
1479}
1480/* END_CASE */
1481
1482/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001483void key_lifetime( int lifetime_arg )
1484{
1485 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001486 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001487 unsigned char key[32] = {0};
1488 psa_key_lifetime_t lifetime_set = lifetime_arg;
1489 psa_key_lifetime_t lifetime_get;
1490
1491 memset( key, 0x2a, sizeof( key ) );
1492
1493 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1494
1495 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1496 lifetime_set ) == PSA_SUCCESS );
1497
1498 TEST_ASSERT( psa_import_key( key_slot, key_type,
1499 key, sizeof( key ) ) == PSA_SUCCESS );
1500
1501 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1502 &lifetime_get ) == PSA_SUCCESS );
1503
1504 TEST_ASSERT( lifetime_get == lifetime_set );
1505
1506exit:
1507 psa_destroy_key( key_slot );
1508 mbedtls_psa_crypto_free( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
1513void key_lifetime_set_fail( int key_slot_arg,
1514 int lifetime_arg,
1515 int expected_status_arg )
1516{
1517 psa_key_slot_t key_slot = key_slot_arg;
1518 psa_key_lifetime_t lifetime_set = lifetime_arg;
1519 psa_status_t actual_status;
1520 psa_status_t expected_status = expected_status_arg;
1521
1522 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1523
1524 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1525
1526 if( actual_status == PSA_SUCCESS )
1527 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1528
1529 TEST_ASSERT( expected_status == actual_status );
1530
1531exit:
1532 psa_destroy_key( key_slot );
1533 mbedtls_psa_crypto_free( );
1534}
1535/* END_CASE */
1536
1537/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001538void hash_setup( int alg_arg,
1539 int expected_status_arg )
1540{
1541 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001542 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001543 psa_hash_operation_t operation;
1544 psa_status_t status;
1545
1546 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1547
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001548 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001549 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001550 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001551
1552exit:
1553 mbedtls_psa_crypto_free( );
1554}
1555/* END_CASE */
1556
1557/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001558void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001559{
1560 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001561 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001562 size_t actual_hash_length;
1563 psa_hash_operation_t operation;
1564
Gilles Peskine69c12672018-06-28 00:07:19 +02001565 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1566 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1567
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001568 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001569 TEST_ASSERT( expected_hash != NULL );
1570 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1571 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001572
1573 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1574
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001575 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001576 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001577 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 TEST_ASSERT( psa_hash_finish( &operation,
1579 actual_hash, sizeof( actual_hash ),
1580 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001581 ASSERT_COMPARE( expected_hash->x, expected_hash->len,
1582 actual_hash, actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001583
1584exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585 mbedtls_psa_crypto_free( );
1586}
1587/* END_CASE */
1588
1589/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001590void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001591{
1592 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593 psa_hash_operation_t operation;
1594
Gilles Peskine69c12672018-06-28 00:07:19 +02001595 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1596 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1597
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001598 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001599 TEST_ASSERT( expected_hash != NULL );
1600 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1601 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001602
1603 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1604
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001605 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001606 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001607 input->x,
1608 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001609 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001610 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001611 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001612
1613exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001614 mbedtls_psa_crypto_free( );
1615}
1616/* END_CASE */
1617
1618/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001619void mac_setup( int key_type_arg,
1620 data_t *key,
1621 int alg_arg,
1622 int expected_status_arg )
1623{
1624 int key_slot = 1;
1625 psa_key_type_t key_type = key_type_arg;
1626 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001627 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001628 psa_mac_operation_t operation;
1629 psa_key_policy_t policy;
1630 psa_status_t status;
1631
1632 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1633
1634 psa_key_policy_init( &policy );
1635 psa_key_policy_set_usage( &policy,
1636 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1637 alg );
1638 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1639
1640 TEST_ASSERT( psa_import_key( key_slot, key_type,
1641 key->x, key->len ) == PSA_SUCCESS );
1642
Gilles Peskine89167cb2018-07-08 20:12:23 +02001643 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001644 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001645 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001646
1647exit:
1648 psa_destroy_key( key_slot );
1649 mbedtls_psa_crypto_free( );
1650}
1651/* END_CASE */
1652
1653/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001654void mac_sign( int key_type_arg,
1655 data_t *key,
1656 int alg_arg,
1657 data_t *input,
1658 data_t *expected_mac )
1659{
1660 int key_slot = 1;
1661 psa_key_type_t key_type = key_type_arg;
1662 psa_algorithm_t alg = alg_arg;
1663 psa_mac_operation_t operation;
1664 psa_key_policy_t policy;
1665 /* Leave a little extra room in the output buffer. At the end of the
1666 * test, we'll check that the implementation didn't overwrite onto
1667 * this extra room. */
1668 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1669 size_t mac_buffer_size =
1670 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1671 size_t mac_length = 0;
1672
1673 memset( actual_mac, '+', sizeof( actual_mac ) );
1674 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1675 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1676
1677 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1678
1679 psa_key_policy_init( &policy );
1680 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1681 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1682
1683 TEST_ASSERT( psa_import_key( key_slot, key_type,
1684 key->x, key->len ) == PSA_SUCCESS );
1685
1686 /* Calculate the MAC. */
1687 TEST_ASSERT( psa_mac_sign_setup( &operation,
1688 key_slot, alg ) == PSA_SUCCESS );
1689 TEST_ASSERT( psa_mac_update( &operation,
1690 input->x, input->len ) == PSA_SUCCESS );
1691 TEST_ASSERT( psa_mac_sign_finish( &operation,
1692 actual_mac, mac_buffer_size,
1693 &mac_length ) == PSA_SUCCESS );
1694
1695 /* Compare with the expected value. */
1696 TEST_ASSERT( mac_length == expected_mac->len );
1697 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1698
1699 /* Verify that the end of the buffer is untouched. */
1700 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1701 sizeof( actual_mac ) - mac_length ) );
1702
1703exit:
1704 psa_destroy_key( key_slot );
1705 mbedtls_psa_crypto_free( );
1706}
1707/* END_CASE */
1708
1709/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001710void mac_verify( int key_type_arg,
1711 data_t *key,
1712 int alg_arg,
1713 data_t *input,
1714 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001715{
1716 int key_slot = 1;
1717 psa_key_type_t key_type = key_type_arg;
1718 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001720 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001721
Gilles Peskine69c12672018-06-28 00:07:19 +02001722 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1723
Gilles Peskine8c9def32018-02-08 10:02:12 +01001724 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001725 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001726 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001727 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001728 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1729 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001730
1731 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1732
mohammad16036df908f2018-04-02 08:34:15 -07001733 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001734 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001735 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1736
Gilles Peskine8c9def32018-02-08 10:02:12 +01001737 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001738 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001739
Gilles Peskine89167cb2018-07-08 20:12:23 +02001740 TEST_ASSERT( psa_mac_verify_setup( &operation,
1741 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001742 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1743 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001744 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001745 TEST_ASSERT( psa_mac_verify_finish( &operation,
1746 expected_mac->x,
1747 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001748
1749exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750 psa_destroy_key( key_slot );
1751 mbedtls_psa_crypto_free( );
1752}
1753/* END_CASE */
1754
1755/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001756void cipher_setup( int key_type_arg,
1757 data_t *key,
1758 int alg_arg,
1759 int expected_status_arg )
1760{
1761 int key_slot = 1;
1762 psa_key_type_t key_type = key_type_arg;
1763 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001764 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001765 psa_cipher_operation_t operation;
1766 psa_key_policy_t policy;
1767 psa_status_t status;
1768
1769 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1770
1771 psa_key_policy_init( &policy );
1772 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1773 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1774
1775 TEST_ASSERT( psa_import_key( key_slot, key_type,
1776 key->x, key->len ) == PSA_SUCCESS );
1777
Gilles Peskinefe119512018-07-08 21:39:34 +02001778 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001779 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001780 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001781
1782exit:
1783 psa_destroy_key( key_slot );
1784 mbedtls_psa_crypto_free( );
1785}
1786/* END_CASE */
1787
1788/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001789void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001790 data_t *key,
1791 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001792 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001793{
1794 int key_slot = 1;
1795 psa_status_t status;
1796 psa_key_type_t key_type = key_type_arg;
1797 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001798 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001799 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001800 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001801 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001802 size_t output_buffer_size = 0;
1803 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001804 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001805 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001806 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001807
Gilles Peskine50e586b2018-06-08 14:28:46 +02001808 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001809 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001810 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001811 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1812 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1813 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001814
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001815 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1816 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001817
1818 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1819
Moran Pekered346952018-07-05 15:22:45 +03001820 psa_key_policy_init( &policy );
1821 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1822 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1823
Gilles Peskine50e586b2018-06-08 14:28:46 +02001824 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001825 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001826
Gilles Peskinefe119512018-07-08 21:39:34 +02001827 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1828 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001829
Gilles Peskinefe119512018-07-08 21:39:34 +02001830 TEST_ASSERT( psa_cipher_set_iv( &operation,
1831 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001832 output_buffer_size = (size_t) input->len +
1833 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001834 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001835
Gilles Peskine4abf7412018-06-18 16:35:34 +02001836 TEST_ASSERT( psa_cipher_update( &operation,
1837 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001838 output, output_buffer_size,
1839 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001840 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001841 status = psa_cipher_finish( &operation,
1842 output + function_output_length,
1843 output_buffer_size,
1844 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001845 total_output_length += function_output_length;
1846
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001847 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001848 if( expected_status == PSA_SUCCESS )
1849 {
1850 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001851 ASSERT_COMPARE( expected_output->x, expected_output->len,
1852 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001853 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001854
Gilles Peskine50e586b2018-06-08 14:28:46 +02001855exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001856 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001857 psa_destroy_key( key_slot );
1858 mbedtls_psa_crypto_free( );
1859}
1860/* END_CASE */
1861
1862/* BEGIN_CASE */
1863void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001864 data_t *key,
1865 data_t *input,
1866 int first_part_size,
1867 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001868{
1869 int key_slot = 1;
1870 psa_key_type_t key_type = key_type_arg;
1871 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001872 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001873 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001874 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001875 size_t output_buffer_size = 0;
1876 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001877 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001878 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001879 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001880
Gilles Peskine50e586b2018-06-08 14:28:46 +02001881 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001882 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001883 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001884 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1885 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1886 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001887
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001888 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1889 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001890
1891 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1892
Moran Pekered346952018-07-05 15:22:45 +03001893 psa_key_policy_init( &policy );
1894 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1895 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1896
Gilles Peskine50e586b2018-06-08 14:28:46 +02001897 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001898 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001899
Gilles Peskinefe119512018-07-08 21:39:34 +02001900 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1901 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001902
Gilles Peskinefe119512018-07-08 21:39:34 +02001903 TEST_ASSERT( psa_cipher_set_iv( &operation,
1904 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001905 output_buffer_size = (size_t) input->len +
1906 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001907 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001908
Gilles Peskine4abf7412018-06-18 16:35:34 +02001909 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001910 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001911 output, output_buffer_size,
1912 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001913 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001914 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001915 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001916 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001917 output, output_buffer_size,
1918 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001919 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001920 TEST_ASSERT( psa_cipher_finish( &operation,
1921 output + function_output_length,
1922 output_buffer_size,
1923 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001924 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001925 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1926
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001927 ASSERT_COMPARE( expected_output->x, expected_output->len,
1928 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001929
1930exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001931 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001932 psa_destroy_key( key_slot );
1933 mbedtls_psa_crypto_free( );
1934}
1935/* END_CASE */
1936
1937/* BEGIN_CASE */
1938void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001939 data_t *key,
1940 data_t *input,
1941 int first_part_size,
1942 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001943{
1944 int key_slot = 1;
1945
1946 psa_key_type_t key_type = key_type_arg;
1947 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001948 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001949 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001950 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001951 size_t output_buffer_size = 0;
1952 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001953 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001954 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001955 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001956
Gilles Peskine50e586b2018-06-08 14:28:46 +02001957 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001958 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001959 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001960 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1961 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1962 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001963
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001964 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1965 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001966
1967 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1968
Moran Pekered346952018-07-05 15:22:45 +03001969 psa_key_policy_init( &policy );
1970 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1971 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1972
Gilles Peskine50e586b2018-06-08 14:28:46 +02001973 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001974 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001975
Gilles Peskinefe119512018-07-08 21:39:34 +02001976 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1977 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001978
Gilles Peskinefe119512018-07-08 21:39:34 +02001979 TEST_ASSERT( psa_cipher_set_iv( &operation,
1980 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001981
mohammad16033d91abe2018-07-03 13:15:54 +03001982 output_buffer_size = (size_t) input->len +
1983 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001984 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001985
Gilles Peskine4abf7412018-06-18 16:35:34 +02001986 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1987 TEST_ASSERT( psa_cipher_update( &operation,
1988 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001989 output, output_buffer_size,
1990 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001991 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001992 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001993 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001994 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001995 output, output_buffer_size,
1996 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001997 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001998 TEST_ASSERT( psa_cipher_finish( &operation,
1999 output + function_output_length,
2000 output_buffer_size,
2001 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002002 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002003 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2004
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002005 ASSERT_COMPARE( expected_output->x, expected_output->len,
2006 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002007
2008exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002009 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002010 psa_destroy_key( key_slot );
2011 mbedtls_psa_crypto_free( );
2012}
2013/* END_CASE */
2014
Gilles Peskine50e586b2018-06-08 14:28:46 +02002015/* BEGIN_CASE */
2016void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002017 data_t *key,
2018 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002019 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002020{
2021 int key_slot = 1;
2022 psa_status_t status;
2023 psa_key_type_t key_type = key_type_arg;
2024 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002025 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002026 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002027 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002028 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002029 size_t output_buffer_size = 0;
2030 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002031 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002032 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002033 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034
Gilles Peskine50e586b2018-06-08 14:28:46 +02002035 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002036 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002037 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002038 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2039 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2040 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002041
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002042 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2043 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002044
2045 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2046
Moran Pekered346952018-07-05 15:22:45 +03002047 psa_key_policy_init( &policy );
2048 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2049 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2050
Gilles Peskine50e586b2018-06-08 14:28:46 +02002051 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002052 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002053
Gilles Peskinefe119512018-07-08 21:39:34 +02002054 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2055 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002056
Gilles Peskinefe119512018-07-08 21:39:34 +02002057 TEST_ASSERT( psa_cipher_set_iv( &operation,
2058 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002059
mohammad16033d91abe2018-07-03 13:15:54 +03002060 output_buffer_size = (size_t) input->len +
2061 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002062 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002063
Gilles Peskine4abf7412018-06-18 16:35:34 +02002064 TEST_ASSERT( psa_cipher_update( &operation,
2065 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002066 output, output_buffer_size,
2067 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002068 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002069 status = psa_cipher_finish( &operation,
2070 output + function_output_length,
2071 output_buffer_size,
2072 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002073 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002074 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002075
2076 if( expected_status == PSA_SUCCESS )
2077 {
2078 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002079 ASSERT_COMPARE( expected_output->x, expected_output->len,
2080 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002081 }
2082
Gilles Peskine50e586b2018-06-08 14:28:46 +02002083exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002084 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002085 psa_destroy_key( key_slot );
2086 mbedtls_psa_crypto_free( );
2087}
2088/* END_CASE */
2089
Gilles Peskine50e586b2018-06-08 14:28:46 +02002090/* BEGIN_CASE */
2091void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002092 data_t *key,
2093 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002094{
2095 int key_slot = 1;
2096 psa_key_type_t key_type = key_type_arg;
2097 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002098 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002099 size_t iv_size = 16;
2100 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002101 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002102 size_t output1_size = 0;
2103 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002104 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002105 size_t output2_size = 0;
2106 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002107 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002108 psa_cipher_operation_t operation1;
2109 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002110 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002111
mohammad1603d7d7ba52018-03-12 18:51:53 +02002112 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002113 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002114 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2115 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002116
mohammad1603d7d7ba52018-03-12 18:51:53 +02002117 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2118
Moran Pekered346952018-07-05 15:22:45 +03002119 psa_key_policy_init( &policy );
2120 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2121 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2122
mohammad1603d7d7ba52018-03-12 18:51:53 +02002123 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002124 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002125
Gilles Peskinefe119512018-07-08 21:39:34 +02002126 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2127 key_slot, alg ) == PSA_SUCCESS );
2128 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2129 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002130
Gilles Peskinefe119512018-07-08 21:39:34 +02002131 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2132 iv, iv_size,
2133 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002134 output1_size = (size_t) input->len +
2135 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002136 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002137
Gilles Peskine4abf7412018-06-18 16:35:34 +02002138 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002139 output1, output1_size,
2140 &output1_length ) == PSA_SUCCESS );
2141 TEST_ASSERT( psa_cipher_finish( &operation1,
2142 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002143 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002144
Gilles Peskine048b7f02018-06-08 14:20:49 +02002145 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002146
2147 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2148
2149 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002150 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002151
Gilles Peskinefe119512018-07-08 21:39:34 +02002152 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2153 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002154 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2155 output2, output2_size,
2156 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002157 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002158 TEST_ASSERT( psa_cipher_finish( &operation2,
2159 output2 + output2_length,
2160 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002161 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002162
Gilles Peskine048b7f02018-06-08 14:20:49 +02002163 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002164
Janos Follath25c4fa82018-07-06 16:23:25 +01002165 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002166
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002167 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002168
2169exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002170 mbedtls_free( output1 );
2171 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002172 psa_destroy_key( key_slot );
2173 mbedtls_psa_crypto_free( );
2174}
2175/* END_CASE */
2176
2177/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002178void cipher_verify_output_multipart( int alg_arg,
2179 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002180 data_t *key,
2181 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002182 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002183{
2184 int key_slot = 1;
2185 psa_key_type_t key_type = key_type_arg;
2186 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002187 unsigned char iv[16] = {0};
2188 size_t iv_size = 16;
2189 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002190 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002191 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002192 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002193 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002194 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002195 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002196 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002197 psa_cipher_operation_t operation1;
2198 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002199 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002200
Moran Pekerded84402018-06-06 16:36:50 +03002201 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002202 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002203 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2204 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002205
Moran Pekerded84402018-06-06 16:36:50 +03002206 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2207
Moran Pekered346952018-07-05 15:22:45 +03002208 psa_key_policy_init( &policy );
2209 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2210 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2211
Moran Pekerded84402018-06-06 16:36:50 +03002212 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002213 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002214
Gilles Peskinefe119512018-07-08 21:39:34 +02002215 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2216 key_slot, alg ) == PSA_SUCCESS );
2217 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2218 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002219
Gilles Peskinefe119512018-07-08 21:39:34 +02002220 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2221 iv, iv_size,
2222 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002223 output1_buffer_size = (size_t) input->len +
2224 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002225 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002226
Gilles Peskine4abf7412018-06-18 16:35:34 +02002227 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002228
itayzafrir3e02b3b2018-06-12 17:06:52 +03002229 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002230 output1, output1_buffer_size,
2231 &function_output_length ) == PSA_SUCCESS );
2232 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002233
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002234 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002235 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002236 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002237 output1, output1_buffer_size,
2238 &function_output_length ) == PSA_SUCCESS );
2239 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002240
Gilles Peskine048b7f02018-06-08 14:20:49 +02002241 TEST_ASSERT( psa_cipher_finish( &operation1,
2242 output1 + output1_length,
2243 output1_buffer_size - output1_length,
2244 &function_output_length ) == PSA_SUCCESS );
2245 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002246
2247 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2248
Gilles Peskine048b7f02018-06-08 14:20:49 +02002249 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002250 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002251
Gilles Peskinefe119512018-07-08 21:39:34 +02002252 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2253 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002254
2255 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002256 output2, output2_buffer_size,
2257 &function_output_length ) == PSA_SUCCESS );
2258 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002259
Gilles Peskine048b7f02018-06-08 14:20:49 +02002260 TEST_ASSERT( psa_cipher_update( &operation2,
2261 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002262 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002263 output2, output2_buffer_size,
2264 &function_output_length ) == PSA_SUCCESS );
2265 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002266
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002267 TEST_ASSERT( psa_cipher_finish( &operation2,
2268 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002269 output2_buffer_size - output2_length,
2270 &function_output_length ) == PSA_SUCCESS );
2271 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002272
Janos Follath25c4fa82018-07-06 16:23:25 +01002273 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002274
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002275 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002276
2277exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002278 mbedtls_free( output1 );
2279 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002280 psa_destroy_key( key_slot );
2281 mbedtls_psa_crypto_free( );
2282}
2283/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002284
Gilles Peskine20035e32018-02-03 22:44:14 +01002285/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002286void aead_encrypt_decrypt( int key_type_arg,
2287 data_t * key_data,
2288 int alg_arg,
2289 data_t * input_data,
2290 data_t * nonce,
2291 data_t * additional_data,
2292 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002293{
2294 int slot = 1;
2295 psa_key_type_t key_type = key_type_arg;
2296 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002297 unsigned char *output_data = NULL;
2298 size_t output_size = 0;
2299 size_t output_length = 0;
2300 unsigned char *output_data2 = NULL;
2301 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002302 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002303 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002304 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002305
Gilles Peskinea1cac842018-06-11 19:33:02 +02002306 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002307 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002308 TEST_ASSERT( nonce != NULL );
2309 TEST_ASSERT( additional_data != NULL );
2310 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2311 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2312 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2313 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2314
Gilles Peskine4abf7412018-06-18 16:35:34 +02002315 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002316 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002317
2318 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2319
2320 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002321 psa_key_policy_set_usage( &policy,
2322 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2323 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002324 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2325
2326 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002327 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002328
2329 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002330 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002331 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002332 additional_data->len,
2333 input_data->x, input_data->len,
2334 output_data, output_size,
2335 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002336
2337 if( PSA_SUCCESS == expected_result )
2338 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002339 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002340
2341 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002342 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002343 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002344 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002345 output_data, output_length,
2346 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002347 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002348
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002349 ASSERT_COMPARE( input_data->x, input_data->len,
2350 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002351 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002352
Gilles Peskinea1cac842018-06-11 19:33:02 +02002353exit:
2354 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002355 mbedtls_free( output_data );
2356 mbedtls_free( output_data2 );
2357 mbedtls_psa_crypto_free( );
2358}
2359/* END_CASE */
2360
2361/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002362void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002363 int alg_arg, data_t * input_data,
2364 data_t * additional_data, data_t * nonce,
2365 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002366{
2367 int slot = 1;
2368 psa_key_type_t key_type = key_type_arg;
2369 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002370 unsigned char *output_data = NULL;
2371 size_t output_size = 0;
2372 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002373 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002374 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002375
Gilles Peskinea1cac842018-06-11 19:33:02 +02002376 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002377 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002378 TEST_ASSERT( additional_data != NULL );
2379 TEST_ASSERT( nonce != NULL );
2380 TEST_ASSERT( expected_result != NULL );
2381 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2382 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2383 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2384 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2385 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2386
Gilles Peskine4abf7412018-06-18 16:35:34 +02002387 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002388 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002389
Gilles Peskinea1cac842018-06-11 19:33:02 +02002390 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2391
2392 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002393 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002394 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2395
2396 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002397 key_data->x,
2398 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002399
2400 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002401 nonce->x, nonce->len,
2402 additional_data->x, additional_data->len,
2403 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002404 output_data, output_size,
2405 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002406
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002407 ASSERT_COMPARE( expected_result->x, expected_result->len,
2408 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002409
Gilles Peskinea1cac842018-06-11 19:33:02 +02002410exit:
2411 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002412 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002413 mbedtls_psa_crypto_free( );
2414}
2415/* END_CASE */
2416
2417/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002418void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002419 int alg_arg, data_t * input_data,
2420 data_t * additional_data, data_t * nonce,
2421 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002422{
2423 int slot = 1;
2424 psa_key_type_t key_type = key_type_arg;
2425 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002426 unsigned char *output_data = NULL;
2427 size_t output_size = 0;
2428 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002429 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002430 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002431 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002432
Gilles Peskinea1cac842018-06-11 19:33:02 +02002433 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002434 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002435 TEST_ASSERT( additional_data != NULL );
2436 TEST_ASSERT( nonce != NULL );
2437 TEST_ASSERT( expected_data != NULL );
2438 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2439 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2440 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2441 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2442 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2443
Gilles Peskine4abf7412018-06-18 16:35:34 +02002444 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002445 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002446
Gilles Peskinea1cac842018-06-11 19:33:02 +02002447 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2448
2449 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002450 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002451 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2452
2453 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002454 key_data->x,
2455 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002456
2457 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002458 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002459 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002460 additional_data->len,
2461 input_data->x, input_data->len,
2462 output_data, output_size,
2463 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002464
Gilles Peskine2d277862018-06-18 15:41:12 +02002465 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002466 ASSERT_COMPARE( expected_data->x, expected_data->len,
2467 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002468
Gilles Peskinea1cac842018-06-11 19:33:02 +02002469exit:
2470 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002471 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002472 mbedtls_psa_crypto_free( );
2473}
2474/* END_CASE */
2475
2476/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002477void signature_size( int type_arg,
2478 int bits,
2479 int alg_arg,
2480 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002481{
2482 psa_key_type_t type = type_arg;
2483 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002484 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002485 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2486exit:
2487 ;
2488}
2489/* END_CASE */
2490
2491/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002492void sign_deterministic( int key_type_arg, data_t *key_data,
2493 int alg_arg, data_t *input_data,
2494 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002495{
2496 int slot = 1;
2497 psa_key_type_t key_type = key_type_arg;
2498 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002499 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002500 unsigned char *signature = NULL;
2501 size_t signature_size;
2502 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002503 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002504
Gilles Peskine20035e32018-02-03 22:44:14 +01002505 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002506 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002507 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002508 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2509 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2510 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002511
2512 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2513
mohammad1603a97cb8c2018-03-28 03:46:26 -07002514 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002515 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002516 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2517
Gilles Peskine20035e32018-02-03 22:44:14 +01002518 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002519 key_data->x,
2520 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002521 TEST_ASSERT( psa_get_key_information( slot,
2522 NULL,
2523 &key_bits ) == PSA_SUCCESS );
2524
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002525 /* Allocate a buffer which has the size advertized by the
2526 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002527 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2528 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002529 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002530 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002531 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002532
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002533 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002534 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002535 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002536 signature, signature_size,
2537 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002538 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002539 ASSERT_COMPARE( output_data->x, output_data->len,
2540 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002541
2542exit:
2543 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002544 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002545 mbedtls_psa_crypto_free( );
2546}
2547/* END_CASE */
2548
2549/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002550void sign_fail( int key_type_arg, data_t *key_data,
2551 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002552 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002553{
2554 int slot = 1;
2555 psa_key_type_t key_type = key_type_arg;
2556 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002557 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002558 psa_status_t actual_status;
2559 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002560 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002561 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002562 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002563
Gilles Peskine20035e32018-02-03 22:44:14 +01002564 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002565 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002566 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2567 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2568
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002569 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002570
2571 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2572
mohammad1603a97cb8c2018-03-28 03:46:26 -07002573 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002574 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002575 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2576
Gilles Peskine20035e32018-02-03 22:44:14 +01002577 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002578 key_data->x,
2579 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002580
2581 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002582 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002583 signature, signature_size,
2584 &signature_length );
2585 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002586 /* The value of *signature_length is unspecified on error, but
2587 * whatever it is, it should be less than signature_size, so that
2588 * if the caller tries to read *signature_length bytes without
2589 * checking the error code then they don't overflow a buffer. */
2590 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002591
2592exit:
2593 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002594 mbedtls_free( signature );
2595 mbedtls_psa_crypto_free( );
2596}
2597/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002598
2599/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002600void sign_verify( int key_type_arg, data_t *key_data,
2601 int alg_arg, data_t *input_data )
2602{
2603 int slot = 1;
2604 psa_key_type_t key_type = key_type_arg;
2605 psa_algorithm_t alg = alg_arg;
2606 size_t key_bits;
2607 unsigned char *signature = NULL;
2608 size_t signature_size;
2609 size_t signature_length = 0xdeadbeef;
2610 psa_key_policy_t policy;
2611
2612 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2613
2614 psa_key_policy_init( &policy );
2615 psa_key_policy_set_usage( &policy,
2616 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2617 alg );
2618 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2619
2620 TEST_ASSERT( psa_import_key( slot, key_type,
2621 key_data->x,
2622 key_data->len ) == PSA_SUCCESS );
2623 TEST_ASSERT( psa_get_key_information( slot,
2624 NULL,
2625 &key_bits ) == PSA_SUCCESS );
2626
2627 /* Allocate a buffer which has the size advertized by the
2628 * library. */
2629 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2630 key_bits, alg );
2631 TEST_ASSERT( signature_size != 0 );
2632 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002633 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002634
2635 /* Perform the signature. */
2636 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2637 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002638 signature, signature_size,
2639 &signature_length ) == PSA_SUCCESS );
2640 /* Check that the signature length looks sensible. */
2641 TEST_ASSERT( signature_length <= signature_size );
2642 TEST_ASSERT( signature_length > 0 );
2643
2644 /* Use the library to verify that the signature is correct. */
2645 TEST_ASSERT( psa_asymmetric_verify(
2646 slot, alg,
2647 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002648 signature, signature_length ) == PSA_SUCCESS );
2649
2650 if( input_data->len != 0 )
2651 {
2652 /* Flip a bit in the input and verify that the signature is now
2653 * detected as invalid. Flip a bit at the beginning, not at the end,
2654 * because ECDSA may ignore the last few bits of the input. */
2655 input_data->x[0] ^= 1;
2656 TEST_ASSERT( psa_asymmetric_verify(
2657 slot, alg,
2658 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002659 signature,
2660 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2661 }
2662
2663exit:
2664 psa_destroy_key( slot );
2665 mbedtls_free( signature );
2666 mbedtls_psa_crypto_free( );
2667}
2668/* END_CASE */
2669
2670/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002671void asymmetric_verify( int key_type_arg, data_t *key_data,
2672 int alg_arg, data_t *hash_data,
2673 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002674{
2675 int slot = 1;
2676 psa_key_type_t key_type = key_type_arg;
2677 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002678 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002679
Gilles Peskine69c12672018-06-28 00:07:19 +02002680 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2681
itayzafrir5c753392018-05-08 11:18:38 +03002682 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002683 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002684 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002685 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2686 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2687 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002688
2689 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2690
2691 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002692 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002693 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2694
2695 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002696 key_data->x,
2697 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002698
2699 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002700 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002701 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002702 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002703exit:
2704 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002705 mbedtls_psa_crypto_free( );
2706}
2707/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002708
2709/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002710void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2711 int alg_arg, data_t *hash_data,
2712 data_t *signature_data,
2713 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002715 int slot = 1;
2716 psa_key_type_t key_type = key_type_arg;
2717 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002718 psa_status_t actual_status;
2719 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002720 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002722 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002723 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002724 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002725 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2726 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2727 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002728
2729 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2730
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002731 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002732 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002733 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2734
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002735 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002736 key_data->x,
2737 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002738
2739 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002740 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002741 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002742 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002743
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002744 TEST_ASSERT( actual_status == expected_status );
2745
2746exit:
2747 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002748 mbedtls_psa_crypto_free( );
2749}
2750/* END_CASE */
2751
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002752/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002753void asymmetric_encrypt( int key_type_arg,
2754 data_t *key_data,
2755 int alg_arg,
2756 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002757 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002758 int expected_output_length_arg,
2759 int expected_status_arg )
2760{
2761 int slot = 1;
2762 psa_key_type_t key_type = key_type_arg;
2763 psa_algorithm_t alg = alg_arg;
2764 size_t expected_output_length = expected_output_length_arg;
2765 size_t key_bits;
2766 unsigned char *output = NULL;
2767 size_t output_size;
2768 size_t output_length = ~0;
2769 psa_status_t actual_status;
2770 psa_status_t expected_status = expected_status_arg;
2771 psa_key_policy_t policy;
2772
2773 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2774
2775 /* Import the key */
2776 psa_key_policy_init( &policy );
2777 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2778 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2779 TEST_ASSERT( psa_import_key( slot, key_type,
2780 key_data->x,
2781 key_data->len ) == PSA_SUCCESS );
2782
2783 /* Determine the maximum output length */
2784 TEST_ASSERT( psa_get_key_information( slot,
2785 NULL,
2786 &key_bits ) == PSA_SUCCESS );
2787 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002788 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002789
2790 /* Encrypt the input */
2791 actual_status = psa_asymmetric_encrypt( slot, alg,
2792 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002793 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002794 output, output_size,
2795 &output_length );
2796 TEST_ASSERT( actual_status == expected_status );
2797 TEST_ASSERT( output_length == expected_output_length );
2798
Gilles Peskine68428122018-06-30 18:42:41 +02002799 /* If the label is empty, the test framework puts a non-null pointer
2800 * in label->x. Test that a null pointer works as well. */
2801 if( label->len == 0 )
2802 {
2803 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002804 if( output_size != 0 )
2805 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002806 actual_status = psa_asymmetric_encrypt( slot, alg,
2807 input_data->x, input_data->len,
2808 NULL, label->len,
2809 output, output_size,
2810 &output_length );
2811 TEST_ASSERT( actual_status == expected_status );
2812 TEST_ASSERT( output_length == expected_output_length );
2813 }
2814
Gilles Peskine656896e2018-06-29 19:12:28 +02002815exit:
2816 psa_destroy_key( slot );
2817 mbedtls_free( output );
2818 mbedtls_psa_crypto_free( );
2819}
2820/* END_CASE */
2821
2822/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002823void asymmetric_encrypt_decrypt( int key_type_arg,
2824 data_t *key_data,
2825 int alg_arg,
2826 data_t *input_data,
2827 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002828{
2829 int slot = 1;
2830 psa_key_type_t key_type = key_type_arg;
2831 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002832 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002833 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002834 size_t output_size;
2835 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002836 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002837 size_t output2_size;
2838 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002839 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002840
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002841 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002842 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002843 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2844 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2845
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002846 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2847
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002848 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002849 psa_key_policy_set_usage( &policy,
2850 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002851 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002852 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2853
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002854 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002855 key_data->x,
2856 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002857
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002858
2859 /* Determine the maximum ciphertext length */
2860 TEST_ASSERT( psa_get_key_information( slot,
2861 NULL,
2862 &key_bits ) == PSA_SUCCESS );
2863 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002864 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002865 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002866 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002867
Gilles Peskineeebd7382018-06-08 18:11:54 +02002868 /* We test encryption by checking that encrypt-then-decrypt gives back
2869 * the original plaintext because of the non-optional random
2870 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002871 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002872 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002873 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002874 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002875 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002876 /* We don't know what ciphertext length to expect, but check that
2877 * it looks sensible. */
2878 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002879
Gilles Peskine2d277862018-06-18 15:41:12 +02002880 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002881 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002882 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002883 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002884 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002885 ASSERT_COMPARE( input_data->x, input_data->len,
2886 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002887
2888exit:
2889 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 mbedtls_free( output );
2891 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002892 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893}
2894/* END_CASE */
2895
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002896/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002897void asymmetric_decrypt( int key_type_arg,
2898 data_t *key_data,
2899 int alg_arg,
2900 data_t *input_data,
2901 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002902 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002903{
2904 int slot = 1;
2905 psa_key_type_t key_type = key_type_arg;
2906 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002907 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002908 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002909 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002910 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002911
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002912 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002913 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002914 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002915 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2916 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2917 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2918
Gilles Peskine4abf7412018-06-18 16:35:34 +02002919 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002920 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002921
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002922 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2923
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002924 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002925 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002926 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2927
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002928 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002929 key_data->x,
2930 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002931
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002932 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002933 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002934 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002935 output,
2936 output_size,
2937 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002938 ASSERT_COMPARE( expected_data->x, expected_data->len,
2939 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002940
Gilles Peskine68428122018-06-30 18:42:41 +02002941 /* If the label is empty, the test framework puts a non-null pointer
2942 * in label->x. Test that a null pointer works as well. */
2943 if( label->len == 0 )
2944 {
2945 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002946 if( output_size != 0 )
2947 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002948 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2949 input_data->x, input_data->len,
2950 NULL, label->len,
2951 output,
2952 output_size,
2953 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002954 ASSERT_COMPARE( expected_data->x, expected_data->len,
2955 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02002956 }
2957
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002958exit:
2959 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002960 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962}
2963/* END_CASE */
2964
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002966void asymmetric_decrypt_fail( int key_type_arg,
2967 data_t *key_data,
2968 int alg_arg,
2969 data_t *input_data,
2970 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002971 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002972{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002973 int slot = 1;
2974 psa_key_type_t key_type = key_type_arg;
2975 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002976 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002977 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002978 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979 psa_status_t actual_status;
2980 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002981 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002983 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002984 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002985 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2986 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2987
Gilles Peskine4abf7412018-06-18 16:35:34 +02002988 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002989 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002990
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002991 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2992
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002993 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002994 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002995 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2996
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002997 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002998 key_data->x,
2999 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003000
Gilles Peskine2d277862018-06-18 15:41:12 +02003001 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003002 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003003 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003004 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003005 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003006 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003007 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003008
Gilles Peskine68428122018-06-30 18:42:41 +02003009 /* If the label is empty, the test framework puts a non-null pointer
3010 * in label->x. Test that a null pointer works as well. */
3011 if( label->len == 0 )
3012 {
3013 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003014 if( output_size != 0 )
3015 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003016 actual_status = psa_asymmetric_decrypt( slot, alg,
3017 input_data->x, input_data->len,
3018 NULL, label->len,
3019 output, output_size,
3020 &output_length );
3021 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003022 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003023 }
3024
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025exit:
3026 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003027 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028 mbedtls_psa_crypto_free( );
3029}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003030/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003031
3032/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003033void derive_setup( int key_type_arg,
3034 data_t *key_data,
3035 int alg_arg,
3036 data_t *salt,
3037 data_t *label,
3038 int requested_capacity_arg,
3039 int expected_status_arg )
3040{
3041 psa_key_slot_t slot = 1;
3042 size_t key_type = key_type_arg;
3043 psa_algorithm_t alg = alg_arg;
3044 size_t requested_capacity = requested_capacity_arg;
3045 psa_status_t expected_status = expected_status_arg;
3046 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3047 psa_key_policy_t policy;
3048
3049 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3050
3051 psa_key_policy_init( &policy );
3052 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3053 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3054
3055 TEST_ASSERT( psa_import_key( slot, key_type,
3056 key_data->x,
3057 key_data->len ) == PSA_SUCCESS );
3058
3059 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3060 salt->x, salt->len,
3061 label->x, label->len,
3062 requested_capacity ) == expected_status );
3063
3064exit:
3065 psa_generator_abort( &generator );
3066 psa_destroy_key( slot );
3067 mbedtls_psa_crypto_free( );
3068}
3069/* END_CASE */
3070
3071/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003072void derive_output( int alg_arg,
3073 data_t *key_data,
3074 data_t *salt,
3075 data_t *label,
3076 int requested_capacity_arg,
3077 data_t *expected_output1,
3078 data_t *expected_output2 )
3079{
3080 psa_key_slot_t slot = 1;
3081 psa_algorithm_t alg = alg_arg;
3082 size_t requested_capacity = requested_capacity_arg;
3083 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3084 uint8_t *expected_outputs[2] =
3085 {expected_output1->x, expected_output2->x};
3086 size_t output_sizes[2] =
3087 {expected_output1->len, expected_output2->len};
3088 size_t output_buffer_size = 0;
3089 uint8_t *output_buffer = NULL;
3090 size_t expected_capacity;
3091 size_t current_capacity;
3092 psa_key_policy_t policy;
3093 psa_status_t status;
3094 unsigned i;
3095
3096 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3097 {
3098 if( output_sizes[i] > output_buffer_size )
3099 output_buffer_size = output_sizes[i];
3100 if( output_sizes[i] == 0 )
3101 expected_outputs[i] = NULL;
3102 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003103 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003104 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3105
3106 psa_key_policy_init( &policy );
3107 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3108 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3109
3110 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3111 key_data->x,
3112 key_data->len ) == PSA_SUCCESS );
3113
3114 /* Extraction phase. */
3115 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3116 salt->x, salt->len,
3117 label->x, label->len,
3118 requested_capacity ) == PSA_SUCCESS );
3119 TEST_ASSERT( psa_get_generator_capacity( &generator,
3120 &current_capacity ) ==
3121 PSA_SUCCESS );
3122 TEST_ASSERT( current_capacity == requested_capacity );
3123 expected_capacity = requested_capacity;
3124
3125 /* Expansion phase. */
3126 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3127 {
3128 /* Read some bytes. */
3129 status = psa_generator_read( &generator,
3130 output_buffer, output_sizes[i] );
3131 if( expected_capacity == 0 && output_sizes[i] == 0 )
3132 {
3133 /* Reading 0 bytes when 0 bytes are available can go either way. */
3134 TEST_ASSERT( status == PSA_SUCCESS ||
3135 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3136 continue;
3137 }
3138 else if( expected_capacity == 0 ||
3139 output_sizes[i] > expected_capacity )
3140 {
3141 /* Capacity exceeded. */
3142 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3143 expected_capacity = 0;
3144 continue;
3145 }
3146 /* Success. Check the read data. */
3147 TEST_ASSERT( status == PSA_SUCCESS );
3148 if( output_sizes[i] != 0 )
3149 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3150 output_sizes[i] ) == 0 );
3151 /* Check the generator status. */
3152 expected_capacity -= output_sizes[i];
3153 TEST_ASSERT( psa_get_generator_capacity( &generator,
3154 &current_capacity ) ==
3155 PSA_SUCCESS );
3156 TEST_ASSERT( expected_capacity == current_capacity );
3157 }
3158 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3159
3160exit:
3161 mbedtls_free( output_buffer );
3162 psa_generator_abort( &generator );
3163 psa_destroy_key( slot );
3164 mbedtls_psa_crypto_free( );
3165}
3166/* END_CASE */
3167
3168/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003169void derive_full( int alg_arg,
3170 data_t *key_data,
3171 data_t *salt,
3172 data_t *label,
3173 int requested_capacity_arg )
3174{
3175 psa_key_slot_t slot = 1;
3176 psa_algorithm_t alg = alg_arg;
3177 size_t requested_capacity = requested_capacity_arg;
3178 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3179 unsigned char output_buffer[16];
3180 size_t expected_capacity = requested_capacity;
3181 size_t current_capacity;
3182 psa_key_policy_t policy;
3183
3184 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3185
3186 psa_key_policy_init( &policy );
3187 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3188 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3189
3190 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3191 key_data->x,
3192 key_data->len ) == PSA_SUCCESS );
3193
3194 /* Extraction phase. */
3195 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3196 salt->x, salt->len,
3197 label->x, label->len,
3198 requested_capacity ) == PSA_SUCCESS );
3199 TEST_ASSERT( psa_get_generator_capacity( &generator,
3200 &current_capacity ) ==
3201 PSA_SUCCESS );
3202 TEST_ASSERT( current_capacity == expected_capacity );
3203
3204 /* Expansion phase. */
3205 while( current_capacity > 0 )
3206 {
3207 size_t read_size = sizeof( output_buffer );
3208 if( read_size > current_capacity )
3209 read_size = current_capacity;
3210 TEST_ASSERT( psa_generator_read( &generator,
3211 output_buffer,
3212 read_size ) == PSA_SUCCESS );
3213 expected_capacity -= read_size;
3214 TEST_ASSERT( psa_get_generator_capacity( &generator,
3215 &current_capacity ) ==
3216 PSA_SUCCESS );
3217 TEST_ASSERT( current_capacity == expected_capacity );
3218 }
3219
3220 /* Check that the generator refuses to go over capacity. */
3221 TEST_ASSERT( psa_generator_read( &generator,
3222 output_buffer,
3223 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3224
3225 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3226
3227exit:
3228 psa_generator_abort( &generator );
3229 psa_destroy_key( slot );
3230 mbedtls_psa_crypto_free( );
3231}
3232/* END_CASE */
3233
3234/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003235void derive_key_exercise( int alg_arg,
3236 data_t *key_data,
3237 data_t *salt,
3238 data_t *label,
3239 int derived_type_arg,
3240 int derived_bits_arg,
3241 int derived_usage_arg,
3242 int derived_alg_arg )
3243{
3244 psa_key_slot_t base_key = 1;
3245 psa_key_slot_t derived_key = 2;
3246 psa_algorithm_t alg = alg_arg;
3247 psa_key_type_t derived_type = derived_type_arg;
3248 size_t derived_bits = derived_bits_arg;
3249 psa_key_usage_t derived_usage = derived_usage_arg;
3250 psa_algorithm_t derived_alg = derived_alg_arg;
3251 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3252 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3253 psa_key_policy_t policy;
3254 psa_key_type_t got_type;
3255 size_t got_bits;
3256
3257 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3258
3259 psa_key_policy_init( &policy );
3260 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3261 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3262 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3263 key_data->x,
3264 key_data->len ) == PSA_SUCCESS );
3265
3266 /* Derive a key. */
3267 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3268 salt->x, salt->len,
3269 label->x, label->len,
3270 capacity ) == PSA_SUCCESS );
3271 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3272 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3273 TEST_ASSERT( psa_generator_import_key( derived_key,
3274 derived_type,
3275 derived_bits,
3276 &generator ) == PSA_SUCCESS );
3277
3278 /* Test the key information */
3279 TEST_ASSERT( psa_get_key_information( derived_key,
3280 &got_type,
3281 &got_bits ) == PSA_SUCCESS );
3282 TEST_ASSERT( got_type == derived_type );
3283 TEST_ASSERT( got_bits == derived_bits );
3284
3285 /* Exercise the derived key. */
3286 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3287 goto exit;
3288
3289exit:
3290 psa_generator_abort( &generator );
3291 psa_destroy_key( base_key );
3292 psa_destroy_key( derived_key );
3293 mbedtls_psa_crypto_free( );
3294}
3295/* END_CASE */
3296
3297/* BEGIN_CASE */
3298void derive_key_export( int alg_arg,
3299 data_t *key_data,
3300 data_t *salt,
3301 data_t *label,
3302 int bytes1_arg,
3303 int bytes2_arg )
3304{
3305 psa_key_slot_t base_key = 1;
3306 psa_key_slot_t derived_key = 2;
3307 psa_algorithm_t alg = alg_arg;
3308 size_t bytes1 = bytes1_arg;
3309 size_t bytes2 = bytes2_arg;
3310 size_t capacity = bytes1 + bytes2;
3311 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003312 uint8_t *output_buffer = NULL;
3313 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003314 psa_key_policy_t policy;
3315 size_t length;
3316
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003317 ASSERT_ALLOC( output_buffer, capacity );
3318 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003319 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3320
3321 psa_key_policy_init( &policy );
3322 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3323 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3324 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3325 key_data->x,
3326 key_data->len ) == PSA_SUCCESS );
3327
3328 /* Derive some material and output it. */
3329 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3330 salt->x, salt->len,
3331 label->x, label->len,
3332 capacity ) == PSA_SUCCESS );
3333 TEST_ASSERT( psa_generator_read( &generator,
3334 output_buffer,
3335 capacity ) == PSA_SUCCESS );
3336 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3337
3338 /* Derive the same output again, but this time store it in key objects. */
3339 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3340 salt->x, salt->len,
3341 label->x, label->len,
3342 capacity ) == PSA_SUCCESS );
3343 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3344 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3345 TEST_ASSERT( psa_generator_import_key( derived_key,
3346 PSA_KEY_TYPE_RAW_DATA,
3347 PSA_BYTES_TO_BITS( bytes1 ),
3348 &generator ) == PSA_SUCCESS );
3349 TEST_ASSERT( psa_export_key( derived_key,
3350 export_buffer, bytes1,
3351 &length ) == PSA_SUCCESS );
3352 TEST_ASSERT( length == bytes1 );
3353 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3354 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3355 TEST_ASSERT( psa_generator_import_key( derived_key,
3356 PSA_KEY_TYPE_RAW_DATA,
3357 PSA_BYTES_TO_BITS( bytes2 ),
3358 &generator ) == PSA_SUCCESS );
3359 TEST_ASSERT( psa_export_key( derived_key,
3360 export_buffer + bytes1, bytes2,
3361 &length ) == PSA_SUCCESS );
3362 TEST_ASSERT( length == bytes2 );
3363
3364 /* Compare the outputs from the two runs. */
3365 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3366
3367exit:
3368 mbedtls_free( output_buffer );
3369 mbedtls_free( export_buffer );
3370 psa_generator_abort( &generator );
3371 psa_destroy_key( base_key );
3372 psa_destroy_key( derived_key );
3373 mbedtls_psa_crypto_free( );
3374}
3375/* END_CASE */
3376
3377/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003378void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003379{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003380 size_t bytes = bytes_arg;
3381 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003382 unsigned char *output = NULL;
3383 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003384 size_t i;
3385 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003386
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003387 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3388 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003389 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003390
3391 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3392
Gilles Peskinea50d7392018-06-21 10:22:13 +02003393 /* Run several times, to ensure that every output byte will be
3394 * nonzero at least once with overwhelming probability
3395 * (2^(-8*number_of_runs)). */
3396 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003397 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003398 if( bytes != 0 )
3399 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003400 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3401
3402 /* Check that no more than bytes have been overwritten */
3403 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3404
3405 for( i = 0; i < bytes; i++ )
3406 {
3407 if( output[i] != 0 )
3408 ++changed[i];
3409 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003410 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003411
3412 /* Check that every byte was changed to nonzero at least once. This
3413 * validates that psa_generate_random is overwriting every byte of
3414 * the output buffer. */
3415 for( i = 0; i < bytes; i++ )
3416 {
3417 TEST_ASSERT( changed[i] != 0 );
3418 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003419
3420exit:
3421 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003422 mbedtls_free( output );
3423 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003424}
3425/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003426
3427/* BEGIN_CASE */
3428void generate_key( int type_arg,
3429 int bits_arg,
3430 int usage_arg,
3431 int alg_arg,
3432 int expected_status_arg )
3433{
3434 int slot = 1;
3435 psa_key_type_t type = type_arg;
3436 psa_key_usage_t usage = usage_arg;
3437 size_t bits = bits_arg;
3438 psa_algorithm_t alg = alg_arg;
3439 psa_status_t expected_status = expected_status_arg;
3440 psa_key_type_t got_type;
3441 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003442 psa_status_t expected_info_status =
3443 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3444 psa_key_policy_t policy;
3445
3446 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3447
3448 psa_key_policy_init( &policy );
3449 psa_key_policy_set_usage( &policy, usage, alg );
3450 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3451
3452 /* Generate a key */
3453 TEST_ASSERT( psa_generate_key( slot, type, bits,
3454 NULL, 0 ) == expected_status );
3455
3456 /* Test the key information */
3457 TEST_ASSERT( psa_get_key_information( slot,
3458 &got_type,
3459 &got_bits ) == expected_info_status );
3460 if( expected_info_status != PSA_SUCCESS )
3461 goto exit;
3462 TEST_ASSERT( got_type == type );
3463 TEST_ASSERT( got_bits == bits );
3464
Gilles Peskine818ca122018-06-20 18:16:48 +02003465 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003466 if( ! exercise_key( slot, usage, alg ) )
3467 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003468
3469exit:
3470 psa_destroy_key( slot );
3471 mbedtls_psa_crypto_free( );
3472}
3473/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003474
3475/* BEGIN_CASE */
3476void validate_module_init_generate_random( )
3477{
3478 psa_status_t status;
3479 uint8_t random[10] = { 0 };
3480 status = psa_generate_random( random, sizeof( random ) );
3481 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3482}
3483/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003484
3485/* BEGIN_CASE */
3486void validate_module_init_key_based( )
3487{
3488 psa_status_t status;
3489 uint8_t data[10] = { 0 };
3490 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3491 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3492}
3493/* END_CASE */