blob: 81ddee003c87ccd62482b6dcd9fc0e558b071860 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskine96ee5c72018-07-12 17:24:54 +020014#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
15
itayzafrir3e02b3b2018-06-12 17:06:52 +030016#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020017#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#else
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskined35a1cc2018-06-26 21:26:10 +020025/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020026 *
27 * \param buffer Pointer to the beginning of the buffer.
28 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020033static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
38 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskine818ca122018-06-20 18:16:48 +0200128static int exercise_mac_key( psa_key_slot_t key,
129 psa_key_usage_t usage,
130 psa_algorithm_t alg )
131{
132 psa_mac_operation_t operation;
133 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200134 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200135 size_t mac_length = sizeof( mac );
136
137 if( usage & PSA_KEY_USAGE_SIGN )
138 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200139 TEST_ASSERT( psa_mac_sign_setup( &operation,
140 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 TEST_ASSERT( psa_mac_update( &operation,
142 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200143 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200144 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200145 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200146 }
147
148 if( usage & PSA_KEY_USAGE_VERIFY )
149 {
150 psa_status_t verify_status =
151 ( usage & PSA_KEY_USAGE_SIGN ?
152 PSA_SUCCESS :
153 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200154 TEST_ASSERT( psa_mac_verify_setup( &operation,
155 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200156 TEST_ASSERT( psa_mac_update( &operation,
157 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200158 TEST_ASSERT( psa_mac_verify_finish( &operation,
159 mac,
160 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200161 }
162
163 return( 1 );
164
165exit:
166 psa_mac_abort( &operation );
167 return( 0 );
168}
169
170static int exercise_cipher_key( psa_key_slot_t key,
171 psa_key_usage_t usage,
172 psa_algorithm_t alg )
173{
174 psa_cipher_operation_t operation;
175 unsigned char iv[16] = {0};
176 size_t iv_length = sizeof( iv );
177 const unsigned char plaintext[16] = "Hello, world...";
178 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
179 size_t ciphertext_length = sizeof( ciphertext );
180 unsigned char decrypted[sizeof( ciphertext )];
181 size_t part_length;
182
183 if( usage & PSA_KEY_USAGE_ENCRYPT )
184 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200185 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
186 key, alg ) == PSA_SUCCESS );
187 TEST_ASSERT( psa_cipher_generate_iv( &operation,
188 iv, sizeof( iv ),
189 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200190 TEST_ASSERT( psa_cipher_update( &operation,
191 plaintext, sizeof( plaintext ),
192 ciphertext, sizeof( ciphertext ),
193 &ciphertext_length ) == PSA_SUCCESS );
194 TEST_ASSERT( psa_cipher_finish( &operation,
195 ciphertext + ciphertext_length,
196 sizeof( ciphertext ) - ciphertext_length,
197 &part_length ) == PSA_SUCCESS );
198 ciphertext_length += part_length;
199 }
200
201 if( usage & PSA_KEY_USAGE_DECRYPT )
202 {
203 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700204 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200205 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
206 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200207 size_t bits;
208 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
209 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
210 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200211 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
212 key, alg ) == PSA_SUCCESS );
213 TEST_ASSERT( psa_cipher_set_iv( &operation,
214 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200215 TEST_ASSERT( psa_cipher_update( &operation,
216 ciphertext, ciphertext_length,
217 decrypted, sizeof( decrypted ),
218 &part_length ) == PSA_SUCCESS );
219 status = psa_cipher_finish( &operation,
220 decrypted + part_length,
221 sizeof( decrypted ) - part_length,
222 &part_length );
223 /* For a stream cipher, all inputs are valid. For a block cipher,
224 * if the input is some aribtrary data rather than an actual
225 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700226 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700227 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200228 TEST_ASSERT( status == PSA_SUCCESS );
229 else
230 TEST_ASSERT( status == PSA_SUCCESS ||
231 status == PSA_ERROR_INVALID_PADDING );
232 }
233
234 return( 1 );
235
236exit:
237 psa_cipher_abort( &operation );
238 return( 0 );
239}
240
241static int exercise_aead_key( psa_key_slot_t key,
242 psa_key_usage_t usage,
243 psa_algorithm_t alg )
244{
245 unsigned char nonce[16] = {0};
246 size_t nonce_length = sizeof( nonce );
247 unsigned char plaintext[16] = "Hello, world...";
248 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
249 size_t ciphertext_length = sizeof( ciphertext );
250 size_t plaintext_length = sizeof( ciphertext );
251
252 if( usage & PSA_KEY_USAGE_ENCRYPT )
253 {
254 TEST_ASSERT( psa_aead_encrypt( key, alg,
255 nonce, nonce_length,
256 NULL, 0,
257 plaintext, sizeof( plaintext ),
258 ciphertext, sizeof( ciphertext ),
259 &ciphertext_length ) == PSA_SUCCESS );
260 }
261
262 if( usage & PSA_KEY_USAGE_DECRYPT )
263 {
264 psa_status_t verify_status =
265 ( usage & PSA_KEY_USAGE_ENCRYPT ?
266 PSA_SUCCESS :
267 PSA_ERROR_INVALID_SIGNATURE );
268 TEST_ASSERT( psa_aead_decrypt( key, alg,
269 nonce, nonce_length,
270 NULL, 0,
271 ciphertext, ciphertext_length,
272 plaintext, sizeof( plaintext ),
273 &plaintext_length ) == verify_status );
274 }
275
276 return( 1 );
277
278exit:
279 return( 0 );
280}
281
282static int exercise_signature_key( psa_key_slot_t key,
283 psa_key_usage_t usage,
284 psa_algorithm_t alg )
285{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200286 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
287 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200288 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200289 size_t signature_length = sizeof( signature );
290
291 if( usage & PSA_KEY_USAGE_SIGN )
292 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200293 /* Some algorithms require the payload to have the size of
294 * the hash encoded in the algorithm. Use this input size
295 * even for algorithms that allow other input sizes. */
296 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
297 if( hash_alg != 0 )
298 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200299 TEST_ASSERT( psa_asymmetric_sign( key, alg,
300 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200301 signature, sizeof( signature ),
302 &signature_length ) == PSA_SUCCESS );
303 }
304
305 if( usage & PSA_KEY_USAGE_VERIFY )
306 {
307 psa_status_t verify_status =
308 ( usage & PSA_KEY_USAGE_SIGN ?
309 PSA_SUCCESS :
310 PSA_ERROR_INVALID_SIGNATURE );
311 TEST_ASSERT( psa_asymmetric_verify( key, alg,
312 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200313 signature, signature_length ) ==
314 verify_status );
315 }
316
317 return( 1 );
318
319exit:
320 return( 0 );
321}
322
323static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
324 psa_key_usage_t usage,
325 psa_algorithm_t alg )
326{
327 unsigned char plaintext[256] = "Hello, world...";
328 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
329 size_t ciphertext_length = sizeof( ciphertext );
330 size_t plaintext_length = 16;
331
332 if( usage & PSA_KEY_USAGE_ENCRYPT )
333 {
334 TEST_ASSERT(
335 psa_asymmetric_encrypt( key, alg,
336 plaintext, plaintext_length,
337 NULL, 0,
338 ciphertext, sizeof( ciphertext ),
339 &ciphertext_length ) == PSA_SUCCESS );
340 }
341
342 if( usage & PSA_KEY_USAGE_DECRYPT )
343 {
344 psa_status_t status =
345 psa_asymmetric_decrypt( key, alg,
346 ciphertext, ciphertext_length,
347 NULL, 0,
348 plaintext, sizeof( plaintext ),
349 &plaintext_length );
350 TEST_ASSERT( status == PSA_SUCCESS ||
351 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
352 ( status == PSA_ERROR_INVALID_ARGUMENT ||
353 status == PSA_ERROR_INVALID_PADDING ) ) );
354 }
355
356 return( 1 );
357
358exit:
359 return( 0 );
360}
Gilles Peskine02b75072018-07-01 22:31:34 +0200361
Gilles Peskineea0fb492018-07-12 17:17:20 +0200362static int exercise_key_derivation_key( psa_key_slot_t key,
363 psa_key_usage_t usage,
364 psa_algorithm_t alg )
365{
366 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
367 unsigned char label[16] = "This is a label.";
368 size_t label_length = sizeof( label );
369 unsigned char seed[16] = "abcdefghijklmnop";
370 size_t seed_length = sizeof( seed );
371 unsigned char output[1];
372
373 if( usage & PSA_KEY_USAGE_DERIVE )
374 {
375 TEST_ASSERT( psa_key_derivation( &generator,
376 key, alg,
377 label, label_length,
378 seed, seed_length,
379 sizeof( output ) ) == PSA_SUCCESS );
380 TEST_ASSERT( psa_generator_read( &generator,
381 output,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
384 }
385
386 return( 1 );
387
388exit:
389 return( 0 );
390}
391
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200392static int is_oid_of_key_type( psa_key_type_t type,
393 const uint8_t *oid, size_t oid_length )
394{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200395 const uint8_t *expected_oid = NULL;
396 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200397#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200398 if( PSA_KEY_TYPE_IS_RSA( type ) )
399 {
400 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
401 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
402 }
403 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200404#endif /* MBEDTLS_RSA_C */
405#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200406 if( PSA_KEY_TYPE_IS_ECC( type ) )
407 {
408 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
409 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
410 }
411 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200412#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200413 {
414 char message[40];
415 mbedtls_snprintf( message, sizeof( message ),
416 "OID not known for key type=0x%08lx",
417 (unsigned long) type );
418 test_fail( message, __LINE__, __FILE__ );
419 return( 0 );
420 }
421
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200422 TEST_ASSERT( oid_length == expected_oid_length );
423 TEST_ASSERT( memcmp( oid, expected_oid, oid_length ) == 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200424 return( 1 );
425
426exit:
427 return( 0 );
428}
429
430static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
431 size_t min_bits, size_t max_bits,
432 int must_be_odd )
433{
434 size_t len;
435 size_t actual_bits;
436 unsigned char msb;
437 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
438 MBEDTLS_ASN1_INTEGER ) == 0 );
439 /* Tolerate a slight departure from DER encoding:
440 * - 0 may be represented by an empty string or a 1-byte string.
441 * - The sign bit may be used as a value bit. */
442 if( ( len == 1 && ( *p )[0] == 0 ) ||
443 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
444 {
445 ++( *p );
446 --len;
447 }
448 if( min_bits == 0 && len == 0 )
449 return( 1 );
450 msb = ( *p )[0];
451 TEST_ASSERT( msb != 0 );
452 actual_bits = 8 * ( len - 1 );
453 while( msb != 0 )
454 {
455 msb >>= 1;
456 ++actual_bits;
457 }
458 TEST_ASSERT( actual_bits >= min_bits );
459 TEST_ASSERT( actual_bits <= max_bits );
460 if( must_be_odd )
461 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
462 *p += len;
463 return( 1 );
464exit:
465 return( 0 );
466}
467
468static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
469 size_t *len,
470 unsigned char n, unsigned char tag )
471{
472 int ret;
473 ret = mbedtls_asn1_get_tag( p, end, len,
474 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
475 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
476 if( ret != 0 )
477 return( ret );
478 end = *p + *len;
479 ret = mbedtls_asn1_get_tag( p, end, len, tag );
480 if( ret != 0 )
481 return( ret );
482 if( *p + *len != end )
483 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
484 return( 0 );
485}
486
487static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
488 uint8_t *exported, size_t exported_length )
489{
490 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200491 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200492 else
493 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200494
495#if defined(MBEDTLS_DES_C)
496 if( type == PSA_KEY_TYPE_DES )
497 {
498 /* Check the parity bits. */
499 unsigned i;
500 for( i = 0; i < bits / 8; i++ )
501 {
502 unsigned bit_count = 0;
503 unsigned m;
504 for( m = 1; m <= 0x100; m <<= 1 )
505 {
506 if( exported[i] & m )
507 ++bit_count;
508 }
509 TEST_ASSERT( bit_count % 2 != 0 );
510 }
511 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200512 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200513#endif
514
515#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
516 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
517 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200518 uint8_t *p = exported;
519 uint8_t *end = exported + exported_length;
520 size_t len;
521 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200522 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200523 * modulus INTEGER, -- n
524 * publicExponent INTEGER, -- e
525 * privateExponent INTEGER, -- d
526 * prime1 INTEGER, -- p
527 * prime2 INTEGER, -- q
528 * exponent1 INTEGER, -- d mod (p-1)
529 * exponent2 INTEGER, -- d mod (q-1)
530 * coefficient INTEGER, -- (inverse of q) mod p
531 * }
532 */
533 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
534 MBEDTLS_ASN1_SEQUENCE |
535 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
536 TEST_ASSERT( p + len == end );
537 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
538 goto exit;
539 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
540 goto exit;
541 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
542 goto exit;
543 /* Require d to be at least half the size of n. */
544 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
545 goto exit;
546 /* Require p and q to be at most half the size of n, rounded up. */
547 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
548 goto exit;
549 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
550 goto exit;
551 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
552 goto exit;
553 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
554 goto exit;
555 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
556 goto exit;
557 TEST_ASSERT( p == end );
558 }
559 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200560#endif /* MBEDTLS_RSA_C */
561
562#if defined(MBEDTLS_ECP_C)
563 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
564 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200565 uint8_t *p = exported;
566 uint8_t *end = exported + exported_length;
567 size_t len;
568 int version;
569 /* ECPrivateKey ::= SEQUENCE {
570 * version INTEGER, -- must be 1
571 * privateKey OCTET STRING,
572 * -- `ceiling(log_{256}(n))`-byte string, big endian,
573 * -- where n is the order of the curve.
574 * parameters ECParameters {{ NamedCurve }}, -- mandatory
575 * publicKey BIT STRING -- mandatory
576 * }
577 */
578 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
579 MBEDTLS_ASN1_SEQUENCE |
580 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
581 TEST_ASSERT( p + len == end );
582 TEST_ASSERT( mbedtls_asn1_get_int( &p, end, &version ) == 0 );
583 TEST_ASSERT( version == 1 );
584 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
585 MBEDTLS_ASN1_OCTET_STRING ) == 0 );
586 /* Bug in Mbed TLS: the length of the octet string depends on the value */
587 // TEST_ASSERT( len == PSA_BITS_TO_BYTES( bits ) );
588 p += len;
589 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 0,
590 MBEDTLS_ASN1_OID ) == 0 );
591 p += len;
Gilles Peskinec6290c02018-08-13 17:24:59 +0200592 /* publicKey: ECPoint in uncompressed representation (as below) */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200593 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 1,
594 MBEDTLS_ASN1_BIT_STRING ) == 0 );
595 TEST_ASSERT( p + len == end );
596 TEST_ASSERT( p[0] == 0 ); /* 0 unused bits in the bit string */
597 ++p;
598 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
599 TEST_ASSERT( p[0] == 4 );
600 }
601 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200602#endif /* MBEDTLS_ECP_C */
603
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200604 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
605 {
606 uint8_t *p = exported;
607 uint8_t *end = exported + exported_length;
608 size_t len;
609 mbedtls_asn1_buf alg;
610 mbedtls_asn1_buf params;
611 mbedtls_asn1_bitstring bitstring;
612 /* SubjectPublicKeyInfo ::= SEQUENCE {
613 * algorithm AlgorithmIdentifier,
614 * subjectPublicKey BIT STRING }
615 * AlgorithmIdentifier ::= SEQUENCE {
616 * algorithm OBJECT IDENTIFIER,
617 * parameters ANY DEFINED BY algorithm OPTIONAL }
618 */
619 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
620 MBEDTLS_ASN1_SEQUENCE |
621 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
622 TEST_ASSERT( p + len == end );
623 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
624 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
625 goto exit;
626 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
627 TEST_ASSERT( p == end );
628 p = bitstring.p;
629#if defined(MBEDTLS_RSA_C)
630 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
631 {
632 /* RSAPublicKey ::= SEQUENCE {
633 * modulus INTEGER, -- n
634 * publicExponent INTEGER } -- e
635 */
636 TEST_ASSERT( bitstring.unused_bits == 0 );
637 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
638 MBEDTLS_ASN1_SEQUENCE |
639 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
640 TEST_ASSERT( p + len == end );
641 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
642 goto exit;
643 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
644 goto exit;
645 TEST_ASSERT( p == end );
646 }
647 else
648#endif /* MBEDTLS_RSA_C */
649#if defined(MBEDTLS_ECP_C)
650 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
651 {
652 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200653 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200654 * -- then x_P as an n-bit string, big endian;
655 * -- then y_P as a n-bit string, big endian,
656 * -- where n is the order of the curve.
657 */
658 TEST_ASSERT( bitstring.unused_bits == 0 );
659 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
660 TEST_ASSERT( p[0] == 4 );
661 }
662 else
663#endif /* MBEDTLS_ECP_C */
664 {
665 char message[40];
666 mbedtls_snprintf( message, sizeof( message ),
667 "No sanity check for public key type=0x%08lx",
668 (unsigned long) type );
669 test_fail( message, __LINE__, __FILE__ );
670 return( 0 );
671 }
672 }
673 else
674
675 {
676 /* No sanity checks for other types */
677 }
678
679 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200680
681exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200683}
684
685static int exercise_export_key( psa_key_slot_t slot,
686 psa_key_usage_t usage )
687{
688 psa_key_type_t type;
689 size_t bits;
690 uint8_t *exported = NULL;
691 size_t exported_size = 0;
692 size_t exported_length = 0;
693 int ok = 0;
694
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200695 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
696
697 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
698 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200699 {
700 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
701 PSA_ERROR_NOT_PERMITTED );
702 return( 1 );
703 }
704
Gilles Peskined14664a2018-08-10 19:07:32 +0200705 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
706 exported = mbedtls_calloc( 1, exported_size );
707 TEST_ASSERT( exported != NULL );
708
709 TEST_ASSERT( psa_export_key( slot,
710 exported, exported_size,
711 &exported_length ) == PSA_SUCCESS );
712 ok = exported_key_sanity_check( type, bits, exported, exported_length );
713
714exit:
715 mbedtls_free( exported );
716 return( ok );
717}
718
719static int exercise_export_public_key( psa_key_slot_t slot )
720{
721 psa_key_type_t type;
722 psa_key_type_t public_type;
723 size_t bits;
724 uint8_t *exported = NULL;
725 size_t exported_size = 0;
726 size_t exported_length = 0;
727 int ok = 0;
728
729 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
730 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
731 {
732 TEST_ASSERT( psa_export_public_key( slot,
733 NULL, 0, &exported_length ) ==
734 PSA_ERROR_INVALID_ARGUMENT );
735 return( 1 );
736 }
737
738 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
739 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
740 exported = mbedtls_calloc( 1, exported_size );
741 TEST_ASSERT( exported != NULL );
742
743 TEST_ASSERT( psa_export_public_key( slot,
744 exported, exported_size,
745 &exported_length ) == PSA_SUCCESS );
746 ok = exported_key_sanity_check( public_type, bits,
747 exported, exported_length );
748
749exit:
750 mbedtls_free( exported );
751 return( ok );
752}
753
Gilles Peskine02b75072018-07-01 22:31:34 +0200754static int exercise_key( psa_key_slot_t slot,
755 psa_key_usage_t usage,
756 psa_algorithm_t alg )
757{
758 int ok;
759 if( alg == 0 )
760 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
761 else if( PSA_ALG_IS_MAC( alg ) )
762 ok = exercise_mac_key( slot, usage, alg );
763 else if( PSA_ALG_IS_CIPHER( alg ) )
764 ok = exercise_cipher_key( slot, usage, alg );
765 else if( PSA_ALG_IS_AEAD( alg ) )
766 ok = exercise_aead_key( slot, usage, alg );
767 else if( PSA_ALG_IS_SIGN( alg ) )
768 ok = exercise_signature_key( slot, usage, alg );
769 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
770 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200771 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
772 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 else
774 {
775 char message[40];
776 mbedtls_snprintf( message, sizeof( message ),
777 "No code to exercise alg=0x%08lx",
778 (unsigned long) alg );
779 test_fail( message, __LINE__, __FILE__ );
780 ok = 0;
781 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200782
783 ok = ok && exercise_export_key( slot, usage );
784 ok = ok && exercise_export_public_key( slot );
785
Gilles Peskine02b75072018-07-01 22:31:34 +0200786 return( ok );
787}
788
Gilles Peskinee59236f2018-01-27 23:32:46 +0100789/* END_HEADER */
790
791/* BEGIN_DEPENDENCIES
792 * depends_on:MBEDTLS_PSA_CRYPTO_C
793 * END_DEPENDENCIES
794 */
795
796/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200797void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100798{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100800 int i;
801 for( i = 0; i <= 1; i++ )
802 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 status = psa_crypto_init( );
804 TEST_ASSERT( status == PSA_SUCCESS );
805 status = psa_crypto_init( );
806 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100807 mbedtls_psa_crypto_free( );
808 }
809}
810/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811
812/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200813void fill_slots( int max_arg )
814{
815 /* Fill all the slots until we run out of memory or out of slots,
816 * or until some limit specified in the test data for the sake of
817 * implementations with an essentially unlimited number of slots.
818 * This test assumes that available slots are numbered from 1. */
819
820 psa_key_slot_t slot;
821 psa_key_slot_t max = 0;
822 psa_key_policy_t policy;
823 uint8_t exported[sizeof( max )];
824 size_t exported_size;
825 psa_status_t status;
826
827 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
828
829 psa_key_policy_init( &policy );
830 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
831
832 for( max = 1; max <= (size_t) max_arg; max++ )
833 {
834 status = psa_set_key_policy( max, &policy );
835 /* Stop filling slots if we run out of memory or out of
836 * available slots. */
837 TEST_ASSERT( status == PSA_SUCCESS ||
838 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
839 status == PSA_ERROR_INVALID_ARGUMENT );
840 if( status != PSA_SUCCESS )
841 break;
842 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
843 (uint8_t*) &max, sizeof( max ) );
844 /* Since psa_set_key_policy succeeded, we know that the slot
845 * number is valid. But we may legitimately run out of memory. */
846 TEST_ASSERT( status == PSA_SUCCESS ||
847 status == PSA_ERROR_INSUFFICIENT_MEMORY );
848 if( status != PSA_SUCCESS )
849 break;
850 }
851 /* `max` is now the first slot number that wasn't filled. */
852 max -= 1;
853
854 for( slot = 1; slot <= max; slot++ )
855 {
856 TEST_ASSERT( psa_export_key( slot,
857 exported, sizeof( exported ),
858 &exported_size ) == PSA_SUCCESS );
859 TEST_ASSERT( exported_size == sizeof( slot ) );
860 TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
Gilles Peskine996deb12018-08-01 15:45:45 +0200861 }
862
863exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200864 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200865 mbedtls_psa_crypto_free( );
866}
867/* END_CASE */
868
869/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200870void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871{
872 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200873 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300877 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
879
Gilles Peskine4abf7412018-06-18 16:35:34 +0200880 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200881 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882 if( status == PSA_SUCCESS )
883 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
884
885exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 mbedtls_psa_crypto_free( );
887}
888/* END_CASE */
889
890/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200891void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
892{
893 int slot = 1;
894 size_t bits = bits_arg;
895 psa_status_t expected_status = expected_status_arg;
896 psa_status_t status;
897 psa_key_type_t type =
898 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
899 size_t buffer_size = /* Slight overapproximations */
900 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
901 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
902 unsigned char *p;
903 int ret;
904 size_t length;
905
906 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
907 TEST_ASSERT( buffer != NULL );
908
909 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
910 bits, keypair ) ) >= 0 );
911 length = ret;
912
913 /* Try importing the key */
914 status = psa_import_key( slot, type, p, length );
915 TEST_ASSERT( status == expected_status );
916 if( status == PSA_SUCCESS )
917 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
918
919exit:
920 mbedtls_free( buffer );
921 mbedtls_psa_crypto_free( );
922}
923/* END_CASE */
924
925/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300926void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300927 int type_arg,
928 int alg_arg,
929 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100930 int expected_bits,
931 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200932 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100933 int canonical_input )
934{
935 int slot = 1;
936 int slot2 = slot + 1;
937 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200938 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200939 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 unsigned char *exported = NULL;
942 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100944 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945 size_t reexported_length;
946 psa_key_type_t got_type;
947 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200948 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300951 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300952 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 exported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100954 TEST_ASSERT( export_size == 0 || exported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955 if( ! canonical_input )
956 {
957 reexported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100958 TEST_ASSERT( export_size == 0 || reexported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959 }
960 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
961
mohammad1603a97cb8c2018-03-28 03:46:26 -0700962 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200963 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700964 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
965
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 /* Import the key */
967 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200968 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969
970 /* Test the key information */
971 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200972 &got_type,
973 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 TEST_ASSERT( got_type == type );
975 TEST_ASSERT( got_bits == (size_t) expected_bits );
976
977 /* Export the key */
978 status = psa_export_key( slot,
979 exported, export_size,
980 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200981 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100982
983 /* The exported length must be set by psa_export_key() to a value between 0
984 * and export_size. On errors, the exported length must be 0. */
985 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
986 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
987 TEST_ASSERT( exported_length <= export_size );
988
Gilles Peskine3f669c32018-06-21 09:21:51 +0200989 TEST_ASSERT( mem_is_zero( exported + exported_length,
990 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200992 {
993 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200995 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996
Gilles Peskine8f609232018-08-11 01:24:55 +0200997 if( ! exercise_export_key( slot, usage_arg ) )
998 goto exit;
999
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 if( canonical_input )
1001 {
Gilles Peskine4abf7412018-06-18 16:35:34 +02001002 TEST_ASSERT( exported_length == data->len );
1003 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004 }
1005 else
1006 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001007 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1008
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001010 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001011 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001013 reexported,
1014 export_size,
1015 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016 TEST_ASSERT( reexported_length == exported_length );
1017 TEST_ASSERT( memcmp( reexported, exported,
1018 exported_length ) == 0 );
1019 }
1020
1021destroy:
1022 /* Destroy the key */
1023 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1024 TEST_ASSERT( psa_get_key_information(
1025 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1026
1027exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001028 mbedtls_free( exported );
1029 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030 mbedtls_psa_crypto_free( );
1031}
1032/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001033
Moran Pekerf709f4a2018-06-06 17:26:04 +03001034/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001035void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001036 int type_arg,
1037 int alg_arg,
1038 int expected_bits,
1039 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001040 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041{
1042 int slot = 1;
1043 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001044 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001045 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001046 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001047 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001048 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001049 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001050 psa_key_type_t got_type;
1051 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001052 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001053
Moran Pekerf709f4a2018-06-06 17:26:04 +03001054 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001055 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001056 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001057 exported = mbedtls_calloc( 1, export_size );
1058 TEST_ASSERT( exported != NULL );
1059
1060 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1061
1062 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001063 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001064 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1065
1066 /* Import the key */
1067 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001068 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001069
1070 /* Test the key information */
1071 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001072 &got_type,
1073 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001074 TEST_ASSERT( got_type == type );
1075 TEST_ASSERT( got_bits == (size_t) expected_bits );
1076
1077 /* Export the key */
1078 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001079 exported, export_size,
1080 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001081 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001082 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
1083 TEST_ASSERT( mem_is_zero( exported + exported_length,
1084 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001085 if( status != PSA_SUCCESS )
1086 goto destroy;
1087
Moran Pekerf709f4a2018-06-06 17:26:04 +03001088destroy:
1089 /* Destroy the key */
1090 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1091 TEST_ASSERT( psa_get_key_information(
1092 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1093
1094exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001095 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001096 mbedtls_psa_crypto_free( );
1097}
1098/* END_CASE */
1099
Gilles Peskine20035e32018-02-03 22:44:14 +01001100/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001101void import_and_exercise_key( data_t *data,
1102 int type_arg,
1103 int bits_arg,
1104 int alg_arg )
1105{
1106 int slot = 1;
1107 psa_key_type_t type = type_arg;
1108 size_t bits = bits_arg;
1109 psa_algorithm_t alg = alg_arg;
1110 psa_key_usage_t usage =
1111 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1112 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1113 PSA_KEY_USAGE_VERIFY :
1114 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1115 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1116 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1117 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1118 PSA_KEY_USAGE_ENCRYPT :
1119 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001120 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001121 0 );
1122 psa_key_policy_t policy;
1123 psa_key_type_t got_type;
1124 size_t got_bits;
1125 psa_status_t status;
1126
1127 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1128
1129 psa_key_policy_init( &policy );
1130 psa_key_policy_set_usage( &policy, usage, alg );
1131 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1132
1133 /* Import the key */
1134 status = psa_import_key( slot, type, data->x, data->len );
1135 TEST_ASSERT( status == PSA_SUCCESS );
1136
1137 /* Test the key information */
1138 TEST_ASSERT( psa_get_key_information( slot,
1139 &got_type,
1140 &got_bits ) == PSA_SUCCESS );
1141 TEST_ASSERT( got_type == type );
1142 TEST_ASSERT( got_bits == bits );
1143
1144 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001145 if( ! exercise_key( slot, usage, alg ) )
1146 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001147
1148exit:
1149 psa_destroy_key( slot );
1150 mbedtls_psa_crypto_free( );
1151}
1152/* END_CASE */
1153
1154/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001155void key_policy( int usage_arg, int alg_arg )
1156{
1157 int key_slot = 1;
1158 psa_algorithm_t alg = alg_arg;
1159 psa_key_usage_t usage = usage_arg;
1160 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1161 unsigned char key[32] = {0};
1162 psa_key_policy_t policy_set;
1163 psa_key_policy_t policy_get;
1164
1165 memset( key, 0x2a, sizeof( key ) );
1166
1167 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1168
1169 psa_key_policy_init( &policy_set );
1170 psa_key_policy_init( &policy_get );
1171
1172 psa_key_policy_set_usage( &policy_set, usage, alg );
1173
1174 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1175 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1176 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1177
1178 TEST_ASSERT( psa_import_key( key_slot, key_type,
1179 key, sizeof( key ) ) == PSA_SUCCESS );
1180
1181 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1182
1183 TEST_ASSERT( policy_get.usage == policy_set.usage );
1184 TEST_ASSERT( policy_get.alg == policy_set.alg );
1185
1186exit:
1187 psa_destroy_key( key_slot );
1188 mbedtls_psa_crypto_free( );
1189}
1190/* END_CASE */
1191
1192/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001193void mac_key_policy( int policy_usage,
1194 int policy_alg,
1195 int key_type,
1196 data_t *key_data,
1197 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001198{
1199 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001200 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201 psa_mac_operation_t operation;
1202 psa_status_t status;
1203 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001204
1205 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1206
1207 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001209 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1210
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 TEST_ASSERT( psa_import_key( key_slot, key_type,
1212 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001213
Gilles Peskine89167cb2018-07-08 20:12:23 +02001214 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 if( policy_alg == exercise_alg &&
1216 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1217 TEST_ASSERT( status == PSA_SUCCESS );
1218 else
1219 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1220 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001221
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001223 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224 if( policy_alg == exercise_alg &&
1225 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001226 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227 else
1228 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1229
1230exit:
1231 psa_mac_abort( &operation );
1232 psa_destroy_key( key_slot );
1233 mbedtls_psa_crypto_free( );
1234}
1235/* END_CASE */
1236
1237/* BEGIN_CASE */
1238void cipher_key_policy( int policy_usage,
1239 int policy_alg,
1240 int key_type,
1241 data_t *key_data,
1242 int exercise_alg )
1243{
1244 int key_slot = 1;
1245 psa_key_policy_t policy;
1246 psa_cipher_operation_t operation;
1247 psa_status_t status;
1248
1249 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1250
1251 psa_key_policy_init( &policy );
1252 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1253 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1254
1255 TEST_ASSERT( psa_import_key( key_slot, key_type,
1256 key_data->x, key_data->len ) == PSA_SUCCESS );
1257
Gilles Peskinefe119512018-07-08 21:39:34 +02001258 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259 if( policy_alg == exercise_alg &&
1260 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1261 TEST_ASSERT( status == PSA_SUCCESS );
1262 else
1263 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1264 psa_cipher_abort( &operation );
1265
Gilles Peskinefe119512018-07-08 21:39:34 +02001266 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001267 if( policy_alg == exercise_alg &&
1268 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1269 TEST_ASSERT( status == PSA_SUCCESS );
1270 else
1271 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1272
1273exit:
1274 psa_cipher_abort( &operation );
1275 psa_destroy_key( key_slot );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
1281void aead_key_policy( int policy_usage,
1282 int policy_alg,
1283 int key_type,
1284 data_t *key_data,
1285 int nonce_length_arg,
1286 int tag_length_arg,
1287 int exercise_alg )
1288{
1289 int key_slot = 1;
1290 psa_key_policy_t policy;
1291 psa_status_t status;
1292 unsigned char nonce[16] = {0};
1293 size_t nonce_length = nonce_length_arg;
1294 unsigned char tag[16];
1295 size_t tag_length = tag_length_arg;
1296 size_t output_length;
1297
1298 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1299 TEST_ASSERT( tag_length <= sizeof( tag ) );
1300
1301 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1302
1303 psa_key_policy_init( &policy );
1304 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1305 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1306
1307 TEST_ASSERT( psa_import_key( key_slot, key_type,
1308 key_data->x, key_data->len ) == PSA_SUCCESS );
1309
1310 status = psa_aead_encrypt( key_slot, exercise_alg,
1311 nonce, nonce_length,
1312 NULL, 0,
1313 NULL, 0,
1314 tag, tag_length,
1315 &output_length );
1316 if( policy_alg == exercise_alg &&
1317 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1318 TEST_ASSERT( status == PSA_SUCCESS );
1319 else
1320 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1321
1322 memset( tag, 0, sizeof( tag ) );
1323 status = psa_aead_decrypt( key_slot, exercise_alg,
1324 nonce, nonce_length,
1325 NULL, 0,
1326 tag, tag_length,
1327 NULL, 0,
1328 &output_length );
1329 if( policy_alg == exercise_alg &&
1330 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1331 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1332 else
1333 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1334
1335exit:
1336 psa_destroy_key( key_slot );
1337 mbedtls_psa_crypto_free( );
1338}
1339/* END_CASE */
1340
1341/* BEGIN_CASE */
1342void asymmetric_encryption_key_policy( int policy_usage,
1343 int policy_alg,
1344 int key_type,
1345 data_t *key_data,
1346 int exercise_alg )
1347{
1348 int key_slot = 1;
1349 psa_key_policy_t policy;
1350 psa_status_t status;
1351 size_t key_bits;
1352 size_t buffer_length;
1353 unsigned char *buffer = NULL;
1354 size_t output_length;
1355
1356 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1357
1358 psa_key_policy_init( &policy );
1359 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1360 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1361
1362 TEST_ASSERT( psa_import_key( key_slot, key_type,
1363 key_data->x, key_data->len ) == PSA_SUCCESS );
1364
1365 TEST_ASSERT( psa_get_key_information( key_slot,
1366 NULL,
1367 &key_bits ) == PSA_SUCCESS );
1368 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1369 exercise_alg );
1370 buffer = mbedtls_calloc( 1, buffer_length );
1371 TEST_ASSERT( buffer != NULL );
1372
1373 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1374 NULL, 0,
1375 NULL, 0,
1376 buffer, buffer_length,
1377 &output_length );
1378 if( policy_alg == exercise_alg &&
1379 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1380 TEST_ASSERT( status == PSA_SUCCESS );
1381 else
1382 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1383
1384 memset( buffer, 0, buffer_length );
1385 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1386 buffer, buffer_length,
1387 NULL, 0,
1388 buffer, buffer_length,
1389 &output_length );
1390 if( policy_alg == exercise_alg &&
1391 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1392 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1393 else
1394 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1395
1396exit:
1397 psa_destroy_key( key_slot );
1398 mbedtls_psa_crypto_free( );
1399 mbedtls_free( buffer );
1400}
1401/* END_CASE */
1402
1403/* BEGIN_CASE */
1404void asymmetric_signature_key_policy( int policy_usage,
1405 int policy_alg,
1406 int key_type,
1407 data_t *key_data,
1408 int exercise_alg )
1409{
1410 int key_slot = 1;
1411 psa_key_policy_t policy;
1412 psa_status_t status;
1413 unsigned char payload[16] = {1};
1414 size_t payload_length = sizeof( payload );
1415 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1416 size_t signature_length;
1417
1418 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1419
1420 psa_key_policy_init( &policy );
1421 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1422 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1423
1424 TEST_ASSERT( psa_import_key( key_slot, key_type,
1425 key_data->x, key_data->len ) == PSA_SUCCESS );
1426
1427 status = psa_asymmetric_sign( key_slot, exercise_alg,
1428 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001429 signature, sizeof( signature ),
1430 &signature_length );
1431 if( policy_alg == exercise_alg &&
1432 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1433 TEST_ASSERT( status == PSA_SUCCESS );
1434 else
1435 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1436
1437 memset( signature, 0, sizeof( signature ) );
1438 status = psa_asymmetric_verify( key_slot, exercise_alg,
1439 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440 signature, sizeof( signature ) );
1441 if( policy_alg == exercise_alg &&
1442 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1443 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1444 else
1445 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001446
1447exit:
1448 psa_destroy_key( key_slot );
1449 mbedtls_psa_crypto_free( );
1450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001454void derive_key_policy( int policy_usage,
1455 int policy_alg,
1456 int key_type,
1457 data_t *key_data,
1458 int exercise_alg )
1459{
1460 int key_slot = 1;
1461 psa_key_policy_t policy;
1462 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1463 psa_status_t status;
1464
1465 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1466
1467 psa_key_policy_init( &policy );
1468 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1469 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1470
1471 TEST_ASSERT( psa_import_key( key_slot, key_type,
1472 key_data->x, key_data->len ) == PSA_SUCCESS );
1473
1474 status = psa_key_derivation( &generator, key_slot,
1475 exercise_alg,
1476 NULL, 0,
1477 NULL, 0,
1478 1 );
1479 if( policy_alg == exercise_alg &&
1480 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1481 TEST_ASSERT( status == PSA_SUCCESS );
1482 else
1483 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1484
1485exit:
1486 psa_generator_abort( &generator );
1487 psa_destroy_key( key_slot );
1488 mbedtls_psa_crypto_free( );
1489}
1490/* END_CASE */
1491
1492/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001493void key_lifetime( int lifetime_arg )
1494{
1495 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001496 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001497 unsigned char key[32] = {0};
1498 psa_key_lifetime_t lifetime_set = lifetime_arg;
1499 psa_key_lifetime_t lifetime_get;
1500
1501 memset( key, 0x2a, sizeof( key ) );
1502
1503 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1504
1505 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1506 lifetime_set ) == PSA_SUCCESS );
1507
1508 TEST_ASSERT( psa_import_key( key_slot, key_type,
1509 key, sizeof( key ) ) == PSA_SUCCESS );
1510
1511 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1512 &lifetime_get ) == PSA_SUCCESS );
1513
1514 TEST_ASSERT( lifetime_get == lifetime_set );
1515
1516exit:
1517 psa_destroy_key( key_slot );
1518 mbedtls_psa_crypto_free( );
1519}
1520/* END_CASE */
1521
1522/* BEGIN_CASE */
1523void key_lifetime_set_fail( int key_slot_arg,
1524 int lifetime_arg,
1525 int expected_status_arg )
1526{
1527 psa_key_slot_t key_slot = key_slot_arg;
1528 psa_key_lifetime_t lifetime_set = lifetime_arg;
1529 psa_status_t actual_status;
1530 psa_status_t expected_status = expected_status_arg;
1531
1532 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1533
1534 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1535
1536 if( actual_status == PSA_SUCCESS )
1537 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1538
1539 TEST_ASSERT( expected_status == actual_status );
1540
1541exit:
1542 psa_destroy_key( key_slot );
1543 mbedtls_psa_crypto_free( );
1544}
1545/* END_CASE */
1546
1547/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001548void hash_setup( int alg_arg,
1549 int expected_status_arg )
1550{
1551 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001552 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001553 psa_hash_operation_t operation;
1554 psa_status_t status;
1555
1556 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1557
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001558 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001559 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001560 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001561
1562exit:
1563 mbedtls_psa_crypto_free( );
1564}
1565/* END_CASE */
1566
1567/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001568void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001569{
1570 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001571 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001572 size_t actual_hash_length;
1573 psa_hash_operation_t operation;
1574
Gilles Peskine69c12672018-06-28 00:07:19 +02001575 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1576 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1577
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001579 TEST_ASSERT( expected_hash != NULL );
1580 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1581 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001582
1583 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1584
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001585 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001587 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001588 TEST_ASSERT( psa_hash_finish( &operation,
1589 actual_hash, sizeof( actual_hash ),
1590 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001591 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001592 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001593 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001594
1595exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001596 mbedtls_psa_crypto_free( );
1597}
1598/* END_CASE */
1599
1600/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001601void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001602{
1603 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001604 psa_hash_operation_t operation;
1605
Gilles Peskine69c12672018-06-28 00:07:19 +02001606 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1607 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1608
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001609 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001610 TEST_ASSERT( expected_hash != NULL );
1611 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1612 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001613
1614 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1615
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001616 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001617 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001618 input->x,
1619 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001620 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001621 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001622 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001623
1624exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001625 mbedtls_psa_crypto_free( );
1626}
1627/* END_CASE */
1628
1629/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001630void mac_setup( int key_type_arg,
1631 data_t *key,
1632 int alg_arg,
1633 int expected_status_arg )
1634{
1635 int key_slot = 1;
1636 psa_key_type_t key_type = key_type_arg;
1637 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001638 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001639 psa_mac_operation_t operation;
1640 psa_key_policy_t policy;
1641 psa_status_t status;
1642
1643 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1644
1645 psa_key_policy_init( &policy );
1646 psa_key_policy_set_usage( &policy,
1647 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1648 alg );
1649 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1650
1651 TEST_ASSERT( psa_import_key( key_slot, key_type,
1652 key->x, key->len ) == PSA_SUCCESS );
1653
Gilles Peskine89167cb2018-07-08 20:12:23 +02001654 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001655 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001656 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001657
1658exit:
1659 psa_destroy_key( key_slot );
1660 mbedtls_psa_crypto_free( );
1661}
1662/* END_CASE */
1663
1664/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001665void mac_verify( int key_type_arg,
1666 data_t *key,
1667 int alg_arg,
1668 data_t *input,
1669 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001670{
1671 int key_slot = 1;
1672 psa_key_type_t key_type = key_type_arg;
1673 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001674 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001675 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676
Gilles Peskine69c12672018-06-28 00:07:19 +02001677 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1678
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001682 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001683 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1684 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685
1686 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1687
mohammad16036df908f2018-04-02 08:34:15 -07001688 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001689 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001690 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1691
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001693 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001694
Gilles Peskine89167cb2018-07-08 20:12:23 +02001695 TEST_ASSERT( psa_mac_verify_setup( &operation,
1696 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1698 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001699 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001700 TEST_ASSERT( psa_mac_verify_finish( &operation,
1701 expected_mac->x,
1702 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001703
1704exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001705 psa_destroy_key( key_slot );
1706 mbedtls_psa_crypto_free( );
1707}
1708/* END_CASE */
1709
1710/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001711void cipher_setup( int key_type_arg,
1712 data_t *key,
1713 int alg_arg,
1714 int expected_status_arg )
1715{
1716 int key_slot = 1;
1717 psa_key_type_t key_type = key_type_arg;
1718 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001719 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001720 psa_cipher_operation_t operation;
1721 psa_key_policy_t policy;
1722 psa_status_t status;
1723
1724 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1725
1726 psa_key_policy_init( &policy );
1727 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1728 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1729
1730 TEST_ASSERT( psa_import_key( key_slot, key_type,
1731 key->x, key->len ) == PSA_SUCCESS );
1732
Gilles Peskinefe119512018-07-08 21:39:34 +02001733 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001734 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001735 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001736
1737exit:
1738 psa_destroy_key( key_slot );
1739 mbedtls_psa_crypto_free( );
1740}
1741/* END_CASE */
1742
1743/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001744void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001745 data_t *key,
1746 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001747 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001748{
1749 int key_slot = 1;
1750 psa_status_t status;
1751 psa_key_type_t key_type = key_type_arg;
1752 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001753 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001754 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001755 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001756 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001757 size_t output_buffer_size = 0;
1758 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001759 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001760 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001761 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001762
Gilles Peskine50e586b2018-06-08 14:28:46 +02001763 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001764 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001765 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001766 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1767 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1768 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001769
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001770 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1771 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001772
1773 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1774
Moran Pekered346952018-07-05 15:22:45 +03001775 psa_key_policy_init( &policy );
1776 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1777 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1778
Gilles Peskine50e586b2018-06-08 14:28:46 +02001779 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001780 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001781
Gilles Peskinefe119512018-07-08 21:39:34 +02001782 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1783 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001784
Gilles Peskinefe119512018-07-08 21:39:34 +02001785 TEST_ASSERT( psa_cipher_set_iv( &operation,
1786 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001787 output_buffer_size = (size_t) input->len +
1788 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001789 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001790 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001791
Gilles Peskine4abf7412018-06-18 16:35:34 +02001792 TEST_ASSERT( psa_cipher_update( &operation,
1793 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001794 output, output_buffer_size,
1795 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001796 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001797 status = psa_cipher_finish( &operation,
1798 output + function_output_length,
1799 output_buffer_size,
1800 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001801 total_output_length += function_output_length;
1802
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001803 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001804 if( expected_status == PSA_SUCCESS )
1805 {
1806 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001807 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001808 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001809 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001810 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001811
Gilles Peskine50e586b2018-06-08 14:28:46 +02001812exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001813 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001814 psa_destroy_key( key_slot );
1815 mbedtls_psa_crypto_free( );
1816}
1817/* END_CASE */
1818
1819/* BEGIN_CASE */
1820void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001821 data_t *key,
1822 data_t *input,
1823 int first_part_size,
1824 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001825{
1826 int key_slot = 1;
1827 psa_key_type_t key_type = key_type_arg;
1828 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001829 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001830 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001831 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001832 size_t output_buffer_size = 0;
1833 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001834 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001835 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001836 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001837
Gilles Peskine50e586b2018-06-08 14:28:46 +02001838 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001839 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001840 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001841 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1842 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1843 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001844
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001845 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1846 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001847
1848 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1849
Moran Pekered346952018-07-05 15:22:45 +03001850 psa_key_policy_init( &policy );
1851 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1852 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1853
Gilles Peskine50e586b2018-06-08 14:28:46 +02001854 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001855 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001856
Gilles Peskinefe119512018-07-08 21:39:34 +02001857 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1858 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001859
Gilles Peskinefe119512018-07-08 21:39:34 +02001860 TEST_ASSERT( psa_cipher_set_iv( &operation,
1861 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001862 output_buffer_size = (size_t) input->len +
1863 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001864 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001865 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001866
Gilles Peskine4abf7412018-06-18 16:35:34 +02001867 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001868 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001869 output, output_buffer_size,
1870 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001871 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001872 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001873 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001874 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001875 output, output_buffer_size,
1876 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001877 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001878 TEST_ASSERT( psa_cipher_finish( &operation,
1879 output + function_output_length,
1880 output_buffer_size,
1881 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001882 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001883 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1884
Gilles Peskine4abf7412018-06-18 16:35:34 +02001885 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001886 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001887 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001888
1889exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001890 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001891 psa_destroy_key( key_slot );
1892 mbedtls_psa_crypto_free( );
1893}
1894/* END_CASE */
1895
1896/* BEGIN_CASE */
1897void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001898 data_t *key,
1899 data_t *input,
1900 int first_part_size,
1901 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001902{
1903 int key_slot = 1;
1904
1905 psa_key_type_t key_type = key_type_arg;
1906 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001907 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001908 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001909 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001910 size_t output_buffer_size = 0;
1911 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001912 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001913 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001914 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001915
Gilles Peskine50e586b2018-06-08 14:28:46 +02001916 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001917 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001918 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001919 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1920 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1921 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001922
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001923 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1924 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001925
1926 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1927
Moran Pekered346952018-07-05 15:22:45 +03001928 psa_key_policy_init( &policy );
1929 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1930 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1931
Gilles Peskine50e586b2018-06-08 14:28:46 +02001932 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001933 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001934
Gilles Peskinefe119512018-07-08 21:39:34 +02001935 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1936 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001937
Gilles Peskinefe119512018-07-08 21:39:34 +02001938 TEST_ASSERT( psa_cipher_set_iv( &operation,
1939 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001940
mohammad16033d91abe2018-07-03 13:15:54 +03001941 output_buffer_size = (size_t) input->len +
1942 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001943 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001944 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001945
Gilles Peskine4abf7412018-06-18 16:35:34 +02001946 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1947 TEST_ASSERT( psa_cipher_update( &operation,
1948 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001949 output, output_buffer_size,
1950 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001951 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001952 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001953 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001954 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001955 output, output_buffer_size,
1956 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001957 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001958 TEST_ASSERT( psa_cipher_finish( &operation,
1959 output + function_output_length,
1960 output_buffer_size,
1961 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001962 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001963 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1964
Gilles Peskine4abf7412018-06-18 16:35:34 +02001965 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001966 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001967 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001968
1969exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001970 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001971 psa_destroy_key( key_slot );
1972 mbedtls_psa_crypto_free( );
1973}
1974/* END_CASE */
1975
Gilles Peskine50e586b2018-06-08 14:28:46 +02001976/* BEGIN_CASE */
1977void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001978 data_t *key,
1979 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001980 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001981{
1982 int key_slot = 1;
1983 psa_status_t status;
1984 psa_key_type_t key_type = key_type_arg;
1985 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001986 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001987 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001988 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001989 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001990 size_t output_buffer_size = 0;
1991 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001992 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001993 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001994 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001995
Gilles Peskine50e586b2018-06-08 14:28:46 +02001996 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001997 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001998 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001999 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2000 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2001 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002002
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002003 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2004 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002005
2006 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2007
Moran Pekered346952018-07-05 15:22:45 +03002008 psa_key_policy_init( &policy );
2009 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2010 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2011
Gilles Peskine50e586b2018-06-08 14:28:46 +02002012 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002013 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002014
Gilles Peskinefe119512018-07-08 21:39:34 +02002015 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2016 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002017
Gilles Peskinefe119512018-07-08 21:39:34 +02002018 TEST_ASSERT( psa_cipher_set_iv( &operation,
2019 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002020
mohammad16033d91abe2018-07-03 13:15:54 +03002021 output_buffer_size = (size_t) input->len +
2022 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002023 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002024 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002025
Gilles Peskine4abf7412018-06-18 16:35:34 +02002026 TEST_ASSERT( psa_cipher_update( &operation,
2027 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002028 output, output_buffer_size,
2029 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002030 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002031 status = psa_cipher_finish( &operation,
2032 output + function_output_length,
2033 output_buffer_size,
2034 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002035 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002036 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002037
2038 if( expected_status == PSA_SUCCESS )
2039 {
2040 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002041 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002042 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002043 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002044 }
2045
Gilles Peskine50e586b2018-06-08 14:28:46 +02002046exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002047 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002048 psa_destroy_key( key_slot );
2049 mbedtls_psa_crypto_free( );
2050}
2051/* END_CASE */
2052
Gilles Peskine50e586b2018-06-08 14:28:46 +02002053/* BEGIN_CASE */
2054void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002055 data_t *key,
2056 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002057{
2058 int key_slot = 1;
2059 psa_key_type_t key_type = key_type_arg;
2060 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002061 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002062 size_t iv_size = 16;
2063 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002064 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002065 size_t output1_size = 0;
2066 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002067 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002068 size_t output2_size = 0;
2069 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002070 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002071 psa_cipher_operation_t operation1;
2072 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002073 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002074
mohammad1603d7d7ba52018-03-12 18:51:53 +02002075 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002076 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002077 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2078 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002079
mohammad1603d7d7ba52018-03-12 18:51:53 +02002080 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2081
Moran Pekered346952018-07-05 15:22:45 +03002082 psa_key_policy_init( &policy );
2083 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2084 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2085
mohammad1603d7d7ba52018-03-12 18:51:53 +02002086 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002087 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002088
Gilles Peskinefe119512018-07-08 21:39:34 +02002089 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2090 key_slot, alg ) == PSA_SUCCESS );
2091 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2092 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002093
Gilles Peskinefe119512018-07-08 21:39:34 +02002094 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2095 iv, iv_size,
2096 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002097 output1_size = (size_t) input->len +
2098 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002099 output1 = mbedtls_calloc( 1, output1_size );
2100 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002101
Gilles Peskine4abf7412018-06-18 16:35:34 +02002102 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002103 output1, output1_size,
2104 &output1_length ) == PSA_SUCCESS );
2105 TEST_ASSERT( psa_cipher_finish( &operation1,
2106 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002107 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002108
Gilles Peskine048b7f02018-06-08 14:20:49 +02002109 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002110
2111 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2112
2113 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002114 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002115 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002116
Gilles Peskinefe119512018-07-08 21:39:34 +02002117 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2118 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002119 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2120 output2, output2_size,
2121 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002122 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002123 TEST_ASSERT( psa_cipher_finish( &operation2,
2124 output2 + output2_length,
2125 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002126 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002127
Gilles Peskine048b7f02018-06-08 14:20:49 +02002128 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002129
Janos Follath25c4fa82018-07-06 16:23:25 +01002130 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002131
Gilles Peskine4abf7412018-06-18 16:35:34 +02002132 TEST_ASSERT( input->len == output2_length );
2133 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03002134
2135exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002136 mbedtls_free( output1 );
2137 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002138 psa_destroy_key( key_slot );
2139 mbedtls_psa_crypto_free( );
2140}
2141/* END_CASE */
2142
2143/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002144void cipher_verify_output_multipart( int alg_arg,
2145 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002146 data_t *key,
2147 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002148 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002149{
2150 int key_slot = 1;
2151 psa_key_type_t key_type = key_type_arg;
2152 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002153 unsigned char iv[16] = {0};
2154 size_t iv_size = 16;
2155 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002156 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002157 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002158 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002159 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002160 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002161 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002162 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002163 psa_cipher_operation_t operation1;
2164 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002165 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002166
Moran Pekerded84402018-06-06 16:36:50 +03002167 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002168 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002169 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2170 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002171
Moran Pekerded84402018-06-06 16:36:50 +03002172 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2173
Moran Pekered346952018-07-05 15:22:45 +03002174 psa_key_policy_init( &policy );
2175 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2176 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2177
Moran Pekerded84402018-06-06 16:36:50 +03002178 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002179 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002180
Gilles Peskinefe119512018-07-08 21:39:34 +02002181 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2182 key_slot, alg ) == PSA_SUCCESS );
2183 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2184 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002185
Gilles Peskinefe119512018-07-08 21:39:34 +02002186 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2187 iv, iv_size,
2188 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002189 output1_buffer_size = (size_t) input->len +
2190 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002191 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002192 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002193
Gilles Peskine4abf7412018-06-18 16:35:34 +02002194 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002195
itayzafrir3e02b3b2018-06-12 17:06:52 +03002196 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002197 output1, output1_buffer_size,
2198 &function_output_length ) == PSA_SUCCESS );
2199 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002200
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002201 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002202 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002203 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002204 output1, output1_buffer_size,
2205 &function_output_length ) == PSA_SUCCESS );
2206 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002207
Gilles Peskine048b7f02018-06-08 14:20:49 +02002208 TEST_ASSERT( psa_cipher_finish( &operation1,
2209 output1 + output1_length,
2210 output1_buffer_size - output1_length,
2211 &function_output_length ) == PSA_SUCCESS );
2212 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002213
2214 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2215
Gilles Peskine048b7f02018-06-08 14:20:49 +02002216 output2_buffer_size = output1_length;
2217 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002218 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002219
Gilles Peskinefe119512018-07-08 21:39:34 +02002220 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2221 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002222
2223 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002224 output2, output2_buffer_size,
2225 &function_output_length ) == PSA_SUCCESS );
2226 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002227
Gilles Peskine048b7f02018-06-08 14:20:49 +02002228 TEST_ASSERT( psa_cipher_update( &operation2,
2229 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002230 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002231 output2, output2_buffer_size,
2232 &function_output_length ) == PSA_SUCCESS );
2233 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002234
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002235 TEST_ASSERT( psa_cipher_finish( &operation2,
2236 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002237 output2_buffer_size - output2_length,
2238 &function_output_length ) == PSA_SUCCESS );
2239 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002240
Janos Follath25c4fa82018-07-06 16:23:25 +01002241 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002242
Gilles Peskine4abf7412018-06-18 16:35:34 +02002243 TEST_ASSERT( input->len == output2_length );
2244 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002245
2246exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002247 mbedtls_free( output1 );
2248 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002249 psa_destroy_key( key_slot );
2250 mbedtls_psa_crypto_free( );
2251}
2252/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002253
Gilles Peskine20035e32018-02-03 22:44:14 +01002254/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002255void aead_encrypt_decrypt( int key_type_arg,
2256 data_t * key_data,
2257 int alg_arg,
2258 data_t * input_data,
2259 data_t * nonce,
2260 data_t * additional_data,
2261 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002262{
2263 int slot = 1;
2264 psa_key_type_t key_type = key_type_arg;
2265 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002266 unsigned char *output_data = NULL;
2267 size_t output_size = 0;
2268 size_t output_length = 0;
2269 unsigned char *output_data2 = NULL;
2270 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002271 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002272 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002273 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002274
Gilles Peskinea1cac842018-06-11 19:33:02 +02002275 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002276 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002277 TEST_ASSERT( nonce != NULL );
2278 TEST_ASSERT( additional_data != NULL );
2279 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2280 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2281 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2282 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2283
Gilles Peskine4abf7412018-06-18 16:35:34 +02002284 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002285 output_data = mbedtls_calloc( 1, output_size );
2286 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002287
2288 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2289
2290 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002291 psa_key_policy_set_usage( &policy,
2292 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2293 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002294 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2295
2296 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002297 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002298
2299 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002300 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002301 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002302 additional_data->len,
2303 input_data->x, input_data->len,
2304 output_data, output_size,
2305 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002306
2307 if( PSA_SUCCESS == expected_result )
2308 {
2309 output_data2 = mbedtls_calloc( 1, output_length );
2310 TEST_ASSERT( output_data2 != NULL );
2311
2312 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002313 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002314 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002315 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002316 output_data, output_length,
2317 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002318 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002319
itayzafrir3e02b3b2018-06-12 17:06:52 +03002320 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002321 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002322 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002323
Gilles Peskinea1cac842018-06-11 19:33:02 +02002324exit:
2325 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002326 mbedtls_free( output_data );
2327 mbedtls_free( output_data2 );
2328 mbedtls_psa_crypto_free( );
2329}
2330/* END_CASE */
2331
2332/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002333void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002334 int alg_arg, data_t * input_data,
2335 data_t * additional_data, data_t * nonce,
2336 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002337{
2338 int slot = 1;
2339 psa_key_type_t key_type = key_type_arg;
2340 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002341 unsigned char *output_data = NULL;
2342 size_t output_size = 0;
2343 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002344 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002345 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002346
Gilles Peskinea1cac842018-06-11 19:33:02 +02002347 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002348 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002349 TEST_ASSERT( additional_data != NULL );
2350 TEST_ASSERT( nonce != NULL );
2351 TEST_ASSERT( expected_result != NULL );
2352 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2353 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2354 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2355 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2356 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2357
Gilles Peskine4abf7412018-06-18 16:35:34 +02002358 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002359 output_data = mbedtls_calloc( 1, output_size );
2360 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002361
Gilles Peskinea1cac842018-06-11 19:33:02 +02002362 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2363
2364 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002365 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002366 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2367
2368 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002369 key_data->x,
2370 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002371
2372 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002373 nonce->x, nonce->len,
2374 additional_data->x, additional_data->len,
2375 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002376 output_data, output_size,
2377 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002378
itayzafrir3e02b3b2018-06-12 17:06:52 +03002379 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002380 output_length ) == 0 );
2381
Gilles Peskinea1cac842018-06-11 19:33:02 +02002382exit:
2383 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002384 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002385 mbedtls_psa_crypto_free( );
2386}
2387/* END_CASE */
2388
2389/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002390void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002391 int alg_arg, data_t * input_data,
2392 data_t * additional_data, data_t * nonce,
2393 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002394{
2395 int slot = 1;
2396 psa_key_type_t key_type = key_type_arg;
2397 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002398 unsigned char *output_data = NULL;
2399 size_t output_size = 0;
2400 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002401 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002402 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002403 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002404
Gilles Peskinea1cac842018-06-11 19:33:02 +02002405 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002406 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002407 TEST_ASSERT( additional_data != NULL );
2408 TEST_ASSERT( nonce != NULL );
2409 TEST_ASSERT( expected_data != NULL );
2410 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2411 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2412 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2413 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2414 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2415
Gilles Peskine4abf7412018-06-18 16:35:34 +02002416 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002417 output_data = mbedtls_calloc( 1, output_size );
2418 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002419
Gilles Peskinea1cac842018-06-11 19:33:02 +02002420 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2421
2422 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002423 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002424 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2425
2426 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002427 key_data->x,
2428 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002429
2430 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002431 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002432 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002433 additional_data->len,
2434 input_data->x, input_data->len,
2435 output_data, output_size,
2436 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002437
Gilles Peskine2d277862018-06-18 15:41:12 +02002438 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002439 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002440 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002441 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002442 }
2443
Gilles Peskinea1cac842018-06-11 19:33:02 +02002444exit:
2445 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002446 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002447 mbedtls_psa_crypto_free( );
2448}
2449/* END_CASE */
2450
2451/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002452void signature_size( int type_arg,
2453 int bits,
2454 int alg_arg,
2455 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002456{
2457 psa_key_type_t type = type_arg;
2458 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002459 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002460 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2461exit:
2462 ;
2463}
2464/* END_CASE */
2465
2466/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002467void sign_deterministic( int key_type_arg, data_t *key_data,
2468 int alg_arg, data_t *input_data,
2469 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002470{
2471 int slot = 1;
2472 psa_key_type_t key_type = key_type_arg;
2473 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002474 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002475 unsigned char *signature = NULL;
2476 size_t signature_size;
2477 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002478 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002479
Gilles Peskine20035e32018-02-03 22:44:14 +01002480 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002481 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002482 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002483 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2484 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2485 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002486
2487 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2488
mohammad1603a97cb8c2018-03-28 03:46:26 -07002489 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002490 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002491 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2492
Gilles Peskine20035e32018-02-03 22:44:14 +01002493 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002494 key_data->x,
2495 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002496 TEST_ASSERT( psa_get_key_information( slot,
2497 NULL,
2498 &key_bits ) == PSA_SUCCESS );
2499
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002500 /* Allocate a buffer which has the size advertized by the
2501 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002502 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2503 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002504 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002505 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002506 signature = mbedtls_calloc( 1, signature_size );
2507 TEST_ASSERT( signature != NULL );
2508
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002509 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002510 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002511 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002512 signature, signature_size,
2513 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002514 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002515 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002516 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002517 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002518
2519exit:
2520 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002521 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002522 mbedtls_psa_crypto_free( );
2523}
2524/* END_CASE */
2525
2526/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002527void sign_fail( int key_type_arg, data_t *key_data,
2528 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002529 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002530{
2531 int slot = 1;
2532 psa_key_type_t key_type = key_type_arg;
2533 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002534 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002535 psa_status_t actual_status;
2536 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002537 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002538 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002539 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002540
Gilles Peskine20035e32018-02-03 22:44:14 +01002541 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002542 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002543 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2544 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2545
Gilles Peskine20035e32018-02-03 22:44:14 +01002546 signature = mbedtls_calloc( 1, signature_size );
2547 TEST_ASSERT( signature != NULL );
2548
2549 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2550
mohammad1603a97cb8c2018-03-28 03:46:26 -07002551 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002552 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002553 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2554
Gilles Peskine20035e32018-02-03 22:44:14 +01002555 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002556 key_data->x,
2557 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002558
2559 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002560 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002561 signature, signature_size,
2562 &signature_length );
2563 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002564 /* The value of *signature_length is unspecified on error, but
2565 * whatever it is, it should be less than signature_size, so that
2566 * if the caller tries to read *signature_length bytes without
2567 * checking the error code then they don't overflow a buffer. */
2568 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002569
2570exit:
2571 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002572 mbedtls_free( signature );
2573 mbedtls_psa_crypto_free( );
2574}
2575/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002576
2577/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002578void sign_verify( int key_type_arg, data_t *key_data,
2579 int alg_arg, data_t *input_data )
2580{
2581 int slot = 1;
2582 psa_key_type_t key_type = key_type_arg;
2583 psa_algorithm_t alg = alg_arg;
2584 size_t key_bits;
2585 unsigned char *signature = NULL;
2586 size_t signature_size;
2587 size_t signature_length = 0xdeadbeef;
2588 psa_key_policy_t policy;
2589
2590 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2591
2592 psa_key_policy_init( &policy );
2593 psa_key_policy_set_usage( &policy,
2594 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2595 alg );
2596 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2597
2598 TEST_ASSERT( psa_import_key( slot, key_type,
2599 key_data->x,
2600 key_data->len ) == PSA_SUCCESS );
2601 TEST_ASSERT( psa_get_key_information( slot,
2602 NULL,
2603 &key_bits ) == PSA_SUCCESS );
2604
2605 /* Allocate a buffer which has the size advertized by the
2606 * library. */
2607 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2608 key_bits, alg );
2609 TEST_ASSERT( signature_size != 0 );
2610 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2611 signature = mbedtls_calloc( 1, signature_size );
2612 TEST_ASSERT( signature != NULL );
2613
2614 /* Perform the signature. */
2615 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2616 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002617 signature, signature_size,
2618 &signature_length ) == PSA_SUCCESS );
2619 /* Check that the signature length looks sensible. */
2620 TEST_ASSERT( signature_length <= signature_size );
2621 TEST_ASSERT( signature_length > 0 );
2622
2623 /* Use the library to verify that the signature is correct. */
2624 TEST_ASSERT( psa_asymmetric_verify(
2625 slot, alg,
2626 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002627 signature, signature_length ) == PSA_SUCCESS );
2628
2629 if( input_data->len != 0 )
2630 {
2631 /* Flip a bit in the input and verify that the signature is now
2632 * detected as invalid. Flip a bit at the beginning, not at the end,
2633 * because ECDSA may ignore the last few bits of the input. */
2634 input_data->x[0] ^= 1;
2635 TEST_ASSERT( psa_asymmetric_verify(
2636 slot, alg,
2637 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002638 signature,
2639 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2640 }
2641
2642exit:
2643 psa_destroy_key( slot );
2644 mbedtls_free( signature );
2645 mbedtls_psa_crypto_free( );
2646}
2647/* END_CASE */
2648
2649/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002650void asymmetric_verify( int key_type_arg, data_t *key_data,
2651 int alg_arg, data_t *hash_data,
2652 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002653{
2654 int slot = 1;
2655 psa_key_type_t key_type = key_type_arg;
2656 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002657 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002658
Gilles Peskine69c12672018-06-28 00:07:19 +02002659 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2660
itayzafrir5c753392018-05-08 11:18:38 +03002661 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002662 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002663 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002664 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2665 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2666 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002667
2668 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2669
2670 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002671 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002672 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2673
2674 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002675 key_data->x,
2676 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002677
2678 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002679 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002680 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002681 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002682exit:
2683 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002684 mbedtls_psa_crypto_free( );
2685}
2686/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002687
2688/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002689void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2690 int alg_arg, data_t *hash_data,
2691 data_t *signature_data,
2692 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002693{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002694 int slot = 1;
2695 psa_key_type_t key_type = key_type_arg;
2696 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002697 psa_status_t actual_status;
2698 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002699 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002700
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002701 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002702 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002703 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002704 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2705 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2706 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002707
2708 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2709
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002710 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002711 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002712 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2713
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002715 key_data->x,
2716 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002717
2718 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002719 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002720 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002721 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002722
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002723 TEST_ASSERT( actual_status == expected_status );
2724
2725exit:
2726 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727 mbedtls_psa_crypto_free( );
2728}
2729/* END_CASE */
2730
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002731/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002732void asymmetric_encrypt( int key_type_arg,
2733 data_t *key_data,
2734 int alg_arg,
2735 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002736 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002737 int expected_output_length_arg,
2738 int expected_status_arg )
2739{
2740 int slot = 1;
2741 psa_key_type_t key_type = key_type_arg;
2742 psa_algorithm_t alg = alg_arg;
2743 size_t expected_output_length = expected_output_length_arg;
2744 size_t key_bits;
2745 unsigned char *output = NULL;
2746 size_t output_size;
2747 size_t output_length = ~0;
2748 psa_status_t actual_status;
2749 psa_status_t expected_status = expected_status_arg;
2750 psa_key_policy_t policy;
2751
2752 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2753
2754 /* Import the key */
2755 psa_key_policy_init( &policy );
2756 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2757 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2758 TEST_ASSERT( psa_import_key( slot, key_type,
2759 key_data->x,
2760 key_data->len ) == PSA_SUCCESS );
2761
2762 /* Determine the maximum output length */
2763 TEST_ASSERT( psa_get_key_information( slot,
2764 NULL,
2765 &key_bits ) == PSA_SUCCESS );
2766 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2767 output = mbedtls_calloc( 1, output_size );
Darryl Green9c862252018-07-24 12:52:44 +01002768 TEST_ASSERT( output_size == 0 || output != NULL );
Gilles Peskine656896e2018-06-29 19:12:28 +02002769
2770 /* Encrypt the input */
2771 actual_status = psa_asymmetric_encrypt( slot, alg,
2772 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002773 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002774 output, output_size,
2775 &output_length );
2776 TEST_ASSERT( actual_status == expected_status );
2777 TEST_ASSERT( output_length == expected_output_length );
2778
Gilles Peskine68428122018-06-30 18:42:41 +02002779 /* If the label is empty, the test framework puts a non-null pointer
2780 * in label->x. Test that a null pointer works as well. */
2781 if( label->len == 0 )
2782 {
2783 output_length = ~0;
2784 memset( output, 0, output_size );
2785 actual_status = psa_asymmetric_encrypt( slot, alg,
2786 input_data->x, input_data->len,
2787 NULL, label->len,
2788 output, output_size,
2789 &output_length );
2790 TEST_ASSERT( actual_status == expected_status );
2791 TEST_ASSERT( output_length == expected_output_length );
2792 }
2793
Gilles Peskine656896e2018-06-29 19:12:28 +02002794exit:
2795 psa_destroy_key( slot );
2796 mbedtls_free( output );
2797 mbedtls_psa_crypto_free( );
2798}
2799/* END_CASE */
2800
2801/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002802void asymmetric_encrypt_decrypt( int key_type_arg,
2803 data_t *key_data,
2804 int alg_arg,
2805 data_t *input_data,
2806 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002807{
2808 int slot = 1;
2809 psa_key_type_t key_type = key_type_arg;
2810 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002811 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002812 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002813 size_t output_size;
2814 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002815 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002816 size_t output2_size;
2817 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002818 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002819
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002820 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002821 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2823 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2824
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002825 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2826
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002827 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002828 psa_key_policy_set_usage( &policy,
2829 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002830 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002831 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2832
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002833 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002834 key_data->x,
2835 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002836
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002837
2838 /* Determine the maximum ciphertext length */
2839 TEST_ASSERT( psa_get_key_information( slot,
2840 NULL,
2841 &key_bits ) == PSA_SUCCESS );
2842 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2843 output = mbedtls_calloc( 1, output_size );
2844 TEST_ASSERT( output != NULL );
2845 output2_size = input_data->len;
2846 output2 = mbedtls_calloc( 1, output2_size );
2847 TEST_ASSERT( output2 != NULL );
2848
Gilles Peskineeebd7382018-06-08 18:11:54 +02002849 /* We test encryption by checking that encrypt-then-decrypt gives back
2850 * the original plaintext because of the non-optional random
2851 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002852 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002853 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002854 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002855 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002856 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002857 /* We don't know what ciphertext length to expect, but check that
2858 * it looks sensible. */
2859 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002860
Gilles Peskine2d277862018-06-18 15:41:12 +02002861 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002862 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002863 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002864 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002865 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002866 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002867 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002868 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002869
2870exit:
2871 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002872 mbedtls_free( output );
2873 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002874 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002875}
2876/* END_CASE */
2877
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002878/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002879void asymmetric_decrypt( int key_type_arg,
2880 data_t *key_data,
2881 int alg_arg,
2882 data_t *input_data,
2883 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002884 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002885{
2886 int slot = 1;
2887 psa_key_type_t key_type = key_type_arg;
2888 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002889 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002890 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002891 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002892 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002894 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002895 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002896 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002897 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2898 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2899 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2900
Gilles Peskine4abf7412018-06-18 16:35:34 +02002901 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002902 output = mbedtls_calloc( 1, output_size );
2903 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002904
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002905 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2906
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002907 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002908 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002909 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2910
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002911 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002912 key_data->x,
2913 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002914
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002915 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002916 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002917 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002918 output,
2919 output_size,
2920 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002921 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002922 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002923
Gilles Peskine68428122018-06-30 18:42:41 +02002924 /* If the label is empty, the test framework puts a non-null pointer
2925 * in label->x. Test that a null pointer works as well. */
2926 if( label->len == 0 )
2927 {
2928 output_length = ~0;
2929 memset( output, 0, output_size );
2930 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2931 input_data->x, input_data->len,
2932 NULL, label->len,
2933 output,
2934 output_size,
2935 &output_length ) == PSA_SUCCESS );
2936 TEST_ASSERT( expected_data->len == output_length );
2937 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2938 }
2939
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002940exit:
2941 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002942 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002943 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002944}
2945/* END_CASE */
2946
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002948void asymmetric_decrypt_fail( int key_type_arg,
2949 data_t *key_data,
2950 int alg_arg,
2951 data_t *input_data,
2952 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002953 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002954{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955 int slot = 1;
2956 psa_key_type_t key_type = key_type_arg;
2957 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002958 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002959 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002960 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961 psa_status_t actual_status;
2962 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002963 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002966 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002967 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2968 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2969
Gilles Peskine4abf7412018-06-18 16:35:34 +02002970 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002971 output = mbedtls_calloc( 1, output_size );
2972 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002973
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002974 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2975
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002976 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002977 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002978 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2979
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002980 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002981 key_data->x,
2982 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002983
Gilles Peskine2d277862018-06-18 15:41:12 +02002984 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002985 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002986 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002987 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002988 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002989 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002990 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002991
Gilles Peskine68428122018-06-30 18:42:41 +02002992 /* If the label is empty, the test framework puts a non-null pointer
2993 * in label->x. Test that a null pointer works as well. */
2994 if( label->len == 0 )
2995 {
2996 output_length = ~0;
2997 memset( output, 0, output_size );
2998 actual_status = psa_asymmetric_decrypt( slot, alg,
2999 input_data->x, input_data->len,
3000 NULL, label->len,
3001 output, output_size,
3002 &output_length );
3003 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003004 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003005 }
3006
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003007exit:
3008 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003009 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003010 mbedtls_psa_crypto_free( );
3011}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003012/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003013
3014/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003015void derive_setup( int key_type_arg,
3016 data_t *key_data,
3017 int alg_arg,
3018 data_t *salt,
3019 data_t *label,
3020 int requested_capacity_arg,
3021 int expected_status_arg )
3022{
3023 psa_key_slot_t slot = 1;
3024 size_t key_type = key_type_arg;
3025 psa_algorithm_t alg = alg_arg;
3026 size_t requested_capacity = requested_capacity_arg;
3027 psa_status_t expected_status = expected_status_arg;
3028 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3029 psa_key_policy_t policy;
3030
3031 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3032
3033 psa_key_policy_init( &policy );
3034 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3035 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3036
3037 TEST_ASSERT( psa_import_key( slot, key_type,
3038 key_data->x,
3039 key_data->len ) == PSA_SUCCESS );
3040
3041 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3042 salt->x, salt->len,
3043 label->x, label->len,
3044 requested_capacity ) == expected_status );
3045
3046exit:
3047 psa_generator_abort( &generator );
3048 psa_destroy_key( slot );
3049 mbedtls_psa_crypto_free( );
3050}
3051/* END_CASE */
3052
3053/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003054void derive_output( int alg_arg,
3055 data_t *key_data,
3056 data_t *salt,
3057 data_t *label,
3058 int requested_capacity_arg,
3059 data_t *expected_output1,
3060 data_t *expected_output2 )
3061{
3062 psa_key_slot_t slot = 1;
3063 psa_algorithm_t alg = alg_arg;
3064 size_t requested_capacity = requested_capacity_arg;
3065 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3066 uint8_t *expected_outputs[2] =
3067 {expected_output1->x, expected_output2->x};
3068 size_t output_sizes[2] =
3069 {expected_output1->len, expected_output2->len};
3070 size_t output_buffer_size = 0;
3071 uint8_t *output_buffer = NULL;
3072 size_t expected_capacity;
3073 size_t current_capacity;
3074 psa_key_policy_t policy;
3075 psa_status_t status;
3076 unsigned i;
3077
3078 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3079 {
3080 if( output_sizes[i] > output_buffer_size )
3081 output_buffer_size = output_sizes[i];
3082 if( output_sizes[i] == 0 )
3083 expected_outputs[i] = NULL;
3084 }
3085 output_buffer = mbedtls_calloc( 1, output_buffer_size );
3086 TEST_ASSERT( output_buffer != NULL );
3087 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3088
3089 psa_key_policy_init( &policy );
3090 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3091 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3092
3093 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3094 key_data->x,
3095 key_data->len ) == PSA_SUCCESS );
3096
3097 /* Extraction phase. */
3098 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3099 salt->x, salt->len,
3100 label->x, label->len,
3101 requested_capacity ) == PSA_SUCCESS );
3102 TEST_ASSERT( psa_get_generator_capacity( &generator,
3103 &current_capacity ) ==
3104 PSA_SUCCESS );
3105 TEST_ASSERT( current_capacity == requested_capacity );
3106 expected_capacity = requested_capacity;
3107
3108 /* Expansion phase. */
3109 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3110 {
3111 /* Read some bytes. */
3112 status = psa_generator_read( &generator,
3113 output_buffer, output_sizes[i] );
3114 if( expected_capacity == 0 && output_sizes[i] == 0 )
3115 {
3116 /* Reading 0 bytes when 0 bytes are available can go either way. */
3117 TEST_ASSERT( status == PSA_SUCCESS ||
3118 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3119 continue;
3120 }
3121 else if( expected_capacity == 0 ||
3122 output_sizes[i] > expected_capacity )
3123 {
3124 /* Capacity exceeded. */
3125 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3126 expected_capacity = 0;
3127 continue;
3128 }
3129 /* Success. Check the read data. */
3130 TEST_ASSERT( status == PSA_SUCCESS );
3131 if( output_sizes[i] != 0 )
3132 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3133 output_sizes[i] ) == 0 );
3134 /* Check the generator status. */
3135 expected_capacity -= output_sizes[i];
3136 TEST_ASSERT( psa_get_generator_capacity( &generator,
3137 &current_capacity ) ==
3138 PSA_SUCCESS );
3139 TEST_ASSERT( expected_capacity == current_capacity );
3140 }
3141 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3142
3143exit:
3144 mbedtls_free( output_buffer );
3145 psa_generator_abort( &generator );
3146 psa_destroy_key( slot );
3147 mbedtls_psa_crypto_free( );
3148}
3149/* END_CASE */
3150
3151/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003152void derive_full( int alg_arg,
3153 data_t *key_data,
3154 data_t *salt,
3155 data_t *label,
3156 int requested_capacity_arg )
3157{
3158 psa_key_slot_t slot = 1;
3159 psa_algorithm_t alg = alg_arg;
3160 size_t requested_capacity = requested_capacity_arg;
3161 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3162 unsigned char output_buffer[16];
3163 size_t expected_capacity = requested_capacity;
3164 size_t current_capacity;
3165 psa_key_policy_t policy;
3166
3167 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3168
3169 psa_key_policy_init( &policy );
3170 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3171 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3172
3173 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3174 key_data->x,
3175 key_data->len ) == PSA_SUCCESS );
3176
3177 /* Extraction phase. */
3178 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3179 salt->x, salt->len,
3180 label->x, label->len,
3181 requested_capacity ) == PSA_SUCCESS );
3182 TEST_ASSERT( psa_get_generator_capacity( &generator,
3183 &current_capacity ) ==
3184 PSA_SUCCESS );
3185 TEST_ASSERT( current_capacity == expected_capacity );
3186
3187 /* Expansion phase. */
3188 while( current_capacity > 0 )
3189 {
3190 size_t read_size = sizeof( output_buffer );
3191 if( read_size > current_capacity )
3192 read_size = current_capacity;
3193 TEST_ASSERT( psa_generator_read( &generator,
3194 output_buffer,
3195 read_size ) == PSA_SUCCESS );
3196 expected_capacity -= read_size;
3197 TEST_ASSERT( psa_get_generator_capacity( &generator,
3198 &current_capacity ) ==
3199 PSA_SUCCESS );
3200 TEST_ASSERT( current_capacity == expected_capacity );
3201 }
3202
3203 /* Check that the generator refuses to go over capacity. */
3204 TEST_ASSERT( psa_generator_read( &generator,
3205 output_buffer,
3206 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3207
3208 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3209
3210exit:
3211 psa_generator_abort( &generator );
3212 psa_destroy_key( slot );
3213 mbedtls_psa_crypto_free( );
3214}
3215/* END_CASE */
3216
3217/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003218void derive_key_exercise( int alg_arg,
3219 data_t *key_data,
3220 data_t *salt,
3221 data_t *label,
3222 int derived_type_arg,
3223 int derived_bits_arg,
3224 int derived_usage_arg,
3225 int derived_alg_arg )
3226{
3227 psa_key_slot_t base_key = 1;
3228 psa_key_slot_t derived_key = 2;
3229 psa_algorithm_t alg = alg_arg;
3230 psa_key_type_t derived_type = derived_type_arg;
3231 size_t derived_bits = derived_bits_arg;
3232 psa_key_usage_t derived_usage = derived_usage_arg;
3233 psa_algorithm_t derived_alg = derived_alg_arg;
3234 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3235 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3236 psa_key_policy_t policy;
3237 psa_key_type_t got_type;
3238 size_t got_bits;
3239
3240 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3241
3242 psa_key_policy_init( &policy );
3243 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3244 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3245 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3246 key_data->x,
3247 key_data->len ) == PSA_SUCCESS );
3248
3249 /* Derive a key. */
3250 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3251 salt->x, salt->len,
3252 label->x, label->len,
3253 capacity ) == PSA_SUCCESS );
3254 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3255 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3256 TEST_ASSERT( psa_generator_import_key( derived_key,
3257 derived_type,
3258 derived_bits,
3259 &generator ) == PSA_SUCCESS );
3260
3261 /* Test the key information */
3262 TEST_ASSERT( psa_get_key_information( derived_key,
3263 &got_type,
3264 &got_bits ) == PSA_SUCCESS );
3265 TEST_ASSERT( got_type == derived_type );
3266 TEST_ASSERT( got_bits == derived_bits );
3267
3268 /* Exercise the derived key. */
3269 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3270 goto exit;
3271
3272exit:
3273 psa_generator_abort( &generator );
3274 psa_destroy_key( base_key );
3275 psa_destroy_key( derived_key );
3276 mbedtls_psa_crypto_free( );
3277}
3278/* END_CASE */
3279
3280/* BEGIN_CASE */
3281void derive_key_export( int alg_arg,
3282 data_t *key_data,
3283 data_t *salt,
3284 data_t *label,
3285 int bytes1_arg,
3286 int bytes2_arg )
3287{
3288 psa_key_slot_t base_key = 1;
3289 psa_key_slot_t derived_key = 2;
3290 psa_algorithm_t alg = alg_arg;
3291 size_t bytes1 = bytes1_arg;
3292 size_t bytes2 = bytes2_arg;
3293 size_t capacity = bytes1 + bytes2;
3294 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3295 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
3296 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
3297 psa_key_policy_t policy;
3298 size_t length;
3299
3300 TEST_ASSERT( output_buffer != NULL );
3301 TEST_ASSERT( export_buffer != NULL );
3302 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3303
3304 psa_key_policy_init( &policy );
3305 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3306 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3307 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3308 key_data->x,
3309 key_data->len ) == PSA_SUCCESS );
3310
3311 /* Derive some material and output it. */
3312 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3313 salt->x, salt->len,
3314 label->x, label->len,
3315 capacity ) == PSA_SUCCESS );
3316 TEST_ASSERT( psa_generator_read( &generator,
3317 output_buffer,
3318 capacity ) == PSA_SUCCESS );
3319 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3320
3321 /* Derive the same output again, but this time store it in key objects. */
3322 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3323 salt->x, salt->len,
3324 label->x, label->len,
3325 capacity ) == PSA_SUCCESS );
3326 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3327 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3328 TEST_ASSERT( psa_generator_import_key( derived_key,
3329 PSA_KEY_TYPE_RAW_DATA,
3330 PSA_BYTES_TO_BITS( bytes1 ),
3331 &generator ) == PSA_SUCCESS );
3332 TEST_ASSERT( psa_export_key( derived_key,
3333 export_buffer, bytes1,
3334 &length ) == PSA_SUCCESS );
3335 TEST_ASSERT( length == bytes1 );
3336 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3337 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3338 TEST_ASSERT( psa_generator_import_key( derived_key,
3339 PSA_KEY_TYPE_RAW_DATA,
3340 PSA_BYTES_TO_BITS( bytes2 ),
3341 &generator ) == PSA_SUCCESS );
3342 TEST_ASSERT( psa_export_key( derived_key,
3343 export_buffer + bytes1, bytes2,
3344 &length ) == PSA_SUCCESS );
3345 TEST_ASSERT( length == bytes2 );
3346
3347 /* Compare the outputs from the two runs. */
3348 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3349
3350exit:
3351 mbedtls_free( output_buffer );
3352 mbedtls_free( export_buffer );
3353 psa_generator_abort( &generator );
3354 psa_destroy_key( base_key );
3355 psa_destroy_key( derived_key );
3356 mbedtls_psa_crypto_free( );
3357}
3358/* END_CASE */
3359
3360/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003361void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003362{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003363 size_t bytes = bytes_arg;
3364 const unsigned char trail[] = "don't overwrite me";
3365 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
3366 unsigned char *changed = mbedtls_calloc( 1, bytes );
3367 size_t i;
3368 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003369
Gilles Peskinea50d7392018-06-21 10:22:13 +02003370 TEST_ASSERT( output != NULL );
Darryl Green9c862252018-07-24 12:52:44 +01003371 TEST_ASSERT( bytes == 0 || changed != NULL );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003372 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003373
3374 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3375
Gilles Peskinea50d7392018-06-21 10:22:13 +02003376 /* Run several times, to ensure that every output byte will be
3377 * nonzero at least once with overwhelming probability
3378 * (2^(-8*number_of_runs)). */
3379 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003380 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003381 memset( output, 0, bytes );
3382 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3383
3384 /* Check that no more than bytes have been overwritten */
3385 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3386
3387 for( i = 0; i < bytes; i++ )
3388 {
3389 if( output[i] != 0 )
3390 ++changed[i];
3391 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003392 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003393
3394 /* Check that every byte was changed to nonzero at least once. This
3395 * validates that psa_generate_random is overwriting every byte of
3396 * the output buffer. */
3397 for( i = 0; i < bytes; i++ )
3398 {
3399 TEST_ASSERT( changed[i] != 0 );
3400 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003401
3402exit:
3403 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003404 mbedtls_free( output );
3405 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003406}
3407/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003408
3409/* BEGIN_CASE */
3410void generate_key( int type_arg,
3411 int bits_arg,
3412 int usage_arg,
3413 int alg_arg,
3414 int expected_status_arg )
3415{
3416 int slot = 1;
3417 psa_key_type_t type = type_arg;
3418 psa_key_usage_t usage = usage_arg;
3419 size_t bits = bits_arg;
3420 psa_algorithm_t alg = alg_arg;
3421 psa_status_t expected_status = expected_status_arg;
3422 psa_key_type_t got_type;
3423 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003424 psa_status_t expected_info_status =
3425 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3426 psa_key_policy_t policy;
3427
3428 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3429
3430 psa_key_policy_init( &policy );
3431 psa_key_policy_set_usage( &policy, usage, alg );
3432 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3433
3434 /* Generate a key */
3435 TEST_ASSERT( psa_generate_key( slot, type, bits,
3436 NULL, 0 ) == expected_status );
3437
3438 /* Test the key information */
3439 TEST_ASSERT( psa_get_key_information( slot,
3440 &got_type,
3441 &got_bits ) == expected_info_status );
3442 if( expected_info_status != PSA_SUCCESS )
3443 goto exit;
3444 TEST_ASSERT( got_type == type );
3445 TEST_ASSERT( got_bits == bits );
3446
Gilles Peskine818ca122018-06-20 18:16:48 +02003447 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003448 if( ! exercise_key( slot, usage, alg ) )
3449 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003450
3451exit:
3452 psa_destroy_key( slot );
3453 mbedtls_psa_crypto_free( );
3454}
3455/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003456
3457/* BEGIN_CASE */
3458void validate_module_init_generate_random( )
3459{
3460 psa_status_t status;
3461 uint8_t random[10] = { 0 };
3462 status = psa_generate_random( random, sizeof( random ) );
3463 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3464}
3465/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003466
3467/* BEGIN_CASE */
3468void validate_module_init_key_based( )
3469{
3470 psa_status_t status;
3471 uint8_t data[10] = { 0 };
3472 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3473 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3474}
3475/* END_CASE */