blob: e14b2256de6e5125fe647ff2ba948458fed47cbf [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 Peskined35a1cc2018-06-26 21:26:10 +020025/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020026 *
27 * \param buffer Pointer to the beginning of the buffer.
28 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020033static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
38 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskine818ca122018-06-20 18:16:48 +0200128static int exercise_mac_key( psa_key_slot_t key,
129 psa_key_usage_t usage,
130 psa_algorithm_t alg )
131{
132 psa_mac_operation_t operation;
133 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200134 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200135 size_t mac_length = sizeof( mac );
136
137 if( usage & PSA_KEY_USAGE_SIGN )
138 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200139 TEST_ASSERT( psa_mac_sign_setup( &operation,
140 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 TEST_ASSERT( psa_mac_update( &operation,
142 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200143 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200144 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200145 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200146 }
147
148 if( usage & PSA_KEY_USAGE_VERIFY )
149 {
150 psa_status_t verify_status =
151 ( usage & PSA_KEY_USAGE_SIGN ?
152 PSA_SUCCESS :
153 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200154 TEST_ASSERT( psa_mac_verify_setup( &operation,
155 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200156 TEST_ASSERT( psa_mac_update( &operation,
157 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200158 TEST_ASSERT( psa_mac_verify_finish( &operation,
159 mac,
160 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200161 }
162
163 return( 1 );
164
165exit:
166 psa_mac_abort( &operation );
167 return( 0 );
168}
169
170static int exercise_cipher_key( psa_key_slot_t key,
171 psa_key_usage_t usage,
172 psa_algorithm_t alg )
173{
174 psa_cipher_operation_t operation;
175 unsigned char iv[16] = {0};
176 size_t iv_length = sizeof( iv );
177 const unsigned char plaintext[16] = "Hello, world...";
178 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
179 size_t ciphertext_length = sizeof( ciphertext );
180 unsigned char decrypted[sizeof( ciphertext )];
181 size_t part_length;
182
183 if( usage & PSA_KEY_USAGE_ENCRYPT )
184 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200185 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
186 key, alg ) == PSA_SUCCESS );
187 TEST_ASSERT( psa_cipher_generate_iv( &operation,
188 iv, sizeof( iv ),
189 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200190 TEST_ASSERT( psa_cipher_update( &operation,
191 plaintext, sizeof( plaintext ),
192 ciphertext, sizeof( ciphertext ),
193 &ciphertext_length ) == PSA_SUCCESS );
194 TEST_ASSERT( psa_cipher_finish( &operation,
195 ciphertext + ciphertext_length,
196 sizeof( ciphertext ) - ciphertext_length,
197 &part_length ) == PSA_SUCCESS );
198 ciphertext_length += part_length;
199 }
200
201 if( usage & PSA_KEY_USAGE_DECRYPT )
202 {
203 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700204 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200205 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
206 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200207 size_t bits;
208 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
209 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
210 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200211 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
212 key, alg ) == PSA_SUCCESS );
213 TEST_ASSERT( psa_cipher_set_iv( &operation,
214 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200215 TEST_ASSERT( psa_cipher_update( &operation,
216 ciphertext, ciphertext_length,
217 decrypted, sizeof( decrypted ),
218 &part_length ) == PSA_SUCCESS );
219 status = psa_cipher_finish( &operation,
220 decrypted + part_length,
221 sizeof( decrypted ) - part_length,
222 &part_length );
223 /* For a stream cipher, all inputs are valid. For a block cipher,
224 * if the input is some aribtrary data rather than an actual
225 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700226 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700227 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200228 TEST_ASSERT( status == PSA_SUCCESS );
229 else
230 TEST_ASSERT( status == PSA_SUCCESS ||
231 status == PSA_ERROR_INVALID_PADDING );
232 }
233
234 return( 1 );
235
236exit:
237 psa_cipher_abort( &operation );
238 return( 0 );
239}
240
241static int exercise_aead_key( psa_key_slot_t key,
242 psa_key_usage_t usage,
243 psa_algorithm_t alg )
244{
245 unsigned char nonce[16] = {0};
246 size_t nonce_length = sizeof( nonce );
247 unsigned char plaintext[16] = "Hello, world...";
248 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
249 size_t ciphertext_length = sizeof( ciphertext );
250 size_t plaintext_length = sizeof( ciphertext );
251
252 if( usage & PSA_KEY_USAGE_ENCRYPT )
253 {
254 TEST_ASSERT( psa_aead_encrypt( key, alg,
255 nonce, nonce_length,
256 NULL, 0,
257 plaintext, sizeof( plaintext ),
258 ciphertext, sizeof( ciphertext ),
259 &ciphertext_length ) == PSA_SUCCESS );
260 }
261
262 if( usage & PSA_KEY_USAGE_DECRYPT )
263 {
264 psa_status_t verify_status =
265 ( usage & PSA_KEY_USAGE_ENCRYPT ?
266 PSA_SUCCESS :
267 PSA_ERROR_INVALID_SIGNATURE );
268 TEST_ASSERT( psa_aead_decrypt( key, alg,
269 nonce, nonce_length,
270 NULL, 0,
271 ciphertext, ciphertext_length,
272 plaintext, sizeof( plaintext ),
273 &plaintext_length ) == verify_status );
274 }
275
276 return( 1 );
277
278exit:
279 return( 0 );
280}
281
282static int exercise_signature_key( psa_key_slot_t key,
283 psa_key_usage_t usage,
284 psa_algorithm_t alg )
285{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200286 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
287 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200288 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200289 size_t signature_length = sizeof( signature );
290
291 if( usage & PSA_KEY_USAGE_SIGN )
292 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200293 /* Some algorithms require the payload to have the size of
294 * the hash encoded in the algorithm. Use this input size
295 * even for algorithms that allow other input sizes. */
296 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
297 if( hash_alg != 0 )
298 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200299 TEST_ASSERT( psa_asymmetric_sign( key, alg,
300 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200301 signature, sizeof( signature ),
302 &signature_length ) == PSA_SUCCESS );
303 }
304
305 if( usage & PSA_KEY_USAGE_VERIFY )
306 {
307 psa_status_t verify_status =
308 ( usage & PSA_KEY_USAGE_SIGN ?
309 PSA_SUCCESS :
310 PSA_ERROR_INVALID_SIGNATURE );
311 TEST_ASSERT( psa_asymmetric_verify( key, alg,
312 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200313 signature, signature_length ) ==
314 verify_status );
315 }
316
317 return( 1 );
318
319exit:
320 return( 0 );
321}
322
323static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
324 psa_key_usage_t usage,
325 psa_algorithm_t alg )
326{
327 unsigned char plaintext[256] = "Hello, world...";
328 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
329 size_t ciphertext_length = sizeof( ciphertext );
330 size_t plaintext_length = 16;
331
332 if( usage & PSA_KEY_USAGE_ENCRYPT )
333 {
334 TEST_ASSERT(
335 psa_asymmetric_encrypt( key, alg,
336 plaintext, plaintext_length,
337 NULL, 0,
338 ciphertext, sizeof( ciphertext ),
339 &ciphertext_length ) == PSA_SUCCESS );
340 }
341
342 if( usage & PSA_KEY_USAGE_DECRYPT )
343 {
344 psa_status_t status =
345 psa_asymmetric_decrypt( key, alg,
346 ciphertext, ciphertext_length,
347 NULL, 0,
348 plaintext, sizeof( plaintext ),
349 &plaintext_length );
350 TEST_ASSERT( status == PSA_SUCCESS ||
351 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
352 ( status == PSA_ERROR_INVALID_ARGUMENT ||
353 status == PSA_ERROR_INVALID_PADDING ) ) );
354 }
355
356 return( 1 );
357
358exit:
359 return( 0 );
360}
Gilles Peskine02b75072018-07-01 22:31:34 +0200361
Gilles Peskineea0fb492018-07-12 17:17:20 +0200362static int exercise_key_derivation_key( psa_key_slot_t key,
363 psa_key_usage_t usage,
364 psa_algorithm_t alg )
365{
366 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
367 unsigned char label[16] = "This is a label.";
368 size_t label_length = sizeof( label );
369 unsigned char seed[16] = "abcdefghijklmnop";
370 size_t seed_length = sizeof( seed );
371 unsigned char output[1];
372
373 if( usage & PSA_KEY_USAGE_DERIVE )
374 {
375 TEST_ASSERT( psa_key_derivation( &generator,
376 key, alg,
377 label, label_length,
378 seed, seed_length,
379 sizeof( output ) ) == PSA_SUCCESS );
380 TEST_ASSERT( psa_generator_read( &generator,
381 output,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
384 }
385
386 return( 1 );
387
388exit:
389 return( 0 );
390}
391
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200392static int is_oid_of_key_type( psa_key_type_t type,
393 const uint8_t *oid, size_t oid_length )
394{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200395 const uint8_t *expected_oid = NULL;
396 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200397#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200398 if( PSA_KEY_TYPE_IS_RSA( type ) )
399 {
400 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
401 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
402 }
403 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200404#endif /* MBEDTLS_RSA_C */
405#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200406 if( PSA_KEY_TYPE_IS_ECC( type ) )
407 {
408 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
409 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
410 }
411 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200412#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200413 {
414 char message[40];
415 mbedtls_snprintf( message, sizeof( message ),
416 "OID not known for key type=0x%08lx",
417 (unsigned long) type );
418 test_fail( message, __LINE__, __FILE__ );
419 return( 0 );
420 }
421
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200422 TEST_ASSERT( oid_length == expected_oid_length );
423 TEST_ASSERT( memcmp( oid, expected_oid, oid_length ) == 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200424 return( 1 );
425
426exit:
427 return( 0 );
428}
429
430static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
431 size_t min_bits, size_t max_bits,
432 int must_be_odd )
433{
434 size_t len;
435 size_t actual_bits;
436 unsigned char msb;
437 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
438 MBEDTLS_ASN1_INTEGER ) == 0 );
439 /* Tolerate a slight departure from DER encoding:
440 * - 0 may be represented by an empty string or a 1-byte string.
441 * - The sign bit may be used as a value bit. */
442 if( ( len == 1 && ( *p )[0] == 0 ) ||
443 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
444 {
445 ++( *p );
446 --len;
447 }
448 if( min_bits == 0 && len == 0 )
449 return( 1 );
450 msb = ( *p )[0];
451 TEST_ASSERT( msb != 0 );
452 actual_bits = 8 * ( len - 1 );
453 while( msb != 0 )
454 {
455 msb >>= 1;
456 ++actual_bits;
457 }
458 TEST_ASSERT( actual_bits >= min_bits );
459 TEST_ASSERT( actual_bits <= max_bits );
460 if( must_be_odd )
461 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
462 *p += len;
463 return( 1 );
464exit:
465 return( 0 );
466}
467
468static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
469 size_t *len,
470 unsigned char n, unsigned char tag )
471{
472 int ret;
473 ret = mbedtls_asn1_get_tag( p, end, len,
474 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
475 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
476 if( ret != 0 )
477 return( ret );
478 end = *p + *len;
479 ret = mbedtls_asn1_get_tag( p, end, len, tag );
480 if( ret != 0 )
481 return( ret );
482 if( *p + *len != end )
483 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
484 return( 0 );
485}
486
487static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
488 uint8_t *exported, size_t exported_length )
489{
490 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200491 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200492 else
493 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200494
495#if defined(MBEDTLS_DES_C)
496 if( type == PSA_KEY_TYPE_DES )
497 {
498 /* Check the parity bits. */
499 unsigned i;
500 for( i = 0; i < bits / 8; i++ )
501 {
502 unsigned bit_count = 0;
503 unsigned m;
504 for( m = 1; m <= 0x100; m <<= 1 )
505 {
506 if( exported[i] & m )
507 ++bit_count;
508 }
509 TEST_ASSERT( bit_count % 2 != 0 );
510 }
511 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200512 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200513#endif
514
515#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
516 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
517 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200518 uint8_t *p = exported;
519 uint8_t *end = exported + exported_length;
520 size_t len;
521 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200522 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200523 * modulus INTEGER, -- n
524 * publicExponent INTEGER, -- e
525 * privateExponent INTEGER, -- d
526 * prime1 INTEGER, -- p
527 * prime2 INTEGER, -- q
528 * exponent1 INTEGER, -- d mod (p-1)
529 * exponent2 INTEGER, -- d mod (q-1)
530 * coefficient INTEGER, -- (inverse of q) mod p
531 * }
532 */
533 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
534 MBEDTLS_ASN1_SEQUENCE |
535 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
536 TEST_ASSERT( p + len == end );
537 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
538 goto exit;
539 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
540 goto exit;
541 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
542 goto exit;
543 /* Require d to be at least half the size of n. */
544 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
545 goto exit;
546 /* Require p and q to be at most half the size of n, rounded up. */
547 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
548 goto exit;
549 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
550 goto exit;
551 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
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 TEST_ASSERT( p == end );
558 }
559 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200560#endif /* MBEDTLS_RSA_C */
561
562#if defined(MBEDTLS_ECP_C)
563 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
564 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200565 uint8_t *p = exported;
566 uint8_t *end = exported + exported_length;
567 size_t len;
568 int version;
569 /* ECPrivateKey ::= SEQUENCE {
570 * version INTEGER, -- must be 1
571 * privateKey OCTET STRING,
572 * -- `ceiling(log_{256}(n))`-byte string, big endian,
573 * -- where n is the order of the curve.
574 * parameters ECParameters {{ NamedCurve }}, -- mandatory
575 * publicKey BIT STRING -- mandatory
576 * }
577 */
578 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
579 MBEDTLS_ASN1_SEQUENCE |
580 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
581 TEST_ASSERT( p + len == end );
582 TEST_ASSERT( mbedtls_asn1_get_int( &p, end, &version ) == 0 );
583 TEST_ASSERT( version == 1 );
584 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
585 MBEDTLS_ASN1_OCTET_STRING ) == 0 );
586 /* Bug in Mbed TLS: the length of the octet string depends on the value */
587 // TEST_ASSERT( len == PSA_BITS_TO_BYTES( bits ) );
588 p += len;
589 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 0,
590 MBEDTLS_ASN1_OID ) == 0 );
591 p += len;
Gilles Peskinec6290c02018-08-13 17:24:59 +0200592 /* publicKey: ECPoint in uncompressed representation (as below) */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200593 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 1,
594 MBEDTLS_ASN1_BIT_STRING ) == 0 );
595 TEST_ASSERT( p + len == end );
596 TEST_ASSERT( p[0] == 0 ); /* 0 unused bits in the bit string */
597 ++p;
598 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
599 TEST_ASSERT( p[0] == 4 );
600 }
601 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200602#endif /* MBEDTLS_ECP_C */
603
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200604 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
605 {
606 uint8_t *p = exported;
607 uint8_t *end = exported + exported_length;
608 size_t len;
609 mbedtls_asn1_buf alg;
610 mbedtls_asn1_buf params;
611 mbedtls_asn1_bitstring bitstring;
612 /* SubjectPublicKeyInfo ::= SEQUENCE {
613 * algorithm AlgorithmIdentifier,
614 * subjectPublicKey BIT STRING }
615 * AlgorithmIdentifier ::= SEQUENCE {
616 * algorithm OBJECT IDENTIFIER,
617 * parameters ANY DEFINED BY algorithm OPTIONAL }
618 */
619 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
620 MBEDTLS_ASN1_SEQUENCE |
621 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
622 TEST_ASSERT( p + len == end );
623 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
624 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
625 goto exit;
626 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
627 TEST_ASSERT( p == end );
628 p = bitstring.p;
629#if defined(MBEDTLS_RSA_C)
630 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
631 {
632 /* RSAPublicKey ::= SEQUENCE {
633 * modulus INTEGER, -- n
634 * publicExponent INTEGER } -- e
635 */
636 TEST_ASSERT( bitstring.unused_bits == 0 );
637 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
638 MBEDTLS_ASN1_SEQUENCE |
639 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
640 TEST_ASSERT( p + len == end );
641 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
642 goto exit;
643 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
644 goto exit;
645 TEST_ASSERT( p == end );
646 }
647 else
648#endif /* MBEDTLS_RSA_C */
649#if defined(MBEDTLS_ECP_C)
650 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
651 {
652 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200653 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200654 * -- then x_P as an n-bit string, big endian;
655 * -- then y_P as a n-bit string, big endian,
656 * -- where n is the order of the curve.
657 */
658 TEST_ASSERT( bitstring.unused_bits == 0 );
659 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
660 TEST_ASSERT( p[0] == 4 );
661 }
662 else
663#endif /* MBEDTLS_ECP_C */
664 {
665 char message[40];
666 mbedtls_snprintf( message, sizeof( message ),
667 "No sanity check for public key type=0x%08lx",
668 (unsigned long) type );
669 test_fail( message, __LINE__, __FILE__ );
670 return( 0 );
671 }
672 }
673 else
674
675 {
676 /* No sanity checks for other types */
677 }
678
679 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200680
681exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200683}
684
685static int exercise_export_key( psa_key_slot_t slot,
686 psa_key_usage_t usage )
687{
688 psa_key_type_t type;
689 size_t bits;
690 uint8_t *exported = NULL;
691 size_t exported_size = 0;
692 size_t exported_length = 0;
693 int ok = 0;
694
695 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 )
696 {
697 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
698 PSA_ERROR_NOT_PERMITTED );
699 return( 1 );
700 }
701
702 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
703 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
704 exported = mbedtls_calloc( 1, exported_size );
705 TEST_ASSERT( exported != NULL );
706
707 TEST_ASSERT( psa_export_key( slot,
708 exported, exported_size,
709 &exported_length ) == PSA_SUCCESS );
710 ok = exported_key_sanity_check( type, bits, exported, exported_length );
711
712exit:
713 mbedtls_free( exported );
714 return( ok );
715}
716
717static int exercise_export_public_key( psa_key_slot_t slot )
718{
719 psa_key_type_t type;
720 psa_key_type_t public_type;
721 size_t bits;
722 uint8_t *exported = NULL;
723 size_t exported_size = 0;
724 size_t exported_length = 0;
725 int ok = 0;
726
727 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
728 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
729 {
730 TEST_ASSERT( psa_export_public_key( slot,
731 NULL, 0, &exported_length ) ==
732 PSA_ERROR_INVALID_ARGUMENT );
733 return( 1 );
734 }
735
736 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
737 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
738 exported = mbedtls_calloc( 1, exported_size );
739 TEST_ASSERT( exported != NULL );
740
741 TEST_ASSERT( psa_export_public_key( slot,
742 exported, exported_size,
743 &exported_length ) == PSA_SUCCESS );
744 ok = exported_key_sanity_check( public_type, bits,
745 exported, exported_length );
746
747exit:
748 mbedtls_free( exported );
749 return( ok );
750}
751
Gilles Peskine02b75072018-07-01 22:31:34 +0200752static int exercise_key( psa_key_slot_t slot,
753 psa_key_usage_t usage,
754 psa_algorithm_t alg )
755{
756 int ok;
757 if( alg == 0 )
758 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
759 else if( PSA_ALG_IS_MAC( alg ) )
760 ok = exercise_mac_key( slot, usage, alg );
761 else if( PSA_ALG_IS_CIPHER( alg ) )
762 ok = exercise_cipher_key( slot, usage, alg );
763 else if( PSA_ALG_IS_AEAD( alg ) )
764 ok = exercise_aead_key( slot, usage, alg );
765 else if( PSA_ALG_IS_SIGN( alg ) )
766 ok = exercise_signature_key( slot, usage, alg );
767 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
768 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200769 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
770 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200771 else
772 {
773 char message[40];
774 mbedtls_snprintf( message, sizeof( message ),
775 "No code to exercise alg=0x%08lx",
776 (unsigned long) alg );
777 test_fail( message, __LINE__, __FILE__ );
778 ok = 0;
779 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200780
781 ok = ok && exercise_export_key( slot, usage );
782 ok = ok && exercise_export_public_key( slot );
783
Gilles Peskine02b75072018-07-01 22:31:34 +0200784 return( ok );
785}
786
Gilles Peskinee59236f2018-01-27 23:32:46 +0100787/* END_HEADER */
788
789/* BEGIN_DEPENDENCIES
790 * depends_on:MBEDTLS_PSA_CRYPTO_C
791 * END_DEPENDENCIES
792 */
793
794/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200795void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100796{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100798 int i;
799 for( i = 0; i <= 1; i++ )
800 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 status = psa_crypto_init( );
802 TEST_ASSERT( status == PSA_SUCCESS );
803 status = psa_crypto_init( );
804 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100805 mbedtls_psa_crypto_free( );
806 }
807}
808/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809
810/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200811void fill_slots( int max_arg )
812{
813 /* Fill all the slots until we run out of memory or out of slots,
814 * or until some limit specified in the test data for the sake of
815 * implementations with an essentially unlimited number of slots.
816 * This test assumes that available slots are numbered from 1. */
817
818 psa_key_slot_t slot;
819 psa_key_slot_t max = 0;
820 psa_key_policy_t policy;
821 uint8_t exported[sizeof( max )];
822 size_t exported_size;
823 psa_status_t status;
824
825 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
826
827 psa_key_policy_init( &policy );
828 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
829
830 for( max = 1; max <= (size_t) max_arg; max++ )
831 {
832 status = psa_set_key_policy( max, &policy );
833 /* Stop filling slots if we run out of memory or out of
834 * available slots. */
835 TEST_ASSERT( status == PSA_SUCCESS ||
836 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
837 status == PSA_ERROR_INVALID_ARGUMENT );
838 if( status != PSA_SUCCESS )
839 break;
840 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
841 (uint8_t*) &max, sizeof( max ) );
842 /* Since psa_set_key_policy succeeded, we know that the slot
843 * number is valid. But we may legitimately run out of memory. */
844 TEST_ASSERT( status == PSA_SUCCESS ||
845 status == PSA_ERROR_INSUFFICIENT_MEMORY );
846 if( status != PSA_SUCCESS )
847 break;
848 }
849 /* `max` is now the first slot number that wasn't filled. */
850 max -= 1;
851
852 for( slot = 1; slot <= max; slot++ )
853 {
854 TEST_ASSERT( psa_export_key( slot,
855 exported, sizeof( exported ),
856 &exported_size ) == PSA_SUCCESS );
857 TEST_ASSERT( exported_size == sizeof( slot ) );
858 TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
Gilles Peskine996deb12018-08-01 15:45:45 +0200859 }
860
861exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200862 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200863 mbedtls_psa_crypto_free( );
864}
865/* END_CASE */
866
867/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200868void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869{
870 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300875 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
877
Gilles Peskine4abf7412018-06-18 16:35:34 +0200878 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200879 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100880 if( status == PSA_SUCCESS )
881 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
882
883exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884 mbedtls_psa_crypto_free( );
885}
886/* END_CASE */
887
888/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200889void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
890{
891 int slot = 1;
892 size_t bits = bits_arg;
893 psa_status_t expected_status = expected_status_arg;
894 psa_status_t status;
895 psa_key_type_t type =
896 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
897 size_t buffer_size = /* Slight overapproximations */
898 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
899 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
900 unsigned char *p;
901 int ret;
902 size_t length;
903
904 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
905 TEST_ASSERT( buffer != NULL );
906
907 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
908 bits, keypair ) ) >= 0 );
909 length = ret;
910
911 /* Try importing the key */
912 status = psa_import_key( slot, type, p, length );
913 TEST_ASSERT( status == expected_status );
914 if( status == PSA_SUCCESS )
915 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
916
917exit:
918 mbedtls_free( buffer );
919 mbedtls_psa_crypto_free( );
920}
921/* END_CASE */
922
923/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300924void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300925 int type_arg,
926 int alg_arg,
927 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928 int expected_bits,
929 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200930 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100931 int canonical_input )
932{
933 int slot = 1;
934 int slot2 = slot + 1;
935 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200936 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200937 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 unsigned char *exported = NULL;
940 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100942 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943 size_t reexported_length;
944 psa_key_type_t got_type;
945 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200946 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300949 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300950 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951 exported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100952 TEST_ASSERT( export_size == 0 || exported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 if( ! canonical_input )
954 {
955 reexported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100956 TEST_ASSERT( export_size == 0 || reexported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957 }
958 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
959
mohammad1603a97cb8c2018-03-28 03:46:26 -0700960 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200961 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700962 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
963
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964 /* Import the key */
965 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200966 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100967
968 /* Test the key information */
969 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200970 &got_type,
971 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972 TEST_ASSERT( got_type == type );
973 TEST_ASSERT( got_bits == (size_t) expected_bits );
974
975 /* Export the key */
976 status = psa_export_key( slot,
977 exported, export_size,
978 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200979 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100980
981 /* The exported length must be set by psa_export_key() to a value between 0
982 * and export_size. On errors, the exported length must be 0. */
983 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
984 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
985 TEST_ASSERT( exported_length <= export_size );
986
Gilles Peskine3f669c32018-06-21 09:21:51 +0200987 TEST_ASSERT( mem_is_zero( exported + exported_length,
988 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200990 {
991 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200993 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994
Gilles Peskine8f609232018-08-11 01:24:55 +0200995 if( ! exercise_export_key( slot, usage_arg ) )
996 goto exit;
997
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 if( canonical_input )
999 {
Gilles Peskine4abf7412018-06-18 16:35:34 +02001000 TEST_ASSERT( exported_length == data->len );
1001 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 }
1003 else
1004 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001005 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1006
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001008 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001009 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001011 reexported,
1012 export_size,
1013 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 TEST_ASSERT( reexported_length == exported_length );
1015 TEST_ASSERT( memcmp( reexported, exported,
1016 exported_length ) == 0 );
1017 }
1018
1019destroy:
1020 /* Destroy the key */
1021 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1022 TEST_ASSERT( psa_get_key_information(
1023 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1024
1025exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001026 mbedtls_free( exported );
1027 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001028 mbedtls_psa_crypto_free( );
1029}
1030/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001031
Moran Pekerf709f4a2018-06-06 17:26:04 +03001032/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001033void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001034 int type_arg,
1035 int alg_arg,
1036 int expected_bits,
1037 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001038 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001039{
1040 int slot = 1;
1041 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001042 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001043 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001044 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001046 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001047 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001048 psa_key_type_t got_type;
1049 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001050 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001053 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001054 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001055 exported = mbedtls_calloc( 1, export_size );
1056 TEST_ASSERT( exported != NULL );
1057
1058 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1059
1060 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001061 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001062 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1063
1064 /* Import the key */
1065 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001066 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001067
1068 /* Test the key information */
1069 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001070 &got_type,
1071 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001072 TEST_ASSERT( got_type == type );
1073 TEST_ASSERT( got_bits == (size_t) expected_bits );
1074
1075 /* Export the key */
1076 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001077 exported, export_size,
1078 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001079 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001080 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
1081 TEST_ASSERT( mem_is_zero( exported + exported_length,
1082 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001083 if( status != PSA_SUCCESS )
1084 goto destroy;
1085
Moran Pekerf709f4a2018-06-06 17:26:04 +03001086destroy:
1087 /* Destroy the key */
1088 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1089 TEST_ASSERT( psa_get_key_information(
1090 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1091
1092exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001093 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001094 mbedtls_psa_crypto_free( );
1095}
1096/* END_CASE */
1097
Gilles Peskine20035e32018-02-03 22:44:14 +01001098/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001099void import_and_exercise_key( data_t *data,
1100 int type_arg,
1101 int bits_arg,
1102 int alg_arg )
1103{
1104 int slot = 1;
1105 psa_key_type_t type = type_arg;
1106 size_t bits = bits_arg;
1107 psa_algorithm_t alg = alg_arg;
1108 psa_key_usage_t usage =
1109 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1110 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1111 PSA_KEY_USAGE_VERIFY :
1112 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1113 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1114 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1115 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1116 PSA_KEY_USAGE_ENCRYPT :
1117 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001118 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001119 0 );
1120 psa_key_policy_t policy;
1121 psa_key_type_t got_type;
1122 size_t got_bits;
1123 psa_status_t status;
1124
1125 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1126
1127 psa_key_policy_init( &policy );
1128 psa_key_policy_set_usage( &policy, usage, alg );
1129 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1130
1131 /* Import the key */
1132 status = psa_import_key( slot, type, data->x, data->len );
1133 TEST_ASSERT( status == PSA_SUCCESS );
1134
1135 /* Test the key information */
1136 TEST_ASSERT( psa_get_key_information( slot,
1137 &got_type,
1138 &got_bits ) == PSA_SUCCESS );
1139 TEST_ASSERT( got_type == type );
1140 TEST_ASSERT( got_bits == bits );
1141
1142 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001143 if( ! exercise_key( slot, usage, alg ) )
1144 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001145
1146exit:
1147 psa_destroy_key( slot );
1148 mbedtls_psa_crypto_free( );
1149}
1150/* END_CASE */
1151
1152/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001153void key_policy( int usage_arg, int alg_arg )
1154{
1155 int key_slot = 1;
1156 psa_algorithm_t alg = alg_arg;
1157 psa_key_usage_t usage = usage_arg;
1158 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1159 unsigned char key[32] = {0};
1160 psa_key_policy_t policy_set;
1161 psa_key_policy_t policy_get;
1162
1163 memset( key, 0x2a, sizeof( key ) );
1164
1165 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1166
1167 psa_key_policy_init( &policy_set );
1168 psa_key_policy_init( &policy_get );
1169
1170 psa_key_policy_set_usage( &policy_set, usage, alg );
1171
1172 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1173 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1174 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1175
1176 TEST_ASSERT( psa_import_key( key_slot, key_type,
1177 key, sizeof( key ) ) == PSA_SUCCESS );
1178
1179 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1180
1181 TEST_ASSERT( policy_get.usage == policy_set.usage );
1182 TEST_ASSERT( policy_get.alg == policy_set.alg );
1183
1184exit:
1185 psa_destroy_key( key_slot );
1186 mbedtls_psa_crypto_free( );
1187}
1188/* END_CASE */
1189
1190/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191void mac_key_policy( int policy_usage,
1192 int policy_alg,
1193 int key_type,
1194 data_t *key_data,
1195 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001196{
1197 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001198 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001199 psa_mac_operation_t operation;
1200 psa_status_t status;
1201 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001202
1203 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1204
1205 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001206 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001207 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1208
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 TEST_ASSERT( psa_import_key( key_slot, key_type,
1210 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001211
Gilles Peskine89167cb2018-07-08 20:12:23 +02001212 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213 if( policy_alg == exercise_alg &&
1214 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1215 TEST_ASSERT( status == PSA_SUCCESS );
1216 else
1217 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1218 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001219
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001221 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222 if( policy_alg == exercise_alg &&
1223 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001224 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 else
1226 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1227
1228exit:
1229 psa_mac_abort( &operation );
1230 psa_destroy_key( key_slot );
1231 mbedtls_psa_crypto_free( );
1232}
1233/* END_CASE */
1234
1235/* BEGIN_CASE */
1236void cipher_key_policy( int policy_usage,
1237 int policy_alg,
1238 int key_type,
1239 data_t *key_data,
1240 int exercise_alg )
1241{
1242 int key_slot = 1;
1243 psa_key_policy_t policy;
1244 psa_cipher_operation_t operation;
1245 psa_status_t status;
1246
1247 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1248
1249 psa_key_policy_init( &policy );
1250 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1251 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1252
1253 TEST_ASSERT( psa_import_key( key_slot, key_type,
1254 key_data->x, key_data->len ) == PSA_SUCCESS );
1255
Gilles Peskinefe119512018-07-08 21:39:34 +02001256 status = psa_cipher_encrypt_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_ENCRYPT ) != 0 )
1259 TEST_ASSERT( status == PSA_SUCCESS );
1260 else
1261 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1262 psa_cipher_abort( &operation );
1263
Gilles Peskinefe119512018-07-08 21:39:34 +02001264 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001265 if( policy_alg == exercise_alg &&
1266 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1267 TEST_ASSERT( status == PSA_SUCCESS );
1268 else
1269 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1270
1271exit:
1272 psa_cipher_abort( &operation );
1273 psa_destroy_key( key_slot );
1274 mbedtls_psa_crypto_free( );
1275}
1276/* END_CASE */
1277
1278/* BEGIN_CASE */
1279void aead_key_policy( int policy_usage,
1280 int policy_alg,
1281 int key_type,
1282 data_t *key_data,
1283 int nonce_length_arg,
1284 int tag_length_arg,
1285 int exercise_alg )
1286{
1287 int key_slot = 1;
1288 psa_key_policy_t policy;
1289 psa_status_t status;
1290 unsigned char nonce[16] = {0};
1291 size_t nonce_length = nonce_length_arg;
1292 unsigned char tag[16];
1293 size_t tag_length = tag_length_arg;
1294 size_t output_length;
1295
1296 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1297 TEST_ASSERT( tag_length <= sizeof( tag ) );
1298
1299 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1300
1301 psa_key_policy_init( &policy );
1302 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1303 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1304
1305 TEST_ASSERT( psa_import_key( key_slot, key_type,
1306 key_data->x, key_data->len ) == PSA_SUCCESS );
1307
1308 status = psa_aead_encrypt( key_slot, exercise_alg,
1309 nonce, nonce_length,
1310 NULL, 0,
1311 NULL, 0,
1312 tag, tag_length,
1313 &output_length );
1314 if( policy_alg == exercise_alg &&
1315 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1316 TEST_ASSERT( status == PSA_SUCCESS );
1317 else
1318 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1319
1320 memset( tag, 0, sizeof( tag ) );
1321 status = psa_aead_decrypt( key_slot, exercise_alg,
1322 nonce, nonce_length,
1323 NULL, 0,
1324 tag, tag_length,
1325 NULL, 0,
1326 &output_length );
1327 if( policy_alg == exercise_alg &&
1328 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1329 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1330 else
1331 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1332
1333exit:
1334 psa_destroy_key( key_slot );
1335 mbedtls_psa_crypto_free( );
1336}
1337/* END_CASE */
1338
1339/* BEGIN_CASE */
1340void asymmetric_encryption_key_policy( int policy_usage,
1341 int policy_alg,
1342 int key_type,
1343 data_t *key_data,
1344 int exercise_alg )
1345{
1346 int key_slot = 1;
1347 psa_key_policy_t policy;
1348 psa_status_t status;
1349 size_t key_bits;
1350 size_t buffer_length;
1351 unsigned char *buffer = NULL;
1352 size_t output_length;
1353
1354 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1355
1356 psa_key_policy_init( &policy );
1357 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1358 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1359
1360 TEST_ASSERT( psa_import_key( key_slot, key_type,
1361 key_data->x, key_data->len ) == PSA_SUCCESS );
1362
1363 TEST_ASSERT( psa_get_key_information( key_slot,
1364 NULL,
1365 &key_bits ) == PSA_SUCCESS );
1366 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1367 exercise_alg );
1368 buffer = mbedtls_calloc( 1, buffer_length );
1369 TEST_ASSERT( buffer != NULL );
1370
1371 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1372 NULL, 0,
1373 NULL, 0,
1374 buffer, buffer_length,
1375 &output_length );
1376 if( policy_alg == exercise_alg &&
1377 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1378 TEST_ASSERT( status == PSA_SUCCESS );
1379 else
1380 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1381
1382 memset( buffer, 0, buffer_length );
1383 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1384 buffer, buffer_length,
1385 NULL, 0,
1386 buffer, buffer_length,
1387 &output_length );
1388 if( policy_alg == exercise_alg &&
1389 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1390 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1391 else
1392 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1393
1394exit:
1395 psa_destroy_key( key_slot );
1396 mbedtls_psa_crypto_free( );
1397 mbedtls_free( buffer );
1398}
1399/* END_CASE */
1400
1401/* BEGIN_CASE */
1402void asymmetric_signature_key_policy( int policy_usage,
1403 int policy_alg,
1404 int key_type,
1405 data_t *key_data,
1406 int exercise_alg )
1407{
1408 int key_slot = 1;
1409 psa_key_policy_t policy;
1410 psa_status_t status;
1411 unsigned char payload[16] = {1};
1412 size_t payload_length = sizeof( payload );
1413 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1414 size_t signature_length;
1415
1416 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1417
1418 psa_key_policy_init( &policy );
1419 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1420 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1421
1422 TEST_ASSERT( psa_import_key( key_slot, key_type,
1423 key_data->x, key_data->len ) == PSA_SUCCESS );
1424
1425 status = psa_asymmetric_sign( key_slot, exercise_alg,
1426 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001427 signature, sizeof( signature ),
1428 &signature_length );
1429 if( policy_alg == exercise_alg &&
1430 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1431 TEST_ASSERT( status == PSA_SUCCESS );
1432 else
1433 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1434
1435 memset( signature, 0, sizeof( signature ) );
1436 status = psa_asymmetric_verify( key_slot, exercise_alg,
1437 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438 signature, sizeof( signature ) );
1439 if( policy_alg == exercise_alg &&
1440 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1441 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1442 else
1443 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001444
1445exit:
1446 psa_destroy_key( key_slot );
1447 mbedtls_psa_crypto_free( );
1448}
1449/* END_CASE */
1450
1451/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001452void derive_key_policy( int policy_usage,
1453 int policy_alg,
1454 int key_type,
1455 data_t *key_data,
1456 int exercise_alg )
1457{
1458 int key_slot = 1;
1459 psa_key_policy_t policy;
1460 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1461 psa_status_t status;
1462
1463 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1464
1465 psa_key_policy_init( &policy );
1466 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1467 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1468
1469 TEST_ASSERT( psa_import_key( key_slot, key_type,
1470 key_data->x, key_data->len ) == PSA_SUCCESS );
1471
1472 status = psa_key_derivation( &generator, key_slot,
1473 exercise_alg,
1474 NULL, 0,
1475 NULL, 0,
1476 1 );
1477 if( policy_alg == exercise_alg &&
1478 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1479 TEST_ASSERT( status == PSA_SUCCESS );
1480 else
1481 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1482
1483exit:
1484 psa_generator_abort( &generator );
1485 psa_destroy_key( key_slot );
1486 mbedtls_psa_crypto_free( );
1487}
1488/* END_CASE */
1489
1490/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001491void key_lifetime( int lifetime_arg )
1492{
1493 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001494 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001495 unsigned char key[32] = {0};
1496 psa_key_lifetime_t lifetime_set = lifetime_arg;
1497 psa_key_lifetime_t lifetime_get;
1498
1499 memset( key, 0x2a, sizeof( key ) );
1500
1501 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1502
1503 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1504 lifetime_set ) == PSA_SUCCESS );
1505
1506 TEST_ASSERT( psa_import_key( key_slot, key_type,
1507 key, sizeof( key ) ) == PSA_SUCCESS );
1508
1509 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1510 &lifetime_get ) == PSA_SUCCESS );
1511
1512 TEST_ASSERT( lifetime_get == lifetime_set );
1513
1514exit:
1515 psa_destroy_key( key_slot );
1516 mbedtls_psa_crypto_free( );
1517}
1518/* END_CASE */
1519
1520/* BEGIN_CASE */
1521void key_lifetime_set_fail( int key_slot_arg,
1522 int lifetime_arg,
1523 int expected_status_arg )
1524{
1525 psa_key_slot_t key_slot = key_slot_arg;
1526 psa_key_lifetime_t lifetime_set = lifetime_arg;
1527 psa_status_t actual_status;
1528 psa_status_t expected_status = expected_status_arg;
1529
1530 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1531
1532 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1533
1534 if( actual_status == PSA_SUCCESS )
1535 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1536
1537 TEST_ASSERT( expected_status == actual_status );
1538
1539exit:
1540 psa_destroy_key( key_slot );
1541 mbedtls_psa_crypto_free( );
1542}
1543/* END_CASE */
1544
1545/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001546void hash_setup( int alg_arg,
1547 int expected_status_arg )
1548{
1549 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001550 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001551 psa_hash_operation_t operation;
1552 psa_status_t status;
1553
1554 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1555
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001556 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001557 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001558 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001559
1560exit:
1561 mbedtls_psa_crypto_free( );
1562}
1563/* END_CASE */
1564
1565/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001566void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001567{
1568 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001569 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001570 size_t actual_hash_length;
1571 psa_hash_operation_t operation;
1572
Gilles Peskine69c12672018-06-28 00:07:19 +02001573 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1574 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1575
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001576 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001577 TEST_ASSERT( expected_hash != NULL );
1578 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1579 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001580
1581 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1582
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001583 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001584 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001585 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586 TEST_ASSERT( psa_hash_finish( &operation,
1587 actual_hash, sizeof( actual_hash ),
1588 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001589 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001590 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001591 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001592
1593exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001594 mbedtls_psa_crypto_free( );
1595}
1596/* END_CASE */
1597
1598/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001599void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001600{
1601 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001602 psa_hash_operation_t operation;
1603
Gilles Peskine69c12672018-06-28 00:07:19 +02001604 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1605 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1606
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001607 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001608 TEST_ASSERT( expected_hash != NULL );
1609 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1610 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001611
1612 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1613
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001614 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001615 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001616 input->x,
1617 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001618 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001619 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001620 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001621
1622exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001623 mbedtls_psa_crypto_free( );
1624}
1625/* END_CASE */
1626
1627/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001628void mac_setup( int key_type_arg,
1629 data_t *key,
1630 int alg_arg,
1631 int expected_status_arg )
1632{
1633 int key_slot = 1;
1634 psa_key_type_t key_type = key_type_arg;
1635 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001636 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001637 psa_mac_operation_t operation;
1638 psa_key_policy_t policy;
1639 psa_status_t status;
1640
1641 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1642
1643 psa_key_policy_init( &policy );
1644 psa_key_policy_set_usage( &policy,
1645 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1646 alg );
1647 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1648
1649 TEST_ASSERT( psa_import_key( key_slot, key_type,
1650 key->x, key->len ) == PSA_SUCCESS );
1651
Gilles Peskine89167cb2018-07-08 20:12:23 +02001652 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001653 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001654 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001655
1656exit:
1657 psa_destroy_key( key_slot );
1658 mbedtls_psa_crypto_free( );
1659}
1660/* END_CASE */
1661
1662/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001663void mac_verify( int key_type_arg,
1664 data_t *key,
1665 int alg_arg,
1666 data_t *input,
1667 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668{
1669 int key_slot = 1;
1670 psa_key_type_t key_type = key_type_arg;
1671 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001673 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001674
Gilles Peskine69c12672018-06-28 00:07:19 +02001675 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1676
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001680 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001681 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1682 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683
1684 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1685
mohammad16036df908f2018-04-02 08:34:15 -07001686 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001687 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001688 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1689
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001691 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001692
Gilles Peskine89167cb2018-07-08 20:12:23 +02001693 TEST_ASSERT( psa_mac_verify_setup( &operation,
1694 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1696 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001697 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001698 TEST_ASSERT( psa_mac_verify_finish( &operation,
1699 expected_mac->x,
1700 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001701
1702exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001703 psa_destroy_key( key_slot );
1704 mbedtls_psa_crypto_free( );
1705}
1706/* END_CASE */
1707
1708/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001709void cipher_setup( int key_type_arg,
1710 data_t *key,
1711 int alg_arg,
1712 int expected_status_arg )
1713{
1714 int key_slot = 1;
1715 psa_key_type_t key_type = key_type_arg;
1716 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001717 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001718 psa_cipher_operation_t operation;
1719 psa_key_policy_t policy;
1720 psa_status_t status;
1721
1722 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1723
1724 psa_key_policy_init( &policy );
1725 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1726 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1727
1728 TEST_ASSERT( psa_import_key( key_slot, key_type,
1729 key->x, key->len ) == PSA_SUCCESS );
1730
Gilles Peskinefe119512018-07-08 21:39:34 +02001731 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001732 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001733 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001734
1735exit:
1736 psa_destroy_key( key_slot );
1737 mbedtls_psa_crypto_free( );
1738}
1739/* END_CASE */
1740
1741/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001742void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001743 data_t *key,
1744 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001745 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001746{
1747 int key_slot = 1;
1748 psa_status_t status;
1749 psa_key_type_t key_type = key_type_arg;
1750 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001751 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001752 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001753 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001754 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001755 size_t output_buffer_size = 0;
1756 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001757 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001758 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001759 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001760
Gilles Peskine50e586b2018-06-08 14:28:46 +02001761 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001762 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001763 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001764 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1765 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1766 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001767
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001768 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1769 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001770
1771 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1772
Moran Pekered346952018-07-05 15:22:45 +03001773 psa_key_policy_init( &policy );
1774 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1775 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1776
Gilles Peskine50e586b2018-06-08 14:28:46 +02001777 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001778 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001779
Gilles Peskinefe119512018-07-08 21:39:34 +02001780 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1781 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001782
Gilles Peskinefe119512018-07-08 21:39:34 +02001783 TEST_ASSERT( psa_cipher_set_iv( &operation,
1784 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001785 output_buffer_size = (size_t) input->len +
1786 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001787 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001788 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001789
Gilles Peskine4abf7412018-06-18 16:35:34 +02001790 TEST_ASSERT( psa_cipher_update( &operation,
1791 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001792 output, output_buffer_size,
1793 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001794 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001795 status = psa_cipher_finish( &operation,
1796 output + function_output_length,
1797 output_buffer_size,
1798 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001799 total_output_length += function_output_length;
1800
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001801 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001802 if( expected_status == PSA_SUCCESS )
1803 {
1804 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001805 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001806 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001807 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001808 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001809
Gilles Peskine50e586b2018-06-08 14:28:46 +02001810exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001811 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001812 psa_destroy_key( key_slot );
1813 mbedtls_psa_crypto_free( );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
1818void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001819 data_t *key,
1820 data_t *input,
1821 int first_part_size,
1822 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001823{
1824 int key_slot = 1;
1825 psa_key_type_t key_type = key_type_arg;
1826 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001827 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001828 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001829 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001830 size_t output_buffer_size = 0;
1831 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001832 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001833 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001834 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001835
Gilles Peskine50e586b2018-06-08 14:28:46 +02001836 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001837 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001838 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001839 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1840 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1841 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001842
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001843 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1844 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001845
1846 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1847
Moran Pekered346952018-07-05 15:22:45 +03001848 psa_key_policy_init( &policy );
1849 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1850 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1851
Gilles Peskine50e586b2018-06-08 14:28:46 +02001852 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001853 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001854
Gilles Peskinefe119512018-07-08 21:39:34 +02001855 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1856 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001857
Gilles Peskinefe119512018-07-08 21:39:34 +02001858 TEST_ASSERT( psa_cipher_set_iv( &operation,
1859 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001860 output_buffer_size = (size_t) input->len +
1861 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001862 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001863 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001864
Gilles Peskine4abf7412018-06-18 16:35:34 +02001865 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001866 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001867 output, output_buffer_size,
1868 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001869 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001870 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001871 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001872 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001873 output, output_buffer_size,
1874 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001875 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001876 TEST_ASSERT( psa_cipher_finish( &operation,
1877 output + function_output_length,
1878 output_buffer_size,
1879 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001880 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001881 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1882
Gilles Peskine4abf7412018-06-18 16:35:34 +02001883 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001884 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001885 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001886
1887exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001888 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001889 psa_destroy_key( key_slot );
1890 mbedtls_psa_crypto_free( );
1891}
1892/* END_CASE */
1893
1894/* BEGIN_CASE */
1895void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001896 data_t *key,
1897 data_t *input,
1898 int first_part_size,
1899 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001900{
1901 int key_slot = 1;
1902
1903 psa_key_type_t key_type = key_type_arg;
1904 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001905 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001906 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001907 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001908 size_t output_buffer_size = 0;
1909 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001910 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001911 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001912 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001913
Gilles Peskine50e586b2018-06-08 14:28:46 +02001914 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001915 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001916 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001917 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1918 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1919 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001920
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001921 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1922 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001923
1924 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1925
Moran Pekered346952018-07-05 15:22:45 +03001926 psa_key_policy_init( &policy );
1927 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1928 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1929
Gilles Peskine50e586b2018-06-08 14:28:46 +02001930 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001931 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001932
Gilles Peskinefe119512018-07-08 21:39:34 +02001933 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1934 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001935
Gilles Peskinefe119512018-07-08 21:39:34 +02001936 TEST_ASSERT( psa_cipher_set_iv( &operation,
1937 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001938
mohammad16033d91abe2018-07-03 13:15:54 +03001939 output_buffer_size = (size_t) input->len +
1940 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001941 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001942 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001943
Gilles Peskine4abf7412018-06-18 16:35:34 +02001944 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1945 TEST_ASSERT( psa_cipher_update( &operation,
1946 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001947 output, output_buffer_size,
1948 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001949 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001950 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001951 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001952 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001953 output, output_buffer_size,
1954 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001955 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001956 TEST_ASSERT( psa_cipher_finish( &operation,
1957 output + function_output_length,
1958 output_buffer_size,
1959 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001960 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001961 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1962
Gilles Peskine4abf7412018-06-18 16:35:34 +02001963 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001964 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001965 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001966
1967exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001968 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001969 psa_destroy_key( key_slot );
1970 mbedtls_psa_crypto_free( );
1971}
1972/* END_CASE */
1973
Gilles Peskine50e586b2018-06-08 14:28:46 +02001974/* BEGIN_CASE */
1975void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001976 data_t *key,
1977 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001978 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001979{
1980 int key_slot = 1;
1981 psa_status_t status;
1982 psa_key_type_t key_type = key_type_arg;
1983 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001984 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001985 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001986 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001987 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001988 size_t output_buffer_size = 0;
1989 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001990 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001991 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001992 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001993
Gilles Peskine50e586b2018-06-08 14:28:46 +02001994 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001995 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001996 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001997 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1998 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1999 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002000
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002001 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2002 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002003
2004 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2005
Moran Pekered346952018-07-05 15:22:45 +03002006 psa_key_policy_init( &policy );
2007 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2008 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2009
Gilles Peskine50e586b2018-06-08 14:28:46 +02002010 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002011 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002012
Gilles Peskinefe119512018-07-08 21:39:34 +02002013 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2014 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002015
Gilles Peskinefe119512018-07-08 21:39:34 +02002016 TEST_ASSERT( psa_cipher_set_iv( &operation,
2017 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002018
mohammad16033d91abe2018-07-03 13:15:54 +03002019 output_buffer_size = (size_t) input->len +
2020 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002021 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002022 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002023
Gilles Peskine4abf7412018-06-18 16:35:34 +02002024 TEST_ASSERT( psa_cipher_update( &operation,
2025 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002026 output, output_buffer_size,
2027 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002028 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002029 status = psa_cipher_finish( &operation,
2030 output + function_output_length,
2031 output_buffer_size,
2032 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002033 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002034 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002035
2036 if( expected_status == PSA_SUCCESS )
2037 {
2038 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002039 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002040 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002041 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002042 }
2043
Gilles Peskine50e586b2018-06-08 14:28:46 +02002044exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002045 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002046 psa_destroy_key( key_slot );
2047 mbedtls_psa_crypto_free( );
2048}
2049/* END_CASE */
2050
Gilles Peskine50e586b2018-06-08 14:28:46 +02002051/* BEGIN_CASE */
2052void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002053 data_t *key,
2054 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002055{
2056 int key_slot = 1;
2057 psa_key_type_t key_type = key_type_arg;
2058 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002059 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002060 size_t iv_size = 16;
2061 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002062 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002063 size_t output1_size = 0;
2064 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002065 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002066 size_t output2_size = 0;
2067 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002068 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002069 psa_cipher_operation_t operation1;
2070 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002071 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002072
mohammad1603d7d7ba52018-03-12 18:51:53 +02002073 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002074 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002075 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2076 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002077
mohammad1603d7d7ba52018-03-12 18:51:53 +02002078 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2079
Moran Pekered346952018-07-05 15:22:45 +03002080 psa_key_policy_init( &policy );
2081 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2082 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2083
mohammad1603d7d7ba52018-03-12 18:51:53 +02002084 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002085 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002086
Gilles Peskinefe119512018-07-08 21:39:34 +02002087 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2088 key_slot, alg ) == PSA_SUCCESS );
2089 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2090 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002091
Gilles Peskinefe119512018-07-08 21:39:34 +02002092 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2093 iv, iv_size,
2094 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002095 output1_size = (size_t) input->len +
2096 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002097 output1 = mbedtls_calloc( 1, output1_size );
2098 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002099
Gilles Peskine4abf7412018-06-18 16:35:34 +02002100 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002101 output1, output1_size,
2102 &output1_length ) == PSA_SUCCESS );
2103 TEST_ASSERT( psa_cipher_finish( &operation1,
2104 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002105 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002106
Gilles Peskine048b7f02018-06-08 14:20:49 +02002107 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002108
2109 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2110
2111 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002112 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002113 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002114
Gilles Peskinefe119512018-07-08 21:39:34 +02002115 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2116 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002117 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2118 output2, output2_size,
2119 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002120 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002121 TEST_ASSERT( psa_cipher_finish( &operation2,
2122 output2 + output2_length,
2123 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002124 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002125
Gilles Peskine048b7f02018-06-08 14:20:49 +02002126 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002127
Janos Follath25c4fa82018-07-06 16:23:25 +01002128 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002129
Gilles Peskine4abf7412018-06-18 16:35:34 +02002130 TEST_ASSERT( input->len == output2_length );
2131 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03002132
2133exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002134 mbedtls_free( output1 );
2135 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002136 psa_destroy_key( key_slot );
2137 mbedtls_psa_crypto_free( );
2138}
2139/* END_CASE */
2140
2141/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002142void cipher_verify_output_multipart( int alg_arg,
2143 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002144 data_t *key,
2145 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002147{
2148 int key_slot = 1;
2149 psa_key_type_t key_type = key_type_arg;
2150 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002151 unsigned char iv[16] = {0};
2152 size_t iv_size = 16;
2153 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002154 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002155 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002156 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002157 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002158 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002159 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002160 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002161 psa_cipher_operation_t operation1;
2162 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002163 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002164
Moran Pekerded84402018-06-06 16:36:50 +03002165 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002166 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002167 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2168 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002169
Moran Pekerded84402018-06-06 16:36:50 +03002170 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2171
Moran Pekered346952018-07-05 15:22:45 +03002172 psa_key_policy_init( &policy );
2173 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2174 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2175
Moran Pekerded84402018-06-06 16:36:50 +03002176 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002177 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002178
Gilles Peskinefe119512018-07-08 21:39:34 +02002179 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2180 key_slot, alg ) == PSA_SUCCESS );
2181 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2182 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002183
Gilles Peskinefe119512018-07-08 21:39:34 +02002184 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2185 iv, iv_size,
2186 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002187 output1_buffer_size = (size_t) input->len +
2188 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002189 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002190 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002191
Gilles Peskine4abf7412018-06-18 16:35:34 +02002192 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002193
itayzafrir3e02b3b2018-06-12 17:06:52 +03002194 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002195 output1, output1_buffer_size,
2196 &function_output_length ) == PSA_SUCCESS );
2197 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002198
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002199 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002200 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002201 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002202 output1, output1_buffer_size,
2203 &function_output_length ) == PSA_SUCCESS );
2204 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002205
Gilles Peskine048b7f02018-06-08 14:20:49 +02002206 TEST_ASSERT( psa_cipher_finish( &operation1,
2207 output1 + output1_length,
2208 output1_buffer_size - output1_length,
2209 &function_output_length ) == PSA_SUCCESS );
2210 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002211
2212 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2213
Gilles Peskine048b7f02018-06-08 14:20:49 +02002214 output2_buffer_size = output1_length;
2215 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002216 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002217
Gilles Peskinefe119512018-07-08 21:39:34 +02002218 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2219 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002220
2221 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002222 output2, output2_buffer_size,
2223 &function_output_length ) == PSA_SUCCESS );
2224 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002225
Gilles Peskine048b7f02018-06-08 14:20:49 +02002226 TEST_ASSERT( psa_cipher_update( &operation2,
2227 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002228 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002229 output2, output2_buffer_size,
2230 &function_output_length ) == PSA_SUCCESS );
2231 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002232
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002233 TEST_ASSERT( psa_cipher_finish( &operation2,
2234 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002235 output2_buffer_size - output2_length,
2236 &function_output_length ) == PSA_SUCCESS );
2237 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002238
Janos Follath25c4fa82018-07-06 16:23:25 +01002239 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002240
Gilles Peskine4abf7412018-06-18 16:35:34 +02002241 TEST_ASSERT( input->len == output2_length );
2242 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002243
2244exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002245 mbedtls_free( output1 );
2246 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002247 psa_destroy_key( key_slot );
2248 mbedtls_psa_crypto_free( );
2249}
2250/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002251
Gilles Peskine20035e32018-02-03 22:44:14 +01002252/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002253void aead_encrypt_decrypt( int key_type_arg,
2254 data_t * key_data,
2255 int alg_arg,
2256 data_t * input_data,
2257 data_t * nonce,
2258 data_t * additional_data,
2259 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002260{
2261 int slot = 1;
2262 psa_key_type_t key_type = key_type_arg;
2263 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002264 unsigned char *output_data = NULL;
2265 size_t output_size = 0;
2266 size_t output_length = 0;
2267 unsigned char *output_data2 = NULL;
2268 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002269 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002270 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002271 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002272
Gilles Peskinea1cac842018-06-11 19:33:02 +02002273 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002274 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002275 TEST_ASSERT( nonce != NULL );
2276 TEST_ASSERT( additional_data != NULL );
2277 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2278 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2279 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2280 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2281
Gilles Peskine4abf7412018-06-18 16:35:34 +02002282 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002283 output_data = mbedtls_calloc( 1, output_size );
2284 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002285
2286 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2287
2288 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002289 psa_key_policy_set_usage( &policy,
2290 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2291 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002292 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2293
2294 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002295 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002296
2297 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002298 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002299 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002300 additional_data->len,
2301 input_data->x, input_data->len,
2302 output_data, output_size,
2303 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002304
2305 if( PSA_SUCCESS == expected_result )
2306 {
2307 output_data2 = mbedtls_calloc( 1, output_length );
2308 TEST_ASSERT( output_data2 != NULL );
2309
2310 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002311 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002312 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002313 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002314 output_data, output_length,
2315 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002316 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002317
itayzafrir3e02b3b2018-06-12 17:06:52 +03002318 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002319 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002320 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002321
Gilles Peskinea1cac842018-06-11 19:33:02 +02002322exit:
2323 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002324 mbedtls_free( output_data );
2325 mbedtls_free( output_data2 );
2326 mbedtls_psa_crypto_free( );
2327}
2328/* END_CASE */
2329
2330/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002331void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002332 int alg_arg, data_t * input_data,
2333 data_t * additional_data, data_t * nonce,
2334 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002335{
2336 int slot = 1;
2337 psa_key_type_t key_type = key_type_arg;
2338 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002339 unsigned char *output_data = NULL;
2340 size_t output_size = 0;
2341 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002342 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002343 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002344
Gilles Peskinea1cac842018-06-11 19:33:02 +02002345 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002346 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002347 TEST_ASSERT( additional_data != NULL );
2348 TEST_ASSERT( nonce != NULL );
2349 TEST_ASSERT( expected_result != NULL );
2350 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2351 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2352 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2353 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2354 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2355
Gilles Peskine4abf7412018-06-18 16:35:34 +02002356 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002357 output_data = mbedtls_calloc( 1, output_size );
2358 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002359
Gilles Peskinea1cac842018-06-11 19:33:02 +02002360 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2361
2362 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002363 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002364 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2365
2366 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002367 key_data->x,
2368 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002369
2370 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002371 nonce->x, nonce->len,
2372 additional_data->x, additional_data->len,
2373 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002374 output_data, output_size,
2375 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002376
itayzafrir3e02b3b2018-06-12 17:06:52 +03002377 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002378 output_length ) == 0 );
2379
Gilles Peskinea1cac842018-06-11 19:33:02 +02002380exit:
2381 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002382 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002383 mbedtls_psa_crypto_free( );
2384}
2385/* END_CASE */
2386
2387/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002388void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002389 int alg_arg, data_t * input_data,
2390 data_t * additional_data, data_t * nonce,
2391 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002392{
2393 int slot = 1;
2394 psa_key_type_t key_type = key_type_arg;
2395 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002396 unsigned char *output_data = NULL;
2397 size_t output_size = 0;
2398 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002399 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002400 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002401 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002402
Gilles Peskinea1cac842018-06-11 19:33:02 +02002403 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002404 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002405 TEST_ASSERT( additional_data != NULL );
2406 TEST_ASSERT( nonce != NULL );
2407 TEST_ASSERT( expected_data != NULL );
2408 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2409 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2410 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2411 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2412 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2413
Gilles Peskine4abf7412018-06-18 16:35:34 +02002414 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002415 output_data = mbedtls_calloc( 1, output_size );
2416 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002417
Gilles Peskinea1cac842018-06-11 19:33:02 +02002418 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2419
2420 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002421 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002422 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2423
2424 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002425 key_data->x,
2426 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002427
2428 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002429 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002430 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002431 additional_data->len,
2432 input_data->x, input_data->len,
2433 output_data, output_size,
2434 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002435
Gilles Peskine2d277862018-06-18 15:41:12 +02002436 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002437 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002438 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002439 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002440 }
2441
Gilles Peskinea1cac842018-06-11 19:33:02 +02002442exit:
2443 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002444 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002445 mbedtls_psa_crypto_free( );
2446}
2447/* END_CASE */
2448
2449/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002450void signature_size( int type_arg,
2451 int bits,
2452 int alg_arg,
2453 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002454{
2455 psa_key_type_t type = type_arg;
2456 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002457 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002458 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2459exit:
2460 ;
2461}
2462/* END_CASE */
2463
2464/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002465void sign_deterministic( int key_type_arg, data_t *key_data,
2466 int alg_arg, data_t *input_data,
2467 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002468{
2469 int slot = 1;
2470 psa_key_type_t key_type = key_type_arg;
2471 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002472 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002473 unsigned char *signature = NULL;
2474 size_t signature_size;
2475 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002476 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002477
Gilles Peskine20035e32018-02-03 22:44:14 +01002478 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002479 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002480 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002481 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2482 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2483 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002484
2485 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2486
mohammad1603a97cb8c2018-03-28 03:46:26 -07002487 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002488 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002489 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2490
Gilles Peskine20035e32018-02-03 22:44:14 +01002491 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002492 key_data->x,
2493 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002494 TEST_ASSERT( psa_get_key_information( slot,
2495 NULL,
2496 &key_bits ) == PSA_SUCCESS );
2497
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002498 /* Allocate a buffer which has the size advertized by the
2499 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002500 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2501 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002502 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002503 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002504 signature = mbedtls_calloc( 1, signature_size );
2505 TEST_ASSERT( signature != NULL );
2506
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002507 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002508 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002509 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002510 signature, signature_size,
2511 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002512 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002513 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002514 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002515 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002516
2517exit:
2518 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002519 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002520 mbedtls_psa_crypto_free( );
2521}
2522/* END_CASE */
2523
2524/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002525void sign_fail( int key_type_arg, data_t *key_data,
2526 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002527 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002528{
2529 int slot = 1;
2530 psa_key_type_t key_type = key_type_arg;
2531 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002532 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002533 psa_status_t actual_status;
2534 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002535 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002536 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002537 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002538
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002540 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002541 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2542 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2543
Gilles Peskine20035e32018-02-03 22:44:14 +01002544 signature = mbedtls_calloc( 1, signature_size );
2545 TEST_ASSERT( signature != NULL );
2546
2547 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2548
mohammad1603a97cb8c2018-03-28 03:46:26 -07002549 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002550 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002551 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2552
Gilles Peskine20035e32018-02-03 22:44:14 +01002553 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002554 key_data->x,
2555 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002556
2557 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002558 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002559 signature, signature_size,
2560 &signature_length );
2561 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002562 /* The value of *signature_length is unspecified on error, but
2563 * whatever it is, it should be less than signature_size, so that
2564 * if the caller tries to read *signature_length bytes without
2565 * checking the error code then they don't overflow a buffer. */
2566 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002567
2568exit:
2569 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002570 mbedtls_free( signature );
2571 mbedtls_psa_crypto_free( );
2572}
2573/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002574
2575/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002576void sign_verify( int key_type_arg, data_t *key_data,
2577 int alg_arg, data_t *input_data )
2578{
2579 int slot = 1;
2580 psa_key_type_t key_type = key_type_arg;
2581 psa_algorithm_t alg = alg_arg;
2582 size_t key_bits;
2583 unsigned char *signature = NULL;
2584 size_t signature_size;
2585 size_t signature_length = 0xdeadbeef;
2586 psa_key_policy_t policy;
2587
2588 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2589
2590 psa_key_policy_init( &policy );
2591 psa_key_policy_set_usage( &policy,
2592 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2593 alg );
2594 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2595
2596 TEST_ASSERT( psa_import_key( slot, key_type,
2597 key_data->x,
2598 key_data->len ) == PSA_SUCCESS );
2599 TEST_ASSERT( psa_get_key_information( slot,
2600 NULL,
2601 &key_bits ) == PSA_SUCCESS );
2602
2603 /* Allocate a buffer which has the size advertized by the
2604 * library. */
2605 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2606 key_bits, alg );
2607 TEST_ASSERT( signature_size != 0 );
2608 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2609 signature = mbedtls_calloc( 1, signature_size );
2610 TEST_ASSERT( signature != NULL );
2611
2612 /* Perform the signature. */
2613 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2614 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002615 signature, signature_size,
2616 &signature_length ) == PSA_SUCCESS );
2617 /* Check that the signature length looks sensible. */
2618 TEST_ASSERT( signature_length <= signature_size );
2619 TEST_ASSERT( signature_length > 0 );
2620
2621 /* Use the library to verify that the signature is correct. */
2622 TEST_ASSERT( psa_asymmetric_verify(
2623 slot, alg,
2624 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002625 signature, signature_length ) == PSA_SUCCESS );
2626
2627 if( input_data->len != 0 )
2628 {
2629 /* Flip a bit in the input and verify that the signature is now
2630 * detected as invalid. Flip a bit at the beginning, not at the end,
2631 * because ECDSA may ignore the last few bits of the input. */
2632 input_data->x[0] ^= 1;
2633 TEST_ASSERT( psa_asymmetric_verify(
2634 slot, alg,
2635 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002636 signature,
2637 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2638 }
2639
2640exit:
2641 psa_destroy_key( slot );
2642 mbedtls_free( signature );
2643 mbedtls_psa_crypto_free( );
2644}
2645/* END_CASE */
2646
2647/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002648void asymmetric_verify( int key_type_arg, data_t *key_data,
2649 int alg_arg, data_t *hash_data,
2650 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002651{
2652 int slot = 1;
2653 psa_key_type_t key_type = key_type_arg;
2654 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002655 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002656
Gilles Peskine69c12672018-06-28 00:07:19 +02002657 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2658
itayzafrir5c753392018-05-08 11:18:38 +03002659 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002660 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002661 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002662 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2663 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2664 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002665
2666 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2667
2668 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002669 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002670 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2671
2672 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002673 key_data->x,
2674 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002675
2676 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002677 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002678 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002679 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002680exit:
2681 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002682 mbedtls_psa_crypto_free( );
2683}
2684/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002685
2686/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002687void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2688 int alg_arg, data_t *hash_data,
2689 data_t *signature_data,
2690 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002691{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002692 int slot = 1;
2693 psa_key_type_t key_type = key_type_arg;
2694 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002695 psa_status_t actual_status;
2696 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002697 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002698
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002699 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002700 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002701 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002702 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2703 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2704 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002705
2706 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2707
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002708 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002709 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002710 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2711
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002712 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002713 key_data->x,
2714 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002715
2716 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002717 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002718 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002719 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002720
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 TEST_ASSERT( actual_status == expected_status );
2722
2723exit:
2724 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725 mbedtls_psa_crypto_free( );
2726}
2727/* END_CASE */
2728
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002729/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002730void asymmetric_encrypt( int key_type_arg,
2731 data_t *key_data,
2732 int alg_arg,
2733 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002734 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002735 int expected_output_length_arg,
2736 int expected_status_arg )
2737{
2738 int slot = 1;
2739 psa_key_type_t key_type = key_type_arg;
2740 psa_algorithm_t alg = alg_arg;
2741 size_t expected_output_length = expected_output_length_arg;
2742 size_t key_bits;
2743 unsigned char *output = NULL;
2744 size_t output_size;
2745 size_t output_length = ~0;
2746 psa_status_t actual_status;
2747 psa_status_t expected_status = expected_status_arg;
2748 psa_key_policy_t policy;
2749
2750 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2751
2752 /* Import the key */
2753 psa_key_policy_init( &policy );
2754 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2755 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2756 TEST_ASSERT( psa_import_key( slot, key_type,
2757 key_data->x,
2758 key_data->len ) == PSA_SUCCESS );
2759
2760 /* Determine the maximum output length */
2761 TEST_ASSERT( psa_get_key_information( slot,
2762 NULL,
2763 &key_bits ) == PSA_SUCCESS );
2764 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2765 output = mbedtls_calloc( 1, output_size );
Darryl Green9c862252018-07-24 12:52:44 +01002766 TEST_ASSERT( output_size == 0 || output != NULL );
Gilles Peskine656896e2018-06-29 19:12:28 +02002767
2768 /* Encrypt the input */
2769 actual_status = psa_asymmetric_encrypt( slot, alg,
2770 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002771 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002772 output, output_size,
2773 &output_length );
2774 TEST_ASSERT( actual_status == expected_status );
2775 TEST_ASSERT( output_length == expected_output_length );
2776
Gilles Peskine68428122018-06-30 18:42:41 +02002777 /* If the label is empty, the test framework puts a non-null pointer
2778 * in label->x. Test that a null pointer works as well. */
2779 if( label->len == 0 )
2780 {
2781 output_length = ~0;
2782 memset( output, 0, output_size );
2783 actual_status = psa_asymmetric_encrypt( slot, alg,
2784 input_data->x, input_data->len,
2785 NULL, label->len,
2786 output, output_size,
2787 &output_length );
2788 TEST_ASSERT( actual_status == expected_status );
2789 TEST_ASSERT( output_length == expected_output_length );
2790 }
2791
Gilles Peskine656896e2018-06-29 19:12:28 +02002792exit:
2793 psa_destroy_key( slot );
2794 mbedtls_free( output );
2795 mbedtls_psa_crypto_free( );
2796}
2797/* END_CASE */
2798
2799/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002800void asymmetric_encrypt_decrypt( int key_type_arg,
2801 data_t *key_data,
2802 int alg_arg,
2803 data_t *input_data,
2804 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002805{
2806 int slot = 1;
2807 psa_key_type_t key_type = key_type_arg;
2808 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002809 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002810 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002811 size_t output_size;
2812 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002813 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002814 size_t output2_size;
2815 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002816 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002817
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002818 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002819 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002820 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2821 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2822
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002823 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2824
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002825 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002826 psa_key_policy_set_usage( &policy,
2827 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002828 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002829 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2830
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002831 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002832 key_data->x,
2833 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002834
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002835
2836 /* Determine the maximum ciphertext length */
2837 TEST_ASSERT( psa_get_key_information( slot,
2838 NULL,
2839 &key_bits ) == PSA_SUCCESS );
2840 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2841 output = mbedtls_calloc( 1, output_size );
2842 TEST_ASSERT( output != NULL );
2843 output2_size = input_data->len;
2844 output2 = mbedtls_calloc( 1, output2_size );
2845 TEST_ASSERT( output2 != NULL );
2846
Gilles Peskineeebd7382018-06-08 18:11:54 +02002847 /* We test encryption by checking that encrypt-then-decrypt gives back
2848 * the original plaintext because of the non-optional random
2849 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002850 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002851 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002852 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002853 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002854 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002855 /* We don't know what ciphertext length to expect, but check that
2856 * it looks sensible. */
2857 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002858
Gilles Peskine2d277862018-06-18 15:41:12 +02002859 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002860 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002861 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002862 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002863 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002864 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002865 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002866 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002867
2868exit:
2869 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002870 mbedtls_free( output );
2871 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002872 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002873}
2874/* END_CASE */
2875
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002876/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002877void asymmetric_decrypt( int key_type_arg,
2878 data_t *key_data,
2879 int alg_arg,
2880 data_t *input_data,
2881 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002882 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002883{
2884 int slot = 1;
2885 psa_key_type_t key_type = key_type_arg;
2886 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002887 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002888 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002889 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002890 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002891
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002892 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002894 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002895 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2896 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2897 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2898
Gilles Peskine4abf7412018-06-18 16:35:34 +02002899 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002900 output = mbedtls_calloc( 1, output_size );
2901 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002902
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002903 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2904
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002905 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002906 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002907 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2908
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002909 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002910 key_data->x,
2911 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002912
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002913 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002914 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002915 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002916 output,
2917 output_size,
2918 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002919 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002920 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002921
Gilles Peskine68428122018-06-30 18:42:41 +02002922 /* If the label is empty, the test framework puts a non-null pointer
2923 * in label->x. Test that a null pointer works as well. */
2924 if( label->len == 0 )
2925 {
2926 output_length = ~0;
2927 memset( output, 0, output_size );
2928 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2929 input_data->x, input_data->len,
2930 NULL, label->len,
2931 output,
2932 output_size,
2933 &output_length ) == PSA_SUCCESS );
2934 TEST_ASSERT( expected_data->len == output_length );
2935 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2936 }
2937
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002938exit:
2939 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002940 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002941 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002942}
2943/* END_CASE */
2944
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002945/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002946void asymmetric_decrypt_fail( int key_type_arg,
2947 data_t *key_data,
2948 int alg_arg,
2949 data_t *input_data,
2950 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002951 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002952{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002953 int slot = 1;
2954 psa_key_type_t key_type = key_type_arg;
2955 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002956 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002957 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002958 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002959 psa_status_t actual_status;
2960 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002961 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002965 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2966 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2967
Gilles Peskine4abf7412018-06-18 16:35:34 +02002968 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002969 output = mbedtls_calloc( 1, output_size );
2970 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002971
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002972 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2973
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002974 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002975 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002976 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2977
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002979 key_data->x,
2980 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002981
Gilles Peskine2d277862018-06-18 15:41:12 +02002982 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002983 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002984 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002985 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002986 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002987 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002988 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002989
Gilles Peskine68428122018-06-30 18:42:41 +02002990 /* If the label is empty, the test framework puts a non-null pointer
2991 * in label->x. Test that a null pointer works as well. */
2992 if( label->len == 0 )
2993 {
2994 output_length = ~0;
2995 memset( output, 0, output_size );
2996 actual_status = psa_asymmetric_decrypt( slot, alg,
2997 input_data->x, input_data->len,
2998 NULL, label->len,
2999 output, output_size,
3000 &output_length );
3001 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003002 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003003 }
3004
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003005exit:
3006 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003007 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003008 mbedtls_psa_crypto_free( );
3009}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003010/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003011
3012/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003013void derive_setup( int key_type_arg,
3014 data_t *key_data,
3015 int alg_arg,
3016 data_t *salt,
3017 data_t *label,
3018 int requested_capacity_arg,
3019 int expected_status_arg )
3020{
3021 psa_key_slot_t slot = 1;
3022 size_t key_type = key_type_arg;
3023 psa_algorithm_t alg = alg_arg;
3024 size_t requested_capacity = requested_capacity_arg;
3025 psa_status_t expected_status = expected_status_arg;
3026 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3027 psa_key_policy_t policy;
3028
3029 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3030
3031 psa_key_policy_init( &policy );
3032 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3033 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3034
3035 TEST_ASSERT( psa_import_key( slot, key_type,
3036 key_data->x,
3037 key_data->len ) == PSA_SUCCESS );
3038
3039 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3040 salt->x, salt->len,
3041 label->x, label->len,
3042 requested_capacity ) == expected_status );
3043
3044exit:
3045 psa_generator_abort( &generator );
3046 psa_destroy_key( slot );
3047 mbedtls_psa_crypto_free( );
3048}
3049/* END_CASE */
3050
3051/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003052void derive_output( int alg_arg,
3053 data_t *key_data,
3054 data_t *salt,
3055 data_t *label,
3056 int requested_capacity_arg,
3057 data_t *expected_output1,
3058 data_t *expected_output2 )
3059{
3060 psa_key_slot_t slot = 1;
3061 psa_algorithm_t alg = alg_arg;
3062 size_t requested_capacity = requested_capacity_arg;
3063 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3064 uint8_t *expected_outputs[2] =
3065 {expected_output1->x, expected_output2->x};
3066 size_t output_sizes[2] =
3067 {expected_output1->len, expected_output2->len};
3068 size_t output_buffer_size = 0;
3069 uint8_t *output_buffer = NULL;
3070 size_t expected_capacity;
3071 size_t current_capacity;
3072 psa_key_policy_t policy;
3073 psa_status_t status;
3074 unsigned i;
3075
3076 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3077 {
3078 if( output_sizes[i] > output_buffer_size )
3079 output_buffer_size = output_sizes[i];
3080 if( output_sizes[i] == 0 )
3081 expected_outputs[i] = NULL;
3082 }
3083 output_buffer = mbedtls_calloc( 1, output_buffer_size );
3084 TEST_ASSERT( output_buffer != NULL );
3085 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3086
3087 psa_key_policy_init( &policy );
3088 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3089 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3090
3091 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3092 key_data->x,
3093 key_data->len ) == PSA_SUCCESS );
3094
3095 /* Extraction phase. */
3096 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3097 salt->x, salt->len,
3098 label->x, label->len,
3099 requested_capacity ) == PSA_SUCCESS );
3100 TEST_ASSERT( psa_get_generator_capacity( &generator,
3101 &current_capacity ) ==
3102 PSA_SUCCESS );
3103 TEST_ASSERT( current_capacity == requested_capacity );
3104 expected_capacity = requested_capacity;
3105
3106 /* Expansion phase. */
3107 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3108 {
3109 /* Read some bytes. */
3110 status = psa_generator_read( &generator,
3111 output_buffer, output_sizes[i] );
3112 if( expected_capacity == 0 && output_sizes[i] == 0 )
3113 {
3114 /* Reading 0 bytes when 0 bytes are available can go either way. */
3115 TEST_ASSERT( status == PSA_SUCCESS ||
3116 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3117 continue;
3118 }
3119 else if( expected_capacity == 0 ||
3120 output_sizes[i] > expected_capacity )
3121 {
3122 /* Capacity exceeded. */
3123 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3124 expected_capacity = 0;
3125 continue;
3126 }
3127 /* Success. Check the read data. */
3128 TEST_ASSERT( status == PSA_SUCCESS );
3129 if( output_sizes[i] != 0 )
3130 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3131 output_sizes[i] ) == 0 );
3132 /* Check the generator status. */
3133 expected_capacity -= output_sizes[i];
3134 TEST_ASSERT( psa_get_generator_capacity( &generator,
3135 &current_capacity ) ==
3136 PSA_SUCCESS );
3137 TEST_ASSERT( expected_capacity == current_capacity );
3138 }
3139 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3140
3141exit:
3142 mbedtls_free( output_buffer );
3143 psa_generator_abort( &generator );
3144 psa_destroy_key( slot );
3145 mbedtls_psa_crypto_free( );
3146}
3147/* END_CASE */
3148
3149/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003150void derive_full( int alg_arg,
3151 data_t *key_data,
3152 data_t *salt,
3153 data_t *label,
3154 int requested_capacity_arg )
3155{
3156 psa_key_slot_t slot = 1;
3157 psa_algorithm_t alg = alg_arg;
3158 size_t requested_capacity = requested_capacity_arg;
3159 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3160 unsigned char output_buffer[16];
3161 size_t expected_capacity = requested_capacity;
3162 size_t current_capacity;
3163 psa_key_policy_t policy;
3164
3165 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3166
3167 psa_key_policy_init( &policy );
3168 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3169 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3170
3171 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3172 key_data->x,
3173 key_data->len ) == PSA_SUCCESS );
3174
3175 /* Extraction phase. */
3176 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3177 salt->x, salt->len,
3178 label->x, label->len,
3179 requested_capacity ) == PSA_SUCCESS );
3180 TEST_ASSERT( psa_get_generator_capacity( &generator,
3181 &current_capacity ) ==
3182 PSA_SUCCESS );
3183 TEST_ASSERT( current_capacity == expected_capacity );
3184
3185 /* Expansion phase. */
3186 while( current_capacity > 0 )
3187 {
3188 size_t read_size = sizeof( output_buffer );
3189 if( read_size > current_capacity )
3190 read_size = current_capacity;
3191 TEST_ASSERT( psa_generator_read( &generator,
3192 output_buffer,
3193 read_size ) == PSA_SUCCESS );
3194 expected_capacity -= read_size;
3195 TEST_ASSERT( psa_get_generator_capacity( &generator,
3196 &current_capacity ) ==
3197 PSA_SUCCESS );
3198 TEST_ASSERT( current_capacity == expected_capacity );
3199 }
3200
3201 /* Check that the generator refuses to go over capacity. */
3202 TEST_ASSERT( psa_generator_read( &generator,
3203 output_buffer,
3204 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3205
3206 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3207
3208exit:
3209 psa_generator_abort( &generator );
3210 psa_destroy_key( slot );
3211 mbedtls_psa_crypto_free( );
3212}
3213/* END_CASE */
3214
3215/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003216void derive_key_exercise( int alg_arg,
3217 data_t *key_data,
3218 data_t *salt,
3219 data_t *label,
3220 int derived_type_arg,
3221 int derived_bits_arg,
3222 int derived_usage_arg,
3223 int derived_alg_arg )
3224{
3225 psa_key_slot_t base_key = 1;
3226 psa_key_slot_t derived_key = 2;
3227 psa_algorithm_t alg = alg_arg;
3228 psa_key_type_t derived_type = derived_type_arg;
3229 size_t derived_bits = derived_bits_arg;
3230 psa_key_usage_t derived_usage = derived_usage_arg;
3231 psa_algorithm_t derived_alg = derived_alg_arg;
3232 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3233 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3234 psa_key_policy_t policy;
3235 psa_key_type_t got_type;
3236 size_t got_bits;
3237
3238 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3239
3240 psa_key_policy_init( &policy );
3241 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3242 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3243 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3244 key_data->x,
3245 key_data->len ) == PSA_SUCCESS );
3246
3247 /* Derive a key. */
3248 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3249 salt->x, salt->len,
3250 label->x, label->len,
3251 capacity ) == PSA_SUCCESS );
3252 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3253 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3254 TEST_ASSERT( psa_generator_import_key( derived_key,
3255 derived_type,
3256 derived_bits,
3257 &generator ) == PSA_SUCCESS );
3258
3259 /* Test the key information */
3260 TEST_ASSERT( psa_get_key_information( derived_key,
3261 &got_type,
3262 &got_bits ) == PSA_SUCCESS );
3263 TEST_ASSERT( got_type == derived_type );
3264 TEST_ASSERT( got_bits == derived_bits );
3265
3266 /* Exercise the derived key. */
3267 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3268 goto exit;
3269
3270exit:
3271 psa_generator_abort( &generator );
3272 psa_destroy_key( base_key );
3273 psa_destroy_key( derived_key );
3274 mbedtls_psa_crypto_free( );
3275}
3276/* END_CASE */
3277
3278/* BEGIN_CASE */
3279void derive_key_export( int alg_arg,
3280 data_t *key_data,
3281 data_t *salt,
3282 data_t *label,
3283 int bytes1_arg,
3284 int bytes2_arg )
3285{
3286 psa_key_slot_t base_key = 1;
3287 psa_key_slot_t derived_key = 2;
3288 psa_algorithm_t alg = alg_arg;
3289 size_t bytes1 = bytes1_arg;
3290 size_t bytes2 = bytes2_arg;
3291 size_t capacity = bytes1 + bytes2;
3292 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3293 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
3294 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
3295 psa_key_policy_t policy;
3296 size_t length;
3297
3298 TEST_ASSERT( output_buffer != NULL );
3299 TEST_ASSERT( export_buffer != NULL );
3300 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3301
3302 psa_key_policy_init( &policy );
3303 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3304 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3305 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3306 key_data->x,
3307 key_data->len ) == PSA_SUCCESS );
3308
3309 /* Derive some material and output it. */
3310 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3311 salt->x, salt->len,
3312 label->x, label->len,
3313 capacity ) == PSA_SUCCESS );
3314 TEST_ASSERT( psa_generator_read( &generator,
3315 output_buffer,
3316 capacity ) == PSA_SUCCESS );
3317 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3318
3319 /* Derive the same output again, but this time store it in key objects. */
3320 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3321 salt->x, salt->len,
3322 label->x, label->len,
3323 capacity ) == PSA_SUCCESS );
3324 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3325 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3326 TEST_ASSERT( psa_generator_import_key( derived_key,
3327 PSA_KEY_TYPE_RAW_DATA,
3328 PSA_BYTES_TO_BITS( bytes1 ),
3329 &generator ) == PSA_SUCCESS );
3330 TEST_ASSERT( psa_export_key( derived_key,
3331 export_buffer, bytes1,
3332 &length ) == PSA_SUCCESS );
3333 TEST_ASSERT( length == bytes1 );
3334 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3335 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3336 TEST_ASSERT( psa_generator_import_key( derived_key,
3337 PSA_KEY_TYPE_RAW_DATA,
3338 PSA_BYTES_TO_BITS( bytes2 ),
3339 &generator ) == PSA_SUCCESS );
3340 TEST_ASSERT( psa_export_key( derived_key,
3341 export_buffer + bytes1, bytes2,
3342 &length ) == PSA_SUCCESS );
3343 TEST_ASSERT( length == bytes2 );
3344
3345 /* Compare the outputs from the two runs. */
3346 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3347
3348exit:
3349 mbedtls_free( output_buffer );
3350 mbedtls_free( export_buffer );
3351 psa_generator_abort( &generator );
3352 psa_destroy_key( base_key );
3353 psa_destroy_key( derived_key );
3354 mbedtls_psa_crypto_free( );
3355}
3356/* END_CASE */
3357
3358/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003359void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003360{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003361 size_t bytes = bytes_arg;
3362 const unsigned char trail[] = "don't overwrite me";
3363 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
3364 unsigned char *changed = mbedtls_calloc( 1, bytes );
3365 size_t i;
3366 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003367
Gilles Peskinea50d7392018-06-21 10:22:13 +02003368 TEST_ASSERT( output != NULL );
Darryl Green9c862252018-07-24 12:52:44 +01003369 TEST_ASSERT( bytes == 0 || changed != NULL );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003370 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003371
3372 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3373
Gilles Peskinea50d7392018-06-21 10:22:13 +02003374 /* Run several times, to ensure that every output byte will be
3375 * nonzero at least once with overwhelming probability
3376 * (2^(-8*number_of_runs)). */
3377 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003378 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003379 memset( output, 0, bytes );
3380 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3381
3382 /* Check that no more than bytes have been overwritten */
3383 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3384
3385 for( i = 0; i < bytes; i++ )
3386 {
3387 if( output[i] != 0 )
3388 ++changed[i];
3389 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003390 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003391
3392 /* Check that every byte was changed to nonzero at least once. This
3393 * validates that psa_generate_random is overwriting every byte of
3394 * the output buffer. */
3395 for( i = 0; i < bytes; i++ )
3396 {
3397 TEST_ASSERT( changed[i] != 0 );
3398 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003399
3400exit:
3401 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003402 mbedtls_free( output );
3403 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003404}
3405/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003406
3407/* BEGIN_CASE */
3408void generate_key( int type_arg,
3409 int bits_arg,
3410 int usage_arg,
3411 int alg_arg,
3412 int expected_status_arg )
3413{
3414 int slot = 1;
3415 psa_key_type_t type = type_arg;
3416 psa_key_usage_t usage = usage_arg;
3417 size_t bits = bits_arg;
3418 psa_algorithm_t alg = alg_arg;
3419 psa_status_t expected_status = expected_status_arg;
3420 psa_key_type_t got_type;
3421 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003422 psa_status_t expected_info_status =
3423 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3424 psa_key_policy_t policy;
3425
3426 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3427
3428 psa_key_policy_init( &policy );
3429 psa_key_policy_set_usage( &policy, usage, alg );
3430 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3431
3432 /* Generate a key */
3433 TEST_ASSERT( psa_generate_key( slot, type, bits,
3434 NULL, 0 ) == expected_status );
3435
3436 /* Test the key information */
3437 TEST_ASSERT( psa_get_key_information( slot,
3438 &got_type,
3439 &got_bits ) == expected_info_status );
3440 if( expected_info_status != PSA_SUCCESS )
3441 goto exit;
3442 TEST_ASSERT( got_type == type );
3443 TEST_ASSERT( got_bits == bits );
3444
Gilles Peskine818ca122018-06-20 18:16:48 +02003445 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003446 if( ! exercise_key( slot, usage, alg ) )
3447 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003448
3449exit:
3450 psa_destroy_key( slot );
3451 mbedtls_psa_crypto_free( );
3452}
3453/* END_CASE */