blob: a55cfc7accab76e3663fbbe2de5a47eda69fc00a [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 );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200706 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200707
708 TEST_ASSERT( psa_export_key( slot,
709 exported, exported_size,
710 &exported_length ) == PSA_SUCCESS );
711 ok = exported_key_sanity_check( type, bits, exported, exported_length );
712
713exit:
714 mbedtls_free( exported );
715 return( ok );
716}
717
718static int exercise_export_public_key( psa_key_slot_t slot )
719{
720 psa_key_type_t type;
721 psa_key_type_t public_type;
722 size_t bits;
723 uint8_t *exported = NULL;
724 size_t exported_size = 0;
725 size_t exported_length = 0;
726 int ok = 0;
727
728 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
729 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
730 {
731 TEST_ASSERT( psa_export_public_key( slot,
732 NULL, 0, &exported_length ) ==
733 PSA_ERROR_INVALID_ARGUMENT );
734 return( 1 );
735 }
736
737 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
738 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200739 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200740
741 TEST_ASSERT( psa_export_public_key( slot,
742 exported, exported_size,
743 &exported_length ) == PSA_SUCCESS );
744 ok = exported_key_sanity_check( public_type, bits,
745 exported, exported_length );
746
747exit:
748 mbedtls_free( exported );
749 return( ok );
750}
751
Gilles Peskine02b75072018-07-01 22:31:34 +0200752static int exercise_key( psa_key_slot_t slot,
753 psa_key_usage_t usage,
754 psa_algorithm_t alg )
755{
756 int ok;
757 if( alg == 0 )
758 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
759 else if( PSA_ALG_IS_MAC( alg ) )
760 ok = exercise_mac_key( slot, usage, alg );
761 else if( PSA_ALG_IS_CIPHER( alg ) )
762 ok = exercise_cipher_key( slot, usage, alg );
763 else if( PSA_ALG_IS_AEAD( alg ) )
764 ok = exercise_aead_key( slot, usage, alg );
765 else if( PSA_ALG_IS_SIGN( alg ) )
766 ok = exercise_signature_key( slot, usage, alg );
767 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
768 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200769 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
770 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200771 else
772 {
773 char message[40];
774 mbedtls_snprintf( message, sizeof( message ),
775 "No code to exercise alg=0x%08lx",
776 (unsigned long) alg );
777 test_fail( message, __LINE__, __FILE__ );
778 ok = 0;
779 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200780
781 ok = ok && exercise_export_key( slot, usage );
782 ok = ok && exercise_export_public_key( slot );
783
Gilles Peskine02b75072018-07-01 22:31:34 +0200784 return( ok );
785}
786
Gilles Peskinee59236f2018-01-27 23:32:46 +0100787/* END_HEADER */
788
789/* BEGIN_DEPENDENCIES
790 * depends_on:MBEDTLS_PSA_CRYPTO_C
791 * END_DEPENDENCIES
792 */
793
794/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200795void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100796{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100798 int i;
799 for( i = 0; i <= 1; i++ )
800 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 status = psa_crypto_init( );
802 TEST_ASSERT( status == PSA_SUCCESS );
803 status = psa_crypto_init( );
804 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100805 mbedtls_psa_crypto_free( );
806 }
807}
808/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809
810/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200811void fill_slots( int max_arg )
812{
813 /* Fill all the slots until we run out of memory or out of slots,
814 * or until some limit specified in the test data for the sake of
815 * implementations with an essentially unlimited number of slots.
816 * This test assumes that available slots are numbered from 1. */
817
818 psa_key_slot_t slot;
819 psa_key_slot_t max = 0;
820 psa_key_policy_t policy;
821 uint8_t exported[sizeof( max )];
822 size_t exported_size;
823 psa_status_t status;
824
825 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
826
827 psa_key_policy_init( &policy );
828 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
829
830 for( max = 1; max <= (size_t) max_arg; max++ )
831 {
832 status = psa_set_key_policy( max, &policy );
833 /* Stop filling slots if we run out of memory or out of
834 * available slots. */
835 TEST_ASSERT( status == PSA_SUCCESS ||
836 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
837 status == PSA_ERROR_INVALID_ARGUMENT );
838 if( status != PSA_SUCCESS )
839 break;
840 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
841 (uint8_t*) &max, sizeof( max ) );
842 /* Since psa_set_key_policy succeeded, we know that the slot
843 * number is valid. But we may legitimately run out of memory. */
844 TEST_ASSERT( status == PSA_SUCCESS ||
845 status == PSA_ERROR_INSUFFICIENT_MEMORY );
846 if( status != PSA_SUCCESS )
847 break;
848 }
849 /* `max` is now the first slot number that wasn't filled. */
850 max -= 1;
851
852 for( slot = 1; slot <= max; slot++ )
853 {
854 TEST_ASSERT( psa_export_key( slot,
855 exported, sizeof( exported ),
856 &exported_size ) == PSA_SUCCESS );
857 TEST_ASSERT( exported_size == sizeof( slot ) );
858 TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
Gilles Peskine996deb12018-08-01 15:45:45 +0200859 }
860
861exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200862 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200863 mbedtls_psa_crypto_free( );
864}
865/* END_CASE */
866
867/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200868void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869{
870 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300875 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
877
Gilles Peskine4abf7412018-06-18 16:35:34 +0200878 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200879 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100880 if( status == PSA_SUCCESS )
881 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
882
883exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884 mbedtls_psa_crypto_free( );
885}
886/* END_CASE */
887
888/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200889void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
890{
891 int slot = 1;
892 size_t bits = bits_arg;
893 psa_status_t expected_status = expected_status_arg;
894 psa_status_t status;
895 psa_key_type_t type =
896 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
897 size_t buffer_size = /* Slight overapproximations */
898 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200899 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200900 unsigned char *p;
901 int ret;
902 size_t length;
903
904 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200905 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200906
907 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
908 bits, keypair ) ) >= 0 );
909 length = ret;
910
911 /* Try importing the key */
912 status = psa_import_key( slot, type, p, length );
913 TEST_ASSERT( status == expected_status );
914 if( status == PSA_SUCCESS )
915 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
916
917exit:
918 mbedtls_free( buffer );
919 mbedtls_psa_crypto_free( );
920}
921/* END_CASE */
922
923/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300924void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300925 int type_arg,
926 int alg_arg,
927 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928 int expected_bits,
929 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200930 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100931 int canonical_input )
932{
933 int slot = 1;
934 int slot2 = slot + 1;
935 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200936 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200937 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 unsigned char *exported = NULL;
940 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100942 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943 size_t reexported_length;
944 psa_key_type_t got_type;
945 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200946 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300949 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300950 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200951 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200953 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100954 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
955
mohammad1603a97cb8c2018-03-28 03:46:26 -0700956 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200957 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700958 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
959
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100960 /* Import the key */
961 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200962 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100963
964 /* Test the key information */
965 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200966 &got_type,
967 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 TEST_ASSERT( got_type == type );
969 TEST_ASSERT( got_bits == (size_t) expected_bits );
970
971 /* Export the key */
972 status = psa_export_key( slot,
973 exported, export_size,
974 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200975 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100976
977 /* The exported length must be set by psa_export_key() to a value between 0
978 * and export_size. On errors, the exported length must be 0. */
979 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
980 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
981 TEST_ASSERT( exported_length <= export_size );
982
Gilles Peskine3f669c32018-06-21 09:21:51 +0200983 TEST_ASSERT( mem_is_zero( exported + exported_length,
984 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200986 {
987 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200989 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990
Gilles Peskine8f609232018-08-11 01:24:55 +0200991 if( ! exercise_export_key( slot, usage_arg ) )
992 goto exit;
993
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 if( canonical_input )
995 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200996 TEST_ASSERT( exported_length == data->len );
997 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 }
999 else
1000 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001001 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1002
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001004 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001005 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001007 reexported,
1008 export_size,
1009 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 TEST_ASSERT( reexported_length == exported_length );
1011 TEST_ASSERT( memcmp( reexported, exported,
1012 exported_length ) == 0 );
1013 }
1014
1015destroy:
1016 /* Destroy the key */
1017 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1018 TEST_ASSERT( psa_get_key_information(
1019 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1020
1021exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001022 mbedtls_free( exported );
1023 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 mbedtls_psa_crypto_free( );
1025}
1026/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001027
Moran Pekerf709f4a2018-06-06 17:26:04 +03001028/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001029void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001030 int type_arg,
1031 int alg_arg,
1032 int expected_bits,
1033 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001034 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001035{
1036 int slot = 1;
1037 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001038 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001039 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001040 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001042 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001043 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001044 psa_key_type_t got_type;
1045 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001046 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001047
Moran Pekerf709f4a2018-06-06 17:26:04 +03001048 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001049 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001050 export_size = (ptrdiff_t) data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001051 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052
1053 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1054
1055 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001056 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001057 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1058
1059 /* Import the key */
1060 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001061 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001062
1063 /* Test the key information */
1064 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001065 &got_type,
1066 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001067 TEST_ASSERT( got_type == type );
1068 TEST_ASSERT( got_bits == (size_t) expected_bits );
1069
1070 /* Export the key */
1071 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001072 exported, export_size,
1073 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001074 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001075 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
1076 TEST_ASSERT( mem_is_zero( exported + exported_length,
1077 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001078 if( status != PSA_SUCCESS )
1079 goto destroy;
1080
Moran Pekerf709f4a2018-06-06 17:26:04 +03001081destroy:
1082 /* Destroy the key */
1083 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1084 TEST_ASSERT( psa_get_key_information(
1085 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1086
1087exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001088 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001089 mbedtls_psa_crypto_free( );
1090}
1091/* END_CASE */
1092
Gilles Peskine20035e32018-02-03 22:44:14 +01001093/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001094void import_and_exercise_key( data_t *data,
1095 int type_arg,
1096 int bits_arg,
1097 int alg_arg )
1098{
1099 int slot = 1;
1100 psa_key_type_t type = type_arg;
1101 size_t bits = bits_arg;
1102 psa_algorithm_t alg = alg_arg;
1103 psa_key_usage_t usage =
1104 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1105 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1106 PSA_KEY_USAGE_VERIFY :
1107 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1108 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1109 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1110 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1111 PSA_KEY_USAGE_ENCRYPT :
1112 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001113 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001114 0 );
1115 psa_key_policy_t policy;
1116 psa_key_type_t got_type;
1117 size_t got_bits;
1118 psa_status_t status;
1119
1120 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1121
1122 psa_key_policy_init( &policy );
1123 psa_key_policy_set_usage( &policy, usage, alg );
1124 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1125
1126 /* Import the key */
1127 status = psa_import_key( slot, type, data->x, data->len );
1128 TEST_ASSERT( status == PSA_SUCCESS );
1129
1130 /* Test the key information */
1131 TEST_ASSERT( psa_get_key_information( slot,
1132 &got_type,
1133 &got_bits ) == PSA_SUCCESS );
1134 TEST_ASSERT( got_type == type );
1135 TEST_ASSERT( got_bits == bits );
1136
1137 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001138 if( ! exercise_key( slot, usage, alg ) )
1139 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001140
1141exit:
1142 psa_destroy_key( slot );
1143 mbedtls_psa_crypto_free( );
1144}
1145/* END_CASE */
1146
1147/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001148void key_policy( int usage_arg, int alg_arg )
1149{
1150 int key_slot = 1;
1151 psa_algorithm_t alg = alg_arg;
1152 psa_key_usage_t usage = usage_arg;
1153 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1154 unsigned char key[32] = {0};
1155 psa_key_policy_t policy_set;
1156 psa_key_policy_t policy_get;
1157
1158 memset( key, 0x2a, sizeof( key ) );
1159
1160 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1161
1162 psa_key_policy_init( &policy_set );
1163 psa_key_policy_init( &policy_get );
1164
1165 psa_key_policy_set_usage( &policy_set, usage, alg );
1166
1167 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1168 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1169 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1170
1171 TEST_ASSERT( psa_import_key( key_slot, key_type,
1172 key, sizeof( key ) ) == PSA_SUCCESS );
1173
1174 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1175
1176 TEST_ASSERT( policy_get.usage == policy_set.usage );
1177 TEST_ASSERT( policy_get.alg == policy_set.alg );
1178
1179exit:
1180 psa_destroy_key( key_slot );
1181 mbedtls_psa_crypto_free( );
1182}
1183/* END_CASE */
1184
1185/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001186void mac_key_policy( int policy_usage,
1187 int policy_alg,
1188 int key_type,
1189 data_t *key_data,
1190 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001191{
1192 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001193 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194 psa_mac_operation_t operation;
1195 psa_status_t status;
1196 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001197
1198 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1199
1200 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001202 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1203
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204 TEST_ASSERT( psa_import_key( key_slot, key_type,
1205 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001206
Gilles Peskine89167cb2018-07-08 20:12:23 +02001207 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208 if( policy_alg == exercise_alg &&
1209 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1210 TEST_ASSERT( status == PSA_SUCCESS );
1211 else
1212 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1213 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001214
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001216 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001217 if( policy_alg == exercise_alg &&
1218 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001219 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220 else
1221 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1222
1223exit:
1224 psa_mac_abort( &operation );
1225 psa_destroy_key( key_slot );
1226 mbedtls_psa_crypto_free( );
1227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
1231void cipher_key_policy( int policy_usage,
1232 int policy_alg,
1233 int key_type,
1234 data_t *key_data,
1235 int exercise_alg )
1236{
1237 int key_slot = 1;
1238 psa_key_policy_t policy;
1239 psa_cipher_operation_t operation;
1240 psa_status_t status;
1241
1242 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1243
1244 psa_key_policy_init( &policy );
1245 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1246 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1247
1248 TEST_ASSERT( psa_import_key( key_slot, key_type,
1249 key_data->x, key_data->len ) == PSA_SUCCESS );
1250
Gilles Peskinefe119512018-07-08 21:39:34 +02001251 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 if( policy_alg == exercise_alg &&
1253 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1254 TEST_ASSERT( status == PSA_SUCCESS );
1255 else
1256 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1257 psa_cipher_abort( &operation );
1258
Gilles Peskinefe119512018-07-08 21:39:34 +02001259 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260 if( policy_alg == exercise_alg &&
1261 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1262 TEST_ASSERT( status == PSA_SUCCESS );
1263 else
1264 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1265
1266exit:
1267 psa_cipher_abort( &operation );
1268 psa_destroy_key( key_slot );
1269 mbedtls_psa_crypto_free( );
1270}
1271/* END_CASE */
1272
1273/* BEGIN_CASE */
1274void aead_key_policy( int policy_usage,
1275 int policy_alg,
1276 int key_type,
1277 data_t *key_data,
1278 int nonce_length_arg,
1279 int tag_length_arg,
1280 int exercise_alg )
1281{
1282 int key_slot = 1;
1283 psa_key_policy_t policy;
1284 psa_status_t status;
1285 unsigned char nonce[16] = {0};
1286 size_t nonce_length = nonce_length_arg;
1287 unsigned char tag[16];
1288 size_t tag_length = tag_length_arg;
1289 size_t output_length;
1290
1291 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1292 TEST_ASSERT( tag_length <= sizeof( tag ) );
1293
1294 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1295
1296 psa_key_policy_init( &policy );
1297 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1298 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1299
1300 TEST_ASSERT( psa_import_key( key_slot, key_type,
1301 key_data->x, key_data->len ) == PSA_SUCCESS );
1302
1303 status = psa_aead_encrypt( key_slot, exercise_alg,
1304 nonce, nonce_length,
1305 NULL, 0,
1306 NULL, 0,
1307 tag, tag_length,
1308 &output_length );
1309 if( policy_alg == exercise_alg &&
1310 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1311 TEST_ASSERT( status == PSA_SUCCESS );
1312 else
1313 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1314
1315 memset( tag, 0, sizeof( tag ) );
1316 status = psa_aead_decrypt( key_slot, exercise_alg,
1317 nonce, nonce_length,
1318 NULL, 0,
1319 tag, tag_length,
1320 NULL, 0,
1321 &output_length );
1322 if( policy_alg == exercise_alg &&
1323 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1324 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1325 else
1326 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1327
1328exit:
1329 psa_destroy_key( key_slot );
1330 mbedtls_psa_crypto_free( );
1331}
1332/* END_CASE */
1333
1334/* BEGIN_CASE */
1335void asymmetric_encryption_key_policy( int policy_usage,
1336 int policy_alg,
1337 int key_type,
1338 data_t *key_data,
1339 int exercise_alg )
1340{
1341 int key_slot = 1;
1342 psa_key_policy_t policy;
1343 psa_status_t status;
1344 size_t key_bits;
1345 size_t buffer_length;
1346 unsigned char *buffer = NULL;
1347 size_t output_length;
1348
1349 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1350
1351 psa_key_policy_init( &policy );
1352 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1353 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1354
1355 TEST_ASSERT( psa_import_key( key_slot, key_type,
1356 key_data->x, key_data->len ) == PSA_SUCCESS );
1357
1358 TEST_ASSERT( psa_get_key_information( key_slot,
1359 NULL,
1360 &key_bits ) == PSA_SUCCESS );
1361 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1362 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001363 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364
1365 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1366 NULL, 0,
1367 NULL, 0,
1368 buffer, buffer_length,
1369 &output_length );
1370 if( policy_alg == exercise_alg &&
1371 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1372 TEST_ASSERT( status == PSA_SUCCESS );
1373 else
1374 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1375
1376 memset( buffer, 0, buffer_length );
1377 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1378 buffer, buffer_length,
1379 NULL, 0,
1380 buffer, buffer_length,
1381 &output_length );
1382 if( policy_alg == exercise_alg &&
1383 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1384 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1385 else
1386 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1387
1388exit:
1389 psa_destroy_key( key_slot );
1390 mbedtls_psa_crypto_free( );
1391 mbedtls_free( buffer );
1392}
1393/* END_CASE */
1394
1395/* BEGIN_CASE */
1396void asymmetric_signature_key_policy( int policy_usage,
1397 int policy_alg,
1398 int key_type,
1399 data_t *key_data,
1400 int exercise_alg )
1401{
1402 int key_slot = 1;
1403 psa_key_policy_t policy;
1404 psa_status_t status;
1405 unsigned char payload[16] = {1};
1406 size_t payload_length = sizeof( payload );
1407 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1408 size_t signature_length;
1409
1410 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1411
1412 psa_key_policy_init( &policy );
1413 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1414 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1415
1416 TEST_ASSERT( psa_import_key( key_slot, key_type,
1417 key_data->x, key_data->len ) == PSA_SUCCESS );
1418
1419 status = psa_asymmetric_sign( key_slot, exercise_alg,
1420 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001421 signature, sizeof( signature ),
1422 &signature_length );
1423 if( policy_alg == exercise_alg &&
1424 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1425 TEST_ASSERT( status == PSA_SUCCESS );
1426 else
1427 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1428
1429 memset( signature, 0, sizeof( signature ) );
1430 status = psa_asymmetric_verify( key_slot, exercise_alg,
1431 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001432 signature, sizeof( signature ) );
1433 if( policy_alg == exercise_alg &&
1434 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1435 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1436 else
1437 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001438
1439exit:
1440 psa_destroy_key( key_slot );
1441 mbedtls_psa_crypto_free( );
1442}
1443/* END_CASE */
1444
1445/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001446void derive_key_policy( int policy_usage,
1447 int policy_alg,
1448 int key_type,
1449 data_t *key_data,
1450 int exercise_alg )
1451{
1452 int key_slot = 1;
1453 psa_key_policy_t policy;
1454 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1455 psa_status_t status;
1456
1457 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1458
1459 psa_key_policy_init( &policy );
1460 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1461 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1462
1463 TEST_ASSERT( psa_import_key( key_slot, key_type,
1464 key_data->x, key_data->len ) == PSA_SUCCESS );
1465
1466 status = psa_key_derivation( &generator, key_slot,
1467 exercise_alg,
1468 NULL, 0,
1469 NULL, 0,
1470 1 );
1471 if( policy_alg == exercise_alg &&
1472 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1473 TEST_ASSERT( status == PSA_SUCCESS );
1474 else
1475 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1476
1477exit:
1478 psa_generator_abort( &generator );
1479 psa_destroy_key( key_slot );
1480 mbedtls_psa_crypto_free( );
1481}
1482/* END_CASE */
1483
1484/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001485void key_lifetime( int lifetime_arg )
1486{
1487 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001488 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001489 unsigned char key[32] = {0};
1490 psa_key_lifetime_t lifetime_set = lifetime_arg;
1491 psa_key_lifetime_t lifetime_get;
1492
1493 memset( key, 0x2a, sizeof( key ) );
1494
1495 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1496
1497 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1498 lifetime_set ) == PSA_SUCCESS );
1499
1500 TEST_ASSERT( psa_import_key( key_slot, key_type,
1501 key, sizeof( key ) ) == PSA_SUCCESS );
1502
1503 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1504 &lifetime_get ) == PSA_SUCCESS );
1505
1506 TEST_ASSERT( lifetime_get == lifetime_set );
1507
1508exit:
1509 psa_destroy_key( key_slot );
1510 mbedtls_psa_crypto_free( );
1511}
1512/* END_CASE */
1513
1514/* BEGIN_CASE */
1515void key_lifetime_set_fail( int key_slot_arg,
1516 int lifetime_arg,
1517 int expected_status_arg )
1518{
1519 psa_key_slot_t key_slot = key_slot_arg;
1520 psa_key_lifetime_t lifetime_set = lifetime_arg;
1521 psa_status_t actual_status;
1522 psa_status_t expected_status = expected_status_arg;
1523
1524 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1525
1526 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1527
1528 if( actual_status == PSA_SUCCESS )
1529 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1530
1531 TEST_ASSERT( expected_status == actual_status );
1532
1533exit:
1534 psa_destroy_key( key_slot );
1535 mbedtls_psa_crypto_free( );
1536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001540void hash_setup( int alg_arg,
1541 int expected_status_arg )
1542{
1543 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001544 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001545 psa_hash_operation_t operation;
1546 psa_status_t status;
1547
1548 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1549
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001550 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001551 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001552 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001553
1554exit:
1555 mbedtls_psa_crypto_free( );
1556}
1557/* END_CASE */
1558
1559/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001560void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001561{
1562 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001563 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001564 size_t actual_hash_length;
1565 psa_hash_operation_t operation;
1566
Gilles Peskine69c12672018-06-28 00:07:19 +02001567 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1568 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1569
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001570 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001571 TEST_ASSERT( expected_hash != NULL );
1572 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1573 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001574
1575 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1576
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001577 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001579 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001580 TEST_ASSERT( psa_hash_finish( &operation,
1581 actual_hash, sizeof( actual_hash ),
1582 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001583 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001584 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001585 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586
1587exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001588 mbedtls_psa_crypto_free( );
1589}
1590/* END_CASE */
1591
1592/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001593void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001594{
1595 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001596 psa_hash_operation_t operation;
1597
Gilles Peskine69c12672018-06-28 00:07:19 +02001598 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1599 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1600
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001601 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001602 TEST_ASSERT( expected_hash != NULL );
1603 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1604 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001605
1606 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1607
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001608 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001609 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001610 input->x,
1611 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001612 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001613 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001614 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001615
1616exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001617 mbedtls_psa_crypto_free( );
1618}
1619/* END_CASE */
1620
1621/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001622void mac_setup( int key_type_arg,
1623 data_t *key,
1624 int alg_arg,
1625 int expected_status_arg )
1626{
1627 int key_slot = 1;
1628 psa_key_type_t key_type = key_type_arg;
1629 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001630 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001631 psa_mac_operation_t operation;
1632 psa_key_policy_t policy;
1633 psa_status_t status;
1634
1635 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1636
1637 psa_key_policy_init( &policy );
1638 psa_key_policy_set_usage( &policy,
1639 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1640 alg );
1641 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1642
1643 TEST_ASSERT( psa_import_key( key_slot, key_type,
1644 key->x, key->len ) == PSA_SUCCESS );
1645
Gilles Peskine89167cb2018-07-08 20:12:23 +02001646 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001647 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001648 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001649
1650exit:
1651 psa_destroy_key( key_slot );
1652 mbedtls_psa_crypto_free( );
1653}
1654/* END_CASE */
1655
1656/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001657void mac_verify( int key_type_arg,
1658 data_t *key,
1659 int alg_arg,
1660 data_t *input,
1661 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001662{
1663 int key_slot = 1;
1664 psa_key_type_t key_type = key_type_arg;
1665 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001666 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001667 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668
Gilles Peskine69c12672018-06-28 00:07:19 +02001669 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1670
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001674 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001675 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1676 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677
1678 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1679
mohammad16036df908f2018-04-02 08:34:15 -07001680 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001681 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001682 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1683
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001685 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001686
Gilles Peskine89167cb2018-07-08 20:12:23 +02001687 TEST_ASSERT( psa_mac_verify_setup( &operation,
1688 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1690 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001691 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001692 TEST_ASSERT( psa_mac_verify_finish( &operation,
1693 expected_mac->x,
1694 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695
1696exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697 psa_destroy_key( key_slot );
1698 mbedtls_psa_crypto_free( );
1699}
1700/* END_CASE */
1701
1702/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001703void cipher_setup( int key_type_arg,
1704 data_t *key,
1705 int alg_arg,
1706 int expected_status_arg )
1707{
1708 int key_slot = 1;
1709 psa_key_type_t key_type = key_type_arg;
1710 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001711 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001712 psa_cipher_operation_t operation;
1713 psa_key_policy_t policy;
1714 psa_status_t status;
1715
1716 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1717
1718 psa_key_policy_init( &policy );
1719 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1720 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1721
1722 TEST_ASSERT( psa_import_key( key_slot, key_type,
1723 key->x, key->len ) == PSA_SUCCESS );
1724
Gilles Peskinefe119512018-07-08 21:39:34 +02001725 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001726 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001727 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001728
1729exit:
1730 psa_destroy_key( key_slot );
1731 mbedtls_psa_crypto_free( );
1732}
1733/* END_CASE */
1734
1735/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001736void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001737 data_t *key,
1738 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001739 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001740{
1741 int key_slot = 1;
1742 psa_status_t status;
1743 psa_key_type_t key_type = key_type_arg;
1744 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001745 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001746 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001747 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001748 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001749 size_t output_buffer_size = 0;
1750 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001751 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001752 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001753 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001754
Gilles Peskine50e586b2018-06-08 14:28:46 +02001755 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001756 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001757 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001758 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1759 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1760 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001761
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001762 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1763 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001764
1765 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1766
Moran Pekered346952018-07-05 15:22:45 +03001767 psa_key_policy_init( &policy );
1768 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1769 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1770
Gilles Peskine50e586b2018-06-08 14:28:46 +02001771 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001772 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001773
Gilles Peskinefe119512018-07-08 21:39:34 +02001774 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1775 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001776
Gilles Peskinefe119512018-07-08 21:39:34 +02001777 TEST_ASSERT( psa_cipher_set_iv( &operation,
1778 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001779 output_buffer_size = (size_t) input->len +
1780 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001781 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001782
Gilles Peskine4abf7412018-06-18 16:35:34 +02001783 TEST_ASSERT( psa_cipher_update( &operation,
1784 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001785 output, output_buffer_size,
1786 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001787 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001788 status = psa_cipher_finish( &operation,
1789 output + function_output_length,
1790 output_buffer_size,
1791 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001792 total_output_length += function_output_length;
1793
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001794 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001795 if( expected_status == PSA_SUCCESS )
1796 {
1797 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001798 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001799 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001800 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001801 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001802
Gilles Peskine50e586b2018-06-08 14:28:46 +02001803exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001804 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001805 psa_destroy_key( key_slot );
1806 mbedtls_psa_crypto_free( );
1807}
1808/* END_CASE */
1809
1810/* BEGIN_CASE */
1811void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001812 data_t *key,
1813 data_t *input,
1814 int first_part_size,
1815 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001816{
1817 int key_slot = 1;
1818 psa_key_type_t key_type = key_type_arg;
1819 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001820 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001821 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001822 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001823 size_t output_buffer_size = 0;
1824 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001825 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001826 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001827 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001828
Gilles Peskine50e586b2018-06-08 14:28:46 +02001829 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001830 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001831 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001832 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1833 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1834 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001835
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001836 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1837 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001838
1839 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1840
Moran Pekered346952018-07-05 15:22:45 +03001841 psa_key_policy_init( &policy );
1842 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1843 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1844
Gilles Peskine50e586b2018-06-08 14:28:46 +02001845 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001846 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001847
Gilles Peskinefe119512018-07-08 21:39:34 +02001848 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1849 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001850
Gilles Peskinefe119512018-07-08 21:39:34 +02001851 TEST_ASSERT( psa_cipher_set_iv( &operation,
1852 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001853 output_buffer_size = (size_t) input->len +
1854 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001855 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001856
Gilles Peskine4abf7412018-06-18 16:35:34 +02001857 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001858 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001859 output, output_buffer_size,
1860 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001861 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001862 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001863 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001864 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001865 output, output_buffer_size,
1866 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001867 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001868 TEST_ASSERT( psa_cipher_finish( &operation,
1869 output + function_output_length,
1870 output_buffer_size,
1871 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001872 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001873 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1874
Gilles Peskine4abf7412018-06-18 16:35:34 +02001875 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001876 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001877 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001878
1879exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001880 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001881 psa_destroy_key( key_slot );
1882 mbedtls_psa_crypto_free( );
1883}
1884/* END_CASE */
1885
1886/* BEGIN_CASE */
1887void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001888 data_t *key,
1889 data_t *input,
1890 int first_part_size,
1891 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001892{
1893 int key_slot = 1;
1894
1895 psa_key_type_t key_type = key_type_arg;
1896 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001897 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001898 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001899 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001900 size_t output_buffer_size = 0;
1901 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001902 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001903 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001904 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001905
Gilles Peskine50e586b2018-06-08 14:28:46 +02001906 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001907 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001908 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001909 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1910 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1911 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001912
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001913 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1914 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001915
1916 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1917
Moran Pekered346952018-07-05 15:22:45 +03001918 psa_key_policy_init( &policy );
1919 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1920 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1921
Gilles Peskine50e586b2018-06-08 14:28:46 +02001922 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001923 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001924
Gilles Peskinefe119512018-07-08 21:39:34 +02001925 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1926 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001927
Gilles Peskinefe119512018-07-08 21:39:34 +02001928 TEST_ASSERT( psa_cipher_set_iv( &operation,
1929 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001930
mohammad16033d91abe2018-07-03 13:15:54 +03001931 output_buffer_size = (size_t) input->len +
1932 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001933 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001934
Gilles Peskine4abf7412018-06-18 16:35:34 +02001935 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1936 TEST_ASSERT( psa_cipher_update( &operation,
1937 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001938 output, output_buffer_size,
1939 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001940 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001941 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001942 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001943 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001944 output, output_buffer_size,
1945 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001946 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001947 TEST_ASSERT( psa_cipher_finish( &operation,
1948 output + function_output_length,
1949 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_abort( &operation ) == PSA_SUCCESS );
1953
Gilles Peskine4abf7412018-06-18 16:35:34 +02001954 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001955 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001956 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001957
1958exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001959 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001960 psa_destroy_key( key_slot );
1961 mbedtls_psa_crypto_free( );
1962}
1963/* END_CASE */
1964
Gilles Peskine50e586b2018-06-08 14:28:46 +02001965/* BEGIN_CASE */
1966void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001967 data_t *key,
1968 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001969 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001970{
1971 int key_slot = 1;
1972 psa_status_t status;
1973 psa_key_type_t key_type = key_type_arg;
1974 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001975 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001976 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001977 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001978 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001979 size_t output_buffer_size = 0;
1980 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001981 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001982 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001983 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001984
Gilles Peskine50e586b2018-06-08 14:28:46 +02001985 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001986 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001987 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001988 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1989 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1990 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001991
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001992 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1993 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001994
1995 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1996
Moran Pekered346952018-07-05 15:22:45 +03001997 psa_key_policy_init( &policy );
1998 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1999 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2000
Gilles Peskine50e586b2018-06-08 14:28:46 +02002001 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002002 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002003
Gilles Peskinefe119512018-07-08 21:39:34 +02002004 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2005 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002006
Gilles Peskinefe119512018-07-08 21:39:34 +02002007 TEST_ASSERT( psa_cipher_set_iv( &operation,
2008 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002009
mohammad16033d91abe2018-07-03 13:15:54 +03002010 output_buffer_size = (size_t) input->len +
2011 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002012 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002013
Gilles Peskine4abf7412018-06-18 16:35:34 +02002014 TEST_ASSERT( psa_cipher_update( &operation,
2015 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002016 output, output_buffer_size,
2017 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002018 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002019 status = psa_cipher_finish( &operation,
2020 output + function_output_length,
2021 output_buffer_size,
2022 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002023 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002024 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002025
2026 if( expected_status == PSA_SUCCESS )
2027 {
2028 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002029 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002030 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002031 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002032 }
2033
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002035 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002036 psa_destroy_key( key_slot );
2037 mbedtls_psa_crypto_free( );
2038}
2039/* END_CASE */
2040
Gilles Peskine50e586b2018-06-08 14:28:46 +02002041/* BEGIN_CASE */
2042void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002043 data_t *key,
2044 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002045{
2046 int key_slot = 1;
2047 psa_key_type_t key_type = key_type_arg;
2048 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002049 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002050 size_t iv_size = 16;
2051 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002052 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002053 size_t output1_size = 0;
2054 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002055 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002056 size_t output2_size = 0;
2057 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002058 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002059 psa_cipher_operation_t operation1;
2060 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002061 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002062
mohammad1603d7d7ba52018-03-12 18:51:53 +02002063 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002064 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002065 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2066 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002067
mohammad1603d7d7ba52018-03-12 18:51:53 +02002068 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2069
Moran Pekered346952018-07-05 15:22:45 +03002070 psa_key_policy_init( &policy );
2071 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2072 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2073
mohammad1603d7d7ba52018-03-12 18:51:53 +02002074 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002075 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002076
Gilles Peskinefe119512018-07-08 21:39:34 +02002077 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2078 key_slot, alg ) == PSA_SUCCESS );
2079 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2080 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002081
Gilles Peskinefe119512018-07-08 21:39:34 +02002082 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2083 iv, iv_size,
2084 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002085 output1_size = (size_t) input->len +
2086 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002087 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002088
Gilles Peskine4abf7412018-06-18 16:35:34 +02002089 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002090 output1, output1_size,
2091 &output1_length ) == PSA_SUCCESS );
2092 TEST_ASSERT( psa_cipher_finish( &operation1,
2093 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002094 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002095
Gilles Peskine048b7f02018-06-08 14:20:49 +02002096 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002097
2098 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2099
2100 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002101 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002102
Gilles Peskinefe119512018-07-08 21:39:34 +02002103 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2104 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002105 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2106 output2, output2_size,
2107 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002108 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002109 TEST_ASSERT( psa_cipher_finish( &operation2,
2110 output2 + output2_length,
2111 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002112 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002113
Gilles Peskine048b7f02018-06-08 14:20:49 +02002114 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002115
Janos Follath25c4fa82018-07-06 16:23:25 +01002116 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002117
Gilles Peskine4abf7412018-06-18 16:35:34 +02002118 TEST_ASSERT( input->len == output2_length );
2119 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03002120
2121exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002122 mbedtls_free( output1 );
2123 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002124 psa_destroy_key( key_slot );
2125 mbedtls_psa_crypto_free( );
2126}
2127/* END_CASE */
2128
2129/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002130void cipher_verify_output_multipart( int alg_arg,
2131 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002132 data_t *key,
2133 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002134 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002135{
2136 int key_slot = 1;
2137 psa_key_type_t key_type = key_type_arg;
2138 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002139 unsigned char iv[16] = {0};
2140 size_t iv_size = 16;
2141 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002142 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002143 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002144 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002145 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002146 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002147 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002148 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002149 psa_cipher_operation_t operation1;
2150 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002151 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002152
Moran Pekerded84402018-06-06 16:36:50 +03002153 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002154 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002155 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2156 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002157
Moran Pekerded84402018-06-06 16:36:50 +03002158 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2159
Moran Pekered346952018-07-05 15:22:45 +03002160 psa_key_policy_init( &policy );
2161 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2162 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2163
Moran Pekerded84402018-06-06 16:36:50 +03002164 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002165 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002166
Gilles Peskinefe119512018-07-08 21:39:34 +02002167 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2168 key_slot, alg ) == PSA_SUCCESS );
2169 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2170 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002171
Gilles Peskinefe119512018-07-08 21:39:34 +02002172 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2173 iv, iv_size,
2174 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002175 output1_buffer_size = (size_t) input->len +
2176 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002177 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002178
Gilles Peskine4abf7412018-06-18 16:35:34 +02002179 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002180
itayzafrir3e02b3b2018-06-12 17:06:52 +03002181 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002182 output1, output1_buffer_size,
2183 &function_output_length ) == PSA_SUCCESS );
2184 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002185
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002186 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002187 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002188 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002189 output1, output1_buffer_size,
2190 &function_output_length ) == PSA_SUCCESS );
2191 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002192
Gilles Peskine048b7f02018-06-08 14:20:49 +02002193 TEST_ASSERT( psa_cipher_finish( &operation1,
2194 output1 + output1_length,
2195 output1_buffer_size - output1_length,
2196 &function_output_length ) == PSA_SUCCESS );
2197 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002198
2199 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2200
Gilles Peskine048b7f02018-06-08 14:20:49 +02002201 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002202 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002203
Gilles Peskinefe119512018-07-08 21:39:34 +02002204 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2205 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002206
2207 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002208 output2, output2_buffer_size,
2209 &function_output_length ) == PSA_SUCCESS );
2210 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002211
Gilles Peskine048b7f02018-06-08 14:20:49 +02002212 TEST_ASSERT( psa_cipher_update( &operation2,
2213 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002214 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002215 output2, output2_buffer_size,
2216 &function_output_length ) == PSA_SUCCESS );
2217 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002218
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002219 TEST_ASSERT( psa_cipher_finish( &operation2,
2220 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002221 output2_buffer_size - output2_length,
2222 &function_output_length ) == PSA_SUCCESS );
2223 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002224
Janos Follath25c4fa82018-07-06 16:23:25 +01002225 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002226
Gilles Peskine4abf7412018-06-18 16:35:34 +02002227 TEST_ASSERT( input->len == output2_length );
2228 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002229
2230exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002231 mbedtls_free( output1 );
2232 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002233 psa_destroy_key( key_slot );
2234 mbedtls_psa_crypto_free( );
2235}
2236/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002237
Gilles Peskine20035e32018-02-03 22:44:14 +01002238/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002239void aead_encrypt_decrypt( int key_type_arg,
2240 data_t * key_data,
2241 int alg_arg,
2242 data_t * input_data,
2243 data_t * nonce,
2244 data_t * additional_data,
2245 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002246{
2247 int slot = 1;
2248 psa_key_type_t key_type = key_type_arg;
2249 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002250 unsigned char *output_data = NULL;
2251 size_t output_size = 0;
2252 size_t output_length = 0;
2253 unsigned char *output_data2 = NULL;
2254 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002255 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002256 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002257 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002258
Gilles Peskinea1cac842018-06-11 19:33:02 +02002259 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002260 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002261 TEST_ASSERT( nonce != NULL );
2262 TEST_ASSERT( additional_data != NULL );
2263 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2264 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2265 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2266 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2267
Gilles Peskine4abf7412018-06-18 16:35:34 +02002268 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002269 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002270
2271 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2272
2273 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002274 psa_key_policy_set_usage( &policy,
2275 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2276 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002277 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2278
2279 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002280 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002281
2282 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002283 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002284 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002285 additional_data->len,
2286 input_data->x, input_data->len,
2287 output_data, output_size,
2288 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002289
2290 if( PSA_SUCCESS == expected_result )
2291 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002292 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002293
2294 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002295 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002296 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002297 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002298 output_data, output_length,
2299 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002300 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002301
itayzafrir3e02b3b2018-06-12 17:06:52 +03002302 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002303 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002304 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002305
Gilles Peskinea1cac842018-06-11 19:33:02 +02002306exit:
2307 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002308 mbedtls_free( output_data );
2309 mbedtls_free( output_data2 );
2310 mbedtls_psa_crypto_free( );
2311}
2312/* END_CASE */
2313
2314/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002315void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002316 int alg_arg, data_t * input_data,
2317 data_t * additional_data, data_t * nonce,
2318 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002319{
2320 int slot = 1;
2321 psa_key_type_t key_type = key_type_arg;
2322 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002323 unsigned char *output_data = NULL;
2324 size_t output_size = 0;
2325 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002326 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002327 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002328
Gilles Peskinea1cac842018-06-11 19:33:02 +02002329 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002330 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002331 TEST_ASSERT( additional_data != NULL );
2332 TEST_ASSERT( nonce != NULL );
2333 TEST_ASSERT( expected_result != NULL );
2334 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2335 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2336 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2337 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2338 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2339
Gilles Peskine4abf7412018-06-18 16:35:34 +02002340 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002341 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002342
Gilles Peskinea1cac842018-06-11 19:33:02 +02002343 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2344
2345 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002346 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002347 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2348
2349 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002350 key_data->x,
2351 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002352
2353 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002354 nonce->x, nonce->len,
2355 additional_data->x, additional_data->len,
2356 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002357 output_data, output_size,
2358 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002359
itayzafrir3e02b3b2018-06-12 17:06:52 +03002360 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002361 output_length ) == 0 );
2362
Gilles Peskinea1cac842018-06-11 19:33:02 +02002363exit:
2364 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002365 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002366 mbedtls_psa_crypto_free( );
2367}
2368/* END_CASE */
2369
2370/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002371void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002372 int alg_arg, data_t * input_data,
2373 data_t * additional_data, data_t * nonce,
2374 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002375{
2376 int slot = 1;
2377 psa_key_type_t key_type = key_type_arg;
2378 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002379 unsigned char *output_data = NULL;
2380 size_t output_size = 0;
2381 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002382 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002383 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002384 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002385
Gilles Peskinea1cac842018-06-11 19:33:02 +02002386 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002387 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002388 TEST_ASSERT( additional_data != NULL );
2389 TEST_ASSERT( nonce != NULL );
2390 TEST_ASSERT( expected_data != NULL );
2391 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2392 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2393 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2394 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2395 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2396
Gilles Peskine4abf7412018-06-18 16:35:34 +02002397 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002398 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002399
Gilles Peskinea1cac842018-06-11 19:33:02 +02002400 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2401
2402 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002403 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002404 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2405
2406 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002407 key_data->x,
2408 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002409
2410 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002411 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002412 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002413 additional_data->len,
2414 input_data->x, input_data->len,
2415 output_data, output_size,
2416 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002417
Gilles Peskine2d277862018-06-18 15:41:12 +02002418 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002419 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002420 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002421 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002422 }
2423
Gilles Peskinea1cac842018-06-11 19:33:02 +02002424exit:
2425 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002426 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002427 mbedtls_psa_crypto_free( );
2428}
2429/* END_CASE */
2430
2431/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002432void signature_size( int type_arg,
2433 int bits,
2434 int alg_arg,
2435 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002436{
2437 psa_key_type_t type = type_arg;
2438 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002439 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002440 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2441exit:
2442 ;
2443}
2444/* END_CASE */
2445
2446/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002447void sign_deterministic( int key_type_arg, data_t *key_data,
2448 int alg_arg, data_t *input_data,
2449 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002450{
2451 int slot = 1;
2452 psa_key_type_t key_type = key_type_arg;
2453 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002454 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002455 unsigned char *signature = NULL;
2456 size_t signature_size;
2457 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002458 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002459
Gilles Peskine20035e32018-02-03 22:44:14 +01002460 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002461 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002462 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002463 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2464 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2465 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002466
2467 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2468
mohammad1603a97cb8c2018-03-28 03:46:26 -07002469 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002470 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002471 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2472
Gilles Peskine20035e32018-02-03 22:44:14 +01002473 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002474 key_data->x,
2475 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002476 TEST_ASSERT( psa_get_key_information( slot,
2477 NULL,
2478 &key_bits ) == PSA_SUCCESS );
2479
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002480 /* Allocate a buffer which has the size advertized by the
2481 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002482 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2483 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002484 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002485 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002486 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002487
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002488 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002489 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002490 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002491 signature, signature_size,
2492 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002493 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002494 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002495 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002496 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002497
2498exit:
2499 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002500 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002501 mbedtls_psa_crypto_free( );
2502}
2503/* END_CASE */
2504
2505/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002506void sign_fail( int key_type_arg, data_t *key_data,
2507 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002508 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002509{
2510 int slot = 1;
2511 psa_key_type_t key_type = key_type_arg;
2512 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002513 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002514 psa_status_t actual_status;
2515 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002516 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002517 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002518 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002519
Gilles Peskine20035e32018-02-03 22:44:14 +01002520 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002521 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002522 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2523 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2524
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002525 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002526
2527 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2528
mohammad1603a97cb8c2018-03-28 03:46:26 -07002529 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002530 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002531 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2532
Gilles Peskine20035e32018-02-03 22:44:14 +01002533 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002534 key_data->x,
2535 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002536
2537 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002538 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 signature, signature_size,
2540 &signature_length );
2541 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002542 /* The value of *signature_length is unspecified on error, but
2543 * whatever it is, it should be less than signature_size, so that
2544 * if the caller tries to read *signature_length bytes without
2545 * checking the error code then they don't overflow a buffer. */
2546 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002547
2548exit:
2549 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002550 mbedtls_free( signature );
2551 mbedtls_psa_crypto_free( );
2552}
2553/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002554
2555/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002556void sign_verify( int key_type_arg, data_t *key_data,
2557 int alg_arg, data_t *input_data )
2558{
2559 int slot = 1;
2560 psa_key_type_t key_type = key_type_arg;
2561 psa_algorithm_t alg = alg_arg;
2562 size_t key_bits;
2563 unsigned char *signature = NULL;
2564 size_t signature_size;
2565 size_t signature_length = 0xdeadbeef;
2566 psa_key_policy_t policy;
2567
2568 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2569
2570 psa_key_policy_init( &policy );
2571 psa_key_policy_set_usage( &policy,
2572 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2573 alg );
2574 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2575
2576 TEST_ASSERT( psa_import_key( slot, key_type,
2577 key_data->x,
2578 key_data->len ) == PSA_SUCCESS );
2579 TEST_ASSERT( psa_get_key_information( slot,
2580 NULL,
2581 &key_bits ) == PSA_SUCCESS );
2582
2583 /* Allocate a buffer which has the size advertized by the
2584 * library. */
2585 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2586 key_bits, alg );
2587 TEST_ASSERT( signature_size != 0 );
2588 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002589 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002590
2591 /* Perform the signature. */
2592 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2593 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002594 signature, signature_size,
2595 &signature_length ) == PSA_SUCCESS );
2596 /* Check that the signature length looks sensible. */
2597 TEST_ASSERT( signature_length <= signature_size );
2598 TEST_ASSERT( signature_length > 0 );
2599
2600 /* Use the library to verify that the signature is correct. */
2601 TEST_ASSERT( psa_asymmetric_verify(
2602 slot, alg,
2603 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002604 signature, signature_length ) == PSA_SUCCESS );
2605
2606 if( input_data->len != 0 )
2607 {
2608 /* Flip a bit in the input and verify that the signature is now
2609 * detected as invalid. Flip a bit at the beginning, not at the end,
2610 * because ECDSA may ignore the last few bits of the input. */
2611 input_data->x[0] ^= 1;
2612 TEST_ASSERT( psa_asymmetric_verify(
2613 slot, alg,
2614 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002615 signature,
2616 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2617 }
2618
2619exit:
2620 psa_destroy_key( slot );
2621 mbedtls_free( signature );
2622 mbedtls_psa_crypto_free( );
2623}
2624/* END_CASE */
2625
2626/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002627void asymmetric_verify( int key_type_arg, data_t *key_data,
2628 int alg_arg, data_t *hash_data,
2629 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002630{
2631 int slot = 1;
2632 psa_key_type_t key_type = key_type_arg;
2633 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002634 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002635
Gilles Peskine69c12672018-06-28 00:07:19 +02002636 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2637
itayzafrir5c753392018-05-08 11:18:38 +03002638 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002639 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002640 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002641 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2642 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2643 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002644
2645 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2646
2647 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002648 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002649 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2650
2651 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002652 key_data->x,
2653 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002654
2655 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002656 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002657 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002658 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002659exit:
2660 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002661 mbedtls_psa_crypto_free( );
2662}
2663/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002664
2665/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002666void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2667 int alg_arg, data_t *hash_data,
2668 data_t *signature_data,
2669 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002670{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002671 int slot = 1;
2672 psa_key_type_t key_type = key_type_arg;
2673 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002674 psa_status_t actual_status;
2675 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002676 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002677
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002678 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002679 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002680 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002681 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2682 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2683 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002684
2685 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2686
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002687 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002688 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002689 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2690
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002691 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002692 key_data->x,
2693 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002694
2695 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002696 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002697 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002698 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002699
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002700 TEST_ASSERT( actual_status == expected_status );
2701
2702exit:
2703 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002704 mbedtls_psa_crypto_free( );
2705}
2706/* END_CASE */
2707
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002708/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002709void asymmetric_encrypt( int key_type_arg,
2710 data_t *key_data,
2711 int alg_arg,
2712 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002713 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002714 int expected_output_length_arg,
2715 int expected_status_arg )
2716{
2717 int slot = 1;
2718 psa_key_type_t key_type = key_type_arg;
2719 psa_algorithm_t alg = alg_arg;
2720 size_t expected_output_length = expected_output_length_arg;
2721 size_t key_bits;
2722 unsigned char *output = NULL;
2723 size_t output_size;
2724 size_t output_length = ~0;
2725 psa_status_t actual_status;
2726 psa_status_t expected_status = expected_status_arg;
2727 psa_key_policy_t policy;
2728
2729 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2730
2731 /* Import the key */
2732 psa_key_policy_init( &policy );
2733 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2734 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2735 TEST_ASSERT( psa_import_key( slot, key_type,
2736 key_data->x,
2737 key_data->len ) == PSA_SUCCESS );
2738
2739 /* Determine the maximum output length */
2740 TEST_ASSERT( psa_get_key_information( slot,
2741 NULL,
2742 &key_bits ) == PSA_SUCCESS );
2743 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002744 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002745
2746 /* Encrypt the input */
2747 actual_status = psa_asymmetric_encrypt( slot, alg,
2748 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002749 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002750 output, output_size,
2751 &output_length );
2752 TEST_ASSERT( actual_status == expected_status );
2753 TEST_ASSERT( output_length == expected_output_length );
2754
Gilles Peskine68428122018-06-30 18:42:41 +02002755 /* If the label is empty, the test framework puts a non-null pointer
2756 * in label->x. Test that a null pointer works as well. */
2757 if( label->len == 0 )
2758 {
2759 output_length = ~0;
2760 memset( output, 0, output_size );
2761 actual_status = psa_asymmetric_encrypt( slot, alg,
2762 input_data->x, input_data->len,
2763 NULL, label->len,
2764 output, output_size,
2765 &output_length );
2766 TEST_ASSERT( actual_status == expected_status );
2767 TEST_ASSERT( output_length == expected_output_length );
2768 }
2769
Gilles Peskine656896e2018-06-29 19:12:28 +02002770exit:
2771 psa_destroy_key( slot );
2772 mbedtls_free( output );
2773 mbedtls_psa_crypto_free( );
2774}
2775/* END_CASE */
2776
2777/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002778void asymmetric_encrypt_decrypt( int key_type_arg,
2779 data_t *key_data,
2780 int alg_arg,
2781 data_t *input_data,
2782 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002783{
2784 int slot = 1;
2785 psa_key_type_t key_type = key_type_arg;
2786 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002787 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002788 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002789 size_t output_size;
2790 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002791 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002792 size_t output2_size;
2793 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002794 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002795
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002796 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002797 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002798 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2799 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2800
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002801 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2802
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002803 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002804 psa_key_policy_set_usage( &policy,
2805 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002806 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002807 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2808
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002809 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002810 key_data->x,
2811 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002812
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002813
2814 /* Determine the maximum ciphertext length */
2815 TEST_ASSERT( psa_get_key_information( slot,
2816 NULL,
2817 &key_bits ) == PSA_SUCCESS );
2818 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002819 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002820 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002821 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002822
Gilles Peskineeebd7382018-06-08 18:11:54 +02002823 /* We test encryption by checking that encrypt-then-decrypt gives back
2824 * the original plaintext because of the non-optional random
2825 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002826 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002827 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002828 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002829 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002830 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002831 /* We don't know what ciphertext length to expect, but check that
2832 * it looks sensible. */
2833 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002834
Gilles Peskine2d277862018-06-18 15:41:12 +02002835 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002836 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002837 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002838 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002839 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002840 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002841 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002842 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002843
2844exit:
2845 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002846 mbedtls_free( output );
2847 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002848 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002849}
2850/* END_CASE */
2851
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002852/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002853void asymmetric_decrypt( int key_type_arg,
2854 data_t *key_data,
2855 int alg_arg,
2856 data_t *input_data,
2857 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002858 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002859{
2860 int slot = 1;
2861 psa_key_type_t key_type = key_type_arg;
2862 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002863 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002864 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002865 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002866 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002867
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002868 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002869 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002870 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002871 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2872 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2873 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2874
Gilles Peskine4abf7412018-06-18 16:35:34 +02002875 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002876 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002877
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002878 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2879
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002880 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002881 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002882 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2883
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002884 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002885 key_data->x,
2886 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002887
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002888 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002889 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002890 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002891 output,
2892 output_size,
2893 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002894 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002895 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002896
Gilles Peskine68428122018-06-30 18:42:41 +02002897 /* If the label is empty, the test framework puts a non-null pointer
2898 * in label->x. Test that a null pointer works as well. */
2899 if( label->len == 0 )
2900 {
2901 output_length = ~0;
2902 memset( output, 0, output_size );
2903 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2904 input_data->x, input_data->len,
2905 NULL, label->len,
2906 output,
2907 output_size,
2908 &output_length ) == PSA_SUCCESS );
2909 TEST_ASSERT( expected_data->len == output_length );
2910 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2911 }
2912
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002913exit:
2914 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002915 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002916 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002917}
2918/* END_CASE */
2919
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002920/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002921void asymmetric_decrypt_fail( int key_type_arg,
2922 data_t *key_data,
2923 int alg_arg,
2924 data_t *input_data,
2925 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002926 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002927{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002928 int slot = 1;
2929 psa_key_type_t key_type = key_type_arg;
2930 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002931 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002932 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002933 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002934 psa_status_t actual_status;
2935 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002936 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002937
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002938 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002939 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002940 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2941 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2942
Gilles Peskine4abf7412018-06-18 16:35:34 +02002943 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002944 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002945
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002946 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2947
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002948 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002949 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002950 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2951
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002952 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002953 key_data->x,
2954 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955
Gilles Peskine2d277862018-06-18 15:41:12 +02002956 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002957 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002958 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002959 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002960 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002962 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963
Gilles Peskine68428122018-06-30 18:42:41 +02002964 /* If the label is empty, the test framework puts a non-null pointer
2965 * in label->x. Test that a null pointer works as well. */
2966 if( label->len == 0 )
2967 {
2968 output_length = ~0;
2969 memset( output, 0, output_size );
2970 actual_status = psa_asymmetric_decrypt( slot, alg,
2971 input_data->x, input_data->len,
2972 NULL, label->len,
2973 output, output_size,
2974 &output_length );
2975 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002976 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002977 }
2978
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979exit:
2980 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002981 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982 mbedtls_psa_crypto_free( );
2983}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002984/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002985
2986/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002987void derive_setup( int key_type_arg,
2988 data_t *key_data,
2989 int alg_arg,
2990 data_t *salt,
2991 data_t *label,
2992 int requested_capacity_arg,
2993 int expected_status_arg )
2994{
2995 psa_key_slot_t slot = 1;
2996 size_t key_type = key_type_arg;
2997 psa_algorithm_t alg = alg_arg;
2998 size_t requested_capacity = requested_capacity_arg;
2999 psa_status_t expected_status = expected_status_arg;
3000 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3001 psa_key_policy_t policy;
3002
3003 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3004
3005 psa_key_policy_init( &policy );
3006 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3007 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3008
3009 TEST_ASSERT( psa_import_key( slot, key_type,
3010 key_data->x,
3011 key_data->len ) == PSA_SUCCESS );
3012
3013 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3014 salt->x, salt->len,
3015 label->x, label->len,
3016 requested_capacity ) == expected_status );
3017
3018exit:
3019 psa_generator_abort( &generator );
3020 psa_destroy_key( slot );
3021 mbedtls_psa_crypto_free( );
3022}
3023/* END_CASE */
3024
3025/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003026void derive_output( int alg_arg,
3027 data_t *key_data,
3028 data_t *salt,
3029 data_t *label,
3030 int requested_capacity_arg,
3031 data_t *expected_output1,
3032 data_t *expected_output2 )
3033{
3034 psa_key_slot_t slot = 1;
3035 psa_algorithm_t alg = alg_arg;
3036 size_t requested_capacity = requested_capacity_arg;
3037 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3038 uint8_t *expected_outputs[2] =
3039 {expected_output1->x, expected_output2->x};
3040 size_t output_sizes[2] =
3041 {expected_output1->len, expected_output2->len};
3042 size_t output_buffer_size = 0;
3043 uint8_t *output_buffer = NULL;
3044 size_t expected_capacity;
3045 size_t current_capacity;
3046 psa_key_policy_t policy;
3047 psa_status_t status;
3048 unsigned i;
3049
3050 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3051 {
3052 if( output_sizes[i] > output_buffer_size )
3053 output_buffer_size = output_sizes[i];
3054 if( output_sizes[i] == 0 )
3055 expected_outputs[i] = NULL;
3056 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003057 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003058 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3059
3060 psa_key_policy_init( &policy );
3061 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3062 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3063
3064 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3065 key_data->x,
3066 key_data->len ) == PSA_SUCCESS );
3067
3068 /* Extraction phase. */
3069 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3070 salt->x, salt->len,
3071 label->x, label->len,
3072 requested_capacity ) == PSA_SUCCESS );
3073 TEST_ASSERT( psa_get_generator_capacity( &generator,
3074 &current_capacity ) ==
3075 PSA_SUCCESS );
3076 TEST_ASSERT( current_capacity == requested_capacity );
3077 expected_capacity = requested_capacity;
3078
3079 /* Expansion phase. */
3080 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3081 {
3082 /* Read some bytes. */
3083 status = psa_generator_read( &generator,
3084 output_buffer, output_sizes[i] );
3085 if( expected_capacity == 0 && output_sizes[i] == 0 )
3086 {
3087 /* Reading 0 bytes when 0 bytes are available can go either way. */
3088 TEST_ASSERT( status == PSA_SUCCESS ||
3089 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3090 continue;
3091 }
3092 else if( expected_capacity == 0 ||
3093 output_sizes[i] > expected_capacity )
3094 {
3095 /* Capacity exceeded. */
3096 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3097 expected_capacity = 0;
3098 continue;
3099 }
3100 /* Success. Check the read data. */
3101 TEST_ASSERT( status == PSA_SUCCESS );
3102 if( output_sizes[i] != 0 )
3103 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3104 output_sizes[i] ) == 0 );
3105 /* Check the generator status. */
3106 expected_capacity -= output_sizes[i];
3107 TEST_ASSERT( psa_get_generator_capacity( &generator,
3108 &current_capacity ) ==
3109 PSA_SUCCESS );
3110 TEST_ASSERT( expected_capacity == current_capacity );
3111 }
3112 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3113
3114exit:
3115 mbedtls_free( output_buffer );
3116 psa_generator_abort( &generator );
3117 psa_destroy_key( slot );
3118 mbedtls_psa_crypto_free( );
3119}
3120/* END_CASE */
3121
3122/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003123void derive_full( int alg_arg,
3124 data_t *key_data,
3125 data_t *salt,
3126 data_t *label,
3127 int requested_capacity_arg )
3128{
3129 psa_key_slot_t slot = 1;
3130 psa_algorithm_t alg = alg_arg;
3131 size_t requested_capacity = requested_capacity_arg;
3132 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3133 unsigned char output_buffer[16];
3134 size_t expected_capacity = requested_capacity;
3135 size_t current_capacity;
3136 psa_key_policy_t policy;
3137
3138 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3139
3140 psa_key_policy_init( &policy );
3141 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3142 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3143
3144 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3145 key_data->x,
3146 key_data->len ) == PSA_SUCCESS );
3147
3148 /* Extraction phase. */
3149 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3150 salt->x, salt->len,
3151 label->x, label->len,
3152 requested_capacity ) == PSA_SUCCESS );
3153 TEST_ASSERT( psa_get_generator_capacity( &generator,
3154 &current_capacity ) ==
3155 PSA_SUCCESS );
3156 TEST_ASSERT( current_capacity == expected_capacity );
3157
3158 /* Expansion phase. */
3159 while( current_capacity > 0 )
3160 {
3161 size_t read_size = sizeof( output_buffer );
3162 if( read_size > current_capacity )
3163 read_size = current_capacity;
3164 TEST_ASSERT( psa_generator_read( &generator,
3165 output_buffer,
3166 read_size ) == PSA_SUCCESS );
3167 expected_capacity -= read_size;
3168 TEST_ASSERT( psa_get_generator_capacity( &generator,
3169 &current_capacity ) ==
3170 PSA_SUCCESS );
3171 TEST_ASSERT( current_capacity == expected_capacity );
3172 }
3173
3174 /* Check that the generator refuses to go over capacity. */
3175 TEST_ASSERT( psa_generator_read( &generator,
3176 output_buffer,
3177 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3178
3179 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3180
3181exit:
3182 psa_generator_abort( &generator );
3183 psa_destroy_key( slot );
3184 mbedtls_psa_crypto_free( );
3185}
3186/* END_CASE */
3187
3188/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003189void derive_key_exercise( int alg_arg,
3190 data_t *key_data,
3191 data_t *salt,
3192 data_t *label,
3193 int derived_type_arg,
3194 int derived_bits_arg,
3195 int derived_usage_arg,
3196 int derived_alg_arg )
3197{
3198 psa_key_slot_t base_key = 1;
3199 psa_key_slot_t derived_key = 2;
3200 psa_algorithm_t alg = alg_arg;
3201 psa_key_type_t derived_type = derived_type_arg;
3202 size_t derived_bits = derived_bits_arg;
3203 psa_key_usage_t derived_usage = derived_usage_arg;
3204 psa_algorithm_t derived_alg = derived_alg_arg;
3205 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3206 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3207 psa_key_policy_t policy;
3208 psa_key_type_t got_type;
3209 size_t got_bits;
3210
3211 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3212
3213 psa_key_policy_init( &policy );
3214 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3215 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3216 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3217 key_data->x,
3218 key_data->len ) == PSA_SUCCESS );
3219
3220 /* Derive a key. */
3221 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3222 salt->x, salt->len,
3223 label->x, label->len,
3224 capacity ) == PSA_SUCCESS );
3225 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3226 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3227 TEST_ASSERT( psa_generator_import_key( derived_key,
3228 derived_type,
3229 derived_bits,
3230 &generator ) == PSA_SUCCESS );
3231
3232 /* Test the key information */
3233 TEST_ASSERT( psa_get_key_information( derived_key,
3234 &got_type,
3235 &got_bits ) == PSA_SUCCESS );
3236 TEST_ASSERT( got_type == derived_type );
3237 TEST_ASSERT( got_bits == derived_bits );
3238
3239 /* Exercise the derived key. */
3240 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3241 goto exit;
3242
3243exit:
3244 psa_generator_abort( &generator );
3245 psa_destroy_key( base_key );
3246 psa_destroy_key( derived_key );
3247 mbedtls_psa_crypto_free( );
3248}
3249/* END_CASE */
3250
3251/* BEGIN_CASE */
3252void derive_key_export( int alg_arg,
3253 data_t *key_data,
3254 data_t *salt,
3255 data_t *label,
3256 int bytes1_arg,
3257 int bytes2_arg )
3258{
3259 psa_key_slot_t base_key = 1;
3260 psa_key_slot_t derived_key = 2;
3261 psa_algorithm_t alg = alg_arg;
3262 size_t bytes1 = bytes1_arg;
3263 size_t bytes2 = bytes2_arg;
3264 size_t capacity = bytes1 + bytes2;
3265 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003266 uint8_t *output_buffer = NULL;
3267 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003268 psa_key_policy_t policy;
3269 size_t length;
3270
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003271 ASSERT_ALLOC( output_buffer, capacity );
3272 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003273 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3274
3275 psa_key_policy_init( &policy );
3276 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3277 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3278 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3279 key_data->x,
3280 key_data->len ) == PSA_SUCCESS );
3281
3282 /* Derive some material and output it. */
3283 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3284 salt->x, salt->len,
3285 label->x, label->len,
3286 capacity ) == PSA_SUCCESS );
3287 TEST_ASSERT( psa_generator_read( &generator,
3288 output_buffer,
3289 capacity ) == PSA_SUCCESS );
3290 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3291
3292 /* Derive the same output again, but this time store it in key objects. */
3293 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3294 salt->x, salt->len,
3295 label->x, label->len,
3296 capacity ) == PSA_SUCCESS );
3297 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3298 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3299 TEST_ASSERT( psa_generator_import_key( derived_key,
3300 PSA_KEY_TYPE_RAW_DATA,
3301 PSA_BYTES_TO_BITS( bytes1 ),
3302 &generator ) == PSA_SUCCESS );
3303 TEST_ASSERT( psa_export_key( derived_key,
3304 export_buffer, bytes1,
3305 &length ) == PSA_SUCCESS );
3306 TEST_ASSERT( length == bytes1 );
3307 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3308 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3309 TEST_ASSERT( psa_generator_import_key( derived_key,
3310 PSA_KEY_TYPE_RAW_DATA,
3311 PSA_BYTES_TO_BITS( bytes2 ),
3312 &generator ) == PSA_SUCCESS );
3313 TEST_ASSERT( psa_export_key( derived_key,
3314 export_buffer + bytes1, bytes2,
3315 &length ) == PSA_SUCCESS );
3316 TEST_ASSERT( length == bytes2 );
3317
3318 /* Compare the outputs from the two runs. */
3319 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3320
3321exit:
3322 mbedtls_free( output_buffer );
3323 mbedtls_free( export_buffer );
3324 psa_generator_abort( &generator );
3325 psa_destroy_key( base_key );
3326 psa_destroy_key( derived_key );
3327 mbedtls_psa_crypto_free( );
3328}
3329/* END_CASE */
3330
3331/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003332void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003333{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003334 size_t bytes = bytes_arg;
3335 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003336 unsigned char *output = NULL;
3337 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003338 size_t i;
3339 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003340
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003341 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3342 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003343 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003344
3345 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3346
Gilles Peskinea50d7392018-06-21 10:22:13 +02003347 /* Run several times, to ensure that every output byte will be
3348 * nonzero at least once with overwhelming probability
3349 * (2^(-8*number_of_runs)). */
3350 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003351 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003352 memset( output, 0, bytes );
3353 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3354
3355 /* Check that no more than bytes have been overwritten */
3356 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3357
3358 for( i = 0; i < bytes; i++ )
3359 {
3360 if( output[i] != 0 )
3361 ++changed[i];
3362 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003363 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003364
3365 /* Check that every byte was changed to nonzero at least once. This
3366 * validates that psa_generate_random is overwriting every byte of
3367 * the output buffer. */
3368 for( i = 0; i < bytes; i++ )
3369 {
3370 TEST_ASSERT( changed[i] != 0 );
3371 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003372
3373exit:
3374 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003375 mbedtls_free( output );
3376 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003377}
3378/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003379
3380/* BEGIN_CASE */
3381void generate_key( int type_arg,
3382 int bits_arg,
3383 int usage_arg,
3384 int alg_arg,
3385 int expected_status_arg )
3386{
3387 int slot = 1;
3388 psa_key_type_t type = type_arg;
3389 psa_key_usage_t usage = usage_arg;
3390 size_t bits = bits_arg;
3391 psa_algorithm_t alg = alg_arg;
3392 psa_status_t expected_status = expected_status_arg;
3393 psa_key_type_t got_type;
3394 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003395 psa_status_t expected_info_status =
3396 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3397 psa_key_policy_t policy;
3398
3399 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3400
3401 psa_key_policy_init( &policy );
3402 psa_key_policy_set_usage( &policy, usage, alg );
3403 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3404
3405 /* Generate a key */
3406 TEST_ASSERT( psa_generate_key( slot, type, bits,
3407 NULL, 0 ) == expected_status );
3408
3409 /* Test the key information */
3410 TEST_ASSERT( psa_get_key_information( slot,
3411 &got_type,
3412 &got_bits ) == expected_info_status );
3413 if( expected_info_status != PSA_SUCCESS )
3414 goto exit;
3415 TEST_ASSERT( got_type == type );
3416 TEST_ASSERT( got_bits == bits );
3417
Gilles Peskine818ca122018-06-20 18:16:48 +02003418 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003419 if( ! exercise_key( slot, usage, alg ) )
3420 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003421
3422exit:
3423 psa_destroy_key( slot );
3424 mbedtls_psa_crypto_free( );
3425}
3426/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003427
3428/* BEGIN_CASE */
3429void validate_module_init_generate_random( )
3430{
3431 psa_status_t status;
3432 uint8_t random[10] = { 0 };
3433 status = psa_generate_random( random, sizeof( random ) );
3434 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3435}
3436/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003437
3438/* BEGIN_CASE */
3439void validate_module_init_key_based( )
3440{
3441 psa_status_t status;
3442 uint8_t data[10] = { 0 };
3443 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3444 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3445}
3446/* END_CASE */