blob: d5922b767016006baba70888bb7596ca77813890 [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 {
522 * version Version, -- 0
523 * 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;
592 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 1,
593 MBEDTLS_ASN1_BIT_STRING ) == 0 );
594 TEST_ASSERT( p + len == end );
595 TEST_ASSERT( p[0] == 0 ); /* 0 unused bits in the bit string */
596 ++p;
597 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
598 TEST_ASSERT( p[0] == 4 );
599 }
600 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200601#endif /* MBEDTLS_ECP_C */
602
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200603 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
604 {
605 uint8_t *p = exported;
606 uint8_t *end = exported + exported_length;
607 size_t len;
608 mbedtls_asn1_buf alg;
609 mbedtls_asn1_buf params;
610 mbedtls_asn1_bitstring bitstring;
611 /* SubjectPublicKeyInfo ::= SEQUENCE {
612 * algorithm AlgorithmIdentifier,
613 * subjectPublicKey BIT STRING }
614 * AlgorithmIdentifier ::= SEQUENCE {
615 * algorithm OBJECT IDENTIFIER,
616 * parameters ANY DEFINED BY algorithm OPTIONAL }
617 */
618 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
619 MBEDTLS_ASN1_SEQUENCE |
620 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
621 TEST_ASSERT( p + len == end );
622 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
623 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
624 goto exit;
625 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
626 TEST_ASSERT( p == end );
627 p = bitstring.p;
628#if defined(MBEDTLS_RSA_C)
629 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
630 {
631 /* RSAPublicKey ::= SEQUENCE {
632 * modulus INTEGER, -- n
633 * publicExponent INTEGER } -- e
634 */
635 TEST_ASSERT( bitstring.unused_bits == 0 );
636 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
637 MBEDTLS_ASN1_SEQUENCE |
638 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
639 TEST_ASSERT( p + len == end );
640 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
641 goto exit;
642 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
643 goto exit;
644 TEST_ASSERT( p == end );
645 }
646 else
647#endif /* MBEDTLS_RSA_C */
648#if defined(MBEDTLS_ECP_C)
649 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
650 {
651 /* ECPoint ::= ...
652 * -- first 8 bits: 0x04;
653 * -- then x_P as an n-bit string, big endian;
654 * -- then y_P as a n-bit string, big endian,
655 * -- where n is the order of the curve.
656 */
657 TEST_ASSERT( bitstring.unused_bits == 0 );
658 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
659 TEST_ASSERT( p[0] == 4 );
660 }
661 else
662#endif /* MBEDTLS_ECP_C */
663 {
664 char message[40];
665 mbedtls_snprintf( message, sizeof( message ),
666 "No sanity check for public key type=0x%08lx",
667 (unsigned long) type );
668 test_fail( message, __LINE__, __FILE__ );
669 return( 0 );
670 }
671 }
672 else
673
674 {
675 /* No sanity checks for other types */
676 }
677
678 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200679
680exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200681 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200682}
683
684static int exercise_export_key( psa_key_slot_t slot,
685 psa_key_usage_t usage )
686{
687 psa_key_type_t type;
688 size_t bits;
689 uint8_t *exported = NULL;
690 size_t exported_size = 0;
691 size_t exported_length = 0;
692 int ok = 0;
693
694 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 )
695 {
696 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
697 PSA_ERROR_NOT_PERMITTED );
698 return( 1 );
699 }
700
701 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
702 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
703 exported = mbedtls_calloc( 1, exported_size );
704 TEST_ASSERT( exported != NULL );
705
706 TEST_ASSERT( psa_export_key( slot,
707 exported, exported_size,
708 &exported_length ) == PSA_SUCCESS );
709 ok = exported_key_sanity_check( type, bits, exported, exported_length );
710
711exit:
712 mbedtls_free( exported );
713 return( ok );
714}
715
716static int exercise_export_public_key( psa_key_slot_t slot )
717{
718 psa_key_type_t type;
719 psa_key_type_t public_type;
720 size_t bits;
721 uint8_t *exported = NULL;
722 size_t exported_size = 0;
723 size_t exported_length = 0;
724 int ok = 0;
725
726 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
727 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
728 {
729 TEST_ASSERT( psa_export_public_key( slot,
730 NULL, 0, &exported_length ) ==
731 PSA_ERROR_INVALID_ARGUMENT );
732 return( 1 );
733 }
734
735 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
736 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
737 exported = mbedtls_calloc( 1, exported_size );
738 TEST_ASSERT( exported != NULL );
739
740 TEST_ASSERT( psa_export_public_key( slot,
741 exported, exported_size,
742 &exported_length ) == PSA_SUCCESS );
743 ok = exported_key_sanity_check( public_type, bits,
744 exported, exported_length );
745
746exit:
747 mbedtls_free( exported );
748 return( ok );
749}
750
Gilles Peskine02b75072018-07-01 22:31:34 +0200751static int exercise_key( psa_key_slot_t slot,
752 psa_key_usage_t usage,
753 psa_algorithm_t alg )
754{
755 int ok;
756 if( alg == 0 )
757 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
758 else if( PSA_ALG_IS_MAC( alg ) )
759 ok = exercise_mac_key( slot, usage, alg );
760 else if( PSA_ALG_IS_CIPHER( alg ) )
761 ok = exercise_cipher_key( slot, usage, alg );
762 else if( PSA_ALG_IS_AEAD( alg ) )
763 ok = exercise_aead_key( slot, usage, alg );
764 else if( PSA_ALG_IS_SIGN( alg ) )
765 ok = exercise_signature_key( slot, usage, alg );
766 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
767 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200768 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
769 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200770 else
771 {
772 char message[40];
773 mbedtls_snprintf( message, sizeof( message ),
774 "No code to exercise alg=0x%08lx",
775 (unsigned long) alg );
776 test_fail( message, __LINE__, __FILE__ );
777 ok = 0;
778 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200779
780 ok = ok && exercise_export_key( slot, usage );
781 ok = ok && exercise_export_public_key( slot );
782
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 return( ok );
784}
785
Gilles Peskinee59236f2018-01-27 23:32:46 +0100786/* END_HEADER */
787
788/* BEGIN_DEPENDENCIES
789 * depends_on:MBEDTLS_PSA_CRYPTO_C
790 * END_DEPENDENCIES
791 */
792
793/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200794void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100795{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100797 int i;
798 for( i = 0; i <= 1; i++ )
799 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 status = psa_crypto_init( );
801 TEST_ASSERT( status == PSA_SUCCESS );
802 status = psa_crypto_init( );
803 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100804 mbedtls_psa_crypto_free( );
805 }
806}
807/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808
809/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200810void fill_slots( int max_arg )
811{
812 /* Fill all the slots until we run out of memory or out of slots,
813 * or until some limit specified in the test data for the sake of
814 * implementations with an essentially unlimited number of slots.
815 * This test assumes that available slots are numbered from 1. */
816
817 psa_key_slot_t slot;
818 psa_key_slot_t max = 0;
819 psa_key_policy_t policy;
820 uint8_t exported[sizeof( max )];
821 size_t exported_size;
822 psa_status_t status;
823
824 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
825
826 psa_key_policy_init( &policy );
827 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
828
829 for( max = 1; max <= (size_t) max_arg; max++ )
830 {
831 status = psa_set_key_policy( max, &policy );
832 /* Stop filling slots if we run out of memory or out of
833 * available slots. */
834 TEST_ASSERT( status == PSA_SUCCESS ||
835 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
836 status == PSA_ERROR_INVALID_ARGUMENT );
837 if( status != PSA_SUCCESS )
838 break;
839 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
840 (uint8_t*) &max, sizeof( max ) );
841 /* Since psa_set_key_policy succeeded, we know that the slot
842 * number is valid. But we may legitimately run out of memory. */
843 TEST_ASSERT( status == PSA_SUCCESS ||
844 status == PSA_ERROR_INSUFFICIENT_MEMORY );
845 if( status != PSA_SUCCESS )
846 break;
847 }
848 /* `max` is now the first slot number that wasn't filled. */
849 max -= 1;
850
851 for( slot = 1; slot <= max; slot++ )
852 {
853 TEST_ASSERT( psa_export_key( slot,
854 exported, sizeof( exported ),
855 &exported_size ) == PSA_SUCCESS );
856 TEST_ASSERT( exported_size == sizeof( slot ) );
857 TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
Gilles Peskine996deb12018-08-01 15:45:45 +0200858 }
859
860exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200861 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200862 mbedtls_psa_crypto_free( );
863}
864/* END_CASE */
865
866/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200867void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100868{
869 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200870 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300874 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
876
Gilles Peskine4abf7412018-06-18 16:35:34 +0200877 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200878 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100879 if( status == PSA_SUCCESS )
880 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
881
882exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 mbedtls_psa_crypto_free( );
884}
885/* END_CASE */
886
887/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200888void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
889{
890 int slot = 1;
891 size_t bits = bits_arg;
892 psa_status_t expected_status = expected_status_arg;
893 psa_status_t status;
894 psa_key_type_t type =
895 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
896 size_t buffer_size = /* Slight overapproximations */
897 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
898 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
899 unsigned char *p;
900 int ret;
901 size_t length;
902
903 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
904 TEST_ASSERT( buffer != NULL );
905
906 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
907 bits, keypair ) ) >= 0 );
908 length = ret;
909
910 /* Try importing the key */
911 status = psa_import_key( slot, type, p, length );
912 TEST_ASSERT( status == expected_status );
913 if( status == PSA_SUCCESS )
914 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
915
916exit:
917 mbedtls_free( buffer );
918 mbedtls_psa_crypto_free( );
919}
920/* END_CASE */
921
922/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300923void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300924 int type_arg,
925 int alg_arg,
926 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100927 int expected_bits,
928 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200929 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100930 int canonical_input )
931{
932 int slot = 1;
933 int slot2 = slot + 1;
934 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200935 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200936 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938 unsigned char *exported = NULL;
939 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100941 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 size_t reexported_length;
943 psa_key_type_t got_type;
944 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200945 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300948 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300949 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 exported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100951 TEST_ASSERT( export_size == 0 || exported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 if( ! canonical_input )
953 {
954 reexported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100955 TEST_ASSERT( export_size == 0 || reexported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 }
957 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
958
mohammad1603a97cb8c2018-03-28 03:46:26 -0700959 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200960 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700961 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
962
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100963 /* Import the key */
964 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200965 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966
967 /* Test the key information */
968 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200969 &got_type,
970 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971 TEST_ASSERT( got_type == type );
972 TEST_ASSERT( got_bits == (size_t) expected_bits );
973
974 /* Export the key */
975 status = psa_export_key( slot,
976 exported, export_size,
977 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200978 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100979
980 /* The exported length must be set by psa_export_key() to a value between 0
981 * and export_size. On errors, the exported length must be 0. */
982 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
983 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
984 TEST_ASSERT( exported_length <= export_size );
985
Gilles Peskine3f669c32018-06-21 09:21:51 +0200986 TEST_ASSERT( mem_is_zero( exported + exported_length,
987 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200989 {
990 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200992 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993
Gilles Peskine8f609232018-08-11 01:24:55 +0200994 if( ! exercise_export_key( slot, usage_arg ) )
995 goto exit;
996
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 if( canonical_input )
998 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200999 TEST_ASSERT( exported_length == data->len );
1000 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001 }
1002 else
1003 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001004 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1005
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001007 exported,
1008 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001010 reexported,
1011 export_size,
1012 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 TEST_ASSERT( reexported_length == exported_length );
1014 TEST_ASSERT( memcmp( reexported, exported,
1015 exported_length ) == 0 );
1016 }
1017
1018destroy:
1019 /* Destroy the key */
1020 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1021 TEST_ASSERT( psa_get_key_information(
1022 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1023
1024exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001025 mbedtls_free( exported );
1026 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027 mbedtls_psa_crypto_free( );
1028}
1029/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001030
Moran Pekerf709f4a2018-06-06 17:26:04 +03001031/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001032void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001033 int type_arg,
1034 int alg_arg,
1035 int expected_bits,
1036 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001037 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001038{
1039 int slot = 1;
1040 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001041 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001042 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001043 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001044 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001046 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001047 psa_key_type_t got_type;
1048 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001049 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001050
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001052 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001053 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001054 exported = mbedtls_calloc( 1, export_size );
1055 TEST_ASSERT( exported != NULL );
1056
1057 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1058
1059 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001060 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001061 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1062
1063 /* Import the key */
1064 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001065 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001066
1067 /* Test the key information */
1068 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001069 &got_type,
1070 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001071 TEST_ASSERT( got_type == type );
1072 TEST_ASSERT( got_bits == (size_t) expected_bits );
1073
1074 /* Export the key */
1075 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001076 exported, export_size,
1077 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001078 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001079 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
1080 TEST_ASSERT( mem_is_zero( exported + exported_length,
1081 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001082 if( status != PSA_SUCCESS )
1083 goto destroy;
1084
Moran Pekerf709f4a2018-06-06 17:26:04 +03001085destroy:
1086 /* Destroy the key */
1087 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1088 TEST_ASSERT( psa_get_key_information(
1089 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1090
1091exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001092 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001093 mbedtls_psa_crypto_free( );
1094}
1095/* END_CASE */
1096
Gilles Peskine20035e32018-02-03 22:44:14 +01001097/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001098void import_and_exercise_key( data_t *data,
1099 int type_arg,
1100 int bits_arg,
1101 int alg_arg )
1102{
1103 int slot = 1;
1104 psa_key_type_t type = type_arg;
1105 size_t bits = bits_arg;
1106 psa_algorithm_t alg = alg_arg;
1107 psa_key_usage_t usage =
1108 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1109 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1110 PSA_KEY_USAGE_VERIFY :
1111 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1112 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1113 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1114 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1115 PSA_KEY_USAGE_ENCRYPT :
1116 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001117 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001118 0 );
1119 psa_key_policy_t policy;
1120 psa_key_type_t got_type;
1121 size_t got_bits;
1122 psa_status_t status;
1123
1124 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1125
1126 psa_key_policy_init( &policy );
1127 psa_key_policy_set_usage( &policy, usage, alg );
1128 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1129
1130 /* Import the key */
1131 status = psa_import_key( slot, type, data->x, data->len );
1132 TEST_ASSERT( status == PSA_SUCCESS );
1133
1134 /* Test the key information */
1135 TEST_ASSERT( psa_get_key_information( slot,
1136 &got_type,
1137 &got_bits ) == PSA_SUCCESS );
1138 TEST_ASSERT( got_type == type );
1139 TEST_ASSERT( got_bits == bits );
1140
1141 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001142 if( ! exercise_key( slot, usage, alg ) )
1143 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001144
1145exit:
1146 psa_destroy_key( slot );
1147 mbedtls_psa_crypto_free( );
1148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001152void key_policy( int usage_arg, int alg_arg )
1153{
1154 int key_slot = 1;
1155 psa_algorithm_t alg = alg_arg;
1156 psa_key_usage_t usage = usage_arg;
1157 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1158 unsigned char key[32] = {0};
1159 psa_key_policy_t policy_set;
1160 psa_key_policy_t policy_get;
1161
1162 memset( key, 0x2a, sizeof( key ) );
1163
1164 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1165
1166 psa_key_policy_init( &policy_set );
1167 psa_key_policy_init( &policy_get );
1168
1169 psa_key_policy_set_usage( &policy_set, usage, alg );
1170
1171 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1172 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1173 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1174
1175 TEST_ASSERT( psa_import_key( key_slot, key_type,
1176 key, sizeof( key ) ) == PSA_SUCCESS );
1177
1178 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1179
1180 TEST_ASSERT( policy_get.usage == policy_set.usage );
1181 TEST_ASSERT( policy_get.alg == policy_set.alg );
1182
1183exit:
1184 psa_destroy_key( key_slot );
1185 mbedtls_psa_crypto_free( );
1186}
1187/* END_CASE */
1188
1189/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001190void mac_key_policy( int policy_usage,
1191 int policy_alg,
1192 int key_type,
1193 data_t *key_data,
1194 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001195{
1196 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001197 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198 psa_mac_operation_t operation;
1199 psa_status_t status;
1200 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001201
1202 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1203
1204 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001205 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001206 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1207
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208 TEST_ASSERT( psa_import_key( key_slot, key_type,
1209 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001210
Gilles Peskine89167cb2018-07-08 20:12:23 +02001211 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 if( policy_alg == exercise_alg &&
1213 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1214 TEST_ASSERT( status == PSA_SUCCESS );
1215 else
1216 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1217 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001218
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001220 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001221 if( policy_alg == exercise_alg &&
1222 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001223 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224 else
1225 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1226
1227exit:
1228 psa_mac_abort( &operation );
1229 psa_destroy_key( key_slot );
1230 mbedtls_psa_crypto_free( );
1231}
1232/* END_CASE */
1233
1234/* BEGIN_CASE */
1235void cipher_key_policy( int policy_usage,
1236 int policy_alg,
1237 int key_type,
1238 data_t *key_data,
1239 int exercise_alg )
1240{
1241 int key_slot = 1;
1242 psa_key_policy_t policy;
1243 psa_cipher_operation_t operation;
1244 psa_status_t status;
1245
1246 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1247
1248 psa_key_policy_init( &policy );
1249 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1250 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1251
1252 TEST_ASSERT( psa_import_key( key_slot, key_type,
1253 key_data->x, key_data->len ) == PSA_SUCCESS );
1254
Gilles Peskinefe119512018-07-08 21:39:34 +02001255 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001256 if( policy_alg == exercise_alg &&
1257 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1258 TEST_ASSERT( status == PSA_SUCCESS );
1259 else
1260 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1261 psa_cipher_abort( &operation );
1262
Gilles Peskinefe119512018-07-08 21:39:34 +02001263 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001264 if( policy_alg == exercise_alg &&
1265 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1266 TEST_ASSERT( status == PSA_SUCCESS );
1267 else
1268 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1269
1270exit:
1271 psa_cipher_abort( &operation );
1272 psa_destroy_key( key_slot );
1273 mbedtls_psa_crypto_free( );
1274}
1275/* END_CASE */
1276
1277/* BEGIN_CASE */
1278void aead_key_policy( int policy_usage,
1279 int policy_alg,
1280 int key_type,
1281 data_t *key_data,
1282 int nonce_length_arg,
1283 int tag_length_arg,
1284 int exercise_alg )
1285{
1286 int key_slot = 1;
1287 psa_key_policy_t policy;
1288 psa_status_t status;
1289 unsigned char nonce[16] = {0};
1290 size_t nonce_length = nonce_length_arg;
1291 unsigned char tag[16];
1292 size_t tag_length = tag_length_arg;
1293 size_t output_length;
1294
1295 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1296 TEST_ASSERT( tag_length <= sizeof( tag ) );
1297
1298 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1299
1300 psa_key_policy_init( &policy );
1301 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1302 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1303
1304 TEST_ASSERT( psa_import_key( key_slot, key_type,
1305 key_data->x, key_data->len ) == PSA_SUCCESS );
1306
1307 status = psa_aead_encrypt( key_slot, exercise_alg,
1308 nonce, nonce_length,
1309 NULL, 0,
1310 NULL, 0,
1311 tag, tag_length,
1312 &output_length );
1313 if( policy_alg == exercise_alg &&
1314 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1315 TEST_ASSERT( status == PSA_SUCCESS );
1316 else
1317 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1318
1319 memset( tag, 0, sizeof( tag ) );
1320 status = psa_aead_decrypt( key_slot, exercise_alg,
1321 nonce, nonce_length,
1322 NULL, 0,
1323 tag, tag_length,
1324 NULL, 0,
1325 &output_length );
1326 if( policy_alg == exercise_alg &&
1327 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1328 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1329 else
1330 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1331
1332exit:
1333 psa_destroy_key( key_slot );
1334 mbedtls_psa_crypto_free( );
1335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
1339void asymmetric_encryption_key_policy( int policy_usage,
1340 int policy_alg,
1341 int key_type,
1342 data_t *key_data,
1343 int exercise_alg )
1344{
1345 int key_slot = 1;
1346 psa_key_policy_t policy;
1347 psa_status_t status;
1348 size_t key_bits;
1349 size_t buffer_length;
1350 unsigned char *buffer = NULL;
1351 size_t output_length;
1352
1353 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1354
1355 psa_key_policy_init( &policy );
1356 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1357 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1358
1359 TEST_ASSERT( psa_import_key( key_slot, key_type,
1360 key_data->x, key_data->len ) == PSA_SUCCESS );
1361
1362 TEST_ASSERT( psa_get_key_information( key_slot,
1363 NULL,
1364 &key_bits ) == PSA_SUCCESS );
1365 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1366 exercise_alg );
1367 buffer = mbedtls_calloc( 1, buffer_length );
1368 TEST_ASSERT( buffer != NULL );
1369
1370 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1371 NULL, 0,
1372 NULL, 0,
1373 buffer, buffer_length,
1374 &output_length );
1375 if( policy_alg == exercise_alg &&
1376 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1377 TEST_ASSERT( status == PSA_SUCCESS );
1378 else
1379 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1380
1381 memset( buffer, 0, buffer_length );
1382 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1383 buffer, buffer_length,
1384 NULL, 0,
1385 buffer, buffer_length,
1386 &output_length );
1387 if( policy_alg == exercise_alg &&
1388 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1389 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1390 else
1391 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1392
1393exit:
1394 psa_destroy_key( key_slot );
1395 mbedtls_psa_crypto_free( );
1396 mbedtls_free( buffer );
1397}
1398/* END_CASE */
1399
1400/* BEGIN_CASE */
1401void asymmetric_signature_key_policy( int policy_usage,
1402 int policy_alg,
1403 int key_type,
1404 data_t *key_data,
1405 int exercise_alg )
1406{
1407 int key_slot = 1;
1408 psa_key_policy_t policy;
1409 psa_status_t status;
1410 unsigned char payload[16] = {1};
1411 size_t payload_length = sizeof( payload );
1412 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1413 size_t signature_length;
1414
1415 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1416
1417 psa_key_policy_init( &policy );
1418 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1419 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1420
1421 TEST_ASSERT( psa_import_key( key_slot, key_type,
1422 key_data->x, key_data->len ) == PSA_SUCCESS );
1423
1424 status = psa_asymmetric_sign( key_slot, exercise_alg,
1425 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001426 signature, sizeof( signature ),
1427 &signature_length );
1428 if( policy_alg == exercise_alg &&
1429 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1430 TEST_ASSERT( status == PSA_SUCCESS );
1431 else
1432 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1433
1434 memset( signature, 0, sizeof( signature ) );
1435 status = psa_asymmetric_verify( key_slot, exercise_alg,
1436 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001437 signature, sizeof( signature ) );
1438 if( policy_alg == exercise_alg &&
1439 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1440 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1441 else
1442 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001443
1444exit:
1445 psa_destroy_key( key_slot );
1446 mbedtls_psa_crypto_free( );
1447}
1448/* END_CASE */
1449
1450/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001451void derive_key_policy( int policy_usage,
1452 int policy_alg,
1453 int key_type,
1454 data_t *key_data,
1455 int exercise_alg )
1456{
1457 int key_slot = 1;
1458 psa_key_policy_t policy;
1459 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1460 psa_status_t status;
1461
1462 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1463
1464 psa_key_policy_init( &policy );
1465 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1466 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1467
1468 TEST_ASSERT( psa_import_key( key_slot, key_type,
1469 key_data->x, key_data->len ) == PSA_SUCCESS );
1470
1471 status = psa_key_derivation( &generator, key_slot,
1472 exercise_alg,
1473 NULL, 0,
1474 NULL, 0,
1475 1 );
1476 if( policy_alg == exercise_alg &&
1477 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1478 TEST_ASSERT( status == PSA_SUCCESS );
1479 else
1480 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1481
1482exit:
1483 psa_generator_abort( &generator );
1484 psa_destroy_key( key_slot );
1485 mbedtls_psa_crypto_free( );
1486}
1487/* END_CASE */
1488
1489/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001490void key_lifetime( int lifetime_arg )
1491{
1492 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001493 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001494 unsigned char key[32] = {0};
1495 psa_key_lifetime_t lifetime_set = lifetime_arg;
1496 psa_key_lifetime_t lifetime_get;
1497
1498 memset( key, 0x2a, sizeof( key ) );
1499
1500 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1501
1502 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1503 lifetime_set ) == PSA_SUCCESS );
1504
1505 TEST_ASSERT( psa_import_key( key_slot, key_type,
1506 key, sizeof( key ) ) == PSA_SUCCESS );
1507
1508 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1509 &lifetime_get ) == PSA_SUCCESS );
1510
1511 TEST_ASSERT( lifetime_get == lifetime_set );
1512
1513exit:
1514 psa_destroy_key( key_slot );
1515 mbedtls_psa_crypto_free( );
1516}
1517/* END_CASE */
1518
1519/* BEGIN_CASE */
1520void key_lifetime_set_fail( int key_slot_arg,
1521 int lifetime_arg,
1522 int expected_status_arg )
1523{
1524 psa_key_slot_t key_slot = key_slot_arg;
1525 psa_key_lifetime_t lifetime_set = lifetime_arg;
1526 psa_status_t actual_status;
1527 psa_status_t expected_status = expected_status_arg;
1528
1529 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1530
1531 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1532
1533 if( actual_status == PSA_SUCCESS )
1534 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1535
1536 TEST_ASSERT( expected_status == actual_status );
1537
1538exit:
1539 psa_destroy_key( key_slot );
1540 mbedtls_psa_crypto_free( );
1541}
1542/* END_CASE */
1543
1544/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001545void hash_setup( int alg_arg,
1546 int expected_status_arg )
1547{
1548 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001549 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001550 psa_hash_operation_t operation;
1551 psa_status_t status;
1552
1553 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1554
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001555 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001556 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001557 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001558
1559exit:
1560 mbedtls_psa_crypto_free( );
1561}
1562/* END_CASE */
1563
1564/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001565void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001566{
1567 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001568 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001569 size_t actual_hash_length;
1570 psa_hash_operation_t operation;
1571
Gilles Peskine69c12672018-06-28 00:07:19 +02001572 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1573 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1574
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001575 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001576 TEST_ASSERT( expected_hash != NULL );
1577 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1578 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001579
1580 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1581
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001582 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001583 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001584 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585 TEST_ASSERT( psa_hash_finish( &operation,
1586 actual_hash, sizeof( actual_hash ),
1587 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001588 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001589 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001590 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001591
1592exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593 mbedtls_psa_crypto_free( );
1594}
1595/* END_CASE */
1596
1597/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001598void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001599{
1600 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001601 psa_hash_operation_t operation;
1602
Gilles Peskine69c12672018-06-28 00:07:19 +02001603 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1604 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1605
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001606 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001607 TEST_ASSERT( expected_hash != NULL );
1608 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1609 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001610
1611 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1612
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001613 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001614 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001615 input->x,
1616 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001617 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001618 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001619 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001620
1621exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001622 mbedtls_psa_crypto_free( );
1623}
1624/* END_CASE */
1625
1626/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001627void mac_setup( int key_type_arg,
1628 data_t *key,
1629 int alg_arg,
1630 int expected_status_arg )
1631{
1632 int key_slot = 1;
1633 psa_key_type_t key_type = key_type_arg;
1634 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001635 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001636 psa_mac_operation_t operation;
1637 psa_key_policy_t policy;
1638 psa_status_t status;
1639
1640 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1641
1642 psa_key_policy_init( &policy );
1643 psa_key_policy_set_usage( &policy,
1644 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1645 alg );
1646 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1647
1648 TEST_ASSERT( psa_import_key( key_slot, key_type,
1649 key->x, key->len ) == PSA_SUCCESS );
1650
Gilles Peskine89167cb2018-07-08 20:12:23 +02001651 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001652 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001653 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001654
1655exit:
1656 psa_destroy_key( key_slot );
1657 mbedtls_psa_crypto_free( );
1658}
1659/* END_CASE */
1660
1661/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001662void mac_verify( int key_type_arg,
1663 data_t *key,
1664 int alg_arg,
1665 data_t *input,
1666 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001667{
1668 int key_slot = 1;
1669 psa_key_type_t key_type = key_type_arg;
1670 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001672 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673
Gilles Peskine69c12672018-06-28 00:07:19 +02001674 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1675
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001679 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001680 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1681 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682
1683 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1684
mohammad16036df908f2018-04-02 08:34:15 -07001685 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001686 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001687 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1688
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001690 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001691
Gilles Peskine89167cb2018-07-08 20:12:23 +02001692 TEST_ASSERT( psa_mac_verify_setup( &operation,
1693 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1695 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001696 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001697 TEST_ASSERT( psa_mac_verify_finish( &operation,
1698 expected_mac->x,
1699 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001700
1701exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702 psa_destroy_key( key_slot );
1703 mbedtls_psa_crypto_free( );
1704}
1705/* END_CASE */
1706
1707/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001708void cipher_setup( int key_type_arg,
1709 data_t *key,
1710 int alg_arg,
1711 int expected_status_arg )
1712{
1713 int key_slot = 1;
1714 psa_key_type_t key_type = key_type_arg;
1715 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001716 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001717 psa_cipher_operation_t operation;
1718 psa_key_policy_t policy;
1719 psa_status_t status;
1720
1721 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1722
1723 psa_key_policy_init( &policy );
1724 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1725 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1726
1727 TEST_ASSERT( psa_import_key( key_slot, key_type,
1728 key->x, key->len ) == PSA_SUCCESS );
1729
Gilles Peskinefe119512018-07-08 21:39:34 +02001730 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001731 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001732 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001733
1734exit:
1735 psa_destroy_key( key_slot );
1736 mbedtls_psa_crypto_free( );
1737}
1738/* END_CASE */
1739
1740/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001741void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001742 data_t *key,
1743 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001744 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001745{
1746 int key_slot = 1;
1747 psa_status_t status;
1748 psa_key_type_t key_type = key_type_arg;
1749 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001750 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001751 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001752 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001753 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001754 size_t output_buffer_size = 0;
1755 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001756 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001757 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001758 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001759
Gilles Peskine50e586b2018-06-08 14:28:46 +02001760 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001761 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001762 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001763 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1764 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1765 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001766
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001767 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1768 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001769
1770 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1771
Moran Pekered346952018-07-05 15:22:45 +03001772 psa_key_policy_init( &policy );
1773 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1774 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1775
Gilles Peskine50e586b2018-06-08 14:28:46 +02001776 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001777 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001778
Gilles Peskinefe119512018-07-08 21:39:34 +02001779 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1780 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001781
Gilles Peskinefe119512018-07-08 21:39:34 +02001782 TEST_ASSERT( psa_cipher_set_iv( &operation,
1783 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001784 output_buffer_size = (size_t) input->len +
1785 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001786 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001787 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001788
Gilles Peskine4abf7412018-06-18 16:35:34 +02001789 TEST_ASSERT( psa_cipher_update( &operation,
1790 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001791 output, output_buffer_size,
1792 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001793 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001794 status = psa_cipher_finish( &operation,
1795 output + function_output_length,
1796 output_buffer_size,
1797 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001798 total_output_length += function_output_length;
1799
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001800 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001801 if( expected_status == PSA_SUCCESS )
1802 {
1803 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001804 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001805 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001806 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001807 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001808
Gilles Peskine50e586b2018-06-08 14:28:46 +02001809exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001810 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001811 psa_destroy_key( key_slot );
1812 mbedtls_psa_crypto_free( );
1813}
1814/* END_CASE */
1815
1816/* BEGIN_CASE */
1817void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001818 data_t *key,
1819 data_t *input,
1820 int first_part_size,
1821 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001822{
1823 int key_slot = 1;
1824 psa_key_type_t key_type = key_type_arg;
1825 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001826 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001827 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001828 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001829 size_t output_buffer_size = 0;
1830 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001831 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001832 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001833 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001834
Gilles Peskine50e586b2018-06-08 14:28:46 +02001835 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001836 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001837 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001838 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1839 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1840 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001841
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001842 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1843 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001844
1845 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1846
Moran Pekered346952018-07-05 15:22:45 +03001847 psa_key_policy_init( &policy );
1848 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1849 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1850
Gilles Peskine50e586b2018-06-08 14:28:46 +02001851 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001852 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001853
Gilles Peskinefe119512018-07-08 21:39:34 +02001854 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1855 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001856
Gilles Peskinefe119512018-07-08 21:39:34 +02001857 TEST_ASSERT( psa_cipher_set_iv( &operation,
1858 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001859 output_buffer_size = (size_t) input->len +
1860 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001861 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001862 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001863
Gilles Peskine4abf7412018-06-18 16:35:34 +02001864 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001865 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001866 output, output_buffer_size,
1867 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001868 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001869 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001870 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001871 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001872 output, output_buffer_size,
1873 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001874 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001875 TEST_ASSERT( psa_cipher_finish( &operation,
1876 output + function_output_length,
1877 output_buffer_size,
1878 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001879 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001880 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1881
Gilles Peskine4abf7412018-06-18 16:35:34 +02001882 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001883 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001884 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001885
1886exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001887 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001888 psa_destroy_key( key_slot );
1889 mbedtls_psa_crypto_free( );
1890}
1891/* END_CASE */
1892
1893/* BEGIN_CASE */
1894void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001895 data_t *key,
1896 data_t *input,
1897 int first_part_size,
1898 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001899{
1900 int key_slot = 1;
1901
1902 psa_key_type_t key_type = key_type_arg;
1903 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001904 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001905 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001906 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001907 size_t output_buffer_size = 0;
1908 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001909 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001910 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001911 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001912
Gilles Peskine50e586b2018-06-08 14:28:46 +02001913 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001914 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001915 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001916 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1917 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1918 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001919
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001920 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1921 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001922
1923 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1924
Moran Pekered346952018-07-05 15:22:45 +03001925 psa_key_policy_init( &policy );
1926 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1927 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1928
Gilles Peskine50e586b2018-06-08 14:28:46 +02001929 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001930 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001931
Gilles Peskinefe119512018-07-08 21:39:34 +02001932 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1933 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001934
Gilles Peskinefe119512018-07-08 21:39:34 +02001935 TEST_ASSERT( psa_cipher_set_iv( &operation,
1936 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001937
mohammad16033d91abe2018-07-03 13:15:54 +03001938 output_buffer_size = (size_t) input->len +
1939 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001940 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001941 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001942
Gilles Peskine4abf7412018-06-18 16:35:34 +02001943 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1944 TEST_ASSERT( psa_cipher_update( &operation,
1945 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001946 output, output_buffer_size,
1947 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001948 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001949 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001950 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001951 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001952 output, output_buffer_size,
1953 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001954 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001955 TEST_ASSERT( psa_cipher_finish( &operation,
1956 output + function_output_length,
1957 output_buffer_size,
1958 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001959 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001960 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1961
Gilles Peskine4abf7412018-06-18 16:35:34 +02001962 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001963 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001964 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001965
1966exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001967 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001968 psa_destroy_key( key_slot );
1969 mbedtls_psa_crypto_free( );
1970}
1971/* END_CASE */
1972
Gilles Peskine50e586b2018-06-08 14:28:46 +02001973/* BEGIN_CASE */
1974void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001975 data_t *key,
1976 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001977 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001978{
1979 int key_slot = 1;
1980 psa_status_t status;
1981 psa_key_type_t key_type = key_type_arg;
1982 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001983 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001984 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001985 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001986 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001987 size_t output_buffer_size = 0;
1988 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001989 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001990 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001991 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001992
Gilles Peskine50e586b2018-06-08 14:28:46 +02001993 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001994 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001995 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001996 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1997 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1998 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001999
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002000 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2001 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002002
2003 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2004
Moran Pekered346952018-07-05 15:22:45 +03002005 psa_key_policy_init( &policy );
2006 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2007 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2008
Gilles Peskine50e586b2018-06-08 14:28:46 +02002009 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002010 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002011
Gilles Peskinefe119512018-07-08 21:39:34 +02002012 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2013 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002014
Gilles Peskinefe119512018-07-08 21:39:34 +02002015 TEST_ASSERT( psa_cipher_set_iv( &operation,
2016 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002017
mohammad16033d91abe2018-07-03 13:15:54 +03002018 output_buffer_size = (size_t) input->len +
2019 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002020 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002021 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002022
Gilles Peskine4abf7412018-06-18 16:35:34 +02002023 TEST_ASSERT( psa_cipher_update( &operation,
2024 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002025 output, output_buffer_size,
2026 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002027 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002028 status = psa_cipher_finish( &operation,
2029 output + function_output_length,
2030 output_buffer_size,
2031 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002032 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002033 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034
2035 if( expected_status == PSA_SUCCESS )
2036 {
2037 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002038 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002039 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002040 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002041 }
2042
Gilles Peskine50e586b2018-06-08 14:28:46 +02002043exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002044 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002045 psa_destroy_key( key_slot );
2046 mbedtls_psa_crypto_free( );
2047}
2048/* END_CASE */
2049
Gilles Peskine50e586b2018-06-08 14:28:46 +02002050/* BEGIN_CASE */
2051void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002052 data_t *key,
2053 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002054{
2055 int key_slot = 1;
2056 psa_key_type_t key_type = key_type_arg;
2057 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002058 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002059 size_t iv_size = 16;
2060 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002061 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002062 size_t output1_size = 0;
2063 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002064 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002065 size_t output2_size = 0;
2066 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002067 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002068 psa_cipher_operation_t operation1;
2069 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002070 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002071
mohammad1603d7d7ba52018-03-12 18:51:53 +02002072 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002073 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002074 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2075 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002076
mohammad1603d7d7ba52018-03-12 18:51:53 +02002077 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2078
Moran Pekered346952018-07-05 15:22:45 +03002079 psa_key_policy_init( &policy );
2080 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2081 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2082
mohammad1603d7d7ba52018-03-12 18:51:53 +02002083 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002084 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002085
Gilles Peskinefe119512018-07-08 21:39:34 +02002086 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2087 key_slot, alg ) == PSA_SUCCESS );
2088 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2089 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002090
Gilles Peskinefe119512018-07-08 21:39:34 +02002091 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2092 iv, iv_size,
2093 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002094 output1_size = (size_t) input->len +
2095 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002096 output1 = mbedtls_calloc( 1, output1_size );
2097 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002098
Gilles Peskine4abf7412018-06-18 16:35:34 +02002099 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002100 output1, output1_size,
2101 &output1_length ) == PSA_SUCCESS );
2102 TEST_ASSERT( psa_cipher_finish( &operation1,
2103 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002104 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002105
Gilles Peskine048b7f02018-06-08 14:20:49 +02002106 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002107
2108 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2109
2110 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002111 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002112 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002113
Gilles Peskinefe119512018-07-08 21:39:34 +02002114 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2115 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002116 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2117 output2, output2_size,
2118 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002119 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002120 TEST_ASSERT( psa_cipher_finish( &operation2,
2121 output2 + output2_length,
2122 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002123 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002124
Gilles Peskine048b7f02018-06-08 14:20:49 +02002125 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002126
Janos Follath25c4fa82018-07-06 16:23:25 +01002127 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002128
Gilles Peskine4abf7412018-06-18 16:35:34 +02002129 TEST_ASSERT( input->len == output2_length );
2130 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03002131
2132exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002133 mbedtls_free( output1 );
2134 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002135 psa_destroy_key( key_slot );
2136 mbedtls_psa_crypto_free( );
2137}
2138/* END_CASE */
2139
2140/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002141void cipher_verify_output_multipart( int alg_arg,
2142 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002143 data_t *key,
2144 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002145 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002146{
2147 int key_slot = 1;
2148 psa_key_type_t key_type = key_type_arg;
2149 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002150 unsigned char iv[16] = {0};
2151 size_t iv_size = 16;
2152 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002153 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002154 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002155 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002156 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002157 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002158 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002159 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002160 psa_cipher_operation_t operation1;
2161 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002162 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002163
Moran Pekerded84402018-06-06 16:36:50 +03002164 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002165 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002166 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2167 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002168
Moran Pekerded84402018-06-06 16:36:50 +03002169 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2170
Moran Pekered346952018-07-05 15:22:45 +03002171 psa_key_policy_init( &policy );
2172 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2173 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2174
Moran Pekerded84402018-06-06 16:36:50 +03002175 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002176 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002177
Gilles Peskinefe119512018-07-08 21:39:34 +02002178 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2179 key_slot, alg ) == PSA_SUCCESS );
2180 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2181 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002182
Gilles Peskinefe119512018-07-08 21:39:34 +02002183 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2184 iv, iv_size,
2185 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002186 output1_buffer_size = (size_t) input->len +
2187 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002188 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002189 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002190
Gilles Peskine4abf7412018-06-18 16:35:34 +02002191 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002192
itayzafrir3e02b3b2018-06-12 17:06:52 +03002193 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002194 output1, output1_buffer_size,
2195 &function_output_length ) == PSA_SUCCESS );
2196 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002197
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002198 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002199 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002200 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002201 output1, output1_buffer_size,
2202 &function_output_length ) == PSA_SUCCESS );
2203 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002204
Gilles Peskine048b7f02018-06-08 14:20:49 +02002205 TEST_ASSERT( psa_cipher_finish( &operation1,
2206 output1 + output1_length,
2207 output1_buffer_size - output1_length,
2208 &function_output_length ) == PSA_SUCCESS );
2209 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002210
2211 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2212
Gilles Peskine048b7f02018-06-08 14:20:49 +02002213 output2_buffer_size = output1_length;
2214 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002215 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002216
Gilles Peskinefe119512018-07-08 21:39:34 +02002217 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2218 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002219
2220 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002221 output2, output2_buffer_size,
2222 &function_output_length ) == PSA_SUCCESS );
2223 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002224
Gilles Peskine048b7f02018-06-08 14:20:49 +02002225 TEST_ASSERT( psa_cipher_update( &operation2,
2226 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002227 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002228 output2, output2_buffer_size,
2229 &function_output_length ) == PSA_SUCCESS );
2230 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002231
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002232 TEST_ASSERT( psa_cipher_finish( &operation2,
2233 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002234 output2_buffer_size - output2_length,
2235 &function_output_length ) == PSA_SUCCESS );
2236 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002237
Janos Follath25c4fa82018-07-06 16:23:25 +01002238 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002239
Gilles Peskine4abf7412018-06-18 16:35:34 +02002240 TEST_ASSERT( input->len == output2_length );
2241 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002242
2243exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002244 mbedtls_free( output1 );
2245 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002246 psa_destroy_key( key_slot );
2247 mbedtls_psa_crypto_free( );
2248}
2249/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002250
Gilles Peskine20035e32018-02-03 22:44:14 +01002251/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002252void aead_encrypt_decrypt( int key_type_arg,
2253 data_t * key_data,
2254 int alg_arg,
2255 data_t * input_data,
2256 data_t * nonce,
2257 data_t * additional_data,
2258 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002259{
2260 int slot = 1;
2261 psa_key_type_t key_type = key_type_arg;
2262 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002263 unsigned char *output_data = NULL;
2264 size_t output_size = 0;
2265 size_t output_length = 0;
2266 unsigned char *output_data2 = NULL;
2267 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002268 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002269 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002270 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002271
Gilles Peskinea1cac842018-06-11 19:33:02 +02002272 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002273 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002274 TEST_ASSERT( nonce != NULL );
2275 TEST_ASSERT( additional_data != NULL );
2276 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2277 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2278 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2279 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2280
Gilles Peskine4abf7412018-06-18 16:35:34 +02002281 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002282 output_data = mbedtls_calloc( 1, output_size );
2283 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002284
2285 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2286
2287 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002288 psa_key_policy_set_usage( &policy,
2289 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2290 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002291 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2292
2293 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002294 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002295
2296 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002297 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002298 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002299 additional_data->len,
2300 input_data->x, input_data->len,
2301 output_data, output_size,
2302 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002303
2304 if( PSA_SUCCESS == expected_result )
2305 {
2306 output_data2 = mbedtls_calloc( 1, output_length );
2307 TEST_ASSERT( output_data2 != NULL );
2308
2309 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002310 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002311 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002312 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002313 output_data, output_length,
2314 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002315 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002316
itayzafrir3e02b3b2018-06-12 17:06:52 +03002317 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002318 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002319 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002320
Gilles Peskinea1cac842018-06-11 19:33:02 +02002321exit:
2322 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002323 mbedtls_free( output_data );
2324 mbedtls_free( output_data2 );
2325 mbedtls_psa_crypto_free( );
2326}
2327/* END_CASE */
2328
2329/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002330void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002331 int alg_arg, data_t * input_data,
2332 data_t * additional_data, data_t * nonce,
2333 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002334{
2335 int slot = 1;
2336 psa_key_type_t key_type = key_type_arg;
2337 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002338 unsigned char *output_data = NULL;
2339 size_t output_size = 0;
2340 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002341 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002342 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002343
Gilles Peskinea1cac842018-06-11 19:33:02 +02002344 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002345 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002346 TEST_ASSERT( additional_data != NULL );
2347 TEST_ASSERT( nonce != NULL );
2348 TEST_ASSERT( expected_result != NULL );
2349 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2350 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2351 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2352 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2353 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2354
Gilles Peskine4abf7412018-06-18 16:35:34 +02002355 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002356 output_data = mbedtls_calloc( 1, output_size );
2357 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002358
Gilles Peskinea1cac842018-06-11 19:33:02 +02002359 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2360
2361 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002362 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002363 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2364
2365 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002366 key_data->x,
2367 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002368
2369 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002370 nonce->x, nonce->len,
2371 additional_data->x, additional_data->len,
2372 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002373 output_data, output_size,
2374 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002375
itayzafrir3e02b3b2018-06-12 17:06:52 +03002376 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002377 output_length ) == 0 );
2378
Gilles Peskinea1cac842018-06-11 19:33:02 +02002379exit:
2380 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002381 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002382 mbedtls_psa_crypto_free( );
2383}
2384/* END_CASE */
2385
2386/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002387void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002388 int alg_arg, data_t * input_data,
2389 data_t * additional_data, data_t * nonce,
2390 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002391{
2392 int slot = 1;
2393 psa_key_type_t key_type = key_type_arg;
2394 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002395 unsigned char *output_data = NULL;
2396 size_t output_size = 0;
2397 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002398 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002399 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002400 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002401
Gilles Peskinea1cac842018-06-11 19:33:02 +02002402 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002403 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002404 TEST_ASSERT( additional_data != NULL );
2405 TEST_ASSERT( nonce != NULL );
2406 TEST_ASSERT( expected_data != NULL );
2407 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2408 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2409 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2410 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2411 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2412
Gilles Peskine4abf7412018-06-18 16:35:34 +02002413 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002414 output_data = mbedtls_calloc( 1, output_size );
2415 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002416
Gilles Peskinea1cac842018-06-11 19:33:02 +02002417 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2418
2419 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002420 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002421 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2422
2423 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002424 key_data->x,
2425 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002426
2427 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002428 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002429 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002430 additional_data->len,
2431 input_data->x, input_data->len,
2432 output_data, output_size,
2433 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002434
Gilles Peskine2d277862018-06-18 15:41:12 +02002435 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002436 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002437 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002438 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002439 }
2440
Gilles Peskinea1cac842018-06-11 19:33:02 +02002441exit:
2442 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002443 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002444 mbedtls_psa_crypto_free( );
2445}
2446/* END_CASE */
2447
2448/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002449void signature_size( int type_arg,
2450 int bits,
2451 int alg_arg,
2452 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002453{
2454 psa_key_type_t type = type_arg;
2455 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002456 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002457 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2458exit:
2459 ;
2460}
2461/* END_CASE */
2462
2463/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002464void sign_deterministic( int key_type_arg, data_t *key_data,
2465 int alg_arg, data_t *input_data,
2466 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002467{
2468 int slot = 1;
2469 psa_key_type_t key_type = key_type_arg;
2470 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002471 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002472 unsigned char *signature = NULL;
2473 size_t signature_size;
2474 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002475 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002476
Gilles Peskine20035e32018-02-03 22:44:14 +01002477 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002478 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002479 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002480 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2481 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2482 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002483
2484 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2485
mohammad1603a97cb8c2018-03-28 03:46:26 -07002486 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002487 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002488 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2489
Gilles Peskine20035e32018-02-03 22:44:14 +01002490 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002491 key_data->x,
2492 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002493 TEST_ASSERT( psa_get_key_information( slot,
2494 NULL,
2495 &key_bits ) == PSA_SUCCESS );
2496
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002497 /* Allocate a buffer which has the size advertized by the
2498 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002499 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2500 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002501 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002502 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002503 signature = mbedtls_calloc( 1, signature_size );
2504 TEST_ASSERT( signature != NULL );
2505
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002506 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002507 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002508 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002509 signature, signature_size,
2510 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002511 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002512 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002513 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002514 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002515
2516exit:
2517 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002518 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002519 mbedtls_psa_crypto_free( );
2520}
2521/* END_CASE */
2522
2523/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002524void sign_fail( int key_type_arg, data_t *key_data,
2525 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002526 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002527{
2528 int slot = 1;
2529 psa_key_type_t key_type = key_type_arg;
2530 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002531 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002532 psa_status_t actual_status;
2533 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002534 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002535 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002536 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002537
Gilles Peskine20035e32018-02-03 22:44:14 +01002538 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002540 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2541 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2542
Gilles Peskine20035e32018-02-03 22:44:14 +01002543 signature = mbedtls_calloc( 1, signature_size );
2544 TEST_ASSERT( signature != NULL );
2545
2546 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2547
mohammad1603a97cb8c2018-03-28 03:46:26 -07002548 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002549 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002550 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2551
Gilles Peskine20035e32018-02-03 22:44:14 +01002552 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002553 key_data->x,
2554 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002555
2556 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002557 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002558 signature, signature_size,
2559 &signature_length );
2560 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002561 /* The value of *signature_length is unspecified on error, but
2562 * whatever it is, it should be less than signature_size, so that
2563 * if the caller tries to read *signature_length bytes without
2564 * checking the error code then they don't overflow a buffer. */
2565 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002566
2567exit:
2568 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002569 mbedtls_free( signature );
2570 mbedtls_psa_crypto_free( );
2571}
2572/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002573
2574/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002575void sign_verify( int key_type_arg, data_t *key_data,
2576 int alg_arg, data_t *input_data )
2577{
2578 int slot = 1;
2579 psa_key_type_t key_type = key_type_arg;
2580 psa_algorithm_t alg = alg_arg;
2581 size_t key_bits;
2582 unsigned char *signature = NULL;
2583 size_t signature_size;
2584 size_t signature_length = 0xdeadbeef;
2585 psa_key_policy_t policy;
2586
2587 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2588
2589 psa_key_policy_init( &policy );
2590 psa_key_policy_set_usage( &policy,
2591 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2592 alg );
2593 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2594
2595 TEST_ASSERT( psa_import_key( slot, key_type,
2596 key_data->x,
2597 key_data->len ) == PSA_SUCCESS );
2598 TEST_ASSERT( psa_get_key_information( slot,
2599 NULL,
2600 &key_bits ) == PSA_SUCCESS );
2601
2602 /* Allocate a buffer which has the size advertized by the
2603 * library. */
2604 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2605 key_bits, alg );
2606 TEST_ASSERT( signature_size != 0 );
2607 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2608 signature = mbedtls_calloc( 1, signature_size );
2609 TEST_ASSERT( signature != NULL );
2610
2611 /* Perform the signature. */
2612 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2613 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002614 signature, signature_size,
2615 &signature_length ) == PSA_SUCCESS );
2616 /* Check that the signature length looks sensible. */
2617 TEST_ASSERT( signature_length <= signature_size );
2618 TEST_ASSERT( signature_length > 0 );
2619
2620 /* Use the library to verify that the signature is correct. */
2621 TEST_ASSERT( psa_asymmetric_verify(
2622 slot, alg,
2623 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002624 signature, signature_length ) == PSA_SUCCESS );
2625
2626 if( input_data->len != 0 )
2627 {
2628 /* Flip a bit in the input and verify that the signature is now
2629 * detected as invalid. Flip a bit at the beginning, not at the end,
2630 * because ECDSA may ignore the last few bits of the input. */
2631 input_data->x[0] ^= 1;
2632 TEST_ASSERT( psa_asymmetric_verify(
2633 slot, alg,
2634 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002635 signature,
2636 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2637 }
2638
2639exit:
2640 psa_destroy_key( slot );
2641 mbedtls_free( signature );
2642 mbedtls_psa_crypto_free( );
2643}
2644/* END_CASE */
2645
2646/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002647void asymmetric_verify( int key_type_arg, data_t *key_data,
2648 int alg_arg, data_t *hash_data,
2649 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002650{
2651 int slot = 1;
2652 psa_key_type_t key_type = key_type_arg;
2653 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002654 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002655
Gilles Peskine69c12672018-06-28 00:07:19 +02002656 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2657
itayzafrir5c753392018-05-08 11:18:38 +03002658 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002659 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002660 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002661 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2662 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2663 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002664
2665 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2666
2667 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002668 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002669 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2670
2671 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002672 key_data->x,
2673 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002674
2675 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002676 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002677 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002678 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002679exit:
2680 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002681 mbedtls_psa_crypto_free( );
2682}
2683/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002684
2685/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002686void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2687 int alg_arg, data_t *hash_data,
2688 data_t *signature_data,
2689 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002690{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002691 int slot = 1;
2692 psa_key_type_t key_type = key_type_arg;
2693 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002694 psa_status_t actual_status;
2695 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002696 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002697
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002698 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002699 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002700 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002701 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2702 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2703 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002704
2705 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2706
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002707 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002708 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002709 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2710
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002711 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002712 key_data->x,
2713 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714
2715 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002716 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002717 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002718 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002719
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002720 TEST_ASSERT( actual_status == expected_status );
2721
2722exit:
2723 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002724 mbedtls_psa_crypto_free( );
2725}
2726/* END_CASE */
2727
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002728/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002729void asymmetric_encrypt( int key_type_arg,
2730 data_t *key_data,
2731 int alg_arg,
2732 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002733 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002734 int expected_output_length_arg,
2735 int expected_status_arg )
2736{
2737 int slot = 1;
2738 psa_key_type_t key_type = key_type_arg;
2739 psa_algorithm_t alg = alg_arg;
2740 size_t expected_output_length = expected_output_length_arg;
2741 size_t key_bits;
2742 unsigned char *output = NULL;
2743 size_t output_size;
2744 size_t output_length = ~0;
2745 psa_status_t actual_status;
2746 psa_status_t expected_status = expected_status_arg;
2747 psa_key_policy_t policy;
2748
2749 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2750
2751 /* Import the key */
2752 psa_key_policy_init( &policy );
2753 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2754 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2755 TEST_ASSERT( psa_import_key( slot, key_type,
2756 key_data->x,
2757 key_data->len ) == PSA_SUCCESS );
2758
2759 /* Determine the maximum output length */
2760 TEST_ASSERT( psa_get_key_information( slot,
2761 NULL,
2762 &key_bits ) == PSA_SUCCESS );
2763 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2764 output = mbedtls_calloc( 1, output_size );
Darryl Green9c862252018-07-24 12:52:44 +01002765 TEST_ASSERT( output_size == 0 || output != NULL );
Gilles Peskine656896e2018-06-29 19:12:28 +02002766
2767 /* Encrypt the input */
2768 actual_status = psa_asymmetric_encrypt( slot, alg,
2769 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002770 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002771 output, output_size,
2772 &output_length );
2773 TEST_ASSERT( actual_status == expected_status );
2774 TEST_ASSERT( output_length == expected_output_length );
2775
Gilles Peskine68428122018-06-30 18:42:41 +02002776 /* If the label is empty, the test framework puts a non-null pointer
2777 * in label->x. Test that a null pointer works as well. */
2778 if( label->len == 0 )
2779 {
2780 output_length = ~0;
2781 memset( output, 0, output_size );
2782 actual_status = psa_asymmetric_encrypt( slot, alg,
2783 input_data->x, input_data->len,
2784 NULL, label->len,
2785 output, output_size,
2786 &output_length );
2787 TEST_ASSERT( actual_status == expected_status );
2788 TEST_ASSERT( output_length == expected_output_length );
2789 }
2790
Gilles Peskine656896e2018-06-29 19:12:28 +02002791exit:
2792 psa_destroy_key( slot );
2793 mbedtls_free( output );
2794 mbedtls_psa_crypto_free( );
2795}
2796/* END_CASE */
2797
2798/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002799void asymmetric_encrypt_decrypt( int key_type_arg,
2800 data_t *key_data,
2801 int alg_arg,
2802 data_t *input_data,
2803 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002804{
2805 int slot = 1;
2806 psa_key_type_t key_type = key_type_arg;
2807 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002808 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002809 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002810 size_t output_size;
2811 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002812 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002813 size_t output2_size;
2814 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002815 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002816
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002817 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002818 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002819 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2820 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2821
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002822 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2823
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002824 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002825 psa_key_policy_set_usage( &policy,
2826 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002827 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002828 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2829
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002830 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002831 key_data->x,
2832 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002833
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002834
2835 /* Determine the maximum ciphertext length */
2836 TEST_ASSERT( psa_get_key_information( slot,
2837 NULL,
2838 &key_bits ) == PSA_SUCCESS );
2839 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2840 output = mbedtls_calloc( 1, output_size );
2841 TEST_ASSERT( output != NULL );
2842 output2_size = input_data->len;
2843 output2 = mbedtls_calloc( 1, output2_size );
2844 TEST_ASSERT( output2 != NULL );
2845
Gilles Peskineeebd7382018-06-08 18:11:54 +02002846 /* We test encryption by checking that encrypt-then-decrypt gives back
2847 * the original plaintext because of the non-optional random
2848 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002849 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002850 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002851 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002852 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002853 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002854 /* We don't know what ciphertext length to expect, but check that
2855 * it looks sensible. */
2856 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002857
Gilles Peskine2d277862018-06-18 15:41:12 +02002858 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002859 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002860 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002861 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002862 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002863 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002864 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002865 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002866
2867exit:
2868 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002869 mbedtls_free( output );
2870 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002871 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002872}
2873/* END_CASE */
2874
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002875/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002876void asymmetric_decrypt( int key_type_arg,
2877 data_t *key_data,
2878 int alg_arg,
2879 data_t *input_data,
2880 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002881 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002882{
2883 int slot = 1;
2884 psa_key_type_t key_type = key_type_arg;
2885 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002886 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002887 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002888 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002889 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002890
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002891 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002892 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002894 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2895 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2896 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2897
Gilles Peskine4abf7412018-06-18 16:35:34 +02002898 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002899 output = mbedtls_calloc( 1, output_size );
2900 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002901
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002902 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2903
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002904 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002905 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002906 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2907
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002908 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002909 key_data->x,
2910 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002911
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002912 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002913 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002914 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002915 output,
2916 output_size,
2917 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002918 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002919 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002920
Gilles Peskine68428122018-06-30 18:42:41 +02002921 /* If the label is empty, the test framework puts a non-null pointer
2922 * in label->x. Test that a null pointer works as well. */
2923 if( label->len == 0 )
2924 {
2925 output_length = ~0;
2926 memset( output, 0, output_size );
2927 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2928 input_data->x, input_data->len,
2929 NULL, label->len,
2930 output,
2931 output_size,
2932 &output_length ) == PSA_SUCCESS );
2933 TEST_ASSERT( expected_data->len == output_length );
2934 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2935 }
2936
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002937exit:
2938 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002939 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002940 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002941}
2942/* END_CASE */
2943
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002944/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002945void asymmetric_decrypt_fail( int key_type_arg,
2946 data_t *key_data,
2947 int alg_arg,
2948 data_t *input_data,
2949 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002950 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002951{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002952 int slot = 1;
2953 psa_key_type_t key_type = key_type_arg;
2954 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002956 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002957 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002958 psa_status_t actual_status;
2959 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002960 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002964 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2965 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2966
Gilles Peskine4abf7412018-06-18 16:35:34 +02002967 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002968 output = mbedtls_calloc( 1, output_size );
2969 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002970
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002971 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2972
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002973 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002974 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002975 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2976
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002977 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002978 key_data->x,
2979 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002980
Gilles Peskine2d277862018-06-18 15:41:12 +02002981 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002982 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002983 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002984 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002985 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002986 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002987 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988
Gilles Peskine68428122018-06-30 18:42:41 +02002989 /* If the label is empty, the test framework puts a non-null pointer
2990 * in label->x. Test that a null pointer works as well. */
2991 if( label->len == 0 )
2992 {
2993 output_length = ~0;
2994 memset( output, 0, output_size );
2995 actual_status = psa_asymmetric_decrypt( slot, alg,
2996 input_data->x, input_data->len,
2997 NULL, label->len,
2998 output, output_size,
2999 &output_length );
3000 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003001 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003002 }
3003
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003004exit:
3005 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003006 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003007 mbedtls_psa_crypto_free( );
3008}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003009/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003010
3011/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003012void derive_setup( int key_type_arg,
3013 data_t *key_data,
3014 int alg_arg,
3015 data_t *salt,
3016 data_t *label,
3017 int requested_capacity_arg,
3018 int expected_status_arg )
3019{
3020 psa_key_slot_t slot = 1;
3021 size_t key_type = key_type_arg;
3022 psa_algorithm_t alg = alg_arg;
3023 size_t requested_capacity = requested_capacity_arg;
3024 psa_status_t expected_status = expected_status_arg;
3025 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3026 psa_key_policy_t policy;
3027
3028 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3029
3030 psa_key_policy_init( &policy );
3031 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3032 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3033
3034 TEST_ASSERT( psa_import_key( slot, key_type,
3035 key_data->x,
3036 key_data->len ) == PSA_SUCCESS );
3037
3038 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3039 salt->x, salt->len,
3040 label->x, label->len,
3041 requested_capacity ) == expected_status );
3042
3043exit:
3044 psa_generator_abort( &generator );
3045 psa_destroy_key( slot );
3046 mbedtls_psa_crypto_free( );
3047}
3048/* END_CASE */
3049
3050/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003051void derive_output( int alg_arg,
3052 data_t *key_data,
3053 data_t *salt,
3054 data_t *label,
3055 int requested_capacity_arg,
3056 data_t *expected_output1,
3057 data_t *expected_output2 )
3058{
3059 psa_key_slot_t slot = 1;
3060 psa_algorithm_t alg = alg_arg;
3061 size_t requested_capacity = requested_capacity_arg;
3062 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3063 uint8_t *expected_outputs[2] =
3064 {expected_output1->x, expected_output2->x};
3065 size_t output_sizes[2] =
3066 {expected_output1->len, expected_output2->len};
3067 size_t output_buffer_size = 0;
3068 uint8_t *output_buffer = NULL;
3069 size_t expected_capacity;
3070 size_t current_capacity;
3071 psa_key_policy_t policy;
3072 psa_status_t status;
3073 unsigned i;
3074
3075 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3076 {
3077 if( output_sizes[i] > output_buffer_size )
3078 output_buffer_size = output_sizes[i];
3079 if( output_sizes[i] == 0 )
3080 expected_outputs[i] = NULL;
3081 }
3082 output_buffer = mbedtls_calloc( 1, output_buffer_size );
3083 TEST_ASSERT( output_buffer != NULL );
3084 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3085
3086 psa_key_policy_init( &policy );
3087 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3088 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3089
3090 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3091 key_data->x,
3092 key_data->len ) == PSA_SUCCESS );
3093
3094 /* Extraction phase. */
3095 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3096 salt->x, salt->len,
3097 label->x, label->len,
3098 requested_capacity ) == PSA_SUCCESS );
3099 TEST_ASSERT( psa_get_generator_capacity( &generator,
3100 &current_capacity ) ==
3101 PSA_SUCCESS );
3102 TEST_ASSERT( current_capacity == requested_capacity );
3103 expected_capacity = requested_capacity;
3104
3105 /* Expansion phase. */
3106 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3107 {
3108 /* Read some bytes. */
3109 status = psa_generator_read( &generator,
3110 output_buffer, output_sizes[i] );
3111 if( expected_capacity == 0 && output_sizes[i] == 0 )
3112 {
3113 /* Reading 0 bytes when 0 bytes are available can go either way. */
3114 TEST_ASSERT( status == PSA_SUCCESS ||
3115 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3116 continue;
3117 }
3118 else if( expected_capacity == 0 ||
3119 output_sizes[i] > expected_capacity )
3120 {
3121 /* Capacity exceeded. */
3122 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3123 expected_capacity = 0;
3124 continue;
3125 }
3126 /* Success. Check the read data. */
3127 TEST_ASSERT( status == PSA_SUCCESS );
3128 if( output_sizes[i] != 0 )
3129 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3130 output_sizes[i] ) == 0 );
3131 /* Check the generator status. */
3132 expected_capacity -= output_sizes[i];
3133 TEST_ASSERT( psa_get_generator_capacity( &generator,
3134 &current_capacity ) ==
3135 PSA_SUCCESS );
3136 TEST_ASSERT( expected_capacity == current_capacity );
3137 }
3138 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3139
3140exit:
3141 mbedtls_free( output_buffer );
3142 psa_generator_abort( &generator );
3143 psa_destroy_key( slot );
3144 mbedtls_psa_crypto_free( );
3145}
3146/* END_CASE */
3147
3148/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003149void derive_full( int alg_arg,
3150 data_t *key_data,
3151 data_t *salt,
3152 data_t *label,
3153 int requested_capacity_arg )
3154{
3155 psa_key_slot_t slot = 1;
3156 psa_algorithm_t alg = alg_arg;
3157 size_t requested_capacity = requested_capacity_arg;
3158 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3159 unsigned char output_buffer[16];
3160 size_t expected_capacity = requested_capacity;
3161 size_t current_capacity;
3162 psa_key_policy_t policy;
3163
3164 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3165
3166 psa_key_policy_init( &policy );
3167 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3168 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3169
3170 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3171 key_data->x,
3172 key_data->len ) == PSA_SUCCESS );
3173
3174 /* Extraction phase. */
3175 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3176 salt->x, salt->len,
3177 label->x, label->len,
3178 requested_capacity ) == PSA_SUCCESS );
3179 TEST_ASSERT( psa_get_generator_capacity( &generator,
3180 &current_capacity ) ==
3181 PSA_SUCCESS );
3182 TEST_ASSERT( current_capacity == expected_capacity );
3183
3184 /* Expansion phase. */
3185 while( current_capacity > 0 )
3186 {
3187 size_t read_size = sizeof( output_buffer );
3188 if( read_size > current_capacity )
3189 read_size = current_capacity;
3190 TEST_ASSERT( psa_generator_read( &generator,
3191 output_buffer,
3192 read_size ) == PSA_SUCCESS );
3193 expected_capacity -= read_size;
3194 TEST_ASSERT( psa_get_generator_capacity( &generator,
3195 &current_capacity ) ==
3196 PSA_SUCCESS );
3197 TEST_ASSERT( current_capacity == expected_capacity );
3198 }
3199
3200 /* Check that the generator refuses to go over capacity. */
3201 TEST_ASSERT( psa_generator_read( &generator,
3202 output_buffer,
3203 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3204
3205 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3206
3207exit:
3208 psa_generator_abort( &generator );
3209 psa_destroy_key( slot );
3210 mbedtls_psa_crypto_free( );
3211}
3212/* END_CASE */
3213
3214/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003215void derive_key_exercise( int alg_arg,
3216 data_t *key_data,
3217 data_t *salt,
3218 data_t *label,
3219 int derived_type_arg,
3220 int derived_bits_arg,
3221 int derived_usage_arg,
3222 int derived_alg_arg )
3223{
3224 psa_key_slot_t base_key = 1;
3225 psa_key_slot_t derived_key = 2;
3226 psa_algorithm_t alg = alg_arg;
3227 psa_key_type_t derived_type = derived_type_arg;
3228 size_t derived_bits = derived_bits_arg;
3229 psa_key_usage_t derived_usage = derived_usage_arg;
3230 psa_algorithm_t derived_alg = derived_alg_arg;
3231 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3232 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3233 psa_key_policy_t policy;
3234 psa_key_type_t got_type;
3235 size_t got_bits;
3236
3237 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3238
3239 psa_key_policy_init( &policy );
3240 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3241 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3242 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3243 key_data->x,
3244 key_data->len ) == PSA_SUCCESS );
3245
3246 /* Derive a key. */
3247 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3248 salt->x, salt->len,
3249 label->x, label->len,
3250 capacity ) == PSA_SUCCESS );
3251 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3252 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3253 TEST_ASSERT( psa_generator_import_key( derived_key,
3254 derived_type,
3255 derived_bits,
3256 &generator ) == PSA_SUCCESS );
3257
3258 /* Test the key information */
3259 TEST_ASSERT( psa_get_key_information( derived_key,
3260 &got_type,
3261 &got_bits ) == PSA_SUCCESS );
3262 TEST_ASSERT( got_type == derived_type );
3263 TEST_ASSERT( got_bits == derived_bits );
3264
3265 /* Exercise the derived key. */
3266 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3267 goto exit;
3268
3269exit:
3270 psa_generator_abort( &generator );
3271 psa_destroy_key( base_key );
3272 psa_destroy_key( derived_key );
3273 mbedtls_psa_crypto_free( );
3274}
3275/* END_CASE */
3276
3277/* BEGIN_CASE */
3278void derive_key_export( int alg_arg,
3279 data_t *key_data,
3280 data_t *salt,
3281 data_t *label,
3282 int bytes1_arg,
3283 int bytes2_arg )
3284{
3285 psa_key_slot_t base_key = 1;
3286 psa_key_slot_t derived_key = 2;
3287 psa_algorithm_t alg = alg_arg;
3288 size_t bytes1 = bytes1_arg;
3289 size_t bytes2 = bytes2_arg;
3290 size_t capacity = bytes1 + bytes2;
3291 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3292 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
3293 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
3294 psa_key_policy_t policy;
3295 size_t length;
3296
3297 TEST_ASSERT( output_buffer != NULL );
3298 TEST_ASSERT( export_buffer != NULL );
3299 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3300
3301 psa_key_policy_init( &policy );
3302 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3303 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3304 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3305 key_data->x,
3306 key_data->len ) == PSA_SUCCESS );
3307
3308 /* Derive some material and output it. */
3309 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3310 salt->x, salt->len,
3311 label->x, label->len,
3312 capacity ) == PSA_SUCCESS );
3313 TEST_ASSERT( psa_generator_read( &generator,
3314 output_buffer,
3315 capacity ) == PSA_SUCCESS );
3316 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3317
3318 /* Derive the same output again, but this time store it in key objects. */
3319 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3320 salt->x, salt->len,
3321 label->x, label->len,
3322 capacity ) == PSA_SUCCESS );
3323 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3324 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3325 TEST_ASSERT( psa_generator_import_key( derived_key,
3326 PSA_KEY_TYPE_RAW_DATA,
3327 PSA_BYTES_TO_BITS( bytes1 ),
3328 &generator ) == PSA_SUCCESS );
3329 TEST_ASSERT( psa_export_key( derived_key,
3330 export_buffer, bytes1,
3331 &length ) == PSA_SUCCESS );
3332 TEST_ASSERT( length == bytes1 );
3333 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3334 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3335 TEST_ASSERT( psa_generator_import_key( derived_key,
3336 PSA_KEY_TYPE_RAW_DATA,
3337 PSA_BYTES_TO_BITS( bytes2 ),
3338 &generator ) == PSA_SUCCESS );
3339 TEST_ASSERT( psa_export_key( derived_key,
3340 export_buffer + bytes1, bytes2,
3341 &length ) == PSA_SUCCESS );
3342 TEST_ASSERT( length == bytes2 );
3343
3344 /* Compare the outputs from the two runs. */
3345 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3346
3347exit:
3348 mbedtls_free( output_buffer );
3349 mbedtls_free( export_buffer );
3350 psa_generator_abort( &generator );
3351 psa_destroy_key( base_key );
3352 psa_destroy_key( derived_key );
3353 mbedtls_psa_crypto_free( );
3354}
3355/* END_CASE */
3356
3357/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003358void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003359{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003360 size_t bytes = bytes_arg;
3361 const unsigned char trail[] = "don't overwrite me";
3362 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
3363 unsigned char *changed = mbedtls_calloc( 1, bytes );
3364 size_t i;
3365 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003366
Gilles Peskinea50d7392018-06-21 10:22:13 +02003367 TEST_ASSERT( output != NULL );
Darryl Green9c862252018-07-24 12:52:44 +01003368 TEST_ASSERT( bytes == 0 || changed != NULL );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003369 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003370
3371 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3372
Gilles Peskinea50d7392018-06-21 10:22:13 +02003373 /* Run several times, to ensure that every output byte will be
3374 * nonzero at least once with overwhelming probability
3375 * (2^(-8*number_of_runs)). */
3376 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003377 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003378 memset( output, 0, bytes );
3379 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3380
3381 /* Check that no more than bytes have been overwritten */
3382 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3383
3384 for( i = 0; i < bytes; i++ )
3385 {
3386 if( output[i] != 0 )
3387 ++changed[i];
3388 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003389 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003390
3391 /* Check that every byte was changed to nonzero at least once. This
3392 * validates that psa_generate_random is overwriting every byte of
3393 * the output buffer. */
3394 for( i = 0; i < bytes; i++ )
3395 {
3396 TEST_ASSERT( changed[i] != 0 );
3397 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003398
3399exit:
3400 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003401 mbedtls_free( output );
3402 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003403}
3404/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003405
3406/* BEGIN_CASE */
3407void generate_key( int type_arg,
3408 int bits_arg,
3409 int usage_arg,
3410 int alg_arg,
3411 int expected_status_arg )
3412{
3413 int slot = 1;
3414 psa_key_type_t type = type_arg;
3415 psa_key_usage_t usage = usage_arg;
3416 size_t bits = bits_arg;
3417 psa_algorithm_t alg = alg_arg;
3418 psa_status_t expected_status = expected_status_arg;
3419 psa_key_type_t got_type;
3420 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003421 psa_status_t expected_info_status =
3422 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3423 psa_key_policy_t policy;
3424
3425 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3426
3427 psa_key_policy_init( &policy );
3428 psa_key_policy_set_usage( &policy, usage, alg );
3429 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3430
3431 /* Generate a key */
3432 TEST_ASSERT( psa_generate_key( slot, type, bits,
3433 NULL, 0 ) == expected_status );
3434
3435 /* Test the key information */
3436 TEST_ASSERT( psa_get_key_information( slot,
3437 &got_type,
3438 &got_bits ) == expected_info_status );
3439 if( expected_info_status != PSA_SUCCESS )
3440 goto exit;
3441 TEST_ASSERT( got_type == type );
3442 TEST_ASSERT( got_bits == bits );
3443
Gilles Peskine818ca122018-06-20 18:16:48 +02003444 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003445 if( ! exercise_key( slot, usage, alg ) )
3446 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003447
3448exit:
3449 psa_destroy_key( slot );
3450 mbedtls_psa_crypto_free( );
3451}
3452/* END_CASE */