blob: 59cc7166d48fc1f8dcf5f24b0ee9dc44f431d720 [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 Peskinebd7dea92018-09-27 13:57:19 +0200422 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200423 return( 1 );
424
425exit:
426 return( 0 );
427}
428
429static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
430 size_t min_bits, size_t max_bits,
431 int must_be_odd )
432{
433 size_t len;
434 size_t actual_bits;
435 unsigned char msb;
436 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
437 MBEDTLS_ASN1_INTEGER ) == 0 );
438 /* Tolerate a slight departure from DER encoding:
439 * - 0 may be represented by an empty string or a 1-byte string.
440 * - The sign bit may be used as a value bit. */
441 if( ( len == 1 && ( *p )[0] == 0 ) ||
442 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
443 {
444 ++( *p );
445 --len;
446 }
447 if( min_bits == 0 && len == 0 )
448 return( 1 );
449 msb = ( *p )[0];
450 TEST_ASSERT( msb != 0 );
451 actual_bits = 8 * ( len - 1 );
452 while( msb != 0 )
453 {
454 msb >>= 1;
455 ++actual_bits;
456 }
457 TEST_ASSERT( actual_bits >= min_bits );
458 TEST_ASSERT( actual_bits <= max_bits );
459 if( must_be_odd )
460 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
461 *p += len;
462 return( 1 );
463exit:
464 return( 0 );
465}
466
467static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
468 size_t *len,
469 unsigned char n, unsigned char tag )
470{
471 int ret;
472 ret = mbedtls_asn1_get_tag( p, end, len,
473 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
474 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
475 if( ret != 0 )
476 return( ret );
477 end = *p + *len;
478 ret = mbedtls_asn1_get_tag( p, end, len, tag );
479 if( ret != 0 )
480 return( ret );
481 if( *p + *len != end )
482 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
483 return( 0 );
484}
485
486static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
487 uint8_t *exported, size_t exported_length )
488{
489 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200490 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 else
492 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200493
494#if defined(MBEDTLS_DES_C)
495 if( type == PSA_KEY_TYPE_DES )
496 {
497 /* Check the parity bits. */
498 unsigned i;
499 for( i = 0; i < bits / 8; i++ )
500 {
501 unsigned bit_count = 0;
502 unsigned m;
503 for( m = 1; m <= 0x100; m <<= 1 )
504 {
505 if( exported[i] & m )
506 ++bit_count;
507 }
508 TEST_ASSERT( bit_count % 2 != 0 );
509 }
510 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200511 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200512#endif
513
514#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
515 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
516 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200517 uint8_t *p = exported;
518 uint8_t *end = exported + exported_length;
519 size_t len;
520 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200521 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200522 * modulus INTEGER, -- n
523 * publicExponent INTEGER, -- e
524 * privateExponent INTEGER, -- d
525 * prime1 INTEGER, -- p
526 * prime2 INTEGER, -- q
527 * exponent1 INTEGER, -- d mod (p-1)
528 * exponent2 INTEGER, -- d mod (q-1)
529 * coefficient INTEGER, -- (inverse of q) mod p
530 * }
531 */
532 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
533 MBEDTLS_ASN1_SEQUENCE |
534 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
535 TEST_ASSERT( p + len == end );
536 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
537 goto exit;
538 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
539 goto exit;
540 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
541 goto exit;
542 /* Require d to be at least half the size of n. */
543 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
544 goto exit;
545 /* Require p and q to be at most half the size of n, rounded up. */
546 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
547 goto exit;
548 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
549 goto exit;
550 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
551 goto exit;
552 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
553 goto exit;
554 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
555 goto exit;
556 TEST_ASSERT( p == end );
557 }
558 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200559#endif /* MBEDTLS_RSA_C */
560
561#if defined(MBEDTLS_ECP_C)
562 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
563 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 uint8_t *p = exported;
565 uint8_t *end = exported + exported_length;
566 size_t len;
567 int version;
568 /* ECPrivateKey ::= SEQUENCE {
569 * version INTEGER, -- must be 1
570 * privateKey OCTET STRING,
571 * -- `ceiling(log_{256}(n))`-byte string, big endian,
572 * -- where n is the order of the curve.
573 * parameters ECParameters {{ NamedCurve }}, -- mandatory
574 * publicKey BIT STRING -- mandatory
575 * }
576 */
577 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
578 MBEDTLS_ASN1_SEQUENCE |
579 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
580 TEST_ASSERT( p + len == end );
581 TEST_ASSERT( mbedtls_asn1_get_int( &p, end, &version ) == 0 );
582 TEST_ASSERT( version == 1 );
583 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
584 MBEDTLS_ASN1_OCTET_STRING ) == 0 );
585 /* Bug in Mbed TLS: the length of the octet string depends on the value */
586 // TEST_ASSERT( len == PSA_BITS_TO_BYTES( bits ) );
587 p += len;
588 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 0,
589 MBEDTLS_ASN1_OID ) == 0 );
590 p += len;
Gilles Peskinec6290c02018-08-13 17:24:59 +0200591 /* publicKey: ECPoint in uncompressed representation (as below) */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200592 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 ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200652 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200653 * -- 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
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200694 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
695
696 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
697 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200698 {
699 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
700 PSA_ERROR_NOT_PERMITTED );
701 return( 1 );
702 }
703
Gilles Peskined14664a2018-08-10 19:07:32 +0200704 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200705 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200706
707 TEST_ASSERT( psa_export_key( slot,
708 exported, exported_size,
709 &exported_length ) == PSA_SUCCESS );
710 ok = exported_key_sanity_check( type, bits, exported, exported_length );
711
712exit:
713 mbedtls_free( exported );
714 return( ok );
715}
716
717static int exercise_export_public_key( psa_key_slot_t slot )
718{
719 psa_key_type_t type;
720 psa_key_type_t public_type;
721 size_t bits;
722 uint8_t *exported = NULL;
723 size_t exported_size = 0;
724 size_t exported_length = 0;
725 int ok = 0;
726
727 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
728 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
729 {
730 TEST_ASSERT( psa_export_public_key( slot,
731 NULL, 0, &exported_length ) ==
732 PSA_ERROR_INVALID_ARGUMENT );
733 return( 1 );
734 }
735
736 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
737 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200738 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200739
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 );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200856 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200857 }
858
859exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200860 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200861 mbedtls_psa_crypto_free( );
862}
863/* END_CASE */
864
865/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200866void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867{
868 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200869 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300873 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
875
Gilles Peskine4abf7412018-06-18 16:35:34 +0200876 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200877 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878 if( status == PSA_SUCCESS )
879 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
880
881exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882 mbedtls_psa_crypto_free( );
883}
884/* END_CASE */
885
886/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200887void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
888{
889 int slot = 1;
890 size_t bits = bits_arg;
891 psa_status_t expected_status = expected_status_arg;
892 psa_status_t status;
893 psa_key_type_t type =
894 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
895 size_t buffer_size = /* Slight overapproximations */
896 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200897 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200898 unsigned char *p;
899 int ret;
900 size_t length;
901
902 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200903 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200904
905 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
906 bits, keypair ) ) >= 0 );
907 length = ret;
908
909 /* Try importing the key */
910 status = psa_import_key( slot, type, p, length );
911 TEST_ASSERT( status == expected_status );
912 if( status == PSA_SUCCESS )
913 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
914
915exit:
916 mbedtls_free( buffer );
917 mbedtls_psa_crypto_free( );
918}
919/* END_CASE */
920
921/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300922void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300923 int type_arg,
924 int alg_arg,
925 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926 int expected_bits,
927 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200928 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100929 int canonical_input )
930{
931 int slot = 1;
932 int slot2 = slot + 1;
933 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200934 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200935 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937 unsigned char *exported = NULL;
938 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100940 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 size_t reexported_length;
942 psa_key_type_t got_type;
943 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200944 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300947 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300948 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200949 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200951 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
953
mohammad1603a97cb8c2018-03-28 03:46:26 -0700954 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200955 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700956 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
957
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958 /* Import the key */
959 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200960 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961
962 /* Test the key information */
963 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200964 &got_type,
965 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 TEST_ASSERT( got_type == type );
967 TEST_ASSERT( got_bits == (size_t) expected_bits );
968
969 /* Export the key */
970 status = psa_export_key( slot,
971 exported, export_size,
972 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200973 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100974
975 /* The exported length must be set by psa_export_key() to a value between 0
976 * and export_size. On errors, the exported length must be 0. */
977 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
978 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
979 TEST_ASSERT( exported_length <= export_size );
980
Gilles Peskine3f669c32018-06-21 09:21:51 +0200981 TEST_ASSERT( mem_is_zero( exported + exported_length,
982 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200984 {
985 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200987 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988
Gilles Peskine8f609232018-08-11 01:24:55 +0200989 if( ! exercise_export_key( slot, usage_arg ) )
990 goto exit;
991
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200993 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 else
995 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700996 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
997
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200999 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001000 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001002 reexported,
1003 export_size,
1004 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001005 ASSERT_COMPARE( exported, exported_length,
1006 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 }
1008
1009destroy:
1010 /* Destroy the key */
1011 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1012 TEST_ASSERT( psa_get_key_information(
1013 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1014
1015exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001016 mbedtls_free( exported );
1017 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018 mbedtls_psa_crypto_free( );
1019}
1020/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001021
Moran Pekerf709f4a2018-06-06 17:26:04 +03001022/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001023void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001024 int type_arg,
1025 int alg_arg,
1026 int expected_bits,
1027 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001028 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001029{
1030 int slot = 1;
1031 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001032 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001033 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001034 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001035 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001036 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001037 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001038 psa_key_type_t got_type;
1039 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001040 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041
Moran Pekerf709f4a2018-06-06 17:26:04 +03001042 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001043 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001044 export_size = (ptrdiff_t) data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001045 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001046
1047 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1048
1049 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001050 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1052
1053 /* Import the key */
1054 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001055 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001056
1057 /* Test the key information */
1058 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001059 &got_type,
1060 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001061 TEST_ASSERT( got_type == type );
1062 TEST_ASSERT( got_bits == (size_t) expected_bits );
1063
1064 /* Export the key */
1065 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001066 exported, export_size,
1067 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001068 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001069 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
1070 TEST_ASSERT( mem_is_zero( exported + exported_length,
1071 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001072 if( status != PSA_SUCCESS )
1073 goto destroy;
1074
Moran Pekerf709f4a2018-06-06 17:26:04 +03001075destroy:
1076 /* Destroy the key */
1077 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1078 TEST_ASSERT( psa_get_key_information(
1079 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1080
1081exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001082 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001083 mbedtls_psa_crypto_free( );
1084}
1085/* END_CASE */
1086
Gilles Peskine20035e32018-02-03 22:44:14 +01001087/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001088void import_and_exercise_key( data_t *data,
1089 int type_arg,
1090 int bits_arg,
1091 int alg_arg )
1092{
1093 int slot = 1;
1094 psa_key_type_t type = type_arg;
1095 size_t bits = bits_arg;
1096 psa_algorithm_t alg = alg_arg;
1097 psa_key_usage_t usage =
1098 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1099 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1100 PSA_KEY_USAGE_VERIFY :
1101 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1102 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1103 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1104 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1105 PSA_KEY_USAGE_ENCRYPT :
1106 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001107 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001108 0 );
1109 psa_key_policy_t policy;
1110 psa_key_type_t got_type;
1111 size_t got_bits;
1112 psa_status_t status;
1113
1114 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1115
1116 psa_key_policy_init( &policy );
1117 psa_key_policy_set_usage( &policy, usage, alg );
1118 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1119
1120 /* Import the key */
1121 status = psa_import_key( slot, type, data->x, data->len );
1122 TEST_ASSERT( status == PSA_SUCCESS );
1123
1124 /* Test the key information */
1125 TEST_ASSERT( psa_get_key_information( slot,
1126 &got_type,
1127 &got_bits ) == PSA_SUCCESS );
1128 TEST_ASSERT( got_type == type );
1129 TEST_ASSERT( got_bits == bits );
1130
1131 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001132 if( ! exercise_key( slot, usage, alg ) )
1133 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001134
1135exit:
1136 psa_destroy_key( slot );
1137 mbedtls_psa_crypto_free( );
1138}
1139/* END_CASE */
1140
1141/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001142void key_policy( int usage_arg, int alg_arg )
1143{
1144 int key_slot = 1;
1145 psa_algorithm_t alg = alg_arg;
1146 psa_key_usage_t usage = usage_arg;
1147 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1148 unsigned char key[32] = {0};
1149 psa_key_policy_t policy_set;
1150 psa_key_policy_t policy_get;
1151
1152 memset( key, 0x2a, sizeof( key ) );
1153
1154 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1155
1156 psa_key_policy_init( &policy_set );
1157 psa_key_policy_init( &policy_get );
1158
1159 psa_key_policy_set_usage( &policy_set, usage, alg );
1160
1161 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1162 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1163 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1164
1165 TEST_ASSERT( psa_import_key( key_slot, key_type,
1166 key, sizeof( key ) ) == PSA_SUCCESS );
1167
1168 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1169
1170 TEST_ASSERT( policy_get.usage == policy_set.usage );
1171 TEST_ASSERT( policy_get.alg == policy_set.alg );
1172
1173exit:
1174 psa_destroy_key( key_slot );
1175 mbedtls_psa_crypto_free( );
1176}
1177/* END_CASE */
1178
1179/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001180void mac_key_policy( int policy_usage,
1181 int policy_alg,
1182 int key_type,
1183 data_t *key_data,
1184 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001185{
1186 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001187 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001188 psa_mac_operation_t operation;
1189 psa_status_t status;
1190 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001191
1192 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1193
1194 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001195 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001196 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1197
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198 TEST_ASSERT( psa_import_key( key_slot, key_type,
1199 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001200
Gilles Peskine89167cb2018-07-08 20:12:23 +02001201 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001202 if( policy_alg == exercise_alg &&
1203 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1204 TEST_ASSERT( status == PSA_SUCCESS );
1205 else
1206 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1207 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001208
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001210 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 if( policy_alg == exercise_alg &&
1212 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001213 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214 else
1215 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1216
1217exit:
1218 psa_mac_abort( &operation );
1219 psa_destroy_key( key_slot );
1220 mbedtls_psa_crypto_free( );
1221}
1222/* END_CASE */
1223
1224/* BEGIN_CASE */
1225void cipher_key_policy( int policy_usage,
1226 int policy_alg,
1227 int key_type,
1228 data_t *key_data,
1229 int exercise_alg )
1230{
1231 int key_slot = 1;
1232 psa_key_policy_t policy;
1233 psa_cipher_operation_t operation;
1234 psa_status_t status;
1235
1236 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1237
1238 psa_key_policy_init( &policy );
1239 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1240 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1241
1242 TEST_ASSERT( psa_import_key( key_slot, key_type,
1243 key_data->x, key_data->len ) == PSA_SUCCESS );
1244
Gilles Peskinefe119512018-07-08 21:39:34 +02001245 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001246 if( policy_alg == exercise_alg &&
1247 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1248 TEST_ASSERT( status == PSA_SUCCESS );
1249 else
1250 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1251 psa_cipher_abort( &operation );
1252
Gilles Peskinefe119512018-07-08 21:39:34 +02001253 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254 if( policy_alg == exercise_alg &&
1255 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1256 TEST_ASSERT( status == PSA_SUCCESS );
1257 else
1258 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1259
1260exit:
1261 psa_cipher_abort( &operation );
1262 psa_destroy_key( key_slot );
1263 mbedtls_psa_crypto_free( );
1264}
1265/* END_CASE */
1266
1267/* BEGIN_CASE */
1268void aead_key_policy( int policy_usage,
1269 int policy_alg,
1270 int key_type,
1271 data_t *key_data,
1272 int nonce_length_arg,
1273 int tag_length_arg,
1274 int exercise_alg )
1275{
1276 int key_slot = 1;
1277 psa_key_policy_t policy;
1278 psa_status_t status;
1279 unsigned char nonce[16] = {0};
1280 size_t nonce_length = nonce_length_arg;
1281 unsigned char tag[16];
1282 size_t tag_length = tag_length_arg;
1283 size_t output_length;
1284
1285 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1286 TEST_ASSERT( tag_length <= sizeof( tag ) );
1287
1288 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1289
1290 psa_key_policy_init( &policy );
1291 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1292 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1293
1294 TEST_ASSERT( psa_import_key( key_slot, key_type,
1295 key_data->x, key_data->len ) == PSA_SUCCESS );
1296
1297 status = psa_aead_encrypt( key_slot, exercise_alg,
1298 nonce, nonce_length,
1299 NULL, 0,
1300 NULL, 0,
1301 tag, tag_length,
1302 &output_length );
1303 if( policy_alg == exercise_alg &&
1304 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1305 TEST_ASSERT( status == PSA_SUCCESS );
1306 else
1307 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1308
1309 memset( tag, 0, sizeof( tag ) );
1310 status = psa_aead_decrypt( key_slot, exercise_alg,
1311 nonce, nonce_length,
1312 NULL, 0,
1313 tag, tag_length,
1314 NULL, 0,
1315 &output_length );
1316 if( policy_alg == exercise_alg &&
1317 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1318 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1319 else
1320 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1321
1322exit:
1323 psa_destroy_key( key_slot );
1324 mbedtls_psa_crypto_free( );
1325}
1326/* END_CASE */
1327
1328/* BEGIN_CASE */
1329void asymmetric_encryption_key_policy( int policy_usage,
1330 int policy_alg,
1331 int key_type,
1332 data_t *key_data,
1333 int exercise_alg )
1334{
1335 int key_slot = 1;
1336 psa_key_policy_t policy;
1337 psa_status_t status;
1338 size_t key_bits;
1339 size_t buffer_length;
1340 unsigned char *buffer = NULL;
1341 size_t output_length;
1342
1343 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1344
1345 psa_key_policy_init( &policy );
1346 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1347 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1348
1349 TEST_ASSERT( psa_import_key( key_slot, key_type,
1350 key_data->x, key_data->len ) == PSA_SUCCESS );
1351
1352 TEST_ASSERT( psa_get_key_information( key_slot,
1353 NULL,
1354 &key_bits ) == PSA_SUCCESS );
1355 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1356 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001357 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001358
1359 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1360 NULL, 0,
1361 NULL, 0,
1362 buffer, buffer_length,
1363 &output_length );
1364 if( policy_alg == exercise_alg &&
1365 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1366 TEST_ASSERT( status == PSA_SUCCESS );
1367 else
1368 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1369
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001370 if( buffer_length != 0 )
1371 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001372 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1373 buffer, buffer_length,
1374 NULL, 0,
1375 buffer, buffer_length,
1376 &output_length );
1377 if( policy_alg == exercise_alg &&
1378 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1379 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1380 else
1381 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1382
1383exit:
1384 psa_destroy_key( key_slot );
1385 mbedtls_psa_crypto_free( );
1386 mbedtls_free( buffer );
1387}
1388/* END_CASE */
1389
1390/* BEGIN_CASE */
1391void asymmetric_signature_key_policy( int policy_usage,
1392 int policy_alg,
1393 int key_type,
1394 data_t *key_data,
1395 int exercise_alg )
1396{
1397 int key_slot = 1;
1398 psa_key_policy_t policy;
1399 psa_status_t status;
1400 unsigned char payload[16] = {1};
1401 size_t payload_length = sizeof( payload );
1402 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1403 size_t signature_length;
1404
1405 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1406
1407 psa_key_policy_init( &policy );
1408 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1409 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1410
1411 TEST_ASSERT( psa_import_key( key_slot, key_type,
1412 key_data->x, key_data->len ) == PSA_SUCCESS );
1413
1414 status = psa_asymmetric_sign( key_slot, exercise_alg,
1415 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001416 signature, sizeof( signature ),
1417 &signature_length );
1418 if( policy_alg == exercise_alg &&
1419 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1420 TEST_ASSERT( status == PSA_SUCCESS );
1421 else
1422 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1423
1424 memset( signature, 0, sizeof( signature ) );
1425 status = psa_asymmetric_verify( key_slot, exercise_alg,
1426 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001427 signature, sizeof( signature ) );
1428 if( policy_alg == exercise_alg &&
1429 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1430 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1431 else
1432 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001433
1434exit:
1435 psa_destroy_key( key_slot );
1436 mbedtls_psa_crypto_free( );
1437}
1438/* END_CASE */
1439
1440/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001441void derive_key_policy( int policy_usage,
1442 int policy_alg,
1443 int key_type,
1444 data_t *key_data,
1445 int exercise_alg )
1446{
1447 int key_slot = 1;
1448 psa_key_policy_t policy;
1449 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1450 psa_status_t status;
1451
1452 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1453
1454 psa_key_policy_init( &policy );
1455 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1456 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1457
1458 TEST_ASSERT( psa_import_key( key_slot, key_type,
1459 key_data->x, key_data->len ) == PSA_SUCCESS );
1460
1461 status = psa_key_derivation( &generator, key_slot,
1462 exercise_alg,
1463 NULL, 0,
1464 NULL, 0,
1465 1 );
1466 if( policy_alg == exercise_alg &&
1467 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1468 TEST_ASSERT( status == PSA_SUCCESS );
1469 else
1470 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1471
1472exit:
1473 psa_generator_abort( &generator );
1474 psa_destroy_key( key_slot );
1475 mbedtls_psa_crypto_free( );
1476}
1477/* END_CASE */
1478
1479/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001480void key_lifetime( int lifetime_arg )
1481{
1482 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001483 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001484 unsigned char key[32] = {0};
1485 psa_key_lifetime_t lifetime_set = lifetime_arg;
1486 psa_key_lifetime_t lifetime_get;
1487
1488 memset( key, 0x2a, sizeof( key ) );
1489
1490 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1491
1492 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1493 lifetime_set ) == PSA_SUCCESS );
1494
1495 TEST_ASSERT( psa_import_key( key_slot, key_type,
1496 key, sizeof( key ) ) == PSA_SUCCESS );
1497
1498 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1499 &lifetime_get ) == PSA_SUCCESS );
1500
1501 TEST_ASSERT( lifetime_get == lifetime_set );
1502
1503exit:
1504 psa_destroy_key( key_slot );
1505 mbedtls_psa_crypto_free( );
1506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
1510void key_lifetime_set_fail( int key_slot_arg,
1511 int lifetime_arg,
1512 int expected_status_arg )
1513{
1514 psa_key_slot_t key_slot = key_slot_arg;
1515 psa_key_lifetime_t lifetime_set = lifetime_arg;
1516 psa_status_t actual_status;
1517 psa_status_t expected_status = expected_status_arg;
1518
1519 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1520
1521 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1522
1523 if( actual_status == PSA_SUCCESS )
1524 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1525
1526 TEST_ASSERT( expected_status == actual_status );
1527
1528exit:
1529 psa_destroy_key( key_slot );
1530 mbedtls_psa_crypto_free( );
1531}
1532/* END_CASE */
1533
1534/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001535void hash_setup( int alg_arg,
1536 int expected_status_arg )
1537{
1538 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001539 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001540 psa_hash_operation_t operation;
1541 psa_status_t status;
1542
1543 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1544
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001545 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001546 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001547 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001548
1549exit:
1550 mbedtls_psa_crypto_free( );
1551}
1552/* END_CASE */
1553
1554/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001555void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001556{
1557 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001558 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001559 size_t actual_hash_length;
1560 psa_hash_operation_t operation;
1561
Gilles Peskine69c12672018-06-28 00:07:19 +02001562 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1563 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1564
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001565 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001566 TEST_ASSERT( expected_hash != NULL );
1567 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1568 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001569
1570 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1571
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001572 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001573 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001574 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001575 TEST_ASSERT( psa_hash_finish( &operation,
1576 actual_hash, sizeof( actual_hash ),
1577 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001578 ASSERT_COMPARE( expected_hash->x, expected_hash->len,
1579 actual_hash, actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001580
1581exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001582 mbedtls_psa_crypto_free( );
1583}
1584/* END_CASE */
1585
1586/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001587void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001588{
1589 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001590 psa_hash_operation_t operation;
1591
Gilles Peskine69c12672018-06-28 00:07:19 +02001592 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1593 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1594
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001595 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001596 TEST_ASSERT( expected_hash != NULL );
1597 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1598 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001599
1600 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1601
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001602 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001603 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001604 input->x,
1605 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001606 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001607 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001608 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001609
1610exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001611 mbedtls_psa_crypto_free( );
1612}
1613/* END_CASE */
1614
1615/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001616void mac_setup( int key_type_arg,
1617 data_t *key,
1618 int alg_arg,
1619 int expected_status_arg )
1620{
1621 int key_slot = 1;
1622 psa_key_type_t key_type = key_type_arg;
1623 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001624 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001625 psa_mac_operation_t operation;
1626 psa_key_policy_t policy;
1627 psa_status_t status;
1628
1629 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1630
1631 psa_key_policy_init( &policy );
1632 psa_key_policy_set_usage( &policy,
1633 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1634 alg );
1635 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1636
1637 TEST_ASSERT( psa_import_key( key_slot, key_type,
1638 key->x, key->len ) == PSA_SUCCESS );
1639
Gilles Peskine89167cb2018-07-08 20:12:23 +02001640 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001641 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001642 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001643
1644exit:
1645 psa_destroy_key( key_slot );
1646 mbedtls_psa_crypto_free( );
1647}
1648/* END_CASE */
1649
1650/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001651void mac_verify( int key_type_arg,
1652 data_t *key,
1653 int alg_arg,
1654 data_t *input,
1655 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001656{
1657 int key_slot = 1;
1658 psa_key_type_t key_type = key_type_arg;
1659 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001660 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001661 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001662
Gilles Peskine69c12672018-06-28 00:07:19 +02001663 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1664
Gilles Peskine8c9def32018-02-08 10:02:12 +01001665 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001666 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001667 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001668 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001669 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1670 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671
1672 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1673
mohammad16036df908f2018-04-02 08:34:15 -07001674 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001675 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001676 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1677
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001679 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001680
Gilles Peskine89167cb2018-07-08 20:12:23 +02001681 TEST_ASSERT( psa_mac_verify_setup( &operation,
1682 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1684 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001685 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001686 TEST_ASSERT( psa_mac_verify_finish( &operation,
1687 expected_mac->x,
1688 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689
1690exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691 psa_destroy_key( key_slot );
1692 mbedtls_psa_crypto_free( );
1693}
1694/* END_CASE */
1695
1696/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001697void cipher_setup( int key_type_arg,
1698 data_t *key,
1699 int alg_arg,
1700 int expected_status_arg )
1701{
1702 int key_slot = 1;
1703 psa_key_type_t key_type = key_type_arg;
1704 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001705 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706 psa_cipher_operation_t operation;
1707 psa_key_policy_t policy;
1708 psa_status_t status;
1709
1710 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1711
1712 psa_key_policy_init( &policy );
1713 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1714 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1715
1716 TEST_ASSERT( psa_import_key( key_slot, key_type,
1717 key->x, key->len ) == PSA_SUCCESS );
1718
Gilles Peskinefe119512018-07-08 21:39:34 +02001719 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001720 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001721 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001722
1723exit:
1724 psa_destroy_key( key_slot );
1725 mbedtls_psa_crypto_free( );
1726}
1727/* END_CASE */
1728
1729/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001730void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001731 data_t *key,
1732 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001733 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001734{
1735 int key_slot = 1;
1736 psa_status_t status;
1737 psa_key_type_t key_type = key_type_arg;
1738 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001739 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001740 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001741 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001742 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001743 size_t output_buffer_size = 0;
1744 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001745 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001746 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001747 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001748
Gilles Peskine50e586b2018-06-08 14:28:46 +02001749 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001750 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001751 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001752 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1753 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1754 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001755
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001756 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1757 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001758
1759 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1760
Moran Pekered346952018-07-05 15:22:45 +03001761 psa_key_policy_init( &policy );
1762 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1763 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1764
Gilles Peskine50e586b2018-06-08 14:28:46 +02001765 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001766 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001767
Gilles Peskinefe119512018-07-08 21:39:34 +02001768 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1769 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001770
Gilles Peskinefe119512018-07-08 21:39:34 +02001771 TEST_ASSERT( psa_cipher_set_iv( &operation,
1772 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001773 output_buffer_size = (size_t) input->len +
1774 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001775 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001776
Gilles Peskine4abf7412018-06-18 16:35:34 +02001777 TEST_ASSERT( psa_cipher_update( &operation,
1778 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001779 output, output_buffer_size,
1780 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001781 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001782 status = psa_cipher_finish( &operation,
1783 output + function_output_length,
1784 output_buffer_size,
1785 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001786 total_output_length += function_output_length;
1787
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001788 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001789 if( expected_status == PSA_SUCCESS )
1790 {
1791 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001792 ASSERT_COMPARE( expected_output->x, expected_output->len,
1793 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001794 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001795
Gilles Peskine50e586b2018-06-08 14:28:46 +02001796exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001797 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001798 psa_destroy_key( key_slot );
1799 mbedtls_psa_crypto_free( );
1800}
1801/* END_CASE */
1802
1803/* BEGIN_CASE */
1804void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001805 data_t *key,
1806 data_t *input,
1807 int first_part_size,
1808 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001809{
1810 int key_slot = 1;
1811 psa_key_type_t key_type = key_type_arg;
1812 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001813 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001814 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001815 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001816 size_t output_buffer_size = 0;
1817 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001818 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001819 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001820 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001821
Gilles Peskine50e586b2018-06-08 14:28:46 +02001822 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001823 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001824 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001825 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1826 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1827 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001828
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001829 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1830 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001831
1832 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1833
Moran Pekered346952018-07-05 15:22:45 +03001834 psa_key_policy_init( &policy );
1835 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1836 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1837
Gilles Peskine50e586b2018-06-08 14:28:46 +02001838 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001839 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001840
Gilles Peskinefe119512018-07-08 21:39:34 +02001841 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1842 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001843
Gilles Peskinefe119512018-07-08 21:39:34 +02001844 TEST_ASSERT( psa_cipher_set_iv( &operation,
1845 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001846 output_buffer_size = (size_t) input->len +
1847 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001848 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001849
Gilles Peskine4abf7412018-06-18 16:35:34 +02001850 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001851 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001852 output, output_buffer_size,
1853 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001854 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001855 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001856 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001857 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001858 output, output_buffer_size,
1859 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001860 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001861 TEST_ASSERT( psa_cipher_finish( &operation,
1862 output + function_output_length,
1863 output_buffer_size,
1864 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001865 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001866 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1867
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001868 ASSERT_COMPARE( expected_output->x, expected_output->len,
1869 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001870
1871exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001872 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001873 psa_destroy_key( key_slot );
1874 mbedtls_psa_crypto_free( );
1875}
1876/* END_CASE */
1877
1878/* BEGIN_CASE */
1879void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001880 data_t *key,
1881 data_t *input,
1882 int first_part_size,
1883 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001884{
1885 int key_slot = 1;
1886
1887 psa_key_type_t key_type = key_type_arg;
1888 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001889 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001890 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001891 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001892 size_t output_buffer_size = 0;
1893 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001894 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001895 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001896 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001897
Gilles Peskine50e586b2018-06-08 14:28:46 +02001898 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001899 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001900 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001901 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1902 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1903 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001904
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001905 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1906 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001907
1908 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1909
Moran Pekered346952018-07-05 15:22:45 +03001910 psa_key_policy_init( &policy );
1911 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1912 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1913
Gilles Peskine50e586b2018-06-08 14:28:46 +02001914 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001915 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001916
Gilles Peskinefe119512018-07-08 21:39:34 +02001917 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1918 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001919
Gilles Peskinefe119512018-07-08 21:39:34 +02001920 TEST_ASSERT( psa_cipher_set_iv( &operation,
1921 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001922
mohammad16033d91abe2018-07-03 13:15:54 +03001923 output_buffer_size = (size_t) input->len +
1924 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001925 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001926
Gilles Peskine4abf7412018-06-18 16:35:34 +02001927 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1928 TEST_ASSERT( psa_cipher_update( &operation,
1929 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001930 output, output_buffer_size,
1931 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001932 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001933 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001934 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001935 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001936 output, output_buffer_size,
1937 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001938 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001939 TEST_ASSERT( psa_cipher_finish( &operation,
1940 output + function_output_length,
1941 output_buffer_size,
1942 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001943 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001944 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1945
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001946 ASSERT_COMPARE( expected_output->x, expected_output->len,
1947 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001948
1949exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001950 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001951 psa_destroy_key( key_slot );
1952 mbedtls_psa_crypto_free( );
1953}
1954/* END_CASE */
1955
Gilles Peskine50e586b2018-06-08 14:28:46 +02001956/* BEGIN_CASE */
1957void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001958 data_t *key,
1959 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001960 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001961{
1962 int key_slot = 1;
1963 psa_status_t status;
1964 psa_key_type_t key_type = key_type_arg;
1965 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001966 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001967 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001968 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001969 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001970 size_t output_buffer_size = 0;
1971 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001972 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001973 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001974 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001975
Gilles Peskine50e586b2018-06-08 14:28:46 +02001976 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001977 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001978 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001979 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1980 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1981 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001982
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001983 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1984 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001985
1986 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1987
Moran Pekered346952018-07-05 15:22:45 +03001988 psa_key_policy_init( &policy );
1989 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1990 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1991
Gilles Peskine50e586b2018-06-08 14:28:46 +02001992 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001993 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001994
Gilles Peskinefe119512018-07-08 21:39:34 +02001995 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1996 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001997
Gilles Peskinefe119512018-07-08 21:39:34 +02001998 TEST_ASSERT( psa_cipher_set_iv( &operation,
1999 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002000
mohammad16033d91abe2018-07-03 13:15:54 +03002001 output_buffer_size = (size_t) input->len +
2002 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002003 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002004
Gilles Peskine4abf7412018-06-18 16:35:34 +02002005 TEST_ASSERT( psa_cipher_update( &operation,
2006 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002007 output, output_buffer_size,
2008 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002009 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002010 status = psa_cipher_finish( &operation,
2011 output + function_output_length,
2012 output_buffer_size,
2013 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002014 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002015 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002016
2017 if( expected_status == PSA_SUCCESS )
2018 {
2019 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002020 ASSERT_COMPARE( expected_output->x, expected_output->len,
2021 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002022 }
2023
Gilles Peskine50e586b2018-06-08 14:28:46 +02002024exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002025 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002026 psa_destroy_key( key_slot );
2027 mbedtls_psa_crypto_free( );
2028}
2029/* END_CASE */
2030
Gilles Peskine50e586b2018-06-08 14:28:46 +02002031/* BEGIN_CASE */
2032void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002033 data_t *key,
2034 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002035{
2036 int key_slot = 1;
2037 psa_key_type_t key_type = key_type_arg;
2038 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002039 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002040 size_t iv_size = 16;
2041 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002042 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002043 size_t output1_size = 0;
2044 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002045 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002046 size_t output2_size = 0;
2047 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002048 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002049 psa_cipher_operation_t operation1;
2050 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002051 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002052
mohammad1603d7d7ba52018-03-12 18:51:53 +02002053 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002054 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002055 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2056 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002057
mohammad1603d7d7ba52018-03-12 18:51:53 +02002058 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2059
Moran Pekered346952018-07-05 15:22:45 +03002060 psa_key_policy_init( &policy );
2061 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2062 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2063
mohammad1603d7d7ba52018-03-12 18:51:53 +02002064 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002065 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002066
Gilles Peskinefe119512018-07-08 21:39:34 +02002067 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2068 key_slot, alg ) == PSA_SUCCESS );
2069 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2070 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002071
Gilles Peskinefe119512018-07-08 21:39:34 +02002072 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2073 iv, iv_size,
2074 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002075 output1_size = (size_t) input->len +
2076 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002077 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002078
Gilles Peskine4abf7412018-06-18 16:35:34 +02002079 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002080 output1, output1_size,
2081 &output1_length ) == PSA_SUCCESS );
2082 TEST_ASSERT( psa_cipher_finish( &operation1,
2083 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002084 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002085
Gilles Peskine048b7f02018-06-08 14:20:49 +02002086 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002087
2088 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2089
2090 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002091 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002092
Gilles Peskinefe119512018-07-08 21:39:34 +02002093 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2094 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002095 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2096 output2, output2_size,
2097 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002098 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002099 TEST_ASSERT( psa_cipher_finish( &operation2,
2100 output2 + output2_length,
2101 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002102 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002103
Gilles Peskine048b7f02018-06-08 14:20:49 +02002104 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002105
Janos Follath25c4fa82018-07-06 16:23:25 +01002106 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002107
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002108 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002109
2110exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002111 mbedtls_free( output1 );
2112 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002113 psa_destroy_key( key_slot );
2114 mbedtls_psa_crypto_free( );
2115}
2116/* END_CASE */
2117
2118/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002119void cipher_verify_output_multipart( int alg_arg,
2120 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002121 data_t *key,
2122 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002123 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002124{
2125 int key_slot = 1;
2126 psa_key_type_t key_type = key_type_arg;
2127 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002128 unsigned char iv[16] = {0};
2129 size_t iv_size = 16;
2130 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002131 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002132 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002133 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002134 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002135 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002136 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002137 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002138 psa_cipher_operation_t operation1;
2139 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002140 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002141
Moran Pekerded84402018-06-06 16:36:50 +03002142 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002143 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002144 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2145 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002146
Moran Pekerded84402018-06-06 16:36:50 +03002147 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2148
Moran Pekered346952018-07-05 15:22:45 +03002149 psa_key_policy_init( &policy );
2150 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2151 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2152
Moran Pekerded84402018-06-06 16:36:50 +03002153 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002154 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002155
Gilles Peskinefe119512018-07-08 21:39:34 +02002156 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2157 key_slot, alg ) == PSA_SUCCESS );
2158 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2159 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002160
Gilles Peskinefe119512018-07-08 21:39:34 +02002161 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2162 iv, iv_size,
2163 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002164 output1_buffer_size = (size_t) input->len +
2165 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002166 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002167
Gilles Peskine4abf7412018-06-18 16:35:34 +02002168 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002169
itayzafrir3e02b3b2018-06-12 17:06:52 +03002170 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002171 output1, output1_buffer_size,
2172 &function_output_length ) == PSA_SUCCESS );
2173 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002174
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002175 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002176 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002177 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002178 output1, output1_buffer_size,
2179 &function_output_length ) == PSA_SUCCESS );
2180 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002181
Gilles Peskine048b7f02018-06-08 14:20:49 +02002182 TEST_ASSERT( psa_cipher_finish( &operation1,
2183 output1 + output1_length,
2184 output1_buffer_size - output1_length,
2185 &function_output_length ) == PSA_SUCCESS );
2186 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002187
2188 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2189
Gilles Peskine048b7f02018-06-08 14:20:49 +02002190 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002191 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002192
Gilles Peskinefe119512018-07-08 21:39:34 +02002193 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2194 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002195
2196 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002197 output2, output2_buffer_size,
2198 &function_output_length ) == PSA_SUCCESS );
2199 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002200
Gilles Peskine048b7f02018-06-08 14:20:49 +02002201 TEST_ASSERT( psa_cipher_update( &operation2,
2202 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002203 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002204 output2, output2_buffer_size,
2205 &function_output_length ) == PSA_SUCCESS );
2206 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002207
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002208 TEST_ASSERT( psa_cipher_finish( &operation2,
2209 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002210 output2_buffer_size - output2_length,
2211 &function_output_length ) == PSA_SUCCESS );
2212 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002213
Janos Follath25c4fa82018-07-06 16:23:25 +01002214 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002215
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002216 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002217
2218exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002219 mbedtls_free( output1 );
2220 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002221 psa_destroy_key( key_slot );
2222 mbedtls_psa_crypto_free( );
2223}
2224/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002225
Gilles Peskine20035e32018-02-03 22:44:14 +01002226/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002227void aead_encrypt_decrypt( int key_type_arg,
2228 data_t * key_data,
2229 int alg_arg,
2230 data_t * input_data,
2231 data_t * nonce,
2232 data_t * additional_data,
2233 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002234{
2235 int slot = 1;
2236 psa_key_type_t key_type = key_type_arg;
2237 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002238 unsigned char *output_data = NULL;
2239 size_t output_size = 0;
2240 size_t output_length = 0;
2241 unsigned char *output_data2 = NULL;
2242 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002243 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002244 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002245 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002246
Gilles Peskinea1cac842018-06-11 19:33:02 +02002247 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002248 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002249 TEST_ASSERT( nonce != NULL );
2250 TEST_ASSERT( additional_data != NULL );
2251 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2252 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2253 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2254 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2255
Gilles Peskine4abf7412018-06-18 16:35:34 +02002256 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002257 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002258
2259 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2260
2261 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002262 psa_key_policy_set_usage( &policy,
2263 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2264 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002265 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2266
2267 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002268 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002269
2270 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002271 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002272 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002273 additional_data->len,
2274 input_data->x, input_data->len,
2275 output_data, output_size,
2276 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002277
2278 if( PSA_SUCCESS == expected_result )
2279 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002280 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002281
2282 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002283 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002284 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002285 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002286 output_data, output_length,
2287 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002288 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002289
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002290 ASSERT_COMPARE( input_data->x, input_data->len,
2291 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002292 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002293
Gilles Peskinea1cac842018-06-11 19:33:02 +02002294exit:
2295 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002296 mbedtls_free( output_data );
2297 mbedtls_free( output_data2 );
2298 mbedtls_psa_crypto_free( );
2299}
2300/* END_CASE */
2301
2302/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002303void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002304 int alg_arg, data_t * input_data,
2305 data_t * additional_data, data_t * nonce,
2306 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002307{
2308 int slot = 1;
2309 psa_key_type_t key_type = key_type_arg;
2310 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002311 unsigned char *output_data = NULL;
2312 size_t output_size = 0;
2313 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002314 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002315 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002316
Gilles Peskinea1cac842018-06-11 19:33:02 +02002317 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002318 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002319 TEST_ASSERT( additional_data != NULL );
2320 TEST_ASSERT( nonce != NULL );
2321 TEST_ASSERT( expected_result != NULL );
2322 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2323 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2324 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2325 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2326 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2327
Gilles Peskine4abf7412018-06-18 16:35:34 +02002328 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002329 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002330
Gilles Peskinea1cac842018-06-11 19:33:02 +02002331 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2332
2333 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002334 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002335 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2336
2337 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002338 key_data->x,
2339 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002340
2341 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002342 nonce->x, nonce->len,
2343 additional_data->x, additional_data->len,
2344 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002345 output_data, output_size,
2346 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002347
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002348 ASSERT_COMPARE( expected_result->x, expected_result->len,
2349 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002350
Gilles Peskinea1cac842018-06-11 19:33:02 +02002351exit:
2352 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002353 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002354 mbedtls_psa_crypto_free( );
2355}
2356/* END_CASE */
2357
2358/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002359void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002360 int alg_arg, data_t * input_data,
2361 data_t * additional_data, data_t * nonce,
2362 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002363{
2364 int slot = 1;
2365 psa_key_type_t key_type = key_type_arg;
2366 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002367 unsigned char *output_data = NULL;
2368 size_t output_size = 0;
2369 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002370 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002371 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002372 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002373
Gilles Peskinea1cac842018-06-11 19:33:02 +02002374 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002375 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002376 TEST_ASSERT( additional_data != NULL );
2377 TEST_ASSERT( nonce != NULL );
2378 TEST_ASSERT( expected_data != NULL );
2379 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2380 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2381 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2382 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2383 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2384
Gilles Peskine4abf7412018-06-18 16:35:34 +02002385 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002386 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002387
Gilles Peskinea1cac842018-06-11 19:33:02 +02002388 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2389
2390 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002391 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002392 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2393
2394 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002395 key_data->x,
2396 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002397
2398 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002399 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002400 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002401 additional_data->len,
2402 input_data->x, input_data->len,
2403 output_data, output_size,
2404 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002405
Gilles Peskine2d277862018-06-18 15:41:12 +02002406 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002407 ASSERT_COMPARE( expected_data->x, expected_data->len,
2408 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002409
Gilles Peskinea1cac842018-06-11 19:33:02 +02002410exit:
2411 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002412 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002413 mbedtls_psa_crypto_free( );
2414}
2415/* END_CASE */
2416
2417/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002418void signature_size( int type_arg,
2419 int bits,
2420 int alg_arg,
2421 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002422{
2423 psa_key_type_t type = type_arg;
2424 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002425 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002426 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2427exit:
2428 ;
2429}
2430/* END_CASE */
2431
2432/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002433void sign_deterministic( int key_type_arg, data_t *key_data,
2434 int alg_arg, data_t *input_data,
2435 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002436{
2437 int slot = 1;
2438 psa_key_type_t key_type = key_type_arg;
2439 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002440 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002441 unsigned char *signature = NULL;
2442 size_t signature_size;
2443 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002444 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002445
Gilles Peskine20035e32018-02-03 22:44:14 +01002446 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002447 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002448 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002449 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2450 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2451 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002452
2453 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2454
mohammad1603a97cb8c2018-03-28 03:46:26 -07002455 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002456 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002457 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2458
Gilles Peskine20035e32018-02-03 22:44:14 +01002459 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002460 key_data->x,
2461 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002462 TEST_ASSERT( psa_get_key_information( slot,
2463 NULL,
2464 &key_bits ) == PSA_SUCCESS );
2465
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002466 /* Allocate a buffer which has the size advertized by the
2467 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002468 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2469 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002470 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002471 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002472 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002473
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002474 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002475 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002476 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002477 signature, signature_size,
2478 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002479 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002480 ASSERT_COMPARE( output_data->x, output_data->len,
2481 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002482
2483exit:
2484 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002485 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002486 mbedtls_psa_crypto_free( );
2487}
2488/* END_CASE */
2489
2490/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002491void sign_fail( int key_type_arg, data_t *key_data,
2492 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002493 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002494{
2495 int slot = 1;
2496 psa_key_type_t key_type = key_type_arg;
2497 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002498 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002499 psa_status_t actual_status;
2500 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002501 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002502 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002503 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002504
Gilles Peskine20035e32018-02-03 22:44:14 +01002505 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002506 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002507 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2508 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2509
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002510 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002511
2512 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2513
mohammad1603a97cb8c2018-03-28 03:46:26 -07002514 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002515 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002516 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2517
Gilles Peskine20035e32018-02-03 22:44:14 +01002518 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002519 key_data->x,
2520 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002521
2522 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002523 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002524 signature, signature_size,
2525 &signature_length );
2526 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002527 /* The value of *signature_length is unspecified on error, but
2528 * whatever it is, it should be less than signature_size, so that
2529 * if the caller tries to read *signature_length bytes without
2530 * checking the error code then they don't overflow a buffer. */
2531 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002532
2533exit:
2534 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002535 mbedtls_free( signature );
2536 mbedtls_psa_crypto_free( );
2537}
2538/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002539
2540/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002541void sign_verify( int key_type_arg, data_t *key_data,
2542 int alg_arg, data_t *input_data )
2543{
2544 int slot = 1;
2545 psa_key_type_t key_type = key_type_arg;
2546 psa_algorithm_t alg = alg_arg;
2547 size_t key_bits;
2548 unsigned char *signature = NULL;
2549 size_t signature_size;
2550 size_t signature_length = 0xdeadbeef;
2551 psa_key_policy_t policy;
2552
2553 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2554
2555 psa_key_policy_init( &policy );
2556 psa_key_policy_set_usage( &policy,
2557 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2558 alg );
2559 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2560
2561 TEST_ASSERT( psa_import_key( slot, key_type,
2562 key_data->x,
2563 key_data->len ) == PSA_SUCCESS );
2564 TEST_ASSERT( psa_get_key_information( slot,
2565 NULL,
2566 &key_bits ) == PSA_SUCCESS );
2567
2568 /* Allocate a buffer which has the size advertized by the
2569 * library. */
2570 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2571 key_bits, alg );
2572 TEST_ASSERT( signature_size != 0 );
2573 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002574 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002575
2576 /* Perform the signature. */
2577 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2578 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002579 signature, signature_size,
2580 &signature_length ) == PSA_SUCCESS );
2581 /* Check that the signature length looks sensible. */
2582 TEST_ASSERT( signature_length <= signature_size );
2583 TEST_ASSERT( signature_length > 0 );
2584
2585 /* Use the library to verify that the signature is correct. */
2586 TEST_ASSERT( psa_asymmetric_verify(
2587 slot, alg,
2588 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002589 signature, signature_length ) == PSA_SUCCESS );
2590
2591 if( input_data->len != 0 )
2592 {
2593 /* Flip a bit in the input and verify that the signature is now
2594 * detected as invalid. Flip a bit at the beginning, not at the end,
2595 * because ECDSA may ignore the last few bits of the input. */
2596 input_data->x[0] ^= 1;
2597 TEST_ASSERT( psa_asymmetric_verify(
2598 slot, alg,
2599 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002600 signature,
2601 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2602 }
2603
2604exit:
2605 psa_destroy_key( slot );
2606 mbedtls_free( signature );
2607 mbedtls_psa_crypto_free( );
2608}
2609/* END_CASE */
2610
2611/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002612void asymmetric_verify( int key_type_arg, data_t *key_data,
2613 int alg_arg, data_t *hash_data,
2614 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002615{
2616 int slot = 1;
2617 psa_key_type_t key_type = key_type_arg;
2618 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002619 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002620
Gilles Peskine69c12672018-06-28 00:07:19 +02002621 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2622
itayzafrir5c753392018-05-08 11:18:38 +03002623 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002624 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002625 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002626 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2627 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2628 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002629
2630 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2631
2632 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002633 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002634 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2635
2636 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002637 key_data->x,
2638 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002639
2640 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002641 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002642 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002643 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002644exit:
2645 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002646 mbedtls_psa_crypto_free( );
2647}
2648/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002649
2650/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002651void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2652 int alg_arg, data_t *hash_data,
2653 data_t *signature_data,
2654 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002655{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002656 int slot = 1;
2657 psa_key_type_t key_type = key_type_arg;
2658 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002659 psa_status_t actual_status;
2660 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002661 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002663 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002664 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002665 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002666 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2667 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2668 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002669
2670 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2671
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002672 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002673 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002674 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2675
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002676 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002677 key_data->x,
2678 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002679
2680 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002681 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002682 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002683 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002684
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002685 TEST_ASSERT( actual_status == expected_status );
2686
2687exit:
2688 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002689 mbedtls_psa_crypto_free( );
2690}
2691/* END_CASE */
2692
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002693/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002694void asymmetric_encrypt( int key_type_arg,
2695 data_t *key_data,
2696 int alg_arg,
2697 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002698 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002699 int expected_output_length_arg,
2700 int expected_status_arg )
2701{
2702 int slot = 1;
2703 psa_key_type_t key_type = key_type_arg;
2704 psa_algorithm_t alg = alg_arg;
2705 size_t expected_output_length = expected_output_length_arg;
2706 size_t key_bits;
2707 unsigned char *output = NULL;
2708 size_t output_size;
2709 size_t output_length = ~0;
2710 psa_status_t actual_status;
2711 psa_status_t expected_status = expected_status_arg;
2712 psa_key_policy_t policy;
2713
2714 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2715
2716 /* Import the key */
2717 psa_key_policy_init( &policy );
2718 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2719 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2720 TEST_ASSERT( psa_import_key( slot, key_type,
2721 key_data->x,
2722 key_data->len ) == PSA_SUCCESS );
2723
2724 /* Determine the maximum output length */
2725 TEST_ASSERT( psa_get_key_information( slot,
2726 NULL,
2727 &key_bits ) == PSA_SUCCESS );
2728 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002729 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002730
2731 /* Encrypt the input */
2732 actual_status = psa_asymmetric_encrypt( slot, alg,
2733 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002734 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002735 output, output_size,
2736 &output_length );
2737 TEST_ASSERT( actual_status == expected_status );
2738 TEST_ASSERT( output_length == expected_output_length );
2739
Gilles Peskine68428122018-06-30 18:42:41 +02002740 /* If the label is empty, the test framework puts a non-null pointer
2741 * in label->x. Test that a null pointer works as well. */
2742 if( label->len == 0 )
2743 {
2744 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002745 if( output_size != 0 )
2746 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002747 actual_status = psa_asymmetric_encrypt( slot, alg,
2748 input_data->x, input_data->len,
2749 NULL, label->len,
2750 output, output_size,
2751 &output_length );
2752 TEST_ASSERT( actual_status == expected_status );
2753 TEST_ASSERT( output_length == expected_output_length );
2754 }
2755
Gilles Peskine656896e2018-06-29 19:12:28 +02002756exit:
2757 psa_destroy_key( slot );
2758 mbedtls_free( output );
2759 mbedtls_psa_crypto_free( );
2760}
2761/* END_CASE */
2762
2763/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002764void asymmetric_encrypt_decrypt( int key_type_arg,
2765 data_t *key_data,
2766 int alg_arg,
2767 data_t *input_data,
2768 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002769{
2770 int slot = 1;
2771 psa_key_type_t key_type = key_type_arg;
2772 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002773 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002774 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002775 size_t output_size;
2776 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002777 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002778 size_t output2_size;
2779 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002780 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002781
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002782 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002783 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002784 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2785 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2786
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002787 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2788
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002789 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002790 psa_key_policy_set_usage( &policy,
2791 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002792 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002793 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2794
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002795 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002796 key_data->x,
2797 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002798
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002799
2800 /* Determine the maximum ciphertext length */
2801 TEST_ASSERT( psa_get_key_information( slot,
2802 NULL,
2803 &key_bits ) == PSA_SUCCESS );
2804 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002805 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002806 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002807 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002808
Gilles Peskineeebd7382018-06-08 18:11:54 +02002809 /* We test encryption by checking that encrypt-then-decrypt gives back
2810 * the original plaintext because of the non-optional random
2811 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002812 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002813 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002814 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002815 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002816 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002817 /* We don't know what ciphertext length to expect, but check that
2818 * it looks sensible. */
2819 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002820
Gilles Peskine2d277862018-06-18 15:41:12 +02002821 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002822 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002823 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002824 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002825 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002826 ASSERT_COMPARE( input_data->x, input_data->len,
2827 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002828
2829exit:
2830 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002831 mbedtls_free( output );
2832 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002833 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002834}
2835/* END_CASE */
2836
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002837/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002838void asymmetric_decrypt( int key_type_arg,
2839 data_t *key_data,
2840 int alg_arg,
2841 data_t *input_data,
2842 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002843 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002844{
2845 int slot = 1;
2846 psa_key_type_t key_type = key_type_arg;
2847 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002848 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002849 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002850 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002851 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002852
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002853 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002854 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002855 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2857 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2858 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2859
Gilles Peskine4abf7412018-06-18 16:35:34 +02002860 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002861 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002862
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002863 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2864
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002865 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002866 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002867 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2868
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002869 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002870 key_data->x,
2871 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002872
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002873 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002874 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002875 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002876 output,
2877 output_size,
2878 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002879 ASSERT_COMPARE( expected_data->x, expected_data->len,
2880 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002881
Gilles Peskine68428122018-06-30 18:42:41 +02002882 /* If the label is empty, the test framework puts a non-null pointer
2883 * in label->x. Test that a null pointer works as well. */
2884 if( label->len == 0 )
2885 {
2886 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002887 if( output_size != 0 )
2888 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002889 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2890 input_data->x, input_data->len,
2891 NULL, label->len,
2892 output,
2893 output_size,
2894 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002895 ASSERT_COMPARE( expected_data->x, expected_data->len,
2896 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02002897 }
2898
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002899exit:
2900 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002901 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002902 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002903}
2904/* END_CASE */
2905
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002906/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002907void asymmetric_decrypt_fail( int key_type_arg,
2908 data_t *key_data,
2909 int alg_arg,
2910 data_t *input_data,
2911 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002912 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002913{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002914 int slot = 1;
2915 psa_key_type_t key_type = key_type_arg;
2916 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002917 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002918 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002919 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002920 psa_status_t actual_status;
2921 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002922 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002923
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002924 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002925 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002926 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2927 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2928
Gilles Peskine4abf7412018-06-18 16:35:34 +02002929 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002930 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002931
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002932 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2933
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002934 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002935 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002936 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2937
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002938 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002939 key_data->x,
2940 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002941
Gilles Peskine2d277862018-06-18 15:41:12 +02002942 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002943 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002944 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002945 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002946 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002948 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002949
Gilles Peskine68428122018-06-30 18:42:41 +02002950 /* If the label is empty, the test framework puts a non-null pointer
2951 * in label->x. Test that a null pointer works as well. */
2952 if( label->len == 0 )
2953 {
2954 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002955 if( output_size != 0 )
2956 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002957 actual_status = psa_asymmetric_decrypt( slot, alg,
2958 input_data->x, input_data->len,
2959 NULL, label->len,
2960 output, output_size,
2961 &output_length );
2962 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002963 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002964 }
2965
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002966exit:
2967 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002968 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002969 mbedtls_psa_crypto_free( );
2970}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002971/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002972
2973/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002974void derive_setup( int key_type_arg,
2975 data_t *key_data,
2976 int alg_arg,
2977 data_t *salt,
2978 data_t *label,
2979 int requested_capacity_arg,
2980 int expected_status_arg )
2981{
2982 psa_key_slot_t slot = 1;
2983 size_t key_type = key_type_arg;
2984 psa_algorithm_t alg = alg_arg;
2985 size_t requested_capacity = requested_capacity_arg;
2986 psa_status_t expected_status = expected_status_arg;
2987 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2988 psa_key_policy_t policy;
2989
2990 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2991
2992 psa_key_policy_init( &policy );
2993 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2994 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2995
2996 TEST_ASSERT( psa_import_key( slot, key_type,
2997 key_data->x,
2998 key_data->len ) == PSA_SUCCESS );
2999
3000 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3001 salt->x, salt->len,
3002 label->x, label->len,
3003 requested_capacity ) == expected_status );
3004
3005exit:
3006 psa_generator_abort( &generator );
3007 psa_destroy_key( slot );
3008 mbedtls_psa_crypto_free( );
3009}
3010/* END_CASE */
3011
3012/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003013void derive_output( int alg_arg,
3014 data_t *key_data,
3015 data_t *salt,
3016 data_t *label,
3017 int requested_capacity_arg,
3018 data_t *expected_output1,
3019 data_t *expected_output2 )
3020{
3021 psa_key_slot_t slot = 1;
3022 psa_algorithm_t alg = alg_arg;
3023 size_t requested_capacity = requested_capacity_arg;
3024 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3025 uint8_t *expected_outputs[2] =
3026 {expected_output1->x, expected_output2->x};
3027 size_t output_sizes[2] =
3028 {expected_output1->len, expected_output2->len};
3029 size_t output_buffer_size = 0;
3030 uint8_t *output_buffer = NULL;
3031 size_t expected_capacity;
3032 size_t current_capacity;
3033 psa_key_policy_t policy;
3034 psa_status_t status;
3035 unsigned i;
3036
3037 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3038 {
3039 if( output_sizes[i] > output_buffer_size )
3040 output_buffer_size = output_sizes[i];
3041 if( output_sizes[i] == 0 )
3042 expected_outputs[i] = NULL;
3043 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003044 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003045 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3046
3047 psa_key_policy_init( &policy );
3048 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3049 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3050
3051 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3052 key_data->x,
3053 key_data->len ) == PSA_SUCCESS );
3054
3055 /* Extraction phase. */
3056 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3057 salt->x, salt->len,
3058 label->x, label->len,
3059 requested_capacity ) == PSA_SUCCESS );
3060 TEST_ASSERT( psa_get_generator_capacity( &generator,
3061 &current_capacity ) ==
3062 PSA_SUCCESS );
3063 TEST_ASSERT( current_capacity == requested_capacity );
3064 expected_capacity = requested_capacity;
3065
3066 /* Expansion phase. */
3067 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3068 {
3069 /* Read some bytes. */
3070 status = psa_generator_read( &generator,
3071 output_buffer, output_sizes[i] );
3072 if( expected_capacity == 0 && output_sizes[i] == 0 )
3073 {
3074 /* Reading 0 bytes when 0 bytes are available can go either way. */
3075 TEST_ASSERT( status == PSA_SUCCESS ||
3076 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3077 continue;
3078 }
3079 else if( expected_capacity == 0 ||
3080 output_sizes[i] > expected_capacity )
3081 {
3082 /* Capacity exceeded. */
3083 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3084 expected_capacity = 0;
3085 continue;
3086 }
3087 /* Success. Check the read data. */
3088 TEST_ASSERT( status == PSA_SUCCESS );
3089 if( output_sizes[i] != 0 )
3090 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3091 output_sizes[i] ) == 0 );
3092 /* Check the generator status. */
3093 expected_capacity -= output_sizes[i];
3094 TEST_ASSERT( psa_get_generator_capacity( &generator,
3095 &current_capacity ) ==
3096 PSA_SUCCESS );
3097 TEST_ASSERT( expected_capacity == current_capacity );
3098 }
3099 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3100
3101exit:
3102 mbedtls_free( output_buffer );
3103 psa_generator_abort( &generator );
3104 psa_destroy_key( slot );
3105 mbedtls_psa_crypto_free( );
3106}
3107/* END_CASE */
3108
3109/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003110void derive_full( int alg_arg,
3111 data_t *key_data,
3112 data_t *salt,
3113 data_t *label,
3114 int requested_capacity_arg )
3115{
3116 psa_key_slot_t slot = 1;
3117 psa_algorithm_t alg = alg_arg;
3118 size_t requested_capacity = requested_capacity_arg;
3119 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3120 unsigned char output_buffer[16];
3121 size_t expected_capacity = requested_capacity;
3122 size_t current_capacity;
3123 psa_key_policy_t policy;
3124
3125 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3126
3127 psa_key_policy_init( &policy );
3128 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3129 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3130
3131 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3132 key_data->x,
3133 key_data->len ) == PSA_SUCCESS );
3134
3135 /* Extraction phase. */
3136 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3137 salt->x, salt->len,
3138 label->x, label->len,
3139 requested_capacity ) == PSA_SUCCESS );
3140 TEST_ASSERT( psa_get_generator_capacity( &generator,
3141 &current_capacity ) ==
3142 PSA_SUCCESS );
3143 TEST_ASSERT( current_capacity == expected_capacity );
3144
3145 /* Expansion phase. */
3146 while( current_capacity > 0 )
3147 {
3148 size_t read_size = sizeof( output_buffer );
3149 if( read_size > current_capacity )
3150 read_size = current_capacity;
3151 TEST_ASSERT( psa_generator_read( &generator,
3152 output_buffer,
3153 read_size ) == PSA_SUCCESS );
3154 expected_capacity -= read_size;
3155 TEST_ASSERT( psa_get_generator_capacity( &generator,
3156 &current_capacity ) ==
3157 PSA_SUCCESS );
3158 TEST_ASSERT( current_capacity == expected_capacity );
3159 }
3160
3161 /* Check that the generator refuses to go over capacity. */
3162 TEST_ASSERT( psa_generator_read( &generator,
3163 output_buffer,
3164 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3165
3166 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3167
3168exit:
3169 psa_generator_abort( &generator );
3170 psa_destroy_key( slot );
3171 mbedtls_psa_crypto_free( );
3172}
3173/* END_CASE */
3174
3175/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003176void derive_key_exercise( int alg_arg,
3177 data_t *key_data,
3178 data_t *salt,
3179 data_t *label,
3180 int derived_type_arg,
3181 int derived_bits_arg,
3182 int derived_usage_arg,
3183 int derived_alg_arg )
3184{
3185 psa_key_slot_t base_key = 1;
3186 psa_key_slot_t derived_key = 2;
3187 psa_algorithm_t alg = alg_arg;
3188 psa_key_type_t derived_type = derived_type_arg;
3189 size_t derived_bits = derived_bits_arg;
3190 psa_key_usage_t derived_usage = derived_usage_arg;
3191 psa_algorithm_t derived_alg = derived_alg_arg;
3192 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3193 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3194 psa_key_policy_t policy;
3195 psa_key_type_t got_type;
3196 size_t got_bits;
3197
3198 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3199
3200 psa_key_policy_init( &policy );
3201 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3202 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3203 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3204 key_data->x,
3205 key_data->len ) == PSA_SUCCESS );
3206
3207 /* Derive a key. */
3208 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3209 salt->x, salt->len,
3210 label->x, label->len,
3211 capacity ) == PSA_SUCCESS );
3212 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3213 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3214 TEST_ASSERT( psa_generator_import_key( derived_key,
3215 derived_type,
3216 derived_bits,
3217 &generator ) == PSA_SUCCESS );
3218
3219 /* Test the key information */
3220 TEST_ASSERT( psa_get_key_information( derived_key,
3221 &got_type,
3222 &got_bits ) == PSA_SUCCESS );
3223 TEST_ASSERT( got_type == derived_type );
3224 TEST_ASSERT( got_bits == derived_bits );
3225
3226 /* Exercise the derived key. */
3227 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3228 goto exit;
3229
3230exit:
3231 psa_generator_abort( &generator );
3232 psa_destroy_key( base_key );
3233 psa_destroy_key( derived_key );
3234 mbedtls_psa_crypto_free( );
3235}
3236/* END_CASE */
3237
3238/* BEGIN_CASE */
3239void derive_key_export( int alg_arg,
3240 data_t *key_data,
3241 data_t *salt,
3242 data_t *label,
3243 int bytes1_arg,
3244 int bytes2_arg )
3245{
3246 psa_key_slot_t base_key = 1;
3247 psa_key_slot_t derived_key = 2;
3248 psa_algorithm_t alg = alg_arg;
3249 size_t bytes1 = bytes1_arg;
3250 size_t bytes2 = bytes2_arg;
3251 size_t capacity = bytes1 + bytes2;
3252 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003253 uint8_t *output_buffer = NULL;
3254 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003255 psa_key_policy_t policy;
3256 size_t length;
3257
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003258 ASSERT_ALLOC( output_buffer, capacity );
3259 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003260 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3261
3262 psa_key_policy_init( &policy );
3263 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3264 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3265 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3266 key_data->x,
3267 key_data->len ) == PSA_SUCCESS );
3268
3269 /* Derive some material and output it. */
3270 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3271 salt->x, salt->len,
3272 label->x, label->len,
3273 capacity ) == PSA_SUCCESS );
3274 TEST_ASSERT( psa_generator_read( &generator,
3275 output_buffer,
3276 capacity ) == PSA_SUCCESS );
3277 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3278
3279 /* Derive the same output again, but this time store it in key objects. */
3280 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3281 salt->x, salt->len,
3282 label->x, label->len,
3283 capacity ) == PSA_SUCCESS );
3284 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3285 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3286 TEST_ASSERT( psa_generator_import_key( derived_key,
3287 PSA_KEY_TYPE_RAW_DATA,
3288 PSA_BYTES_TO_BITS( bytes1 ),
3289 &generator ) == PSA_SUCCESS );
3290 TEST_ASSERT( psa_export_key( derived_key,
3291 export_buffer, bytes1,
3292 &length ) == PSA_SUCCESS );
3293 TEST_ASSERT( length == bytes1 );
3294 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3295 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3296 TEST_ASSERT( psa_generator_import_key( derived_key,
3297 PSA_KEY_TYPE_RAW_DATA,
3298 PSA_BYTES_TO_BITS( bytes2 ),
3299 &generator ) == PSA_SUCCESS );
3300 TEST_ASSERT( psa_export_key( derived_key,
3301 export_buffer + bytes1, bytes2,
3302 &length ) == PSA_SUCCESS );
3303 TEST_ASSERT( length == bytes2 );
3304
3305 /* Compare the outputs from the two runs. */
3306 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3307
3308exit:
3309 mbedtls_free( output_buffer );
3310 mbedtls_free( export_buffer );
3311 psa_generator_abort( &generator );
3312 psa_destroy_key( base_key );
3313 psa_destroy_key( derived_key );
3314 mbedtls_psa_crypto_free( );
3315}
3316/* END_CASE */
3317
3318/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003319void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003320{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003321 size_t bytes = bytes_arg;
3322 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003323 unsigned char *output = NULL;
3324 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003325 size_t i;
3326 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003327
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003328 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3329 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003330 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003331
3332 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3333
Gilles Peskinea50d7392018-06-21 10:22:13 +02003334 /* Run several times, to ensure that every output byte will be
3335 * nonzero at least once with overwhelming probability
3336 * (2^(-8*number_of_runs)). */
3337 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003338 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003339 if( bytes != 0 )
3340 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003341 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3342
3343 /* Check that no more than bytes have been overwritten */
3344 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3345
3346 for( i = 0; i < bytes; i++ )
3347 {
3348 if( output[i] != 0 )
3349 ++changed[i];
3350 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003351 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003352
3353 /* Check that every byte was changed to nonzero at least once. This
3354 * validates that psa_generate_random is overwriting every byte of
3355 * the output buffer. */
3356 for( i = 0; i < bytes; i++ )
3357 {
3358 TEST_ASSERT( changed[i] != 0 );
3359 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003360
3361exit:
3362 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003363 mbedtls_free( output );
3364 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003365}
3366/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003367
3368/* BEGIN_CASE */
3369void generate_key( int type_arg,
3370 int bits_arg,
3371 int usage_arg,
3372 int alg_arg,
3373 int expected_status_arg )
3374{
3375 int slot = 1;
3376 psa_key_type_t type = type_arg;
3377 psa_key_usage_t usage = usage_arg;
3378 size_t bits = bits_arg;
3379 psa_algorithm_t alg = alg_arg;
3380 psa_status_t expected_status = expected_status_arg;
3381 psa_key_type_t got_type;
3382 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003383 psa_status_t expected_info_status =
3384 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3385 psa_key_policy_t policy;
3386
3387 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3388
3389 psa_key_policy_init( &policy );
3390 psa_key_policy_set_usage( &policy, usage, alg );
3391 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3392
3393 /* Generate a key */
3394 TEST_ASSERT( psa_generate_key( slot, type, bits,
3395 NULL, 0 ) == expected_status );
3396
3397 /* Test the key information */
3398 TEST_ASSERT( psa_get_key_information( slot,
3399 &got_type,
3400 &got_bits ) == expected_info_status );
3401 if( expected_info_status != PSA_SUCCESS )
3402 goto exit;
3403 TEST_ASSERT( got_type == type );
3404 TEST_ASSERT( got_bits == bits );
3405
Gilles Peskine818ca122018-06-20 18:16:48 +02003406 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003407 if( ! exercise_key( slot, usage, alg ) )
3408 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003409
3410exit:
3411 psa_destroy_key( slot );
3412 mbedtls_psa_crypto_free( );
3413}
3414/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003415
3416/* BEGIN_CASE */
3417void validate_module_init_generate_random( )
3418{
3419 psa_status_t status;
3420 uint8_t random[10] = { 0 };
3421 status = psa_generate_random( random, sizeof( random ) );
3422 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3423}
3424/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003425
3426/* BEGIN_CASE */
3427void validate_module_init_key_based( )
3428{
3429 psa_status_t status;
3430 uint8_t data[10] = { 0 };
3431 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3432 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3433}
3434/* END_CASE */