blob: 5503c94b6e525feec1445cef68e00cd228b52d6c [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 Peskine7da96b02018-08-17 18:45:42 +02002286void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002287 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002288 data_t *nonce,
2289 data_t *additional_data,
2290 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002291 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002292{
2293 int slot = 1;
2294 psa_key_type_t key_type = key_type_arg;
2295 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002296 unsigned char *output_data = NULL;
2297 size_t output_size = 0;
2298 size_t output_length = 0;
2299 unsigned char *output_data2 = NULL;
2300 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002301 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002302 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002303 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002304
Gilles Peskinea1cac842018-06-11 19:33:02 +02002305 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002306 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002307 TEST_ASSERT( nonce != NULL );
2308 TEST_ASSERT( additional_data != NULL );
2309 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2310 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2311 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2312 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2313
Gilles Peskine4abf7412018-06-18 16:35:34 +02002314 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002315 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002316
2317 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2318
2319 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002320 psa_key_policy_set_usage( &policy,
2321 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2322 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002323 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2324
2325 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002326 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002327
2328 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002329 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002330 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002331 additional_data->len,
2332 input_data->x, input_data->len,
2333 output_data, output_size,
2334 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002335
2336 if( PSA_SUCCESS == expected_result )
2337 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002338 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002339
2340 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002341 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002342 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002343 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002344 output_data, output_length,
2345 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002346 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002347
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002348 ASSERT_COMPARE( input_data->x, input_data->len,
2349 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002350 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002351
Gilles Peskinea1cac842018-06-11 19:33:02 +02002352exit:
2353 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002354 mbedtls_free( output_data );
2355 mbedtls_free( output_data2 );
2356 mbedtls_psa_crypto_free( );
2357}
2358/* END_CASE */
2359
2360/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002361void aead_encrypt( int key_type_arg, data_t *key_data,
2362 int alg_arg,
2363 data_t *nonce,
2364 data_t *additional_data,
2365 data_t *input_data,
2366 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002367{
2368 int slot = 1;
2369 psa_key_type_t key_type = key_type_arg;
2370 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002371 unsigned char *output_data = NULL;
2372 size_t output_size = 0;
2373 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002374 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002375 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002376
Gilles Peskinea1cac842018-06-11 19:33:02 +02002377 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002378 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002379 TEST_ASSERT( additional_data != NULL );
2380 TEST_ASSERT( nonce != NULL );
2381 TEST_ASSERT( expected_result != NULL );
2382 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2383 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2384 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2385 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2386 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2387
Gilles Peskine4abf7412018-06-18 16:35:34 +02002388 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002389 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002390
Gilles Peskinea1cac842018-06-11 19:33:02 +02002391 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2392
2393 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002394 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002395 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2396
2397 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002398 key_data->x,
2399 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002400
2401 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002402 nonce->x, nonce->len,
2403 additional_data->x, additional_data->len,
2404 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002405 output_data, output_size,
2406 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002407
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002408 ASSERT_COMPARE( expected_result->x, expected_result->len,
2409 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002410
Gilles Peskinea1cac842018-06-11 19:33:02 +02002411exit:
2412 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002413 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002414 mbedtls_psa_crypto_free( );
2415}
2416/* END_CASE */
2417
2418/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002419void aead_decrypt( int key_type_arg, data_t *key_data,
2420 int alg_arg,
2421 data_t *nonce,
2422 data_t *additional_data,
2423 data_t *input_data,
2424 data_t *expected_data,
2425 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002426{
2427 int slot = 1;
2428 psa_key_type_t key_type = key_type_arg;
2429 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002430 unsigned char *output_data = NULL;
2431 size_t output_size = 0;
2432 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002433 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002434 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002435 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002436
Gilles Peskinea1cac842018-06-11 19:33:02 +02002437 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002438 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002439 TEST_ASSERT( additional_data != NULL );
2440 TEST_ASSERT( nonce != NULL );
2441 TEST_ASSERT( expected_data != NULL );
2442 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2443 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2444 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2445 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2446 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2447
Gilles Peskine4abf7412018-06-18 16:35:34 +02002448 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002449 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002450
Gilles Peskinea1cac842018-06-11 19:33:02 +02002451 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2452
2453 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002454 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002455 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2456
2457 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002458 key_data->x,
2459 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002460
2461 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002462 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002463 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002464 additional_data->len,
2465 input_data->x, input_data->len,
2466 output_data, output_size,
2467 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002468
Gilles Peskine2d277862018-06-18 15:41:12 +02002469 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002470 ASSERT_COMPARE( expected_data->x, expected_data->len,
2471 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002472
Gilles Peskinea1cac842018-06-11 19:33:02 +02002473exit:
2474 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002475 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002476 mbedtls_psa_crypto_free( );
2477}
2478/* END_CASE */
2479
2480/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002481void signature_size( int type_arg,
2482 int bits,
2483 int alg_arg,
2484 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002485{
2486 psa_key_type_t type = type_arg;
2487 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002488 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002489 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2490exit:
2491 ;
2492}
2493/* END_CASE */
2494
2495/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496void sign_deterministic( int key_type_arg, data_t *key_data,
2497 int alg_arg, data_t *input_data,
2498 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002499{
2500 int slot = 1;
2501 psa_key_type_t key_type = key_type_arg;
2502 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002503 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002504 unsigned char *signature = NULL;
2505 size_t signature_size;
2506 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002507 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002508
Gilles Peskine20035e32018-02-03 22:44:14 +01002509 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002510 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002511 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002512 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2513 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2514 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002515
2516 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2517
mohammad1603a97cb8c2018-03-28 03:46:26 -07002518 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002519 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002520 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2521
Gilles Peskine20035e32018-02-03 22:44:14 +01002522 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002523 key_data->x,
2524 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002525 TEST_ASSERT( psa_get_key_information( slot,
2526 NULL,
2527 &key_bits ) == PSA_SUCCESS );
2528
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002529 /* Allocate a buffer which has the size advertized by the
2530 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002531 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2532 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002533 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002534 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002535 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002536
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002537 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002538 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002539 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002540 signature, signature_size,
2541 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002542 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002543 ASSERT_COMPARE( output_data->x, output_data->len,
2544 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002545
2546exit:
2547 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002548 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002549 mbedtls_psa_crypto_free( );
2550}
2551/* END_CASE */
2552
2553/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002554void sign_fail( int key_type_arg, data_t *key_data,
2555 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002556 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002557{
2558 int slot = 1;
2559 psa_key_type_t key_type = key_type_arg;
2560 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002561 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002562 psa_status_t actual_status;
2563 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002564 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002565 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002566 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002567
Gilles Peskine20035e32018-02-03 22:44:14 +01002568 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002569 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002570 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2571 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2572
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002573 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002574
2575 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2576
mohammad1603a97cb8c2018-03-28 03:46:26 -07002577 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002578 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002579 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2580
Gilles Peskine20035e32018-02-03 22:44:14 +01002581 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002582 key_data->x,
2583 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002584
2585 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002586 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002587 signature, signature_size,
2588 &signature_length );
2589 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002590 /* The value of *signature_length is unspecified on error, but
2591 * whatever it is, it should be less than signature_size, so that
2592 * if the caller tries to read *signature_length bytes without
2593 * checking the error code then they don't overflow a buffer. */
2594 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002595
2596exit:
2597 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002598 mbedtls_free( signature );
2599 mbedtls_psa_crypto_free( );
2600}
2601/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002602
2603/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002604void sign_verify( int key_type_arg, data_t *key_data,
2605 int alg_arg, data_t *input_data )
2606{
2607 int slot = 1;
2608 psa_key_type_t key_type = key_type_arg;
2609 psa_algorithm_t alg = alg_arg;
2610 size_t key_bits;
2611 unsigned char *signature = NULL;
2612 size_t signature_size;
2613 size_t signature_length = 0xdeadbeef;
2614 psa_key_policy_t policy;
2615
2616 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2617
2618 psa_key_policy_init( &policy );
2619 psa_key_policy_set_usage( &policy,
2620 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2621 alg );
2622 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2623
2624 TEST_ASSERT( psa_import_key( slot, key_type,
2625 key_data->x,
2626 key_data->len ) == PSA_SUCCESS );
2627 TEST_ASSERT( psa_get_key_information( slot,
2628 NULL,
2629 &key_bits ) == PSA_SUCCESS );
2630
2631 /* Allocate a buffer which has the size advertized by the
2632 * library. */
2633 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2634 key_bits, alg );
2635 TEST_ASSERT( signature_size != 0 );
2636 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002637 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002638
2639 /* Perform the signature. */
2640 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2641 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002642 signature, signature_size,
2643 &signature_length ) == PSA_SUCCESS );
2644 /* Check that the signature length looks sensible. */
2645 TEST_ASSERT( signature_length <= signature_size );
2646 TEST_ASSERT( signature_length > 0 );
2647
2648 /* Use the library to verify that the signature is correct. */
2649 TEST_ASSERT( psa_asymmetric_verify(
2650 slot, alg,
2651 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002652 signature, signature_length ) == PSA_SUCCESS );
2653
2654 if( input_data->len != 0 )
2655 {
2656 /* Flip a bit in the input and verify that the signature is now
2657 * detected as invalid. Flip a bit at the beginning, not at the end,
2658 * because ECDSA may ignore the last few bits of the input. */
2659 input_data->x[0] ^= 1;
2660 TEST_ASSERT( psa_asymmetric_verify(
2661 slot, alg,
2662 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002663 signature,
2664 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2665 }
2666
2667exit:
2668 psa_destroy_key( slot );
2669 mbedtls_free( signature );
2670 mbedtls_psa_crypto_free( );
2671}
2672/* END_CASE */
2673
2674/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002675void asymmetric_verify( int key_type_arg, data_t *key_data,
2676 int alg_arg, data_t *hash_data,
2677 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002678{
2679 int slot = 1;
2680 psa_key_type_t key_type = key_type_arg;
2681 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002682 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002683
Gilles Peskine69c12672018-06-28 00:07:19 +02002684 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2685
itayzafrir5c753392018-05-08 11:18:38 +03002686 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002687 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002688 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002689 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2690 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2691 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002692
2693 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2694
2695 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002696 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002697 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2698
2699 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002700 key_data->x,
2701 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002702
2703 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002704 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002705 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002706 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002707exit:
2708 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002709 mbedtls_psa_crypto_free( );
2710}
2711/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002712
2713/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002714void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2715 int alg_arg, data_t *hash_data,
2716 data_t *signature_data,
2717 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002718{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002719 int slot = 1;
2720 psa_key_type_t key_type = key_type_arg;
2721 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002722 psa_status_t actual_status;
2723 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002724 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002726 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002728 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002729 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2730 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2731 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002732
2733 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2734
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002735 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002736 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002737 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2738
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002739 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002740 key_data->x,
2741 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002742
2743 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002744 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002745 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002746 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002747
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002748 TEST_ASSERT( actual_status == expected_status );
2749
2750exit:
2751 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002752 mbedtls_psa_crypto_free( );
2753}
2754/* END_CASE */
2755
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002756/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002757void asymmetric_encrypt( int key_type_arg,
2758 data_t *key_data,
2759 int alg_arg,
2760 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002761 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002762 int expected_output_length_arg,
2763 int expected_status_arg )
2764{
2765 int slot = 1;
2766 psa_key_type_t key_type = key_type_arg;
2767 psa_algorithm_t alg = alg_arg;
2768 size_t expected_output_length = expected_output_length_arg;
2769 size_t key_bits;
2770 unsigned char *output = NULL;
2771 size_t output_size;
2772 size_t output_length = ~0;
2773 psa_status_t actual_status;
2774 psa_status_t expected_status = expected_status_arg;
2775 psa_key_policy_t policy;
2776
2777 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2778
2779 /* Import the key */
2780 psa_key_policy_init( &policy );
2781 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2782 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2783 TEST_ASSERT( psa_import_key( slot, key_type,
2784 key_data->x,
2785 key_data->len ) == PSA_SUCCESS );
2786
2787 /* Determine the maximum output length */
2788 TEST_ASSERT( psa_get_key_information( slot,
2789 NULL,
2790 &key_bits ) == PSA_SUCCESS );
2791 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002792 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002793
2794 /* Encrypt the input */
2795 actual_status = psa_asymmetric_encrypt( slot, alg,
2796 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002797 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002798 output, output_size,
2799 &output_length );
2800 TEST_ASSERT( actual_status == expected_status );
2801 TEST_ASSERT( output_length == expected_output_length );
2802
Gilles Peskine68428122018-06-30 18:42:41 +02002803 /* If the label is empty, the test framework puts a non-null pointer
2804 * in label->x. Test that a null pointer works as well. */
2805 if( label->len == 0 )
2806 {
2807 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002808 if( output_size != 0 )
2809 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002810 actual_status = psa_asymmetric_encrypt( slot, alg,
2811 input_data->x, input_data->len,
2812 NULL, label->len,
2813 output, output_size,
2814 &output_length );
2815 TEST_ASSERT( actual_status == expected_status );
2816 TEST_ASSERT( output_length == expected_output_length );
2817 }
2818
Gilles Peskine656896e2018-06-29 19:12:28 +02002819exit:
2820 psa_destroy_key( slot );
2821 mbedtls_free( output );
2822 mbedtls_psa_crypto_free( );
2823}
2824/* END_CASE */
2825
2826/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002827void asymmetric_encrypt_decrypt( int key_type_arg,
2828 data_t *key_data,
2829 int alg_arg,
2830 data_t *input_data,
2831 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002832{
2833 int slot = 1;
2834 psa_key_type_t key_type = key_type_arg;
2835 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002836 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002837 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002838 size_t output_size;
2839 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002840 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002841 size_t output2_size;
2842 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002843 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002844
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002845 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002846 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002847 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2848 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2849
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002850 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2851
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002852 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002853 psa_key_policy_set_usage( &policy,
2854 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002855 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002856 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2857
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002858 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002859 key_data->x,
2860 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002861
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002862
2863 /* Determine the maximum ciphertext length */
2864 TEST_ASSERT( psa_get_key_information( slot,
2865 NULL,
2866 &key_bits ) == PSA_SUCCESS );
2867 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002868 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002869 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002870 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002871
Gilles Peskineeebd7382018-06-08 18:11:54 +02002872 /* We test encryption by checking that encrypt-then-decrypt gives back
2873 * the original plaintext because of the non-optional random
2874 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002875 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002876 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002877 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002878 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002879 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002880 /* We don't know what ciphertext length to expect, but check that
2881 * it looks sensible. */
2882 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002883
Gilles Peskine2d277862018-06-18 15:41:12 +02002884 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002885 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002886 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002887 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002888 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002889 ASSERT_COMPARE( input_data->x, input_data->len,
2890 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002891
2892exit:
2893 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002894 mbedtls_free( output );
2895 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002896 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002897}
2898/* END_CASE */
2899
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002900/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002901void asymmetric_decrypt( int key_type_arg,
2902 data_t *key_data,
2903 int alg_arg,
2904 data_t *input_data,
2905 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002906 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002907{
2908 int slot = 1;
2909 psa_key_type_t key_type = key_type_arg;
2910 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002911 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002912 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002913 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002914 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002915
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002916 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002917 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002918 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002919 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2920 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2921 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2922
Gilles Peskine4abf7412018-06-18 16:35:34 +02002923 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002924 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002925
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002926 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2927
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002928 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002929 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002930 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2931
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002932 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002933 key_data->x,
2934 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002935
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002936 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002937 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002938 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002939 output,
2940 output_size,
2941 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002942 ASSERT_COMPARE( expected_data->x, expected_data->len,
2943 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002944
Gilles Peskine68428122018-06-30 18:42:41 +02002945 /* If the label is empty, the test framework puts a non-null pointer
2946 * in label->x. Test that a null pointer works as well. */
2947 if( label->len == 0 )
2948 {
2949 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002950 if( output_size != 0 )
2951 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002952 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2953 input_data->x, input_data->len,
2954 NULL, label->len,
2955 output,
2956 output_size,
2957 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002958 ASSERT_COMPARE( expected_data->x, expected_data->len,
2959 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02002960 }
2961
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962exit:
2963 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002964 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002966}
2967/* END_CASE */
2968
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002969/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002970void asymmetric_decrypt_fail( int key_type_arg,
2971 data_t *key_data,
2972 int alg_arg,
2973 data_t *input_data,
2974 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002975 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002976{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002977 int slot = 1;
2978 psa_key_type_t key_type = key_type_arg;
2979 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002980 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002981 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002982 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002983 psa_status_t actual_status;
2984 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002985 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002986
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002987 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002989 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2990 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2991
Gilles Peskine4abf7412018-06-18 16:35:34 +02002992 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002993 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002994
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002995 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2996
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002997 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002998 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002999 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3000
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003001 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003002 key_data->x,
3003 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003004
Gilles Peskine2d277862018-06-18 15:41:12 +02003005 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003006 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003007 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003008 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003009 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003010 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003011 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003012
Gilles Peskine68428122018-06-30 18:42:41 +02003013 /* If the label is empty, the test framework puts a non-null pointer
3014 * in label->x. Test that a null pointer works as well. */
3015 if( label->len == 0 )
3016 {
3017 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003018 if( output_size != 0 )
3019 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003020 actual_status = psa_asymmetric_decrypt( slot, alg,
3021 input_data->x, input_data->len,
3022 NULL, label->len,
3023 output, output_size,
3024 &output_length );
3025 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003026 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003027 }
3028
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003029exit:
3030 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003031 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032 mbedtls_psa_crypto_free( );
3033}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003034/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003035
3036/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003037void derive_setup( int key_type_arg,
3038 data_t *key_data,
3039 int alg_arg,
3040 data_t *salt,
3041 data_t *label,
3042 int requested_capacity_arg,
3043 int expected_status_arg )
3044{
3045 psa_key_slot_t slot = 1;
3046 size_t key_type = key_type_arg;
3047 psa_algorithm_t alg = alg_arg;
3048 size_t requested_capacity = requested_capacity_arg;
3049 psa_status_t expected_status = expected_status_arg;
3050 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3051 psa_key_policy_t policy;
3052
3053 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3054
3055 psa_key_policy_init( &policy );
3056 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3057 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3058
3059 TEST_ASSERT( psa_import_key( slot, key_type,
3060 key_data->x,
3061 key_data->len ) == PSA_SUCCESS );
3062
3063 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3064 salt->x, salt->len,
3065 label->x, label->len,
3066 requested_capacity ) == expected_status );
3067
3068exit:
3069 psa_generator_abort( &generator );
3070 psa_destroy_key( slot );
3071 mbedtls_psa_crypto_free( );
3072}
3073/* END_CASE */
3074
3075/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003076void derive_output( int alg_arg,
3077 data_t *key_data,
3078 data_t *salt,
3079 data_t *label,
3080 int requested_capacity_arg,
3081 data_t *expected_output1,
3082 data_t *expected_output2 )
3083{
3084 psa_key_slot_t slot = 1;
3085 psa_algorithm_t alg = alg_arg;
3086 size_t requested_capacity = requested_capacity_arg;
3087 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3088 uint8_t *expected_outputs[2] =
3089 {expected_output1->x, expected_output2->x};
3090 size_t output_sizes[2] =
3091 {expected_output1->len, expected_output2->len};
3092 size_t output_buffer_size = 0;
3093 uint8_t *output_buffer = NULL;
3094 size_t expected_capacity;
3095 size_t current_capacity;
3096 psa_key_policy_t policy;
3097 psa_status_t status;
3098 unsigned i;
3099
3100 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3101 {
3102 if( output_sizes[i] > output_buffer_size )
3103 output_buffer_size = output_sizes[i];
3104 if( output_sizes[i] == 0 )
3105 expected_outputs[i] = NULL;
3106 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003107 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003108 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3109
3110 psa_key_policy_init( &policy );
3111 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3112 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3113
3114 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3115 key_data->x,
3116 key_data->len ) == PSA_SUCCESS );
3117
3118 /* Extraction phase. */
3119 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3120 salt->x, salt->len,
3121 label->x, label->len,
3122 requested_capacity ) == PSA_SUCCESS );
3123 TEST_ASSERT( psa_get_generator_capacity( &generator,
3124 &current_capacity ) ==
3125 PSA_SUCCESS );
3126 TEST_ASSERT( current_capacity == requested_capacity );
3127 expected_capacity = requested_capacity;
3128
3129 /* Expansion phase. */
3130 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3131 {
3132 /* Read some bytes. */
3133 status = psa_generator_read( &generator,
3134 output_buffer, output_sizes[i] );
3135 if( expected_capacity == 0 && output_sizes[i] == 0 )
3136 {
3137 /* Reading 0 bytes when 0 bytes are available can go either way. */
3138 TEST_ASSERT( status == PSA_SUCCESS ||
3139 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3140 continue;
3141 }
3142 else if( expected_capacity == 0 ||
3143 output_sizes[i] > expected_capacity )
3144 {
3145 /* Capacity exceeded. */
3146 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3147 expected_capacity = 0;
3148 continue;
3149 }
3150 /* Success. Check the read data. */
3151 TEST_ASSERT( status == PSA_SUCCESS );
3152 if( output_sizes[i] != 0 )
3153 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3154 output_sizes[i] ) == 0 );
3155 /* Check the generator status. */
3156 expected_capacity -= output_sizes[i];
3157 TEST_ASSERT( psa_get_generator_capacity( &generator,
3158 &current_capacity ) ==
3159 PSA_SUCCESS );
3160 TEST_ASSERT( expected_capacity == current_capacity );
3161 }
3162 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3163
3164exit:
3165 mbedtls_free( output_buffer );
3166 psa_generator_abort( &generator );
3167 psa_destroy_key( slot );
3168 mbedtls_psa_crypto_free( );
3169}
3170/* END_CASE */
3171
3172/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003173void derive_full( int alg_arg,
3174 data_t *key_data,
3175 data_t *salt,
3176 data_t *label,
3177 int requested_capacity_arg )
3178{
3179 psa_key_slot_t slot = 1;
3180 psa_algorithm_t alg = alg_arg;
3181 size_t requested_capacity = requested_capacity_arg;
3182 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3183 unsigned char output_buffer[16];
3184 size_t expected_capacity = requested_capacity;
3185 size_t current_capacity;
3186 psa_key_policy_t policy;
3187
3188 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3189
3190 psa_key_policy_init( &policy );
3191 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3192 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3193
3194 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3195 key_data->x,
3196 key_data->len ) == PSA_SUCCESS );
3197
3198 /* Extraction phase. */
3199 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3200 salt->x, salt->len,
3201 label->x, label->len,
3202 requested_capacity ) == PSA_SUCCESS );
3203 TEST_ASSERT( psa_get_generator_capacity( &generator,
3204 &current_capacity ) ==
3205 PSA_SUCCESS );
3206 TEST_ASSERT( current_capacity == expected_capacity );
3207
3208 /* Expansion phase. */
3209 while( current_capacity > 0 )
3210 {
3211 size_t read_size = sizeof( output_buffer );
3212 if( read_size > current_capacity )
3213 read_size = current_capacity;
3214 TEST_ASSERT( psa_generator_read( &generator,
3215 output_buffer,
3216 read_size ) == PSA_SUCCESS );
3217 expected_capacity -= read_size;
3218 TEST_ASSERT( psa_get_generator_capacity( &generator,
3219 &current_capacity ) ==
3220 PSA_SUCCESS );
3221 TEST_ASSERT( current_capacity == expected_capacity );
3222 }
3223
3224 /* Check that the generator refuses to go over capacity. */
3225 TEST_ASSERT( psa_generator_read( &generator,
3226 output_buffer,
3227 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3228
3229 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3230
3231exit:
3232 psa_generator_abort( &generator );
3233 psa_destroy_key( slot );
3234 mbedtls_psa_crypto_free( );
3235}
3236/* END_CASE */
3237
3238/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003239void derive_key_exercise( int alg_arg,
3240 data_t *key_data,
3241 data_t *salt,
3242 data_t *label,
3243 int derived_type_arg,
3244 int derived_bits_arg,
3245 int derived_usage_arg,
3246 int derived_alg_arg )
3247{
3248 psa_key_slot_t base_key = 1;
3249 psa_key_slot_t derived_key = 2;
3250 psa_algorithm_t alg = alg_arg;
3251 psa_key_type_t derived_type = derived_type_arg;
3252 size_t derived_bits = derived_bits_arg;
3253 psa_key_usage_t derived_usage = derived_usage_arg;
3254 psa_algorithm_t derived_alg = derived_alg_arg;
3255 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3256 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3257 psa_key_policy_t policy;
3258 psa_key_type_t got_type;
3259 size_t got_bits;
3260
3261 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3262
3263 psa_key_policy_init( &policy );
3264 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3265 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3266 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3267 key_data->x,
3268 key_data->len ) == PSA_SUCCESS );
3269
3270 /* Derive a key. */
3271 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3272 salt->x, salt->len,
3273 label->x, label->len,
3274 capacity ) == PSA_SUCCESS );
3275 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3276 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3277 TEST_ASSERT( psa_generator_import_key( derived_key,
3278 derived_type,
3279 derived_bits,
3280 &generator ) == PSA_SUCCESS );
3281
3282 /* Test the key information */
3283 TEST_ASSERT( psa_get_key_information( derived_key,
3284 &got_type,
3285 &got_bits ) == PSA_SUCCESS );
3286 TEST_ASSERT( got_type == derived_type );
3287 TEST_ASSERT( got_bits == derived_bits );
3288
3289 /* Exercise the derived key. */
3290 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3291 goto exit;
3292
3293exit:
3294 psa_generator_abort( &generator );
3295 psa_destroy_key( base_key );
3296 psa_destroy_key( derived_key );
3297 mbedtls_psa_crypto_free( );
3298}
3299/* END_CASE */
3300
3301/* BEGIN_CASE */
3302void derive_key_export( int alg_arg,
3303 data_t *key_data,
3304 data_t *salt,
3305 data_t *label,
3306 int bytes1_arg,
3307 int bytes2_arg )
3308{
3309 psa_key_slot_t base_key = 1;
3310 psa_key_slot_t derived_key = 2;
3311 psa_algorithm_t alg = alg_arg;
3312 size_t bytes1 = bytes1_arg;
3313 size_t bytes2 = bytes2_arg;
3314 size_t capacity = bytes1 + bytes2;
3315 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003316 uint8_t *output_buffer = NULL;
3317 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003318 psa_key_policy_t policy;
3319 size_t length;
3320
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003321 ASSERT_ALLOC( output_buffer, capacity );
3322 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003323 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3324
3325 psa_key_policy_init( &policy );
3326 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3327 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3328 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3329 key_data->x,
3330 key_data->len ) == PSA_SUCCESS );
3331
3332 /* Derive some material and output it. */
3333 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3334 salt->x, salt->len,
3335 label->x, label->len,
3336 capacity ) == PSA_SUCCESS );
3337 TEST_ASSERT( psa_generator_read( &generator,
3338 output_buffer,
3339 capacity ) == PSA_SUCCESS );
3340 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3341
3342 /* Derive the same output again, but this time store it in key objects. */
3343 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3344 salt->x, salt->len,
3345 label->x, label->len,
3346 capacity ) == PSA_SUCCESS );
3347 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3348 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3349 TEST_ASSERT( psa_generator_import_key( derived_key,
3350 PSA_KEY_TYPE_RAW_DATA,
3351 PSA_BYTES_TO_BITS( bytes1 ),
3352 &generator ) == PSA_SUCCESS );
3353 TEST_ASSERT( psa_export_key( derived_key,
3354 export_buffer, bytes1,
3355 &length ) == PSA_SUCCESS );
3356 TEST_ASSERT( length == bytes1 );
3357 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3358 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3359 TEST_ASSERT( psa_generator_import_key( derived_key,
3360 PSA_KEY_TYPE_RAW_DATA,
3361 PSA_BYTES_TO_BITS( bytes2 ),
3362 &generator ) == PSA_SUCCESS );
3363 TEST_ASSERT( psa_export_key( derived_key,
3364 export_buffer + bytes1, bytes2,
3365 &length ) == PSA_SUCCESS );
3366 TEST_ASSERT( length == bytes2 );
3367
3368 /* Compare the outputs from the two runs. */
3369 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3370
3371exit:
3372 mbedtls_free( output_buffer );
3373 mbedtls_free( export_buffer );
3374 psa_generator_abort( &generator );
3375 psa_destroy_key( base_key );
3376 psa_destroy_key( derived_key );
3377 mbedtls_psa_crypto_free( );
3378}
3379/* END_CASE */
3380
3381/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003382void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003383{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003384 size_t bytes = bytes_arg;
3385 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003386 unsigned char *output = NULL;
3387 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003388 size_t i;
3389 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003390
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003391 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3392 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003393 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003394
3395 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3396
Gilles Peskinea50d7392018-06-21 10:22:13 +02003397 /* Run several times, to ensure that every output byte will be
3398 * nonzero at least once with overwhelming probability
3399 * (2^(-8*number_of_runs)). */
3400 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003401 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003402 if( bytes != 0 )
3403 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003404 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3405
3406 /* Check that no more than bytes have been overwritten */
3407 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3408
3409 for( i = 0; i < bytes; i++ )
3410 {
3411 if( output[i] != 0 )
3412 ++changed[i];
3413 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003414 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003415
3416 /* Check that every byte was changed to nonzero at least once. This
3417 * validates that psa_generate_random is overwriting every byte of
3418 * the output buffer. */
3419 for( i = 0; i < bytes; i++ )
3420 {
3421 TEST_ASSERT( changed[i] != 0 );
3422 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003423
3424exit:
3425 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003426 mbedtls_free( output );
3427 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003428}
3429/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003430
3431/* BEGIN_CASE */
3432void generate_key( int type_arg,
3433 int bits_arg,
3434 int usage_arg,
3435 int alg_arg,
3436 int expected_status_arg )
3437{
3438 int slot = 1;
3439 psa_key_type_t type = type_arg;
3440 psa_key_usage_t usage = usage_arg;
3441 size_t bits = bits_arg;
3442 psa_algorithm_t alg = alg_arg;
3443 psa_status_t expected_status = expected_status_arg;
3444 psa_key_type_t got_type;
3445 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003446 psa_status_t expected_info_status =
3447 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3448 psa_key_policy_t policy;
3449
3450 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3451
3452 psa_key_policy_init( &policy );
3453 psa_key_policy_set_usage( &policy, usage, alg );
3454 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3455
3456 /* Generate a key */
3457 TEST_ASSERT( psa_generate_key( slot, type, bits,
3458 NULL, 0 ) == expected_status );
3459
3460 /* Test the key information */
3461 TEST_ASSERT( psa_get_key_information( slot,
3462 &got_type,
3463 &got_bits ) == expected_info_status );
3464 if( expected_info_status != PSA_SUCCESS )
3465 goto exit;
3466 TEST_ASSERT( got_type == type );
3467 TEST_ASSERT( got_bits == bits );
3468
Gilles Peskine818ca122018-06-20 18:16:48 +02003469 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003470 if( ! exercise_key( slot, usage, alg ) )
3471 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003472
3473exit:
3474 psa_destroy_key( slot );
3475 mbedtls_psa_crypto_free( );
3476}
3477/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003478
3479/* BEGIN_CASE */
3480void validate_module_init_generate_random( )
3481{
3482 psa_status_t status;
3483 uint8_t random[10] = { 0 };
3484 status = psa_generate_random( random, sizeof( random ) );
3485 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3486}
3487/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003488
3489/* BEGIN_CASE */
3490void validate_module_init_key_based( )
3491{
3492 psa_status_t status;
3493 uint8_t data[10] = { 0 };
3494 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3495 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3496}
3497/* END_CASE */