blob: 576d467008653a7aced506cf7558b3259192e23a [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
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \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 Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
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 Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliottd3f82412021-06-16 16:52:21 +0100267static psa_status_t aead_multipart_encrypt_internal( int key_type_arg,
268 data_t *key_data,
269 int alg_arg,
270 data_t *nonce,
271 data_t *additional_data,
272 int ad_part_len,
273 data_t *input_data,
274 int data_part_len,
275 data_t *expected_result )
276{
277 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
278 psa_key_type_t key_type = key_type_arg;
279 psa_algorithm_t alg = alg_arg;
280 psa_aead_operation_t operation;
281 unsigned char *output_data = NULL;
282 unsigned char *part_data = NULL;
283 unsigned char *final_data = NULL;
284 size_t output_size = 0;
285 size_t finish_output_size;
286 size_t part_data_size = 0;
287 size_t output_length = 0;
288 size_t key_bits = 0;
289 size_t tag_length = 0;
290 size_t tag_size = 0;
291 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
292 uint32_t part_offset = 0;
293 size_t part_length = 0;
294 size_t output_part_length = 0;
295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
296 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
297
298 PSA_ASSERT( psa_crypto_init( ) );
299
300 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
301 psa_set_key_algorithm( &attributes, alg );
302 psa_set_key_type( &attributes, key_type );
303
304 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
305 &key ) );
306
307 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
308 key_bits = psa_get_key_bits( &attributes );
309
310 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
311
312 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
313
314 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
315 ( input_data->len +
316 tag_length ) );
317
318 ASSERT_ALLOC( output_data, output_size );
319
320 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
321
322 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
323
324 ASSERT_ALLOC( final_data, finish_output_size );
325
326 operation = psa_aead_operation_init( );
327
328 status = psa_aead_encrypt_setup( &operation, key, alg );
329
330 /* If the operation is not supported, just skip and not fail in case the
331 * encryption involves a common limitation of cryptography hardwares and
332 * an alternative implementation. */
333 if( status == PSA_ERROR_NOT_SUPPORTED )
334 {
335 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
336 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
337 }
338
339 PSA_ASSERT( status );
340
341 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
342
343#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
344 if( operation.alg == PSA_ALG_GCM )
345 {
346 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
347 input_data->len ) );
348 }
349#endif
350
351 if( ad_part_len != -1 )
352 {
353 /* Pass additional data in parts */
354 part_offset = 0;
355
356 while( part_offset < additional_data->len )
357 {
358 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
359 {
360 part_length = additional_data->len - part_offset;
361 }
362 else
363 {
364 part_length = ad_part_len;
365 }
366
367 PSA_ASSERT( psa_aead_update_ad( &operation,
368 additional_data->x + part_offset,
369 part_length ) );
370
371 part_offset += part_length;
372 }
373 }
374 else
375 {
376 /* Pass additional data in one go. */
377 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
378 additional_data->len ) );
379 }
380
381 if( data_part_len != -1 )
382 {
383 /* Pass data in parts */
384 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
385 ( size_t ) data_part_len );
386
387 ASSERT_ALLOC( part_data, part_data_size );
388
389 part_offset = 0;
390
391 while( part_offset < input_data->len )
392 {
393 if( input_data->len - part_offset < ( uint32_t ) data_part_len )
394 {
395 part_length = input_data->len - part_offset;
396 }
397 else
398 {
399 part_length = data_part_len;
400 }
401
402 PSA_ASSERT( psa_aead_update( &operation,
403 ( input_data->x + part_offset ),
404 part_length, part_data,
405 part_data_size,
406 &output_part_length ) );
407
408 if( output_data && output_part_length )
409 {
410 memcpy( ( output_data + part_offset ), part_data,
411 output_part_length );
412 }
413
414 part_offset += part_length;
415 output_length += output_part_length;
416 }
417 }
418 else
419 {
420 /* Pass whole data in one go */
421 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
422 input_data->len, output_data,
423 output_size, &output_length ) );
424 }
425
426 PSA_ASSERT( psa_aead_finish( &operation, final_data,
427 finish_output_size,
428 &output_part_length,
429 tag_buffer, tag_length,
430 &tag_size ) );
431
432 if( output_data && output_part_length )
433 {
434 memcpy( ( output_data + output_length ), final_data,
435 output_part_length );
436 }
437
438 TEST_EQUAL( tag_length, tag_size );
439
440 output_length += output_part_length;
441
442 if( output_data && tag_length )
443 {
444 memcpy( ( output_data + output_length ), tag_buffer, tag_length );
445 }
446
447 output_length += tag_length;
448
449 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
450 * should be exact. */
451 TEST_EQUAL( output_length,
452 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
453 input_data->len ) );
454 TEST_ASSERT( output_length <=
455 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
456
457 ASSERT_COMPARE( expected_result->x, expected_result->len,
458 output_data, output_length );
459
460exit:
461 psa_destroy_key( key );
462 psa_aead_abort( &operation );
463 mbedtls_free( output_data );
464 mbedtls_free( part_data );
465 mbedtls_free( final_data );
466 PSA_DONE( );
467
468 return( status );
469}
470
471void aead_multipart_decrypt_internal( int key_type_arg, data_t *key_data,
472 int alg_arg,
473 data_t *nonce,
474 data_t *additional_data,
475 int ad_part_len,
476 data_t *input_data,
477 int data_part_len,
478 data_t *expected_data,
479 int expected_result_arg )
480{
481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
482 psa_key_type_t key_type = key_type_arg;
483 psa_algorithm_t alg = alg_arg;
484 psa_aead_operation_t operation;
485 unsigned char *output_data = NULL;
486 unsigned char *part_data = NULL;
487 unsigned char *final_data = NULL;
488 size_t part_data_size;
489 size_t output_size = 0;
490 size_t verify_output_size = 0;
491 size_t output_length = 0;
492 size_t key_bits = 0;
493 size_t tag_length = 0;
494 uint32_t part_offset = 0;
495 size_t part_length = 0;
496 size_t output_part_length = 0;
497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
498 psa_status_t expected_result = expected_result_arg;
499 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
500
501 PSA_ASSERT( psa_crypto_init( ) );
502
503 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
504 psa_set_key_algorithm( &attributes, alg );
505 psa_set_key_type( &attributes, key_type );
506
507 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
508 &key ) );
509
510 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
511 key_bits = psa_get_key_bits( &attributes );
512
513 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
514
515 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
516 ( input_data->len -
517 tag_length ) );
518
519 ASSERT_ALLOC( output_data, output_size );
520
521 verify_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
522 TEST_ASSERT( verify_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
523 ASSERT_ALLOC( final_data, verify_output_size );
524
525 operation = psa_aead_operation_init( );
526
527 status = psa_aead_decrypt_setup( &operation, key, alg );
528
529 /* If the operation is not supported, just skip and not fail in case the
530 * encryption involves a common limitation of cryptography hardwares and
531 * an alternative implementation. */
532 if( status == PSA_ERROR_NOT_SUPPORTED )
533 {
534 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
535 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
536 }
537
538 if( status != PSA_SUCCESS )
539 {
540 TEST_EQUAL( status, expected_result_arg );
541 goto exit;
542 }
543
544 status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
545
546 if( status != PSA_SUCCESS )
547 {
548 TEST_EQUAL( status, expected_result_arg );
549 goto exit;
550 }
551
552#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
553 if( operation.alg == PSA_ALG_GCM )
554 {
555 status = psa_aead_set_lengths( &operation, additional_data->len,
556 ( input_data->len - tag_length ) );
557
558 if( status != PSA_SUCCESS )
559 {
560 TEST_EQUAL( status, expected_result_arg );
561 goto exit;
562 }
563 }
564#endif
565
566 if( ad_part_len != -1 )
567 {
568 part_offset = 0;
569
570 while( part_offset < additional_data->len )
571 {
572 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
573 {
574 part_length = additional_data->len - part_offset;
575 }
576 else
577 {
578 part_length = ad_part_len;
579 }
580
581 status = psa_aead_update_ad( &operation,
582 additional_data->x + part_offset,
583 part_length );
584
585 if( status != PSA_SUCCESS )
586 {
587 TEST_EQUAL( status, expected_result_arg );
588 goto exit;
589 }
590
591 part_offset += part_length;
592 }
593 }
594 else
595 {
596 status = psa_aead_update_ad( &operation, additional_data->x,
597 additional_data->len );
598
599 if( status != PSA_SUCCESS )
600 {
601 TEST_EQUAL( status, expected_result_arg );
602 goto exit;
603 }
604 }
605
606 if( data_part_len != -1 )
607 {
608 /* Pass data in parts */
609 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
610 ( size_t ) data_part_len );
611
612 ASSERT_ALLOC( part_data, part_data_size );
613
614 part_offset = 0;
615
616 while( part_offset < ( input_data->len - tag_length) )
617 {
618 if( (input_data->len - tag_length - part_offset ) <
619 ( uint32_t ) data_part_len )
620 {
621 part_length = ( input_data->len - tag_length - part_offset );
622 }
623 else
624 {
625 part_length = data_part_len;
626 }
627
628 status = psa_aead_update( &operation,
629 ( input_data->x + part_offset ),
630 part_length, part_data,
631 part_data_size, &output_part_length );
632
633 if( status != PSA_SUCCESS )
634 {
635 TEST_EQUAL( status, expected_result_arg );
636 goto exit;
637 }
638
639 if( output_data && output_part_length )
640 {
641 memcpy( ( output_data + part_offset ), part_data,
642 output_part_length );
643 }
644
645 part_offset += part_length;
646 output_length += output_part_length;
647 }
648 }
649 else
650 {
651 status = psa_aead_update( &operation, input_data->x,
652 ( input_data->len - tag_length ), output_data,
653 output_size, &output_length );
654
655 if( status != PSA_SUCCESS )
656 {
657 TEST_EQUAL( status, expected_result_arg );
658 goto exit;
659 }
660 }
661
662 status = psa_aead_verify( &operation, final_data,
663 verify_output_size,
664 &output_part_length,
665 ( input_data->x + input_data->len - tag_length ),
666 tag_length );
667
668 if( status != PSA_SUCCESS )
669 {
670 TEST_EQUAL( status, expected_result_arg );
671 goto exit;
672 }
673
674 if( output_data && output_part_length )
675 {
676 memcpy( ( output_data + output_length ), final_data,
677 output_part_length );
678 }
679
680 output_length += output_part_length;
681
682 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
683 {
684 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
685 * should be exact. */
686 TEST_EQUAL( output_length,
687 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
688 input_data->len ) );
689 TEST_ASSERT( output_length <=
690 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
691 }
692
693 if( expected_result == PSA_SUCCESS )
694 {
695 ASSERT_COMPARE( expected_data->x, expected_data->len,
696 output_data, output_length );
697 }
698
699exit:
700 psa_destroy_key( key );
701 psa_aead_abort( &operation );
702 mbedtls_free( output_data );
703 mbedtls_free( part_data );
704 mbedtls_free( final_data );
705 PSA_DONE( );
706}
707
708void aead_multipart_encrypt_decrypt_internal( int key_type_arg,
709 data_t *key_data,
710 int alg_arg,
711 data_t *nonce,
712 data_t *additional_data,
713 int ad_part_len,
714 data_t *input_data,
715 int data_part_len,
716 int expected_status_arg )
717{
718 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
719 psa_key_type_t key_type = key_type_arg;
720 psa_algorithm_t alg = alg_arg;
721 psa_aead_operation_t operation;
722 unsigned char *output_data = NULL;
723 unsigned char *part_data = NULL;
724 unsigned char *final_data = NULL;
725 size_t part_data_size;
726 size_t output_size = 0;
727 size_t finish_output_size = 0;
728 size_t output_length = 0;
729 unsigned char *output_data2 = NULL;
730 size_t output_size2 = 0;
731 size_t output_length2 = 0;
732 size_t key_bits = 0;
733 size_t tag_length = 0;
734 size_t tag_size = 0;
735 size_t nonce_length = 0;
736 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
737 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
738 uint32_t part_offset = 0;
739 size_t part_length = 0;
740 size_t output_part_length = 0;
741 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
742 psa_status_t expected_status = expected_status_arg;
743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
744
745 PSA_ASSERT( psa_crypto_init( ) );
746
747 psa_set_key_usage_flags( &attributes,
748 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
749 psa_set_key_algorithm( &attributes, alg );
750 psa_set_key_type( &attributes, key_type );
751
752 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
753 &key ) );
754
755 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
756 key_bits = psa_get_key_bits( &attributes );
757
758 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
759
760 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
761
762 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
763
764 ASSERT_ALLOC( output_data, output_size );
765
766 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
767
768 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
769
770 ASSERT_ALLOC( final_data, finish_output_size );
771
772 operation = psa_aead_operation_init( );
773
774 status = psa_aead_encrypt_setup( &operation, key, alg );
775
776 /* If the operation is not supported, just skip and not fail in case the
777 * encryption involves a common limitation of cryptography hardwares and
778 * an alternative implementation. */
779 if( status == PSA_ERROR_NOT_SUPPORTED )
780 {
781 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
782 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
783 }
784
785 if( status != PSA_SUCCESS )
786 {
787 TEST_EQUAL( status, expected_status );
788 goto exit;
789 }
790
791 nonce_length = nonce->len;
792 status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
793
794 if( status != PSA_SUCCESS )
795 {
796 TEST_EQUAL( status, expected_status );
797 goto exit;
798 }
799
800#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
801 if( operation.alg == PSA_ALG_GCM )
802 {
803 status = psa_aead_set_lengths( &operation, additional_data->len,
804 input_data->len );
805
806 if( status != PSA_SUCCESS )
807 {
808 TEST_EQUAL( status, expected_status );
809 goto exit;
810 }
811 }
812#endif
813
814 if( ad_part_len != -1 )
815 {
816 part_offset = 0;
817
818 while( part_offset < additional_data->len )
819 {
820 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
821 {
822 part_length = additional_data->len - part_offset;
823 }
824 else
825 {
826 part_length = ad_part_len;
827 }
828
829 status = psa_aead_update_ad( &operation,
830 additional_data->x + part_offset,
831 part_length );
832
833 if( status != PSA_SUCCESS )
834 {
835 TEST_EQUAL( status, expected_status );
836 goto exit;
837 }
838
839 part_offset += part_length;
840 }
841 }
842 else
843 {
844 status = psa_aead_update_ad( &operation, additional_data->x,
845 additional_data->len );
846
847 if( status != PSA_SUCCESS )
848 {
849 TEST_EQUAL( status, expected_status );
850 goto exit;
851 }
852 }
853
854 if( data_part_len != -1 )
855 {
856 /* Pass data in parts */
857 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
858 ( size_t ) data_part_len );
859
860 ASSERT_ALLOC( part_data, part_data_size );
861
862 part_offset = 0;
863
864 while( part_offset < input_data->len )
865 {
866 if( input_data->len - part_offset < ( uint32_t ) data_part_len )
867 {
868 part_length = input_data->len - part_offset;
869 }
870 else
871 {
872 part_length = data_part_len;
873 }
874
875 status = psa_aead_update( &operation,
876 ( input_data->x + part_offset ),
877 part_length, part_data,
878 part_data_size, &output_part_length );
879
880 if( status != PSA_SUCCESS )
881 {
882 TEST_EQUAL( status, expected_status );
883 goto exit;
884 }
885
886 if( output_data && output_part_length )
887 {
888 memcpy( ( output_data + part_offset ), part_data,
889 output_part_length );
890 }
891
892 part_offset += part_length;
893 output_length += output_part_length;
894 }
895 }
896 else
897 {
898 status = psa_aead_update( &operation, input_data->x,
899 input_data->len, output_data,
900 output_size, &output_length );
901
902 if( status != PSA_SUCCESS )
903 {
904 TEST_EQUAL( status, expected_status );
905 goto exit;
906 }
907 }
908
909 status = psa_aead_finish( &operation, final_data,
910 finish_output_size,
911 &output_part_length,
912 tag_buffer, tag_length,
913 &tag_size );
914
915 if( status != PSA_SUCCESS )
916 {
917 TEST_EQUAL( status, expected_status );
918 goto exit;
919 }
920
921 if( output_data && output_part_length )
922 {
923 memcpy( ( output_data + output_length ), final_data,
924 output_part_length );
925 }
926
927 output_length += output_part_length;
928
929 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
930 * should be exact. */
931 if( expected_status != PSA_ERROR_INVALID_ARGUMENT )
932 {
933 TEST_EQUAL( ( output_length + tag_length ),
934 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
935 input_data->len ) );
936 }
937
938 TEST_EQUAL( tag_length, tag_size );
939
940 if( PSA_SUCCESS == expected_status )
941 {
942 output_size2 = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
943 output_length );
944 ASSERT_ALLOC( output_data2, output_size2 );
945
946 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
947 * should be exact. */
948 TEST_EQUAL( input_data->len,
949 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
950 ( output_length +
951 tag_length ) ) );
952
953 TEST_ASSERT( input_data->len <=
954 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length +
955 tag_length ) );
956
957 operation = psa_aead_operation_init( );
958
959 status = psa_aead_decrypt_setup( &operation, key, alg );
960
961 /* If the operation is not supported, just skip and not fail in case the
962 * encryption involves a common limitation of cryptography hardwares and
963 * an alternative implementation. */
964 if( status == PSA_ERROR_NOT_SUPPORTED )
965 {
966 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
967 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg,
968 nonce->len );
969 }
970
971 TEST_EQUAL( status, expected_status );
972
973 if( nonce->len == 0 )
974 {
975 /* Use previously generated nonce. */
976 status = psa_aead_set_nonce( &operation, nonce_buffer,
977 nonce_length );
978 }
979 else
980 {
981 nonce_length = nonce->len;
982 status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
983 }
984
985 if( status != PSA_SUCCESS )
986 {
987 TEST_EQUAL( status, expected_status);
988 }
989
990#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
991 if( operation.alg == PSA_ALG_GCM )
992 {
993 status = psa_aead_set_lengths( &operation, additional_data->len,
994 output_length );
995
996 if( status != PSA_SUCCESS )
997 {
998 TEST_EQUAL( status, expected_status );
999 }
1000 }
1001#endif
1002
1003 if( ad_part_len != -1 )
1004 {
1005 part_offset = 0;
1006
1007 while( part_offset < additional_data->len )
1008 {
1009 if( additional_data->len - part_offset <
1010 ( uint32_t ) ad_part_len )
1011 {
1012 part_length = additional_data->len - part_offset;
1013 }
1014 else
1015 {
1016 part_length = ad_part_len;
1017 }
1018
1019 PSA_ASSERT( psa_aead_update_ad( &operation,
1020 additional_data->x +
1021 part_offset,
1022 part_length ) );
1023
1024 part_offset += part_length;
1025 }
1026 }
1027 else
1028 {
1029 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
1030 additional_data->len ) );
1031 }
1032
1033 if( data_part_len != -1 )
1034 {
1035 /* Pass data in parts */
1036 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
1037 ( size_t ) data_part_len );
1038
1039 part_data = NULL;
1040 ASSERT_ALLOC( part_data, part_data_size );
1041
1042 part_offset = 0;
1043
1044 while( part_offset < output_length )
1045 {
1046 if( ( output_length - part_offset ) <
1047 ( uint32_t ) data_part_len )
1048 {
1049 part_length = ( output_length - part_offset );
1050 }
1051 else
1052 {
1053 part_length = data_part_len;
1054 }
1055
1056 PSA_ASSERT( psa_aead_update( &operation,
1057 ( output_data + part_offset ),
1058 part_length, part_data,
1059 part_data_size,
1060 &output_part_length ) );
1061
1062 if( output_data2 && output_part_length )
1063 {
1064 memcpy( ( output_data2 + part_offset ),
1065 part_data, output_part_length );
1066 }
1067
1068 part_offset += part_length;
1069 output_length2 += output_part_length;
1070 }
1071 }
1072 else
1073 {
1074 PSA_ASSERT( psa_aead_update( &operation, output_data,
1075 output_length, output_data2,
1076 output_size2, &output_length2 ) );
1077 }
1078
1079 PSA_ASSERT( psa_aead_verify( &operation, final_data,
1080 finish_output_size,
1081 &output_part_length,
1082 tag_buffer, tag_length ) );
1083
1084 if( output_data2 && output_part_length )
1085 {
1086 memcpy( ( output_data2 + output_length2 ), final_data,
1087 output_part_length );
1088 }
1089
1090 output_length2 += output_part_length;
1091
1092 ASSERT_COMPARE( input_data->x, input_data->len,
1093 output_data2, output_length2 );
1094 }
1095
1096exit:
1097 psa_destroy_key( key );
1098 psa_aead_abort( &operation );
1099 mbedtls_free( output_data );
1100 mbedtls_free( output_data2 );
1101 mbedtls_free( part_data );
1102 mbedtls_free( final_data );
1103 PSA_DONE( );
1104}
1105
Gilles Peskinee59236f2018-01-27 23:32:46 +01001106/* END_HEADER */
1107
1108/* BEGIN_DEPENDENCIES
1109 * depends_on:MBEDTLS_PSA_CRYPTO_C
1110 * END_DEPENDENCIES
1111 */
1112
1113/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001114void static_checks( )
1115{
1116 size_t max_truncated_mac_size =
1117 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1118
1119 /* Check that the length for a truncated MAC always fits in the algorithm
1120 * encoding. The shifted mask is the maximum truncated value. The
1121 * untruncated algorithm may be one byte larger. */
1122 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1123}
1124/* END_CASE */
1125
1126/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001127void import_with_policy( int type_arg,
1128 int usage_arg, int alg_arg,
1129 int expected_status_arg )
1130{
1131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1132 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001134 psa_key_type_t type = type_arg;
1135 psa_key_usage_t usage = usage_arg;
1136 psa_algorithm_t alg = alg_arg;
1137 psa_status_t expected_status = expected_status_arg;
1138 const uint8_t key_material[16] = {0};
1139 psa_status_t status;
1140
1141 PSA_ASSERT( psa_crypto_init( ) );
1142
1143 psa_set_key_type( &attributes, type );
1144 psa_set_key_usage_flags( &attributes, usage );
1145 psa_set_key_algorithm( &attributes, alg );
1146
1147 status = psa_import_key( &attributes,
1148 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001149 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001150 TEST_EQUAL( status, expected_status );
1151 if( status != PSA_SUCCESS )
1152 goto exit;
1153
Ronald Cron5425a212020-08-04 14:58:35 +02001154 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001155 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1156 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1157 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001158 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001159
Ronald Cron5425a212020-08-04 14:58:35 +02001160 PSA_ASSERT( psa_destroy_key( key ) );
1161 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001162
1163exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001164 /*
1165 * Key attributes may have been returned by psa_get_key_attributes()
1166 * thus reset them as required.
1167 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001168 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001169
1170 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001171 PSA_DONE( );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
1176void import_with_data( data_t *data, int type_arg,
1177 int attr_bits_arg,
1178 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001179{
1180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1181 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001184 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001185 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001187
Gilles Peskine8817f612018-12-18 00:18:46 +01001188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001189
Gilles Peskine4747d192019-04-17 15:05:45 +02001190 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001191 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001192
Ronald Cron5425a212020-08-04 14:58:35 +02001193 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001194 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001195 if( status != PSA_SUCCESS )
1196 goto exit;
1197
Ronald Cron5425a212020-08-04 14:58:35 +02001198 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001199 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001200 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001201 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001202 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203
Ronald Cron5425a212020-08-04 14:58:35 +02001204 PSA_ASSERT( psa_destroy_key( key ) );
1205 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206
1207exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001208 /*
1209 * Key attributes may have been returned by psa_get_key_attributes()
1210 * thus reset them as required.
1211 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001212 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001213
1214 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001215 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001216}
1217/* END_CASE */
1218
1219/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001220void import_large_key( int type_arg, int byte_size_arg,
1221 int expected_status_arg )
1222{
1223 psa_key_type_t type = type_arg;
1224 size_t byte_size = byte_size_arg;
1225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1226 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001227 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001228 psa_status_t status;
1229 uint8_t *buffer = NULL;
1230 size_t buffer_size = byte_size + 1;
1231 size_t n;
1232
Steven Cooreman69967ce2021-01-18 18:01:08 +01001233 /* Skip the test case if the target running the test cannot
1234 * accomodate large keys due to heap size constraints */
1235 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001236 memset( buffer, 'K', byte_size );
1237
1238 PSA_ASSERT( psa_crypto_init( ) );
1239
1240 /* Try importing the key */
1241 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1242 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001243 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001244 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001245 TEST_EQUAL( status, expected_status );
1246
1247 if( status == PSA_SUCCESS )
1248 {
Ronald Cron5425a212020-08-04 14:58:35 +02001249 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001250 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1251 TEST_EQUAL( psa_get_key_bits( &attributes ),
1252 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001253 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001254 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001255 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001256 for( n = 0; n < byte_size; n++ )
1257 TEST_EQUAL( buffer[n], 'K' );
1258 for( n = byte_size; n < buffer_size; n++ )
1259 TEST_EQUAL( buffer[n], 0 );
1260 }
1261
1262exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001263 /*
1264 * Key attributes may have been returned by psa_get_key_attributes()
1265 * thus reset them as required.
1266 */
1267 psa_reset_key_attributes( &attributes );
1268
Ronald Cron5425a212020-08-04 14:58:35 +02001269 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001270 PSA_DONE( );
1271 mbedtls_free( buffer );
1272}
1273/* END_CASE */
1274
1275/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001276void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1277{
Ronald Cron5425a212020-08-04 14:58:35 +02001278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001279 size_t bits = bits_arg;
1280 psa_status_t expected_status = expected_status_arg;
1281 psa_status_t status;
1282 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001283 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001284 size_t buffer_size = /* Slight overapproximations */
1285 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001286 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001287 unsigned char *p;
1288 int ret;
1289 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001291
Gilles Peskine8817f612018-12-18 00:18:46 +01001292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001293 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001294
1295 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1296 bits, keypair ) ) >= 0 );
1297 length = ret;
1298
1299 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001300 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001301 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001302 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001303
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001304 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001305 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001306
1307exit:
1308 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001309 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001310}
1311/* END_CASE */
1312
1313/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001314void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001315 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001316 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317 int expected_bits,
1318 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001319 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001320 int canonical_input )
1321{
Ronald Cron5425a212020-08-04 14:58:35 +02001322 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001323 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001324 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001325 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001326 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327 unsigned char *exported = NULL;
1328 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001330 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001332 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001333 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334
Moran Pekercb088e72018-07-17 17:36:59 +03001335 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001336 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001338 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340
Gilles Peskine4747d192019-04-17 15:05:45 +02001341 psa_set_key_usage_flags( &attributes, usage_arg );
1342 psa_set_key_algorithm( &attributes, alg );
1343 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001345 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001346 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001347
1348 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001349 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001350 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1351 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001352 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353
1354 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001355 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001356 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001357
1358 /* The exported length must be set by psa_export_key() to a value between 0
1359 * and export_size. On errors, the exported length must be 0. */
1360 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1361 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1362 TEST_ASSERT( exported_length <= export_size );
1363
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001364 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001365 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001366 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001368 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001370 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001371
Gilles Peskineea38a922021-02-13 00:05:16 +01001372 /* Run sanity checks on the exported key. For non-canonical inputs,
1373 * this validates the canonical representations. For canonical inputs,
1374 * this doesn't directly validate the implementation, but it still helps
1375 * by cross-validating the test data with the sanity check code. */
1376 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001377 goto exit;
1378
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001380 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001381 else
1382 {
Ronald Cron5425a212020-08-04 14:58:35 +02001383 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001384 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001385 &key2 ) );
1386 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001387 reexported,
1388 export_size,
1389 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001390 ASSERT_COMPARE( exported, exported_length,
1391 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001392 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001393 }
gabor-mezei-armceface22021-01-21 12:26:17 +01001394 TEST_ASSERT( exported_length <=
1395 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
1396 psa_get_key_bits( &got_attributes ) ) );
1397 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398
1399destroy:
1400 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001401 PSA_ASSERT( psa_destroy_key( key ) );
1402 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403
1404exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001405 /*
1406 * Key attributes may have been returned by psa_get_key_attributes()
1407 * thus reset them as required.
1408 */
1409 psa_reset_key_attributes( &got_attributes );
1410
itayzafrir3e02b3b2018-06-12 17:06:52 +03001411 mbedtls_free( exported );
1412 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001413 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001414}
1415/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001416
Moran Pekerf709f4a2018-06-06 17:26:04 +03001417/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001418void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001419 int type_arg,
1420 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001421 int export_size_delta,
1422 int expected_export_status_arg,
1423 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001424{
Ronald Cron5425a212020-08-04 14:58:35 +02001425 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001426 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001427 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001428 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001429 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001430 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001431 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001432 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001434
Gilles Peskine8817f612018-12-18 00:18:46 +01001435 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001436
Gilles Peskine4747d192019-04-17 15:05:45 +02001437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1438 psa_set_key_algorithm( &attributes, alg );
1439 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001440
1441 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001442 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001443
Gilles Peskine49c25912018-10-29 15:15:31 +01001444 /* Export the public key */
1445 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001446 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001447 exported, export_size,
1448 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001449 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001450 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001451 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001452 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001453 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001454 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001455 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001456 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001457 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01001458 TEST_ASSERT( expected_public_key->len <=
1459 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1460 TEST_ASSERT( expected_public_key->len <=
1461 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001462 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1463 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001464 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001465
1466exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001467 /*
1468 * Key attributes may have been returned by psa_get_key_attributes()
1469 * thus reset them as required.
1470 */
1471 psa_reset_key_attributes( &attributes );
1472
itayzafrir3e02b3b2018-06-12 17:06:52 +03001473 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001474 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001475 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001476}
1477/* END_CASE */
1478
Gilles Peskine20035e32018-02-03 22:44:14 +01001479/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001480void import_and_exercise_key( data_t *data,
1481 int type_arg,
1482 int bits_arg,
1483 int alg_arg )
1484{
Ronald Cron5425a212020-08-04 14:58:35 +02001485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001486 psa_key_type_t type = type_arg;
1487 size_t bits = bits_arg;
1488 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001489 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001491 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001492
Gilles Peskine8817f612018-12-18 00:18:46 +01001493 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001494
Gilles Peskine4747d192019-04-17 15:05:45 +02001495 psa_set_key_usage_flags( &attributes, usage );
1496 psa_set_key_algorithm( &attributes, alg );
1497 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001498
1499 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001500 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001501
1502 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001503 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001504 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1505 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001506
1507 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001508 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001509 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001510
Ronald Cron5425a212020-08-04 14:58:35 +02001511 PSA_ASSERT( psa_destroy_key( key ) );
1512 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001513
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001514exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001515 /*
1516 * Key attributes may have been returned by psa_get_key_attributes()
1517 * thus reset them as required.
1518 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001519 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001520
1521 psa_reset_key_attributes( &attributes );
1522 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001523 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001528void effective_key_attributes( int type_arg, int expected_type_arg,
1529 int bits_arg, int expected_bits_arg,
1530 int usage_arg, int expected_usage_arg,
1531 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001532{
Ronald Cron5425a212020-08-04 14:58:35 +02001533 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001534 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001535 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001536 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001537 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001538 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001539 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001540 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001541 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001542 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001543
Gilles Peskine8817f612018-12-18 00:18:46 +01001544 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001545
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001546 psa_set_key_usage_flags( &attributes, usage );
1547 psa_set_key_algorithm( &attributes, alg );
1548 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001549 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001550
Ronald Cron5425a212020-08-04 14:58:35 +02001551 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001552 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001553
Ronald Cron5425a212020-08-04 14:58:35 +02001554 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001555 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1556 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1557 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1558 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001559
1560exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001561 /*
1562 * Key attributes may have been returned by psa_get_key_attributes()
1563 * thus reset them as required.
1564 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001565 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001566
1567 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001568 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001569}
1570/* END_CASE */
1571
1572/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001573void check_key_policy( int type_arg, int bits_arg,
1574 int usage_arg, int alg_arg )
1575{
1576 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1577 usage_arg, usage_arg, alg_arg, alg_arg );
1578 goto exit;
1579}
1580/* END_CASE */
1581
1582/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001583void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001584{
1585 /* Test each valid way of initializing the object, except for `= {0}`, as
1586 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1587 * though it's OK by the C standard. We could test for this, but we'd need
1588 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001589 psa_key_attributes_t func = psa_key_attributes_init( );
1590 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1591 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001592
1593 memset( &zero, 0, sizeof( zero ) );
1594
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001595 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1596 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1597 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001598
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001599 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1600 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1601 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1602
1603 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1604 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1605 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1606
1607 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1608 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1609 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1610
1611 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1612 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1613 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001614}
1615/* END_CASE */
1616
1617/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001618void mac_key_policy( int policy_usage,
1619 int policy_alg,
1620 int key_type,
1621 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001622 int exercise_alg,
1623 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001624{
Ronald Cron5425a212020-08-04 14:58:35 +02001625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001627 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001629 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001631
Gilles Peskine8817f612018-12-18 00:18:46 +01001632 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001633
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001634 psa_set_key_usage_flags( &attributes, policy_usage );
1635 psa_set_key_algorithm( &attributes, policy_alg );
1636 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001637
Gilles Peskine049c7532019-05-15 20:22:09 +02001638 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001639 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001640
Ronald Cron5425a212020-08-04 14:58:35 +02001641 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001642 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001643 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001644 else
1645 TEST_EQUAL( status, expected_status );
1646
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001648
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001649 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001650 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001651 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001652 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001653 else
1654 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655
1656exit:
1657 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001658 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001659 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660}
1661/* END_CASE */
1662
1663/* BEGIN_CASE */
1664void cipher_key_policy( int policy_usage,
1665 int policy_alg,
1666 int key_type,
1667 data_t *key_data,
1668 int exercise_alg )
1669{
Ronald Cron5425a212020-08-04 14:58:35 +02001670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001672 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001673 psa_status_t status;
1674
Gilles Peskine8817f612018-12-18 00:18:46 +01001675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001676
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001677 psa_set_key_usage_flags( &attributes, policy_usage );
1678 psa_set_key_algorithm( &attributes, policy_alg );
1679 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680
Gilles Peskine049c7532019-05-15 20:22:09 +02001681 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001682 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683
Ronald Cron5425a212020-08-04 14:58:35 +02001684 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685 if( policy_alg == exercise_alg &&
1686 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001688 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001689 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001690 psa_cipher_abort( &operation );
1691
Ronald Cron5425a212020-08-04 14:58:35 +02001692 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693 if( policy_alg == exercise_alg &&
1694 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001697 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698
1699exit:
1700 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001702 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703}
1704/* END_CASE */
1705
1706/* BEGIN_CASE */
1707void aead_key_policy( int policy_usage,
1708 int policy_alg,
1709 int key_type,
1710 data_t *key_data,
1711 int nonce_length_arg,
1712 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001713 int exercise_alg,
1714 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001715{
Ronald Cron5425a212020-08-04 14:58:35 +02001716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001718 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001719 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001720 unsigned char nonce[16] = {0};
1721 size_t nonce_length = nonce_length_arg;
1722 unsigned char tag[16];
1723 size_t tag_length = tag_length_arg;
1724 size_t output_length;
1725
1726 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1727 TEST_ASSERT( tag_length <= sizeof( tag ) );
1728
Gilles Peskine8817f612018-12-18 00:18:46 +01001729 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001730
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001731 psa_set_key_usage_flags( &attributes, policy_usage );
1732 psa_set_key_algorithm( &attributes, policy_alg );
1733 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734
Gilles Peskine049c7532019-05-15 20:22:09 +02001735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001736 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001737
Ronald Cron5425a212020-08-04 14:58:35 +02001738 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001739 nonce, nonce_length,
1740 NULL, 0,
1741 NULL, 0,
1742 tag, tag_length,
1743 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001744 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1745 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001746 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001747 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001748
1749 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001750 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001751 nonce, nonce_length,
1752 NULL, 0,
1753 tag, tag_length,
1754 NULL, 0,
1755 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001756 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1757 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1758 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001759 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001760 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001761 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001762
1763exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001764 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001765 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE */
1770void asymmetric_encryption_key_policy( int policy_usage,
1771 int policy_alg,
1772 int key_type,
1773 data_t *key_data,
1774 int exercise_alg )
1775{
Ronald Cron5425a212020-08-04 14:58:35 +02001776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001778 psa_status_t status;
1779 size_t key_bits;
1780 size_t buffer_length;
1781 unsigned char *buffer = NULL;
1782 size_t output_length;
1783
Gilles Peskine8817f612018-12-18 00:18:46 +01001784 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001785
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001786 psa_set_key_usage_flags( &attributes, policy_usage );
1787 psa_set_key_algorithm( &attributes, policy_alg );
1788 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001789
Gilles Peskine049c7532019-05-15 20:22:09 +02001790 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001791 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001792
Ronald Cron5425a212020-08-04 14:58:35 +02001793 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001794 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001795 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1796 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001797 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001798
Ronald Cron5425a212020-08-04 14:58:35 +02001799 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 NULL, 0,
1801 NULL, 0,
1802 buffer, buffer_length,
1803 &output_length );
1804 if( policy_alg == exercise_alg &&
1805 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001806 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001807 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001808 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001809
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001810 if( buffer_length != 0 )
1811 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001812 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001813 buffer, buffer_length,
1814 NULL, 0,
1815 buffer, buffer_length,
1816 &output_length );
1817 if( policy_alg == exercise_alg &&
1818 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001819 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001820 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001821 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001822
1823exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001824 /*
1825 * Key attributes may have been returned by psa_get_key_attributes()
1826 * thus reset them as required.
1827 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001828 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001829
1830 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001831 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001832 mbedtls_free( buffer );
1833}
1834/* END_CASE */
1835
1836/* BEGIN_CASE */
1837void asymmetric_signature_key_policy( int policy_usage,
1838 int policy_alg,
1839 int key_type,
1840 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001841 int exercise_alg,
1842 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001843{
Ronald Cron5425a212020-08-04 14:58:35 +02001844 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001845 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001846 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001847 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1848 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1849 * compatible with the policy and `payload_length_arg` is supposed to be
1850 * a valid input length to sign. If `payload_length_arg <= 0`,
1851 * `exercise_alg` is supposed to be forbidden by the policy. */
1852 int compatible_alg = payload_length_arg > 0;
1853 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001854 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001855 size_t signature_length;
1856
Gilles Peskine8817f612018-12-18 00:18:46 +01001857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001859 psa_set_key_usage_flags( &attributes, policy_usage );
1860 psa_set_key_algorithm( &attributes, policy_alg );
1861 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862
Gilles Peskine049c7532019-05-15 20:22:09 +02001863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001864 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001865
Ronald Cron5425a212020-08-04 14:58:35 +02001866 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001867 payload, payload_length,
1868 signature, sizeof( signature ),
1869 &signature_length );
1870 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001871 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001873 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001874
1875 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001876 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001877 payload, payload_length,
1878 signature, sizeof( signature ) );
1879 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001880 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001881 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001882 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001883
1884exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001885 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001886 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001887}
1888/* END_CASE */
1889
Janos Follathba3fab92019-06-11 14:50:16 +01001890/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001891void derive_key_policy( int policy_usage,
1892 int policy_alg,
1893 int key_type,
1894 data_t *key_data,
1895 int exercise_alg )
1896{
Ronald Cron5425a212020-08-04 14:58:35 +02001897 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001899 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001900 psa_status_t status;
1901
Gilles Peskine8817f612018-12-18 00:18:46 +01001902 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001903
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001904 psa_set_key_usage_flags( &attributes, policy_usage );
1905 psa_set_key_algorithm( &attributes, policy_alg );
1906 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001907
Gilles Peskine049c7532019-05-15 20:22:09 +02001908 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001909 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001910
Janos Follathba3fab92019-06-11 14:50:16 +01001911 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1912
1913 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1914 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001915 {
Janos Follathba3fab92019-06-11 14:50:16 +01001916 PSA_ASSERT( psa_key_derivation_input_bytes(
1917 &operation,
1918 PSA_KEY_DERIVATION_INPUT_SEED,
1919 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001920 }
Janos Follathba3fab92019-06-11 14:50:16 +01001921
1922 status = psa_key_derivation_input_key( &operation,
1923 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001924 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001925
Gilles Peskineea0fb492018-07-12 17:17:20 +02001926 if( policy_alg == exercise_alg &&
1927 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001928 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001929 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001930 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001931
1932exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001933 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001934 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001935 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001936}
1937/* END_CASE */
1938
1939/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001940void agreement_key_policy( int policy_usage,
1941 int policy_alg,
1942 int key_type_arg,
1943 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001944 int exercise_alg,
1945 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001946{
Ronald Cron5425a212020-08-04 14:58:35 +02001947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001949 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001950 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001951 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001952 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001953
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001955
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001956 psa_set_key_usage_flags( &attributes, policy_usage );
1957 psa_set_key_algorithm( &attributes, policy_alg );
1958 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001959
Gilles Peskine049c7532019-05-15 20:22:09 +02001960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001961 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001963 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001964 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001965
Steven Cooremance48e852020-10-05 16:02:45 +02001966 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001967
1968exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001969 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001970 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001971 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001972}
1973/* END_CASE */
1974
1975/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001976void key_policy_alg2( int key_type_arg, data_t *key_data,
1977 int usage_arg, int alg_arg, int alg2_arg )
1978{
Ronald Cron5425a212020-08-04 14:58:35 +02001979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001980 psa_key_type_t key_type = key_type_arg;
1981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1982 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1983 psa_key_usage_t usage = usage_arg;
1984 psa_algorithm_t alg = alg_arg;
1985 psa_algorithm_t alg2 = alg2_arg;
1986
1987 PSA_ASSERT( psa_crypto_init( ) );
1988
1989 psa_set_key_usage_flags( &attributes, usage );
1990 psa_set_key_algorithm( &attributes, alg );
1991 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1992 psa_set_key_type( &attributes, key_type );
1993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001994 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001995
Ronald Cron5425a212020-08-04 14:58:35 +02001996 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001997 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1998 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1999 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2000
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002001 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002002 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002004 goto exit;
2005
2006exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002007 /*
2008 * Key attributes may have been returned by psa_get_key_attributes()
2009 * thus reset them as required.
2010 */
2011 psa_reset_key_attributes( &got_attributes );
2012
Ronald Cron5425a212020-08-04 14:58:35 +02002013 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002014 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002015}
2016/* END_CASE */
2017
2018/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002019void raw_agreement_key_policy( int policy_usage,
2020 int policy_alg,
2021 int key_type_arg,
2022 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002023 int exercise_alg,
2024 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002025{
Ronald Cron5425a212020-08-04 14:58:35 +02002026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002028 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002029 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002030 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002031 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002032
2033 PSA_ASSERT( psa_crypto_init( ) );
2034
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002035 psa_set_key_usage_flags( &attributes, policy_usage );
2036 psa_set_key_algorithm( &attributes, policy_alg );
2037 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002038
Gilles Peskine049c7532019-05-15 20:22:09 +02002039 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002040 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002041
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002042 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002043
Steven Cooremance48e852020-10-05 16:02:45 +02002044 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002045
2046exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002047 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002048 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002049 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002050}
2051/* END_CASE */
2052
2053/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002054void copy_success( int source_usage_arg,
2055 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002056 int type_arg, data_t *material,
2057 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002058 int target_usage_arg,
2059 int target_alg_arg, int target_alg2_arg,
2060 int expected_usage_arg,
2061 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002062{
Gilles Peskineca25db92019-04-19 11:43:08 +02002063 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2064 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002065 psa_key_usage_t expected_usage = expected_usage_arg;
2066 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002067 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002068 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2069 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002070 uint8_t *export_buffer = NULL;
2071
Gilles Peskine57ab7212019-01-28 13:03:09 +01002072 PSA_ASSERT( psa_crypto_init( ) );
2073
Gilles Peskineca25db92019-04-19 11:43:08 +02002074 /* Prepare the source key. */
2075 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2076 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002077 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002078 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002079 PSA_ASSERT( psa_import_key( &source_attributes,
2080 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002081 &source_key ) );
2082 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002083
Gilles Peskineca25db92019-04-19 11:43:08 +02002084 /* Prepare the target attributes. */
2085 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002086 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002087 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002088 /* Set volatile lifetime to reset the key identifier to 0. */
2089 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2090 }
2091
Gilles Peskineca25db92019-04-19 11:43:08 +02002092 if( target_usage_arg != -1 )
2093 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2094 if( target_alg_arg != -1 )
2095 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002096 if( target_alg2_arg != -1 )
2097 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002098
2099 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002100 PSA_ASSERT( psa_copy_key( source_key,
2101 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002102
2103 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002104 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002105
2106 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002107 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002108 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2109 psa_get_key_type( &target_attributes ) );
2110 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2111 psa_get_key_bits( &target_attributes ) );
2112 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2113 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002114 TEST_EQUAL( expected_alg2,
2115 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002116 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2117 {
2118 size_t length;
2119 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002120 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002121 material->len, &length ) );
2122 ASSERT_COMPARE( material->x, material->len,
2123 export_buffer, length );
2124 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002125
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002126 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002127 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002128 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002129 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002130
Ronald Cron5425a212020-08-04 14:58:35 +02002131 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002132
2133exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002134 /*
2135 * Source and target key attributes may have been returned by
2136 * psa_get_key_attributes() thus reset them as required.
2137 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002138 psa_reset_key_attributes( &source_attributes );
2139 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002140
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002141 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002142 mbedtls_free( export_buffer );
2143}
2144/* END_CASE */
2145
2146/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002147void copy_fail( int source_usage_arg,
2148 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002149 int type_arg, data_t *material,
2150 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002151 int target_usage_arg,
2152 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002153 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002154 int expected_status_arg )
2155{
2156 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2157 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002158 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2159 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002160 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002161
2162 PSA_ASSERT( psa_crypto_init( ) );
2163
2164 /* Prepare the source key. */
2165 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2166 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002167 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002168 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002169 PSA_ASSERT( psa_import_key( &source_attributes,
2170 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002171 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002172
2173 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002174 psa_set_key_id( &target_attributes, key_id );
2175 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002176 psa_set_key_type( &target_attributes, target_type_arg );
2177 psa_set_key_bits( &target_attributes, target_bits_arg );
2178 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2179 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002180 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002181
2182 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002183 TEST_EQUAL( psa_copy_key( source_key,
2184 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002185 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002186
Ronald Cron5425a212020-08-04 14:58:35 +02002187 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002188
Gilles Peskine4a644642019-05-03 17:14:08 +02002189exit:
2190 psa_reset_key_attributes( &source_attributes );
2191 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002192 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002193}
2194/* END_CASE */
2195
2196/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002197void hash_operation_init( )
2198{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002199 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002200 /* Test each valid way of initializing the object, except for `= {0}`, as
2201 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2202 * though it's OK by the C standard. We could test for this, but we'd need
2203 * to supress the Clang warning for the test. */
2204 psa_hash_operation_t func = psa_hash_operation_init( );
2205 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2206 psa_hash_operation_t zero;
2207
2208 memset( &zero, 0, sizeof( zero ) );
2209
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002210 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002211 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2212 PSA_ERROR_BAD_STATE );
2213 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2214 PSA_ERROR_BAD_STATE );
2215 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2216 PSA_ERROR_BAD_STATE );
2217
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002218 /* A default hash operation should be abortable without error. */
2219 PSA_ASSERT( psa_hash_abort( &func ) );
2220 PSA_ASSERT( psa_hash_abort( &init ) );
2221 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002222}
2223/* END_CASE */
2224
2225/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002226void hash_setup( int alg_arg,
2227 int expected_status_arg )
2228{
2229 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002230 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002231 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002232 psa_status_t status;
2233
Gilles Peskine8817f612018-12-18 00:18:46 +01002234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002235
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002236 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002237 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002238
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002239 /* Whether setup succeeded or failed, abort must succeed. */
2240 PSA_ASSERT( psa_hash_abort( &operation ) );
2241
2242 /* If setup failed, reproduce the failure, so as to
2243 * test the resulting state of the operation object. */
2244 if( status != PSA_SUCCESS )
2245 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2246
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002247 /* Now the operation object should be reusable. */
2248#if defined(KNOWN_SUPPORTED_HASH_ALG)
2249 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2250 PSA_ASSERT( psa_hash_abort( &operation ) );
2251#endif
2252
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002253exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002254 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002255}
2256/* END_CASE */
2257
2258/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002259void hash_compute_fail( int alg_arg, data_t *input,
2260 int output_size_arg, int expected_status_arg )
2261{
2262 psa_algorithm_t alg = alg_arg;
2263 uint8_t *output = NULL;
2264 size_t output_size = output_size_arg;
2265 size_t output_length = INVALID_EXPORT_LENGTH;
2266 psa_status_t expected_status = expected_status_arg;
2267 psa_status_t status;
2268
2269 ASSERT_ALLOC( output, output_size );
2270
2271 PSA_ASSERT( psa_crypto_init( ) );
2272
2273 status = psa_hash_compute( alg, input->x, input->len,
2274 output, output_size, &output_length );
2275 TEST_EQUAL( status, expected_status );
2276 TEST_ASSERT( output_length <= output_size );
2277
2278exit:
2279 mbedtls_free( output );
2280 PSA_DONE( );
2281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002285void hash_compare_fail( int alg_arg, data_t *input,
2286 data_t *reference_hash,
2287 int expected_status_arg )
2288{
2289 psa_algorithm_t alg = alg_arg;
2290 psa_status_t expected_status = expected_status_arg;
2291 psa_status_t status;
2292
2293 PSA_ASSERT( psa_crypto_init( ) );
2294
2295 status = psa_hash_compare( alg, input->x, input->len,
2296 reference_hash->x, reference_hash->len );
2297 TEST_EQUAL( status, expected_status );
2298
2299exit:
2300 PSA_DONE( );
2301}
2302/* END_CASE */
2303
2304/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002305void hash_compute_compare( int alg_arg, data_t *input,
2306 data_t *expected_output )
2307{
2308 psa_algorithm_t alg = alg_arg;
2309 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2310 size_t output_length = INVALID_EXPORT_LENGTH;
2311 size_t i;
2312
2313 PSA_ASSERT( psa_crypto_init( ) );
2314
2315 /* Compute with tight buffer */
2316 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002317 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002318 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002319 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002320 ASSERT_COMPARE( output, output_length,
2321 expected_output->x, expected_output->len );
2322
2323 /* Compute with larger buffer */
2324 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2325 output, sizeof( output ),
2326 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002327 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002328 ASSERT_COMPARE( output, output_length,
2329 expected_output->x, expected_output->len );
2330
2331 /* Compare with correct hash */
2332 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2333 output, output_length ) );
2334
2335 /* Compare with trailing garbage */
2336 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2337 output, output_length + 1 ),
2338 PSA_ERROR_INVALID_SIGNATURE );
2339
2340 /* Compare with truncated hash */
2341 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2342 output, output_length - 1 ),
2343 PSA_ERROR_INVALID_SIGNATURE );
2344
2345 /* Compare with corrupted value */
2346 for( i = 0; i < output_length; i++ )
2347 {
Chris Jones9634bb12021-01-20 15:56:42 +00002348 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002349 output[i] ^= 1;
2350 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2351 output, output_length ),
2352 PSA_ERROR_INVALID_SIGNATURE );
2353 output[i] ^= 1;
2354 }
2355
2356exit:
2357 PSA_DONE( );
2358}
2359/* END_CASE */
2360
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002361/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002362void hash_bad_order( )
2363{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002364 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002365 unsigned char input[] = "";
2366 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002367 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002368 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2369 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2370 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002371 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002372 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002373 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002374
Gilles Peskine8817f612018-12-18 00:18:46 +01002375 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002376
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002377 /* Call setup twice in a row. */
2378 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2379 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2380 PSA_ERROR_BAD_STATE );
2381 PSA_ASSERT( psa_hash_abort( &operation ) );
2382
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002383 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002384 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002385 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002386 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002387
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002388 /* Call update after finish. */
2389 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2390 PSA_ASSERT( psa_hash_finish( &operation,
2391 hash, sizeof( hash ), &hash_len ) );
2392 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002393 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002394 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002395
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002396 /* Call verify without calling setup beforehand. */
2397 TEST_EQUAL( psa_hash_verify( &operation,
2398 valid_hash, sizeof( valid_hash ) ),
2399 PSA_ERROR_BAD_STATE );
2400 PSA_ASSERT( psa_hash_abort( &operation ) );
2401
2402 /* Call verify after finish. */
2403 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2404 PSA_ASSERT( psa_hash_finish( &operation,
2405 hash, sizeof( hash ), &hash_len ) );
2406 TEST_EQUAL( psa_hash_verify( &operation,
2407 valid_hash, sizeof( valid_hash ) ),
2408 PSA_ERROR_BAD_STATE );
2409 PSA_ASSERT( psa_hash_abort( &operation ) );
2410
2411 /* Call verify twice in a row. */
2412 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2413 PSA_ASSERT( psa_hash_verify( &operation,
2414 valid_hash, sizeof( valid_hash ) ) );
2415 TEST_EQUAL( psa_hash_verify( &operation,
2416 valid_hash, sizeof( valid_hash ) ),
2417 PSA_ERROR_BAD_STATE );
2418 PSA_ASSERT( psa_hash_abort( &operation ) );
2419
2420 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002421 TEST_EQUAL( psa_hash_finish( &operation,
2422 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002423 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002424 PSA_ASSERT( psa_hash_abort( &operation ) );
2425
2426 /* Call finish twice in a row. */
2427 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2428 PSA_ASSERT( psa_hash_finish( &operation,
2429 hash, sizeof( hash ), &hash_len ) );
2430 TEST_EQUAL( psa_hash_finish( &operation,
2431 hash, sizeof( hash ), &hash_len ),
2432 PSA_ERROR_BAD_STATE );
2433 PSA_ASSERT( psa_hash_abort( &operation ) );
2434
2435 /* Call finish after calling verify. */
2436 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2437 PSA_ASSERT( psa_hash_verify( &operation,
2438 valid_hash, sizeof( valid_hash ) ) );
2439 TEST_EQUAL( psa_hash_finish( &operation,
2440 hash, sizeof( hash ), &hash_len ),
2441 PSA_ERROR_BAD_STATE );
2442 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002443
2444exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002445 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002446}
2447/* END_CASE */
2448
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002449/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002450void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002451{
2452 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002453 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2454 * appended to it */
2455 unsigned char hash[] = {
2456 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2457 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2458 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002459 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002460 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002461
Gilles Peskine8817f612018-12-18 00:18:46 +01002462 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002463
itayzafrir27e69452018-11-01 14:26:34 +02002464 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002465 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002466 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002467 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002468
itayzafrir27e69452018-11-01 14:26:34 +02002469 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002470 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002471 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002472 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002473
itayzafrir27e69452018-11-01 14:26:34 +02002474 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002475 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002476 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002477 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002478
itayzafrirec93d302018-10-18 18:01:10 +03002479exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002480 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002481}
2482/* END_CASE */
2483
Ronald Cronee414c72021-03-18 18:50:08 +01002484/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002485void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002486{
2487 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002488 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002489 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002490 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002491 size_t hash_len;
2492
Gilles Peskine8817f612018-12-18 00:18:46 +01002493 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002494
itayzafrir58028322018-10-25 10:22:01 +03002495 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002496 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002497 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002498 hash, expected_size - 1, &hash_len ),
2499 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002500
2501exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002502 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002503}
2504/* END_CASE */
2505
Ronald Cronee414c72021-03-18 18:50:08 +01002506/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002507void hash_clone_source_state( )
2508{
2509 psa_algorithm_t alg = PSA_ALG_SHA_256;
2510 unsigned char hash[PSA_HASH_MAX_SIZE];
2511 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2512 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2513 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2514 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2515 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2516 size_t hash_len;
2517
2518 PSA_ASSERT( psa_crypto_init( ) );
2519 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2520
2521 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2522 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2523 PSA_ASSERT( psa_hash_finish( &op_finished,
2524 hash, sizeof( hash ), &hash_len ) );
2525 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2526 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2527
2528 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2529 PSA_ERROR_BAD_STATE );
2530
2531 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2532 PSA_ASSERT( psa_hash_finish( &op_init,
2533 hash, sizeof( hash ), &hash_len ) );
2534 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2535 PSA_ASSERT( psa_hash_finish( &op_finished,
2536 hash, sizeof( hash ), &hash_len ) );
2537 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2538 PSA_ASSERT( psa_hash_finish( &op_aborted,
2539 hash, sizeof( hash ), &hash_len ) );
2540
2541exit:
2542 psa_hash_abort( &op_source );
2543 psa_hash_abort( &op_init );
2544 psa_hash_abort( &op_setup );
2545 psa_hash_abort( &op_finished );
2546 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002547 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002548}
2549/* END_CASE */
2550
Ronald Cronee414c72021-03-18 18:50:08 +01002551/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002552void hash_clone_target_state( )
2553{
2554 psa_algorithm_t alg = PSA_ALG_SHA_256;
2555 unsigned char hash[PSA_HASH_MAX_SIZE];
2556 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2557 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2558 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2559 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2560 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2561 size_t hash_len;
2562
2563 PSA_ASSERT( psa_crypto_init( ) );
2564
2565 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2566 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2567 PSA_ASSERT( psa_hash_finish( &op_finished,
2568 hash, sizeof( hash ), &hash_len ) );
2569 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2570 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2571
2572 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2573 PSA_ASSERT( psa_hash_finish( &op_target,
2574 hash, sizeof( hash ), &hash_len ) );
2575
2576 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2577 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2578 PSA_ERROR_BAD_STATE );
2579 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2580 PSA_ERROR_BAD_STATE );
2581
2582exit:
2583 psa_hash_abort( &op_target );
2584 psa_hash_abort( &op_init );
2585 psa_hash_abort( &op_setup );
2586 psa_hash_abort( &op_finished );
2587 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002588 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002589}
2590/* END_CASE */
2591
itayzafrir58028322018-10-25 10:22:01 +03002592/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002593void mac_operation_init( )
2594{
Jaeden Amero252ef282019-02-15 14:05:35 +00002595 const uint8_t input[1] = { 0 };
2596
Jaeden Amero769ce272019-01-04 11:48:03 +00002597 /* Test each valid way of initializing the object, except for `= {0}`, as
2598 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2599 * though it's OK by the C standard. We could test for this, but we'd need
2600 * to supress the Clang warning for the test. */
2601 psa_mac_operation_t func = psa_mac_operation_init( );
2602 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2603 psa_mac_operation_t zero;
2604
2605 memset( &zero, 0, sizeof( zero ) );
2606
Jaeden Amero252ef282019-02-15 14:05:35 +00002607 /* A freshly-initialized MAC operation should not be usable. */
2608 TEST_EQUAL( psa_mac_update( &func,
2609 input, sizeof( input ) ),
2610 PSA_ERROR_BAD_STATE );
2611 TEST_EQUAL( psa_mac_update( &init,
2612 input, sizeof( input ) ),
2613 PSA_ERROR_BAD_STATE );
2614 TEST_EQUAL( psa_mac_update( &zero,
2615 input, sizeof( input ) ),
2616 PSA_ERROR_BAD_STATE );
2617
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002618 /* A default MAC operation should be abortable without error. */
2619 PSA_ASSERT( psa_mac_abort( &func ) );
2620 PSA_ASSERT( psa_mac_abort( &init ) );
2621 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002622}
2623/* END_CASE */
2624
2625/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002626void mac_setup( int key_type_arg,
2627 data_t *key,
2628 int alg_arg,
2629 int expected_status_arg )
2630{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631 psa_key_type_t key_type = key_type_arg;
2632 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002633 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002634 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002635 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2636#if defined(KNOWN_SUPPORTED_MAC_ALG)
2637 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2638#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002639
Gilles Peskine8817f612018-12-18 00:18:46 +01002640 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002642 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2643 &operation, &status ) )
2644 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002645 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002646
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002647 /* The operation object should be reusable. */
2648#if defined(KNOWN_SUPPORTED_MAC_ALG)
2649 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2650 smoke_test_key_data,
2651 sizeof( smoke_test_key_data ),
2652 KNOWN_SUPPORTED_MAC_ALG,
2653 &operation, &status ) )
2654 goto exit;
2655 TEST_EQUAL( status, PSA_SUCCESS );
2656#endif
2657
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002658exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002659 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002660}
2661/* END_CASE */
2662
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002663/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002664void mac_bad_order( )
2665{
Ronald Cron5425a212020-08-04 14:58:35 +02002666 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002667 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2668 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002669 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002670 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2671 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2672 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002674 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2675 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2676 size_t sign_mac_length = 0;
2677 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2678 const uint8_t verify_mac[] = {
2679 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2680 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2681 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2682
2683 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002684 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002685 psa_set_key_algorithm( &attributes, alg );
2686 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002687
Ronald Cron5425a212020-08-04 14:58:35 +02002688 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2689 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002690
Jaeden Amero252ef282019-02-15 14:05:35 +00002691 /* Call update without calling setup beforehand. */
2692 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2693 PSA_ERROR_BAD_STATE );
2694 PSA_ASSERT( psa_mac_abort( &operation ) );
2695
2696 /* Call sign finish without calling setup beforehand. */
2697 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2698 &sign_mac_length),
2699 PSA_ERROR_BAD_STATE );
2700 PSA_ASSERT( psa_mac_abort( &operation ) );
2701
2702 /* Call verify finish without calling setup beforehand. */
2703 TEST_EQUAL( psa_mac_verify_finish( &operation,
2704 verify_mac, sizeof( verify_mac ) ),
2705 PSA_ERROR_BAD_STATE );
2706 PSA_ASSERT( psa_mac_abort( &operation ) );
2707
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002708 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002709 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2710 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002711 PSA_ERROR_BAD_STATE );
2712 PSA_ASSERT( psa_mac_abort( &operation ) );
2713
Jaeden Amero252ef282019-02-15 14:05:35 +00002714 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002715 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002716 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2717 PSA_ASSERT( psa_mac_sign_finish( &operation,
2718 sign_mac, sizeof( sign_mac ),
2719 &sign_mac_length ) );
2720 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2721 PSA_ERROR_BAD_STATE );
2722 PSA_ASSERT( psa_mac_abort( &operation ) );
2723
2724 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002725 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002726 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2727 PSA_ASSERT( psa_mac_verify_finish( &operation,
2728 verify_mac, sizeof( verify_mac ) ) );
2729 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2730 PSA_ERROR_BAD_STATE );
2731 PSA_ASSERT( psa_mac_abort( &operation ) );
2732
2733 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002734 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002735 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2736 PSA_ASSERT( psa_mac_sign_finish( &operation,
2737 sign_mac, sizeof( sign_mac ),
2738 &sign_mac_length ) );
2739 TEST_EQUAL( psa_mac_sign_finish( &operation,
2740 sign_mac, sizeof( sign_mac ),
2741 &sign_mac_length ),
2742 PSA_ERROR_BAD_STATE );
2743 PSA_ASSERT( psa_mac_abort( &operation ) );
2744
2745 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002746 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002747 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2748 PSA_ASSERT( psa_mac_verify_finish( &operation,
2749 verify_mac, sizeof( verify_mac ) ) );
2750 TEST_EQUAL( psa_mac_verify_finish( &operation,
2751 verify_mac, sizeof( verify_mac ) ),
2752 PSA_ERROR_BAD_STATE );
2753 PSA_ASSERT( psa_mac_abort( &operation ) );
2754
2755 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002756 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002757 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2758 TEST_EQUAL( psa_mac_verify_finish( &operation,
2759 verify_mac, sizeof( verify_mac ) ),
2760 PSA_ERROR_BAD_STATE );
2761 PSA_ASSERT( psa_mac_abort( &operation ) );
2762
2763 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002764 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002765 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2766 TEST_EQUAL( psa_mac_sign_finish( &operation,
2767 sign_mac, sizeof( sign_mac ),
2768 &sign_mac_length ),
2769 PSA_ERROR_BAD_STATE );
2770 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002771
Ronald Cron5425a212020-08-04 14:58:35 +02002772 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002773
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002774exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002775 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002776}
2777/* END_CASE */
2778
2779/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002780void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002781 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002782 int alg_arg,
2783 data_t *input,
2784 data_t *expected_mac )
2785{
Ronald Cron5425a212020-08-04 14:58:35 +02002786 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002787 psa_key_type_t key_type = key_type_arg;
2788 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002789 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002791 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002792 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002793 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002794 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002795 const size_t output_sizes_to_test[] = {
2796 0,
2797 1,
2798 expected_mac->len - 1,
2799 expected_mac->len,
2800 expected_mac->len + 1,
2801 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002802
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002803 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002804 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002805 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002806
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002808
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002810 psa_set_key_algorithm( &attributes, alg );
2811 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002812
Ronald Cron5425a212020-08-04 14:58:35 +02002813 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2814 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002815
Gilles Peskine8b356b52020-08-25 23:44:59 +02002816 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2817 {
2818 const size_t output_size = output_sizes_to_test[i];
2819 psa_status_t expected_status =
2820 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2821 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002822
Chris Jones9634bb12021-01-20 15:56:42 +00002823 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002824 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002825
Gilles Peskine8b356b52020-08-25 23:44:59 +02002826 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002827 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002828 PSA_ASSERT( psa_mac_update( &operation,
2829 input->x, input->len ) );
2830 TEST_EQUAL( psa_mac_sign_finish( &operation,
2831 actual_mac, output_size,
2832 &mac_length ),
2833 expected_status );
2834 PSA_ASSERT( psa_mac_abort( &operation ) );
2835
2836 if( expected_status == PSA_SUCCESS )
2837 {
2838 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2839 actual_mac, mac_length );
2840 }
2841 mbedtls_free( actual_mac );
2842 actual_mac = NULL;
2843 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002844
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002845exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002846 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002847 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002848 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002849 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002850}
2851/* END_CASE */
2852
2853/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002854void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002855 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002856 int alg_arg,
2857 data_t *input,
2858 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859{
Ronald Cron5425a212020-08-04 14:58:35 +02002860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861 psa_key_type_t key_type = key_type_arg;
2862 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002863 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002865 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002866
Gilles Peskine69c12672018-06-28 00:07:19 +02002867 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2868
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002872 psa_set_key_algorithm( &attributes, alg );
2873 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002874
Ronald Cron5425a212020-08-04 14:58:35 +02002875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2876 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002877
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002878 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002879 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002880 PSA_ASSERT( psa_mac_update( &operation,
2881 input->x, input->len ) );
2882 PSA_ASSERT( psa_mac_verify_finish( &operation,
2883 expected_mac->x,
2884 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002886 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002887 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002888 PSA_ASSERT( psa_mac_update( &operation,
2889 input->x, input->len ) );
2890 TEST_EQUAL( psa_mac_verify_finish( &operation,
2891 expected_mac->x,
2892 expected_mac->len - 1 ),
2893 PSA_ERROR_INVALID_SIGNATURE );
2894
2895 /* Test a MAC that's too long. */
2896 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2897 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002898 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002899 PSA_ASSERT( psa_mac_update( &operation,
2900 input->x, input->len ) );
2901 TEST_EQUAL( psa_mac_verify_finish( &operation,
2902 perturbed_mac,
2903 expected_mac->len + 1 ),
2904 PSA_ERROR_INVALID_SIGNATURE );
2905
2906 /* Test changing one byte. */
2907 for( size_t i = 0; i < expected_mac->len; i++ )
2908 {
Chris Jones9634bb12021-01-20 15:56:42 +00002909 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002910 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002911 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002912 PSA_ASSERT( psa_mac_update( &operation,
2913 input->x, input->len ) );
2914 TEST_EQUAL( psa_mac_verify_finish( &operation,
2915 perturbed_mac,
2916 expected_mac->len ),
2917 PSA_ERROR_INVALID_SIGNATURE );
2918 perturbed_mac[i] ^= 1;
2919 }
2920
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002922 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002923 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002924 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002925 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002926}
2927/* END_CASE */
2928
2929/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002930void cipher_operation_init( )
2931{
Jaeden Ameroab439972019-02-15 14:12:05 +00002932 const uint8_t input[1] = { 0 };
2933 unsigned char output[1] = { 0 };
2934 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002935 /* Test each valid way of initializing the object, except for `= {0}`, as
2936 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2937 * though it's OK by the C standard. We could test for this, but we'd need
2938 * to supress the Clang warning for the test. */
2939 psa_cipher_operation_t func = psa_cipher_operation_init( );
2940 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2941 psa_cipher_operation_t zero;
2942
2943 memset( &zero, 0, sizeof( zero ) );
2944
Jaeden Ameroab439972019-02-15 14:12:05 +00002945 /* A freshly-initialized cipher operation should not be usable. */
2946 TEST_EQUAL( psa_cipher_update( &func,
2947 input, sizeof( input ),
2948 output, sizeof( output ),
2949 &output_length ),
2950 PSA_ERROR_BAD_STATE );
2951 TEST_EQUAL( psa_cipher_update( &init,
2952 input, sizeof( input ),
2953 output, sizeof( output ),
2954 &output_length ),
2955 PSA_ERROR_BAD_STATE );
2956 TEST_EQUAL( psa_cipher_update( &zero,
2957 input, sizeof( input ),
2958 output, sizeof( output ),
2959 &output_length ),
2960 PSA_ERROR_BAD_STATE );
2961
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002962 /* A default cipher operation should be abortable without error. */
2963 PSA_ASSERT( psa_cipher_abort( &func ) );
2964 PSA_ASSERT( psa_cipher_abort( &init ) );
2965 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002966}
2967/* END_CASE */
2968
2969/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002970void cipher_setup( int key_type_arg,
2971 data_t *key,
2972 int alg_arg,
2973 int expected_status_arg )
2974{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002975 psa_key_type_t key_type = key_type_arg;
2976 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002977 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002978 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002979 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002980#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002981 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2982#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002983
Gilles Peskine8817f612018-12-18 00:18:46 +01002984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002985
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002986 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2987 &operation, &status ) )
2988 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002989 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002990
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002991 /* The operation object should be reusable. */
2992#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2993 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2994 smoke_test_key_data,
2995 sizeof( smoke_test_key_data ),
2996 KNOWN_SUPPORTED_CIPHER_ALG,
2997 &operation, &status ) )
2998 goto exit;
2999 TEST_EQUAL( status, PSA_SUCCESS );
3000#endif
3001
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003002exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003003 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003004 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003005}
3006/* END_CASE */
3007
Ronald Cronee414c72021-03-18 18:50:08 +01003008/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003009void cipher_bad_order( )
3010{
Ronald Cron5425a212020-08-04 14:58:35 +02003011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003012 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3013 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003015 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003016 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003017 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003018 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3019 0xaa, 0xaa, 0xaa, 0xaa };
3020 const uint8_t text[] = {
3021 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3022 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003023 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003024 size_t length = 0;
3025
3026 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003027 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3028 psa_set_key_algorithm( &attributes, alg );
3029 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003030 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3031 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003032
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003033 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003034 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3035 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003036 PSA_ERROR_BAD_STATE );
3037 PSA_ASSERT( psa_cipher_abort( &operation ) );
3038
3039 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003040 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3041 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003042 PSA_ERROR_BAD_STATE );
3043 PSA_ASSERT( psa_cipher_abort( &operation ) );
3044
Jaeden Ameroab439972019-02-15 14:12:05 +00003045 /* Generate an IV without calling setup beforehand. */
3046 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3047 buffer, sizeof( buffer ),
3048 &length ),
3049 PSA_ERROR_BAD_STATE );
3050 PSA_ASSERT( psa_cipher_abort( &operation ) );
3051
3052 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003053 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003054 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3055 buffer, sizeof( buffer ),
3056 &length ) );
3057 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3058 buffer, sizeof( buffer ),
3059 &length ),
3060 PSA_ERROR_BAD_STATE );
3061 PSA_ASSERT( psa_cipher_abort( &operation ) );
3062
3063 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003064 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003065 PSA_ASSERT( psa_cipher_set_iv( &operation,
3066 iv, sizeof( iv ) ) );
3067 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3068 buffer, sizeof( buffer ),
3069 &length ),
3070 PSA_ERROR_BAD_STATE );
3071 PSA_ASSERT( psa_cipher_abort( &operation ) );
3072
3073 /* Set an IV without calling setup beforehand. */
3074 TEST_EQUAL( psa_cipher_set_iv( &operation,
3075 iv, sizeof( iv ) ),
3076 PSA_ERROR_BAD_STATE );
3077 PSA_ASSERT( psa_cipher_abort( &operation ) );
3078
3079 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003080 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003081 PSA_ASSERT( psa_cipher_set_iv( &operation,
3082 iv, sizeof( iv ) ) );
3083 TEST_EQUAL( psa_cipher_set_iv( &operation,
3084 iv, sizeof( iv ) ),
3085 PSA_ERROR_BAD_STATE );
3086 PSA_ASSERT( psa_cipher_abort( &operation ) );
3087
3088 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003089 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003090 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3091 buffer, sizeof( buffer ),
3092 &length ) );
3093 TEST_EQUAL( psa_cipher_set_iv( &operation,
3094 iv, sizeof( iv ) ),
3095 PSA_ERROR_BAD_STATE );
3096 PSA_ASSERT( psa_cipher_abort( &operation ) );
3097
3098 /* Call update without calling setup beforehand. */
3099 TEST_EQUAL( psa_cipher_update( &operation,
3100 text, sizeof( text ),
3101 buffer, sizeof( buffer ),
3102 &length ),
3103 PSA_ERROR_BAD_STATE );
3104 PSA_ASSERT( psa_cipher_abort( &operation ) );
3105
3106 /* Call update without an IV where an IV is required. */
3107 TEST_EQUAL( psa_cipher_update( &operation,
3108 text, sizeof( text ),
3109 buffer, sizeof( buffer ),
3110 &length ),
3111 PSA_ERROR_BAD_STATE );
3112 PSA_ASSERT( psa_cipher_abort( &operation ) );
3113
3114 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003115 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003116 PSA_ASSERT( psa_cipher_set_iv( &operation,
3117 iv, sizeof( iv ) ) );
3118 PSA_ASSERT( psa_cipher_finish( &operation,
3119 buffer, sizeof( buffer ), &length ) );
3120 TEST_EQUAL( psa_cipher_update( &operation,
3121 text, sizeof( text ),
3122 buffer, sizeof( buffer ),
3123 &length ),
3124 PSA_ERROR_BAD_STATE );
3125 PSA_ASSERT( psa_cipher_abort( &operation ) );
3126
3127 /* Call finish without calling setup beforehand. */
3128 TEST_EQUAL( psa_cipher_finish( &operation,
3129 buffer, sizeof( buffer ), &length ),
3130 PSA_ERROR_BAD_STATE );
3131 PSA_ASSERT( psa_cipher_abort( &operation ) );
3132
3133 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003134 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003135 /* Not calling update means we are encrypting an empty buffer, which is OK
3136 * for cipher modes with padding. */
3137 TEST_EQUAL( psa_cipher_finish( &operation,
3138 buffer, sizeof( buffer ), &length ),
3139 PSA_ERROR_BAD_STATE );
3140 PSA_ASSERT( psa_cipher_abort( &operation ) );
3141
3142 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003143 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003144 PSA_ASSERT( psa_cipher_set_iv( &operation,
3145 iv, sizeof( iv ) ) );
3146 PSA_ASSERT( psa_cipher_finish( &operation,
3147 buffer, sizeof( buffer ), &length ) );
3148 TEST_EQUAL( psa_cipher_finish( &operation,
3149 buffer, sizeof( buffer ), &length ),
3150 PSA_ERROR_BAD_STATE );
3151 PSA_ASSERT( psa_cipher_abort( &operation ) );
3152
Ronald Cron5425a212020-08-04 14:58:35 +02003153 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003154
Jaeden Ameroab439972019-02-15 14:12:05 +00003155exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003156 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003157 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003158}
3159/* END_CASE */
3160
3161/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003162void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003163 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003164 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003165 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003166{
Ronald Cron5425a212020-08-04 14:58:35 +02003167 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003168 psa_status_t status;
3169 psa_key_type_t key_type = key_type_arg;
3170 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003171 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003172 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003173 size_t output_buffer_size = 0;
3174 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003175 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003176 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003178
Gilles Peskine8817f612018-12-18 00:18:46 +01003179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003180
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3182 psa_set_key_algorithm( &attributes, alg );
3183 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003184
Ronald Cron5425a212020-08-04 14:58:35 +02003185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3186 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003187
Ronald Cron5425a212020-08-04 14:58:35 +02003188 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003189
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003190 if( iv->len > 0 )
3191 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003192 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003193 }
3194
gabor-mezei-armceface22021-01-21 12:26:17 +01003195 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3196 TEST_ASSERT( output_buffer_size <=
3197 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003198 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003199
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_cipher_update( &operation,
3201 input->x, input->len,
3202 output, output_buffer_size,
3203 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003204 TEST_ASSERT( function_output_length <=
3205 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003208 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003209
Gilles Peskine50e586b2018-06-08 14:28:46 +02003210 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003211 ( output_buffer_size == 0 ? NULL :
3212 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003213 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003214 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003215 TEST_ASSERT( function_output_length <=
3216 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3217 TEST_ASSERT( function_output_length <=
3218 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003219 total_output_length += function_output_length;
3220
Gilles Peskinefe11b722018-12-18 00:24:04 +01003221 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003222 if( expected_status == PSA_SUCCESS )
3223 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003224 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003225 ASSERT_COMPARE( expected_output->x, expected_output->len,
3226 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003227 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003228
Gilles Peskine50e586b2018-06-08 14:28:46 +02003229exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003230 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003231 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003232 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003233 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003234}
3235/* END_CASE */
3236
3237/* BEGIN_CASE */
3238void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003239 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003240 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003241 int first_part_size_arg,
3242 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003243 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003244{
Ronald Cron5425a212020-08-04 14:58:35 +02003245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003246 psa_key_type_t key_type = key_type_arg;
3247 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003248 size_t first_part_size = first_part_size_arg;
3249 size_t output1_length = output1_length_arg;
3250 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003251 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003252 size_t output_buffer_size = 0;
3253 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003254 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003255 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003257
Gilles Peskine8817f612018-12-18 00:18:46 +01003258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3261 psa_set_key_algorithm( &attributes, alg );
3262 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003263
Ronald Cron5425a212020-08-04 14:58:35 +02003264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3265 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003266
Ronald Cron5425a212020-08-04 14:58:35 +02003267 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003268
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003269 if( iv->len > 0 )
3270 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003271 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003272 }
3273
gabor-mezei-armceface22021-01-21 12:26:17 +01003274 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3275 TEST_ASSERT( output_buffer_size <=
3276 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003277 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003278
Gilles Peskinee0866522019-02-19 19:44:00 +01003279 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3281 output, output_buffer_size,
3282 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003283 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003284 TEST_ASSERT( function_output_length <=
3285 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3286 TEST_ASSERT( function_output_length <=
3287 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003288 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003289
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 PSA_ASSERT( psa_cipher_update( &operation,
3291 input->x + first_part_size,
3292 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003293 ( output_buffer_size == 0 ? NULL :
3294 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003295 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003296 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003297 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003298 TEST_ASSERT( function_output_length <=
3299 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3300 alg,
3301 input->len - first_part_size ) );
3302 TEST_ASSERT( function_output_length <=
3303 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003304 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003305
Gilles Peskine8817f612018-12-18 00:18:46 +01003306 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003307 ( output_buffer_size == 0 ? NULL :
3308 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003309 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003310 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003311 TEST_ASSERT( function_output_length <=
3312 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3313 TEST_ASSERT( function_output_length <=
3314 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003315 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003317
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003318 ASSERT_COMPARE( expected_output->x, expected_output->len,
3319 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003320
3321exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003322 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003323 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003324 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003325 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003326}
3327/* END_CASE */
3328
3329/* BEGIN_CASE */
3330void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003331 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003332 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003333 int first_part_size_arg,
3334 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003335 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003336{
Ronald Cron5425a212020-08-04 14:58:35 +02003337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003338 psa_key_type_t key_type = key_type_arg;
3339 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003340 size_t first_part_size = first_part_size_arg;
3341 size_t output1_length = output1_length_arg;
3342 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003343 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003344 size_t output_buffer_size = 0;
3345 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003346 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003347 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003349
Gilles Peskine8817f612018-12-18 00:18:46 +01003350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003351
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3353 psa_set_key_algorithm( &attributes, alg );
3354 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003355
Ronald Cron5425a212020-08-04 14:58:35 +02003356 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3357 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003358
Ronald Cron5425a212020-08-04 14:58:35 +02003359 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003360
Steven Cooreman177deba2020-09-07 17:14:14 +02003361 if( iv->len > 0 )
3362 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003363 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003364 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003365
gabor-mezei-armceface22021-01-21 12:26:17 +01003366 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3367 TEST_ASSERT( output_buffer_size <=
3368 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003369 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003370
Gilles Peskinee0866522019-02-19 19:44:00 +01003371 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003372 PSA_ASSERT( psa_cipher_update( &operation,
3373 input->x, first_part_size,
3374 output, output_buffer_size,
3375 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003376 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003377 TEST_ASSERT( function_output_length <=
3378 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3379 TEST_ASSERT( function_output_length <=
3380 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003381 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003382
Gilles Peskine8817f612018-12-18 00:18:46 +01003383 PSA_ASSERT( psa_cipher_update( &operation,
3384 input->x + first_part_size,
3385 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003386 ( output_buffer_size == 0 ? NULL :
3387 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003388 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003389 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003390 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003391 TEST_ASSERT( function_output_length <=
3392 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3393 alg,
3394 input->len - first_part_size ) );
3395 TEST_ASSERT( function_output_length <=
3396 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003397 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003398
Gilles Peskine8817f612018-12-18 00:18:46 +01003399 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003400 ( output_buffer_size == 0 ? NULL :
3401 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003402 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003403 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003404 TEST_ASSERT( function_output_length <=
3405 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3406 TEST_ASSERT( function_output_length <=
3407 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003408 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003409 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003411 ASSERT_COMPARE( expected_output->x, expected_output->len,
3412 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003413
3414exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003415 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003416 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003417 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003418 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003419}
3420/* END_CASE */
3421
Gilles Peskine50e586b2018-06-08 14:28:46 +02003422/* BEGIN_CASE */
3423void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003424 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003425 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003426 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003427{
Ronald Cron5425a212020-08-04 14:58:35 +02003428 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003429 psa_status_t status;
3430 psa_key_type_t key_type = key_type_arg;
3431 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003432 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003433 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003434 size_t output_buffer_size = 0;
3435 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003436 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003437 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003439
Gilles Peskine8817f612018-12-18 00:18:46 +01003440 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003441
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003442 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3443 psa_set_key_algorithm( &attributes, alg );
3444 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003445
Ronald Cron5425a212020-08-04 14:58:35 +02003446 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3447 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003448
Ronald Cron5425a212020-08-04 14:58:35 +02003449 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003450
Steven Cooreman177deba2020-09-07 17:14:14 +02003451 if( iv->len > 0 )
3452 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003453 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003454 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003455
gabor-mezei-armceface22021-01-21 12:26:17 +01003456 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3457 TEST_ASSERT( output_buffer_size <=
3458 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003459 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003460
Gilles Peskine8817f612018-12-18 00:18:46 +01003461 PSA_ASSERT( psa_cipher_update( &operation,
3462 input->x, input->len,
3463 output, output_buffer_size,
3464 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003465 TEST_ASSERT( function_output_length <=
3466 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3467 TEST_ASSERT( function_output_length <=
3468 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003469 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003470
Gilles Peskine50e586b2018-06-08 14:28:46 +02003471 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003472 ( output_buffer_size == 0 ? NULL :
3473 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003474 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003475 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003476 TEST_ASSERT( function_output_length <=
3477 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3478 TEST_ASSERT( function_output_length <=
3479 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003480 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003481 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003482
3483 if( expected_status == PSA_SUCCESS )
3484 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003485 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003486 ASSERT_COMPARE( expected_output->x, expected_output->len,
3487 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488 }
3489
Gilles Peskine50e586b2018-06-08 14:28:46 +02003490exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003491 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003492 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003493 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003494 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495}
3496/* END_CASE */
3497
Gilles Peskine50e586b2018-06-08 14:28:46 +02003498/* BEGIN_CASE */
3499void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003500 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003501 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003502{
Ronald Cron5425a212020-08-04 14:58:35 +02003503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003504 psa_key_type_t key_type = key_type_arg;
3505 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003506 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003507 size_t iv_size = 16;
3508 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003509 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003510 size_t output1_size = 0;
3511 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003512 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003513 size_t output2_size = 0;
3514 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003515 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003516 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3517 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003518 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003519
Gilles Peskine8817f612018-12-18 00:18:46 +01003520 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003521
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003522 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3523 psa_set_key_algorithm( &attributes, alg );
3524 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003525
Ronald Cron5425a212020-08-04 14:58:35 +02003526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3527 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003528
Ronald Cron5425a212020-08-04 14:58:35 +02003529 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3530 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003531
Steven Cooreman177deba2020-09-07 17:14:14 +02003532 if( alg != PSA_ALG_ECB_NO_PADDING )
3533 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003534 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3535 iv, iv_size,
3536 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003537 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003538 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3539 TEST_ASSERT( output1_size <=
3540 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003541 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003542
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3544 output1, output1_size,
3545 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003546 TEST_ASSERT( output1_length <=
3547 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3548 TEST_ASSERT( output1_length <=
3549 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3550
Gilles Peskine8817f612018-12-18 00:18:46 +01003551 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003552 output1 + output1_length,
3553 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003554 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003555 TEST_ASSERT( function_output_length <=
3556 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3557 TEST_ASSERT( function_output_length <=
3558 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003559
Gilles Peskine048b7f02018-06-08 14:20:49 +02003560 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003561
Gilles Peskine8817f612018-12-18 00:18:46 +01003562 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003563
3564 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003565 TEST_ASSERT( output2_size <=
3566 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3567 TEST_ASSERT( output2_size <=
3568 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003569 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003570
Steven Cooreman177deba2020-09-07 17:14:14 +02003571 if( iv_length > 0 )
3572 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003573 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3574 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003575 }
3576
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3578 output2, output2_size,
3579 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003580 TEST_ASSERT( output2_length <=
3581 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3582 TEST_ASSERT( output2_length <=
3583 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3584
Gilles Peskine048b7f02018-06-08 14:20:49 +02003585 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003586 PSA_ASSERT( psa_cipher_finish( &operation2,
3587 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003588 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003590 TEST_ASSERT( function_output_length <=
3591 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3592 TEST_ASSERT( function_output_length <=
3593 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003594
Gilles Peskine048b7f02018-06-08 14:20:49 +02003595 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003596
Gilles Peskine8817f612018-12-18 00:18:46 +01003597 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003598
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003599 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003600
3601exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003602 psa_cipher_abort( &operation1 );
3603 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003604 mbedtls_free( output1 );
3605 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003606 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003607 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003608}
3609/* END_CASE */
3610
3611/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003612void cipher_verify_output_multipart( int alg_arg,
3613 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003614 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003615 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003616 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003617{
Ronald Cron5425a212020-08-04 14:58:35 +02003618 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003619 psa_key_type_t key_type = key_type_arg;
3620 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003621 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003622 unsigned char iv[16] = {0};
3623 size_t iv_size = 16;
3624 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003625 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003626 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003627 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003628 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003629 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003630 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003631 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003632 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3633 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003635
Gilles Peskine8817f612018-12-18 00:18:46 +01003636 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003637
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003638 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3639 psa_set_key_algorithm( &attributes, alg );
3640 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003641
Ronald Cron5425a212020-08-04 14:58:35 +02003642 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3643 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003644
Ronald Cron5425a212020-08-04 14:58:35 +02003645 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3646 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003647
Steven Cooreman177deba2020-09-07 17:14:14 +02003648 if( alg != PSA_ALG_ECB_NO_PADDING )
3649 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003650 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3651 iv, iv_size,
3652 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003653 }
3654
gabor-mezei-armceface22021-01-21 12:26:17 +01003655 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3656 TEST_ASSERT( output1_buffer_size <=
3657 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003658 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003659
Gilles Peskinee0866522019-02-19 19:44:00 +01003660 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003661
Gilles Peskine8817f612018-12-18 00:18:46 +01003662 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3663 output1, output1_buffer_size,
3664 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003665 TEST_ASSERT( function_output_length <=
3666 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3667 TEST_ASSERT( function_output_length <=
3668 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003669 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003670
Gilles Peskine8817f612018-12-18 00:18:46 +01003671 PSA_ASSERT( psa_cipher_update( &operation1,
3672 input->x + first_part_size,
3673 input->len - first_part_size,
3674 output1, output1_buffer_size,
3675 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003676 TEST_ASSERT( function_output_length <=
3677 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3678 alg,
3679 input->len - first_part_size ) );
3680 TEST_ASSERT( function_output_length <=
3681 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003682 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003683
Gilles Peskine8817f612018-12-18 00:18:46 +01003684 PSA_ASSERT( psa_cipher_finish( &operation1,
3685 output1 + output1_length,
3686 output1_buffer_size - output1_length,
3687 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003688 TEST_ASSERT( function_output_length <=
3689 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3690 TEST_ASSERT( function_output_length <=
3691 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003692 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003693
Gilles Peskine8817f612018-12-18 00:18:46 +01003694 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003695
Gilles Peskine048b7f02018-06-08 14:20:49 +02003696 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003697 TEST_ASSERT( output2_buffer_size <=
3698 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3699 TEST_ASSERT( output2_buffer_size <=
3700 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003701 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003702
Steven Cooreman177deba2020-09-07 17:14:14 +02003703 if( iv_length > 0 )
3704 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003705 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3706 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003707 }
Moran Pekerded84402018-06-06 16:36:50 +03003708
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3710 output2, output2_buffer_size,
3711 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003712 TEST_ASSERT( function_output_length <=
3713 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3714 TEST_ASSERT( function_output_length <=
3715 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003716 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003717
Gilles Peskine8817f612018-12-18 00:18:46 +01003718 PSA_ASSERT( psa_cipher_update( &operation2,
3719 output1 + first_part_size,
3720 output1_length - first_part_size,
3721 output2, output2_buffer_size,
3722 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003723 TEST_ASSERT( function_output_length <=
3724 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3725 alg,
3726 output1_length - first_part_size ) );
3727 TEST_ASSERT( function_output_length <=
3728 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003729 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003730
Gilles Peskine8817f612018-12-18 00:18:46 +01003731 PSA_ASSERT( psa_cipher_finish( &operation2,
3732 output2 + output2_length,
3733 output2_buffer_size - output2_length,
3734 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003735 TEST_ASSERT( function_output_length <=
3736 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3737 TEST_ASSERT( function_output_length <=
3738 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003739 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003740
Gilles Peskine8817f612018-12-18 00:18:46 +01003741 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003742
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003743 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003744
3745exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003746 psa_cipher_abort( &operation1 );
3747 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003748 mbedtls_free( output1 );
3749 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003750 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003751 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003752}
3753/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003754
Gilles Peskine20035e32018-02-03 22:44:14 +01003755/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003756void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003757 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003758 data_t *nonce,
3759 data_t *additional_data,
3760 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003761 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003762{
Ronald Cron5425a212020-08-04 14:58:35 +02003763 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003764 psa_key_type_t key_type = key_type_arg;
3765 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003766 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003767 unsigned char *output_data = NULL;
3768 size_t output_size = 0;
3769 size_t output_length = 0;
3770 unsigned char *output_data2 = NULL;
3771 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003772 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003773 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003775
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003777
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3779 psa_set_key_algorithm( &attributes, alg );
3780 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003781
Gilles Peskine049c7532019-05-15 20:22:09 +02003782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003783 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003784 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3785 key_bits = psa_get_key_bits( &attributes );
3786
3787 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3788 alg );
3789 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3790 * should be exact. */
3791 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3792 expected_result != PSA_ERROR_NOT_SUPPORTED )
3793 {
3794 TEST_EQUAL( output_size,
3795 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3796 TEST_ASSERT( output_size <=
3797 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3798 }
3799 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003800
Steven Cooremanf49478b2021-02-15 15:19:25 +01003801 status = psa_aead_encrypt( key, alg,
3802 nonce->x, nonce->len,
3803 additional_data->x,
3804 additional_data->len,
3805 input_data->x, input_data->len,
3806 output_data, output_size,
3807 &output_length );
3808
3809 /* If the operation is not supported, just skip and not fail in case the
3810 * encryption involves a common limitation of cryptography hardwares and
3811 * an alternative implementation. */
3812 if( status == PSA_ERROR_NOT_SUPPORTED )
3813 {
3814 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3815 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3816 }
3817
3818 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003819
3820 if( PSA_SUCCESS == expected_result )
3821 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003822 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003823
Gilles Peskine003a4a92019-05-14 16:09:40 +02003824 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3825 * should be exact. */
3826 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003827 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003828
gabor-mezei-armceface22021-01-21 12:26:17 +01003829 TEST_ASSERT( input_data->len <=
3830 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3831
Ronald Cron5425a212020-08-04 14:58:35 +02003832 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003833 nonce->x, nonce->len,
3834 additional_data->x,
3835 additional_data->len,
3836 output_data, output_length,
3837 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003838 &output_length2 ),
3839 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003840
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003841 ASSERT_COMPARE( input_data->x, input_data->len,
3842 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003843 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003844
Gilles Peskinea1cac842018-06-11 19:33:02 +02003845exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003846 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003847 mbedtls_free( output_data );
3848 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003849 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003850}
3851/* END_CASE */
3852
3853/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003854void aead_encrypt( int key_type_arg, data_t *key_data,
3855 int alg_arg,
3856 data_t *nonce,
3857 data_t *additional_data,
3858 data_t *input_data,
3859 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003860{
Ronald Cron5425a212020-08-04 14:58:35 +02003861 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003862 psa_key_type_t key_type = key_type_arg;
3863 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003864 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003865 unsigned char *output_data = NULL;
3866 size_t output_size = 0;
3867 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003868 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003869 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003870
Gilles Peskine8817f612018-12-18 00:18:46 +01003871 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003872
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003873 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3874 psa_set_key_algorithm( &attributes, alg );
3875 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003876
Gilles Peskine049c7532019-05-15 20:22:09 +02003877 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003878 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003879 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3880 key_bits = psa_get_key_bits( &attributes );
3881
3882 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3883 alg );
3884 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3885 * should be exact. */
3886 TEST_EQUAL( output_size,
3887 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3888 TEST_ASSERT( output_size <=
3889 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3890 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003891
Steven Cooremand588ea12021-01-11 19:36:04 +01003892 status = psa_aead_encrypt( key, alg,
3893 nonce->x, nonce->len,
3894 additional_data->x, additional_data->len,
3895 input_data->x, input_data->len,
3896 output_data, output_size,
3897 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003898
Ronald Cron28a45ed2021-02-09 20:35:42 +01003899 /* If the operation is not supported, just skip and not fail in case the
3900 * encryption involves a common limitation of cryptography hardwares and
3901 * an alternative implementation. */
3902 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003903 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003904 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3905 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003906 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003907
3908 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003909 ASSERT_COMPARE( expected_result->x, expected_result->len,
3910 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003911
Gilles Peskinea1cac842018-06-11 19:33:02 +02003912exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003913 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003914 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003915 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003916}
3917/* END_CASE */
3918
3919/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003920void aead_decrypt( int key_type_arg, data_t *key_data,
3921 int alg_arg,
3922 data_t *nonce,
3923 data_t *additional_data,
3924 data_t *input_data,
3925 data_t *expected_data,
3926 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003927{
Ronald Cron5425a212020-08-04 14:58:35 +02003928 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003929 psa_key_type_t key_type = key_type_arg;
3930 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003931 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003932 unsigned char *output_data = NULL;
3933 size_t output_size = 0;
3934 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003936 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003937 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003938
Gilles Peskine8817f612018-12-18 00:18:46 +01003939 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003940
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003941 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3942 psa_set_key_algorithm( &attributes, alg );
3943 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003944
Gilles Peskine049c7532019-05-15 20:22:09 +02003945 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003946 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003947 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3948 key_bits = psa_get_key_bits( &attributes );
3949
3950 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3951 alg );
3952 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3953 expected_result != PSA_ERROR_NOT_SUPPORTED )
3954 {
3955 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3956 * should be exact. */
3957 TEST_EQUAL( output_size,
3958 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3959 TEST_ASSERT( output_size <=
3960 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3961 }
3962 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003963
Steven Cooremand588ea12021-01-11 19:36:04 +01003964 status = psa_aead_decrypt( key, alg,
3965 nonce->x, nonce->len,
3966 additional_data->x,
3967 additional_data->len,
3968 input_data->x, input_data->len,
3969 output_data, output_size,
3970 &output_length );
3971
Ronald Cron28a45ed2021-02-09 20:35:42 +01003972 /* If the operation is not supported, just skip and not fail in case the
3973 * decryption involves a common limitation of cryptography hardwares and
3974 * an alternative implementation. */
3975 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003976 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003977 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3978 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003979 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003980
3981 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003982
Gilles Peskine2d277862018-06-18 15:41:12 +02003983 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003984 ASSERT_COMPARE( expected_data->x, expected_data->len,
3985 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003986
Gilles Peskinea1cac842018-06-11 19:33:02 +02003987exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003988 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003989 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003990 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003991}
3992/* END_CASE */
3993
3994/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003995void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3996 int alg_arg,
3997 data_t *nonce,
3998 data_t *additional_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003999 int test_ad_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004000 data_t *input_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01004001 int test_data_mp_arg,
4002 data_t *expected_result_arg )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004003{
Paul Elliottd3f82412021-06-16 16:52:21 +01004004 size_t ad_part_len = 0;
4005 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004006
Paul Elliottd3f82412021-06-16 16:52:21 +01004007 if( test_ad_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004008 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004009 for( ad_part_len = 1; ad_part_len <= additional_data->len;
4010 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004011 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004012 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004013
Paul Elliottd3f82412021-06-16 16:52:21 +01004014 aead_multipart_encrypt_internal( key_type_arg, key_data,
4015 alg_arg,nonce,
4016 additional_data,
4017 ad_part_len,
4018 input_data, -1,
4019 expected_result_arg );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004020 }
4021 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004022
4023 if( test_data_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004024 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004025 for( data_part_len = 1; data_part_len <= input_data->len;
4026 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004027 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004028 aead_multipart_encrypt_internal( key_type_arg, key_data,
4029 alg_arg, nonce,
4030 additional_data, -1,
4031 input_data, data_part_len,
4032 expected_result_arg );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004033 }
4034 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004035
Paul Elliottd3f82412021-06-16 16:52:21 +01004036 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004037
4038exit:
Paul Elliott0023e0a2021-04-27 10:06:22 +01004039}
4040/* END_CASE */
4041
4042/* BEGIN_CASE */
4043void aead_multipart_encrypt_decrypt( int key_type_arg, data_t *key_data,
4044 int alg_arg,
4045 data_t *nonce,
4046 data_t *additional_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01004047 int test_ad_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004048 data_t *input_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01004049 int test_data_mp_arg,
4050 int expected_status_arg )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004051{
Paul Elliottd3f82412021-06-16 16:52:21 +01004052 size_t ad_part_len = 0;
4053 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004054
Paul Elliottd3f82412021-06-16 16:52:21 +01004055 if( test_ad_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004056 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004057 for( ad_part_len = 1; ad_part_len <= additional_data->len;
4058 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004059 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004060 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004061
Paul Elliottd3f82412021-06-16 16:52:21 +01004062 aead_multipart_encrypt_decrypt_internal( key_type_arg, key_data,
4063 alg_arg, nonce,
4064 additional_data,
4065 ad_part_len,
4066 input_data, -1,
4067 expected_status_arg );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004068 }
4069 }
4070
Paul Elliottd3f82412021-06-16 16:52:21 +01004071 if( test_data_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004072 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004073 for( data_part_len = 1; data_part_len <= input_data->len;
4074 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004075 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004076 aead_multipart_encrypt_decrypt_internal( key_type_arg, key_data,
4077 alg_arg, nonce,
4078 additional_data, -1,
4079 input_data, data_part_len,
4080 expected_status_arg );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004081 }
4082 }
4083
Paul Elliottd3f82412021-06-16 16:52:21 +01004084 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004085
4086exit:
Paul Elliott0023e0a2021-04-27 10:06:22 +01004087}
4088/* END_CASE */
4089
4090/* BEGIN_CASE */
4091void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4092 int alg_arg,
4093 data_t *nonce,
4094 data_t *additional_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01004095 int test_ad_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004096 data_t *input_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01004097 int test_data_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004098 data_t *expected_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01004099 int expected_status )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004100{
Paul Elliottd3f82412021-06-16 16:52:21 +01004101 size_t ad_part_len = 0;
4102 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004103
Paul Elliottd3f82412021-06-16 16:52:21 +01004104 if( test_ad_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004105 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004106 for( ad_part_len = 1; ad_part_len <= additional_data->len;
4107 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004108 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004109 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004110
Paul Elliottd3f82412021-06-16 16:52:21 +01004111 aead_multipart_decrypt_internal( key_type_arg, key_data,
4112 alg_arg, nonce,
4113 additional_data,
4114 ad_part_len,
4115 input_data, -1,
4116 expected_data, expected_status );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004117 }
4118 }
4119
Paul Elliottd3f82412021-06-16 16:52:21 +01004120 if( test_data_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004121 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004122 for( data_part_len = 1; data_part_len <= input_data->len;
4123 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004124 {
Paul Elliottd3f82412021-06-16 16:52:21 +01004125 aead_multipart_decrypt_internal( key_type_arg, key_data,
4126 alg_arg, nonce,
4127 additional_data, -1,
4128 input_data, data_part_len,
4129 expected_data, expected_status );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004130 }
4131 }
4132
Paul Elliottd3f82412021-06-16 16:52:21 +01004133 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004134
4135exit:
Paul Elliott0023e0a2021-04-27 10:06:22 +01004136}
4137/* END_CASE */
4138
4139/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004140void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4141 int alg_arg,
4142 int nonce_len,
4143 int expected_result_arg )
4144{
4145
4146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4147 psa_key_type_t key_type = key_type_arg;
4148 psa_algorithm_t alg = alg_arg;
4149 psa_aead_operation_t operation;
4150 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4152 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4153 size_t nonce_generated_len = 0;
4154
4155 PSA_ASSERT( psa_crypto_init( ) );
4156
4157 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4158 psa_set_key_algorithm( & attributes, alg );
4159 psa_set_key_type( & attributes, key_type );
4160
4161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4162 &key ) );
4163
4164 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4165
4166 operation = psa_aead_operation_init( );
4167
4168 status = psa_aead_encrypt_setup( &operation, key, alg );
4169
4170 /* If the operation is not supported, just skip and not fail in case the
4171 * encryption involves a common limitation of cryptography hardwares and
4172 * an alternative implementation. */
4173 if( status == PSA_ERROR_NOT_SUPPORTED )
4174 {
4175 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4176 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len );
4177 }
4178
4179 PSA_ASSERT( status );
4180
4181 TEST_ASSERT( nonce_len < PSA_AEAD_NONCE_MAX_SIZE );
4182
4183 status = psa_aead_generate_nonce( &operation, nonce_buffer,
4184 nonce_len,
4185 &nonce_generated_len );
4186
4187 TEST_ASSERT( status == expected_result_arg );
4188
4189exit:
4190 psa_destroy_key( key );
4191 psa_aead_abort( &operation );
4192 PSA_DONE( );
4193}
4194/* END_CASE */
4195
4196/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004197void signature_size( int type_arg,
4198 int bits,
4199 int alg_arg,
4200 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004201{
4202 psa_key_type_t type = type_arg;
4203 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004204 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004205
Gilles Peskinefe11b722018-12-18 00:24:04 +01004206 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004207
Gilles Peskinee59236f2018-01-27 23:32:46 +01004208exit:
4209 ;
4210}
4211/* END_CASE */
4212
4213/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004214void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4215 int alg_arg, data_t *input_data,
4216 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004217{
Ronald Cron5425a212020-08-04 14:58:35 +02004218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004219 psa_key_type_t key_type = key_type_arg;
4220 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004221 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004222 unsigned char *signature = NULL;
4223 size_t signature_size;
4224 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004226
Gilles Peskine8817f612018-12-18 00:18:46 +01004227 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004228
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004229 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004230 psa_set_key_algorithm( &attributes, alg );
4231 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004232
Gilles Peskine049c7532019-05-15 20:22:09 +02004233 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004234 &key ) );
4235 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004236 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004237
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004238 /* Allocate a buffer which has the size advertized by the
4239 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004240 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004241 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004242 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004243 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004244 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004245
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004246 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004247 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004248 input_data->x, input_data->len,
4249 signature, signature_size,
4250 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004251 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004252 ASSERT_COMPARE( output_data->x, output_data->len,
4253 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004254
4255exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004256 /*
4257 * Key attributes may have been returned by psa_get_key_attributes()
4258 * thus reset them as required.
4259 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004260 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004261
Ronald Cron5425a212020-08-04 14:58:35 +02004262 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004263 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004264 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004265}
4266/* END_CASE */
4267
4268/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004269void sign_hash_fail( int key_type_arg, data_t *key_data,
4270 int alg_arg, data_t *input_data,
4271 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004272{
Ronald Cron5425a212020-08-04 14:58:35 +02004273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004274 psa_key_type_t key_type = key_type_arg;
4275 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004276 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004277 psa_status_t actual_status;
4278 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004279 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004280 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004282
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004283 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004284
Gilles Peskine8817f612018-12-18 00:18:46 +01004285 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004286
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004287 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004288 psa_set_key_algorithm( &attributes, alg );
4289 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004290
Gilles Peskine049c7532019-05-15 20:22:09 +02004291 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004292 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004293
Ronald Cron5425a212020-08-04 14:58:35 +02004294 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004295 input_data->x, input_data->len,
4296 signature, signature_size,
4297 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004298 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004299 /* The value of *signature_length is unspecified on error, but
4300 * whatever it is, it should be less than signature_size, so that
4301 * if the caller tries to read *signature_length bytes without
4302 * checking the error code then they don't overflow a buffer. */
4303 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004304
4305exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004306 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004307 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004308 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004309 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004310}
4311/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004312
4313/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004314void sign_verify_hash( int key_type_arg, data_t *key_data,
4315 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004316{
Ronald Cron5425a212020-08-04 14:58:35 +02004317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004318 psa_key_type_t key_type = key_type_arg;
4319 psa_algorithm_t alg = alg_arg;
4320 size_t key_bits;
4321 unsigned char *signature = NULL;
4322 size_t signature_size;
4323 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004325
Gilles Peskine8817f612018-12-18 00:18:46 +01004326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004327
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004328 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004329 psa_set_key_algorithm( &attributes, alg );
4330 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004331
Gilles Peskine049c7532019-05-15 20:22:09 +02004332 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004333 &key ) );
4334 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004335 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004336
4337 /* Allocate a buffer which has the size advertized by the
4338 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004339 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004340 key_bits, alg );
4341 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004342 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004343 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004344
4345 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004346 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004347 input_data->x, input_data->len,
4348 signature, signature_size,
4349 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004350 /* Check that the signature length looks sensible. */
4351 TEST_ASSERT( signature_length <= signature_size );
4352 TEST_ASSERT( signature_length > 0 );
4353
4354 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004355 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004356 input_data->x, input_data->len,
4357 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004358
4359 if( input_data->len != 0 )
4360 {
4361 /* Flip a bit in the input and verify that the signature is now
4362 * detected as invalid. Flip a bit at the beginning, not at the end,
4363 * because ECDSA may ignore the last few bits of the input. */
4364 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004365 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004366 input_data->x, input_data->len,
4367 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004368 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004369 }
4370
4371exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004372 /*
4373 * Key attributes may have been returned by psa_get_key_attributes()
4374 * thus reset them as required.
4375 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004376 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004377
Ronald Cron5425a212020-08-04 14:58:35 +02004378 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004379 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004380 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004381}
4382/* END_CASE */
4383
4384/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004385void verify_hash( int key_type_arg, data_t *key_data,
4386 int alg_arg, data_t *hash_data,
4387 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004388{
Ronald Cron5425a212020-08-04 14:58:35 +02004389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004390 psa_key_type_t key_type = key_type_arg;
4391 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004393
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004394 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004395
Gilles Peskine8817f612018-12-18 00:18:46 +01004396 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004397
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004398 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004399 psa_set_key_algorithm( &attributes, alg );
4400 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004401
Gilles Peskine049c7532019-05-15 20:22:09 +02004402 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004403 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004404
Ronald Cron5425a212020-08-04 14:58:35 +02004405 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004406 hash_data->x, hash_data->len,
4407 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004408
itayzafrir5c753392018-05-08 11:18:38 +03004409exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004410 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004411 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004412 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004413}
4414/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004415
4416/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004417void verify_hash_fail( int key_type_arg, data_t *key_data,
4418 int alg_arg, data_t *hash_data,
4419 data_t *signature_data,
4420 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004421{
Ronald Cron5425a212020-08-04 14:58:35 +02004422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004423 psa_key_type_t key_type = key_type_arg;
4424 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004425 psa_status_t actual_status;
4426 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004428
Gilles Peskine8817f612018-12-18 00:18:46 +01004429 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004430
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004431 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004432 psa_set_key_algorithm( &attributes, alg );
4433 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004434
Gilles Peskine049c7532019-05-15 20:22:09 +02004435 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004436 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004437
Ronald Cron5425a212020-08-04 14:58:35 +02004438 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004439 hash_data->x, hash_data->len,
4440 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004441 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004442
4443exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004444 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004445 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004446 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004447}
4448/* END_CASE */
4449
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004450/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004451void sign_message_deterministic( int key_type_arg,
4452 data_t *key_data,
4453 int alg_arg,
4454 data_t *input_data,
4455 data_t *output_data )
4456{
4457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4458 psa_key_type_t key_type = key_type_arg;
4459 psa_algorithm_t alg = alg_arg;
4460 size_t key_bits;
4461 unsigned char *signature = NULL;
4462 size_t signature_size;
4463 size_t signature_length = 0xdeadbeef;
4464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4465
4466 PSA_ASSERT( psa_crypto_init( ) );
4467
4468 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4469 psa_set_key_algorithm( &attributes, alg );
4470 psa_set_key_type( &attributes, key_type );
4471
4472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4473 &key ) );
4474 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4475 key_bits = psa_get_key_bits( &attributes );
4476
4477 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4478 TEST_ASSERT( signature_size != 0 );
4479 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4480 ASSERT_ALLOC( signature, signature_size );
4481
4482 PSA_ASSERT( psa_sign_message( key, alg,
4483 input_data->x, input_data->len,
4484 signature, signature_size,
4485 &signature_length ) );
4486
4487 ASSERT_COMPARE( output_data->x, output_data->len,
4488 signature, signature_length );
4489
4490exit:
4491 psa_reset_key_attributes( &attributes );
4492
4493 psa_destroy_key( key );
4494 mbedtls_free( signature );
4495 PSA_DONE( );
4496
4497}
4498/* END_CASE */
4499
4500/* BEGIN_CASE */
4501void sign_message_fail( int key_type_arg,
4502 data_t *key_data,
4503 int alg_arg,
4504 data_t *input_data,
4505 int signature_size_arg,
4506 int expected_status_arg )
4507{
4508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4509 psa_key_type_t key_type = key_type_arg;
4510 psa_algorithm_t alg = alg_arg;
4511 size_t signature_size = signature_size_arg;
4512 psa_status_t actual_status;
4513 psa_status_t expected_status = expected_status_arg;
4514 unsigned char *signature = NULL;
4515 size_t signature_length = 0xdeadbeef;
4516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4517
4518 ASSERT_ALLOC( signature, signature_size );
4519
4520 PSA_ASSERT( psa_crypto_init( ) );
4521
4522 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4523 psa_set_key_algorithm( &attributes, alg );
4524 psa_set_key_type( &attributes, key_type );
4525
4526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4527 &key ) );
4528
4529 actual_status = psa_sign_message( key, alg,
4530 input_data->x, input_data->len,
4531 signature, signature_size,
4532 &signature_length );
4533 TEST_EQUAL( actual_status, expected_status );
4534 /* The value of *signature_length is unspecified on error, but
4535 * whatever it is, it should be less than signature_size, so that
4536 * if the caller tries to read *signature_length bytes without
4537 * checking the error code then they don't overflow a buffer. */
4538 TEST_ASSERT( signature_length <= signature_size );
4539
4540exit:
4541 psa_reset_key_attributes( &attributes );
4542 psa_destroy_key( key );
4543 mbedtls_free( signature );
4544 PSA_DONE( );
4545}
4546/* END_CASE */
4547
4548/* BEGIN_CASE */
4549void sign_verify_message( int key_type_arg,
4550 data_t *key_data,
4551 int alg_arg,
4552 data_t *input_data )
4553{
4554 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4555 psa_key_type_t key_type = key_type_arg;
4556 psa_algorithm_t alg = alg_arg;
4557 size_t key_bits;
4558 unsigned char *signature = NULL;
4559 size_t signature_size;
4560 size_t signature_length = 0xdeadbeef;
4561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4562
4563 PSA_ASSERT( psa_crypto_init( ) );
4564
4565 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4566 PSA_KEY_USAGE_VERIFY_MESSAGE );
4567 psa_set_key_algorithm( &attributes, alg );
4568 psa_set_key_type( &attributes, key_type );
4569
4570 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4571 &key ) );
4572 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4573 key_bits = psa_get_key_bits( &attributes );
4574
4575 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4576 TEST_ASSERT( signature_size != 0 );
4577 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4578 ASSERT_ALLOC( signature, signature_size );
4579
4580 PSA_ASSERT( psa_sign_message( key, alg,
4581 input_data->x, input_data->len,
4582 signature, signature_size,
4583 &signature_length ) );
4584 TEST_ASSERT( signature_length <= signature_size );
4585 TEST_ASSERT( signature_length > 0 );
4586
4587 PSA_ASSERT( psa_verify_message( key, alg,
4588 input_data->x, input_data->len,
4589 signature, signature_length ) );
4590
4591 if( input_data->len != 0 )
4592 {
4593 /* Flip a bit in the input and verify that the signature is now
4594 * detected as invalid. Flip a bit at the beginning, not at the end,
4595 * because ECDSA may ignore the last few bits of the input. */
4596 input_data->x[0] ^= 1;
4597 TEST_EQUAL( psa_verify_message( key, alg,
4598 input_data->x, input_data->len,
4599 signature, signature_length ),
4600 PSA_ERROR_INVALID_SIGNATURE );
4601 }
4602
4603exit:
4604 psa_reset_key_attributes( &attributes );
4605
4606 psa_destroy_key( key );
4607 mbedtls_free( signature );
4608 PSA_DONE( );
4609}
4610/* END_CASE */
4611
4612/* BEGIN_CASE */
4613void verify_message( int key_type_arg,
4614 data_t *key_data,
4615 int alg_arg,
4616 data_t *input_data,
4617 data_t *signature_data )
4618{
4619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4620 psa_key_type_t key_type = key_type_arg;
4621 psa_algorithm_t alg = alg_arg;
4622 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4623
4624 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4625
4626 PSA_ASSERT( psa_crypto_init( ) );
4627
4628 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4629 psa_set_key_algorithm( &attributes, alg );
4630 psa_set_key_type( &attributes, key_type );
4631
4632 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4633 &key ) );
4634
4635 PSA_ASSERT( psa_verify_message( key, alg,
4636 input_data->x, input_data->len,
4637 signature_data->x, signature_data->len ) );
4638
4639exit:
4640 psa_reset_key_attributes( &attributes );
4641 psa_destroy_key( key );
4642 PSA_DONE( );
4643}
4644/* END_CASE */
4645
4646/* BEGIN_CASE */
4647void verify_message_fail( int key_type_arg,
4648 data_t *key_data,
4649 int alg_arg,
4650 data_t *hash_data,
4651 data_t *signature_data,
4652 int expected_status_arg )
4653{
4654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4655 psa_key_type_t key_type = key_type_arg;
4656 psa_algorithm_t alg = alg_arg;
4657 psa_status_t actual_status;
4658 psa_status_t expected_status = expected_status_arg;
4659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4660
4661 PSA_ASSERT( psa_crypto_init( ) );
4662
4663 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4664 psa_set_key_algorithm( &attributes, alg );
4665 psa_set_key_type( &attributes, key_type );
4666
4667 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4668 &key ) );
4669
4670 actual_status = psa_verify_message( key, alg,
4671 hash_data->x, hash_data->len,
4672 signature_data->x,
4673 signature_data->len );
4674 TEST_EQUAL( actual_status, expected_status );
4675
4676exit:
4677 psa_reset_key_attributes( &attributes );
4678 psa_destroy_key( key );
4679 PSA_DONE( );
4680}
4681/* END_CASE */
4682
4683/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004684void asymmetric_encrypt( int key_type_arg,
4685 data_t *key_data,
4686 int alg_arg,
4687 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004688 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004689 int expected_output_length_arg,
4690 int expected_status_arg )
4691{
Ronald Cron5425a212020-08-04 14:58:35 +02004692 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004693 psa_key_type_t key_type = key_type_arg;
4694 psa_algorithm_t alg = alg_arg;
4695 size_t expected_output_length = expected_output_length_arg;
4696 size_t key_bits;
4697 unsigned char *output = NULL;
4698 size_t output_size;
4699 size_t output_length = ~0;
4700 psa_status_t actual_status;
4701 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004703
Gilles Peskine8817f612018-12-18 00:18:46 +01004704 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004705
Gilles Peskine656896e2018-06-29 19:12:28 +02004706 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4708 psa_set_key_algorithm( &attributes, alg );
4709 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004710 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004711 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004712
4713 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004714 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004715 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004716
Gilles Peskine656896e2018-06-29 19:12:28 +02004717 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004718 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004719 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004720
4721 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004722 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004723 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004724 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004725 output, output_size,
4726 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004727 TEST_EQUAL( actual_status, expected_status );
4728 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004729
Gilles Peskine68428122018-06-30 18:42:41 +02004730 /* If the label is empty, the test framework puts a non-null pointer
4731 * in label->x. Test that a null pointer works as well. */
4732 if( label->len == 0 )
4733 {
4734 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004735 if( output_size != 0 )
4736 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004737 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004738 input_data->x, input_data->len,
4739 NULL, label->len,
4740 output, output_size,
4741 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004742 TEST_EQUAL( actual_status, expected_status );
4743 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004744 }
4745
Gilles Peskine656896e2018-06-29 19:12:28 +02004746exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004747 /*
4748 * Key attributes may have been returned by psa_get_key_attributes()
4749 * thus reset them as required.
4750 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004751 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004752
Ronald Cron5425a212020-08-04 14:58:35 +02004753 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004754 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004755 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004756}
4757/* END_CASE */
4758
4759/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004760void asymmetric_encrypt_decrypt( int key_type_arg,
4761 data_t *key_data,
4762 int alg_arg,
4763 data_t *input_data,
4764 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004765{
Ronald Cron5425a212020-08-04 14:58:35 +02004766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004767 psa_key_type_t key_type = key_type_arg;
4768 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004769 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004770 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004771 size_t output_size;
4772 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004773 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004774 size_t output2_size;
4775 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004777
Gilles Peskine8817f612018-12-18 00:18:46 +01004778 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004779
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004780 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4781 psa_set_key_algorithm( &attributes, alg );
4782 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004783
Gilles Peskine049c7532019-05-15 20:22:09 +02004784 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004785 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004786
4787 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004788 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004789 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004790
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004791 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004792 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004793 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004794
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004795 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004796 TEST_ASSERT( output2_size <=
4797 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4798 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004799 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004800
Gilles Peskineeebd7382018-06-08 18:11:54 +02004801 /* We test encryption by checking that encrypt-then-decrypt gives back
4802 * the original plaintext because of the non-optional random
4803 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004804 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004805 input_data->x, input_data->len,
4806 label->x, label->len,
4807 output, output_size,
4808 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004809 /* We don't know what ciphertext length to expect, but check that
4810 * it looks sensible. */
4811 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004812
Ronald Cron5425a212020-08-04 14:58:35 +02004813 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004814 output, output_length,
4815 label->x, label->len,
4816 output2, output2_size,
4817 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004818 ASSERT_COMPARE( input_data->x, input_data->len,
4819 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004820
4821exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004822 /*
4823 * Key attributes may have been returned by psa_get_key_attributes()
4824 * thus reset them as required.
4825 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004826 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004827
Ronald Cron5425a212020-08-04 14:58:35 +02004828 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004829 mbedtls_free( output );
4830 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004831 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004832}
4833/* END_CASE */
4834
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004835/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004836void asymmetric_decrypt( int key_type_arg,
4837 data_t *key_data,
4838 int alg_arg,
4839 data_t *input_data,
4840 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004841 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004842{
Ronald Cron5425a212020-08-04 14:58:35 +02004843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004844 psa_key_type_t key_type = key_type_arg;
4845 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004846 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004847 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004848 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004849 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004851
Gilles Peskine8817f612018-12-18 00:18:46 +01004852 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004853
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004854 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4855 psa_set_key_algorithm( &attributes, alg );
4856 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004857
Gilles Peskine049c7532019-05-15 20:22:09 +02004858 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004859 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004860
gabor-mezei-armceface22021-01-21 12:26:17 +01004861 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4862 key_bits = psa_get_key_bits( &attributes );
4863
4864 /* Determine the maximum ciphertext length */
4865 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4866 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4867 ASSERT_ALLOC( output, output_size );
4868
Ronald Cron5425a212020-08-04 14:58:35 +02004869 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004870 input_data->x, input_data->len,
4871 label->x, label->len,
4872 output,
4873 output_size,
4874 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004875 ASSERT_COMPARE( expected_data->x, expected_data->len,
4876 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004877
Gilles Peskine68428122018-06-30 18:42:41 +02004878 /* If the label is empty, the test framework puts a non-null pointer
4879 * in label->x. Test that a null pointer works as well. */
4880 if( label->len == 0 )
4881 {
4882 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004883 if( output_size != 0 )
4884 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004885 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004886 input_data->x, input_data->len,
4887 NULL, label->len,
4888 output,
4889 output_size,
4890 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004891 ASSERT_COMPARE( expected_data->x, expected_data->len,
4892 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004893 }
4894
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004895exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004896 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004897 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004898 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004899 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004900}
4901/* END_CASE */
4902
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004903/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004904void asymmetric_decrypt_fail( int key_type_arg,
4905 data_t *key_data,
4906 int alg_arg,
4907 data_t *input_data,
4908 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004909 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004910 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004911{
Ronald Cron5425a212020-08-04 14:58:35 +02004912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004913 psa_key_type_t key_type = key_type_arg;
4914 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004915 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004916 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004917 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004918 psa_status_t actual_status;
4919 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004921
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004922 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004923
Gilles Peskine8817f612018-12-18 00:18:46 +01004924 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004925
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4927 psa_set_key_algorithm( &attributes, alg );
4928 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004929
Gilles Peskine049c7532019-05-15 20:22:09 +02004930 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004931 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004932
Ronald Cron5425a212020-08-04 14:58:35 +02004933 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004934 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004935 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004936 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004937 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004938 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004939 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004940
Gilles Peskine68428122018-06-30 18:42:41 +02004941 /* If the label is empty, the test framework puts a non-null pointer
4942 * in label->x. Test that a null pointer works as well. */
4943 if( label->len == 0 )
4944 {
4945 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004946 if( output_size != 0 )
4947 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004948 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004949 input_data->x, input_data->len,
4950 NULL, label->len,
4951 output, output_size,
4952 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004953 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004954 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004955 }
4956
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004957exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004958 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004959 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004960 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004961 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004962}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004963/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004964
4965/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004966void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004967{
4968 /* Test each valid way of initializing the object, except for `= {0}`, as
4969 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4970 * though it's OK by the C standard. We could test for this, but we'd need
4971 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004972 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004973 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4974 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4975 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004976
4977 memset( &zero, 0, sizeof( zero ) );
4978
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004980 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004981 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004982 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004983 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004984 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004985 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004986
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004988 PSA_ASSERT( psa_key_derivation_abort(&func) );
4989 PSA_ASSERT( psa_key_derivation_abort(&init) );
4990 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004991}
4992/* END_CASE */
4993
Janos Follath16de4a42019-06-13 16:32:24 +01004994/* BEGIN_CASE */
4995void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004996{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004997 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004998 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005000
Gilles Peskine8817f612018-12-18 00:18:46 +01005001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005002
Janos Follath16de4a42019-06-13 16:32:24 +01005003 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005004 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005005
5006exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005008 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005009}
5010/* END_CASE */
5011
Janos Follathaf3c2a02019-06-12 12:34:34 +01005012/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005013void derive_set_capacity( int alg_arg, int capacity_arg,
5014 int expected_status_arg )
5015{
5016 psa_algorithm_t alg = alg_arg;
5017 size_t capacity = capacity_arg;
5018 psa_status_t expected_status = expected_status_arg;
5019 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5020
5021 PSA_ASSERT( psa_crypto_init( ) );
5022
5023 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5024
5025 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5026 expected_status );
5027
5028exit:
5029 psa_key_derivation_abort( &operation );
5030 PSA_DONE( );
5031}
5032/* END_CASE */
5033
5034/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005035void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005036 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005037 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005038 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005039 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005040 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005041 int expected_status_arg3,
5042 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005043{
5044 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005045 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5046 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005047 psa_status_t expected_statuses[] = {expected_status_arg1,
5048 expected_status_arg2,
5049 expected_status_arg3};
5050 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005051 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5052 MBEDTLS_SVC_KEY_ID_INIT,
5053 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005054 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5056 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005057 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005058 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005059 psa_status_t expected_output_status = expected_output_status_arg;
5060 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005061
5062 PSA_ASSERT( psa_crypto_init( ) );
5063
5064 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5065 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005066
5067 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5068
5069 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5070 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005071 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005072 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005073 psa_set_key_type( &attributes, key_types[i] );
5074 PSA_ASSERT( psa_import_key( &attributes,
5075 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005076 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005077 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5078 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5079 {
5080 // When taking a private key as secret input, use key agreement
5081 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005082 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5083 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005084 expected_statuses[i] );
5085 }
5086 else
5087 {
5088 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005089 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005090 expected_statuses[i] );
5091 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005092 }
5093 else
5094 {
5095 TEST_EQUAL( psa_key_derivation_input_bytes(
5096 &operation, steps[i],
5097 inputs[i]->x, inputs[i]->len ),
5098 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005099 }
5100 }
5101
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005102 if( output_key_type != PSA_KEY_TYPE_NONE )
5103 {
5104 psa_reset_key_attributes( &attributes );
5105 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5106 psa_set_key_bits( &attributes, 8 );
5107 actual_output_status =
5108 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005109 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005110 }
5111 else
5112 {
5113 uint8_t buffer[1];
5114 actual_output_status =
5115 psa_key_derivation_output_bytes( &operation,
5116 buffer, sizeof( buffer ) );
5117 }
5118 TEST_EQUAL( actual_output_status, expected_output_status );
5119
Janos Follathaf3c2a02019-06-12 12:34:34 +01005120exit:
5121 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005122 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5123 psa_destroy_key( keys[i] );
5124 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005125 PSA_DONE( );
5126}
5127/* END_CASE */
5128
Janos Follathd958bb72019-07-03 15:02:16 +01005129/* BEGIN_CASE */
5130void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005131{
Janos Follathd958bb72019-07-03 15:02:16 +01005132 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005134 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005136 unsigned char input1[] = "Input 1";
5137 size_t input1_length = sizeof( input1 );
5138 unsigned char input2[] = "Input 2";
5139 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005140 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005141 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005142 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5143 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5144 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005146
Gilles Peskine8817f612018-12-18 00:18:46 +01005147 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005148
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005149 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5150 psa_set_key_algorithm( &attributes, alg );
5151 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005152
Gilles Peskine73676cb2019-05-15 20:15:10 +02005153 PSA_ASSERT( psa_import_key( &attributes,
5154 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005155 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005156
5157 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005158 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5159 input1, input1_length,
5160 input2, input2_length,
5161 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005162 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005163
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005165 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005166 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005167
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005169
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005170 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005171 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005172
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005173exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005176 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005177}
5178/* END_CASE */
5179
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005180/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005181void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005182{
5183 uint8_t output_buffer[16];
5184 size_t buffer_size = 16;
5185 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005187
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005188 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5189 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005190 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005191
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005192 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005193 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005195 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005196
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005197 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5198 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005199 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005200
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005201 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005202 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005203
5204exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005206}
5207/* END_CASE */
5208
5209/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005210void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005211 int step1_arg, data_t *input1,
5212 int step2_arg, data_t *input2,
5213 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005214 int requested_capacity_arg,
5215 data_t *expected_output1,
5216 data_t *expected_output2 )
5217{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005218 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005219 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5220 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005221 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5222 MBEDTLS_SVC_KEY_ID_INIT,
5223 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005224 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005226 uint8_t *expected_outputs[2] =
5227 {expected_output1->x, expected_output2->x};
5228 size_t output_sizes[2] =
5229 {expected_output1->len, expected_output2->len};
5230 size_t output_buffer_size = 0;
5231 uint8_t *output_buffer = NULL;
5232 size_t expected_capacity;
5233 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005235 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005236 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005237
5238 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5239 {
5240 if( output_sizes[i] > output_buffer_size )
5241 output_buffer_size = output_sizes[i];
5242 if( output_sizes[i] == 0 )
5243 expected_outputs[i] = NULL;
5244 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005245 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005247
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5249 psa_set_key_algorithm( &attributes, alg );
5250 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005251
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005252 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005253 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5254 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5255 requested_capacity ) );
5256 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005258 switch( steps[i] )
5259 {
5260 case 0:
5261 break;
5262 case PSA_KEY_DERIVATION_INPUT_SECRET:
5263 PSA_ASSERT( psa_import_key( &attributes,
5264 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005265 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005266
5267 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5268 {
5269 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5270 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5271 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5272 }
5273
Gilles Peskine1468da72019-05-29 17:35:49 +02005274 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005275 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005276 break;
5277 default:
5278 PSA_ASSERT( psa_key_derivation_input_bytes(
5279 &operation, steps[i],
5280 inputs[i]->x, inputs[i]->len ) );
5281 break;
5282 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005284
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005286 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005287 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005288 expected_capacity = requested_capacity;
5289
5290 /* Expansion phase. */
5291 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5292 {
5293 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005294 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005295 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005296 if( expected_capacity == 0 && output_sizes[i] == 0 )
5297 {
5298 /* Reading 0 bytes when 0 bytes are available can go either way. */
5299 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005300 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005301 continue;
5302 }
5303 else if( expected_capacity == 0 ||
5304 output_sizes[i] > expected_capacity )
5305 {
5306 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005307 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005308 expected_capacity = 0;
5309 continue;
5310 }
5311 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005312 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005313 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005314 ASSERT_COMPARE( output_buffer, output_sizes[i],
5315 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005317 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005319 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005320 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005321 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005323
5324exit:
5325 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005326 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005327 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5328 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005329 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005330}
5331/* END_CASE */
5332
5333/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005334void derive_full( int alg_arg,
5335 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005336 data_t *input1,
5337 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005338 int requested_capacity_arg )
5339{
Ronald Cron5425a212020-08-04 14:58:35 +02005340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005341 psa_algorithm_t alg = alg_arg;
5342 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005344 unsigned char output_buffer[16];
5345 size_t expected_capacity = requested_capacity;
5346 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005348
Gilles Peskine8817f612018-12-18 00:18:46 +01005349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005350
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005351 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5352 psa_set_key_algorithm( &attributes, alg );
5353 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005354
Gilles Peskine049c7532019-05-15 20:22:09 +02005355 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005356 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005357
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005358 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5359 input1->x, input1->len,
5360 input2->x, input2->len,
5361 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005362 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005363
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005364 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005365 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005366 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005367
5368 /* Expansion phase. */
5369 while( current_capacity > 0 )
5370 {
5371 size_t read_size = sizeof( output_buffer );
5372 if( read_size > current_capacity )
5373 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005375 output_buffer,
5376 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005377 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005379 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005380 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005381 }
5382
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 /* Check that the operation refuses to go over capacity. */
5384 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005385 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005386
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005388
5389exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005390 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005391 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005392 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005393}
5394/* END_CASE */
5395
Janos Follathe60c9052019-07-03 13:51:30 +01005396/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005397void derive_key_exercise( int alg_arg,
5398 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005399 data_t *input1,
5400 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005401 int derived_type_arg,
5402 int derived_bits_arg,
5403 int derived_usage_arg,
5404 int derived_alg_arg )
5405{
Ronald Cron5425a212020-08-04 14:58:35 +02005406 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5407 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005408 psa_algorithm_t alg = alg_arg;
5409 psa_key_type_t derived_type = derived_type_arg;
5410 size_t derived_bits = derived_bits_arg;
5411 psa_key_usage_t derived_usage = derived_usage_arg;
5412 psa_algorithm_t derived_alg = derived_alg_arg;
5413 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005414 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005416 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005417
Gilles Peskine8817f612018-12-18 00:18:46 +01005418 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005419
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005420 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5421 psa_set_key_algorithm( &attributes, alg );
5422 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005423 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005424 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005425
5426 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005427 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5428 input1->x, input1->len,
5429 input2->x, input2->len,
5430 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005431 goto exit;
5432
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005433 psa_set_key_usage_flags( &attributes, derived_usage );
5434 psa_set_key_algorithm( &attributes, derived_alg );
5435 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005436 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005438 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005439
5440 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005441 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005442 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5443 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005444
5445 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005446 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005447 goto exit;
5448
5449exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005450 /*
5451 * Key attributes may have been returned by psa_get_key_attributes()
5452 * thus reset them as required.
5453 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005454 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005455
5456 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005457 psa_destroy_key( base_key );
5458 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005459 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005460}
5461/* END_CASE */
5462
Janos Follath42fd8882019-07-03 14:17:09 +01005463/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005464void derive_key_export( int alg_arg,
5465 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005466 data_t *input1,
5467 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005468 int bytes1_arg,
5469 int bytes2_arg )
5470{
Ronald Cron5425a212020-08-04 14:58:35 +02005471 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5472 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005473 psa_algorithm_t alg = alg_arg;
5474 size_t bytes1 = bytes1_arg;
5475 size_t bytes2 = bytes2_arg;
5476 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005478 uint8_t *output_buffer = NULL;
5479 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005480 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5481 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005482 size_t length;
5483
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005484 ASSERT_ALLOC( output_buffer, capacity );
5485 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005487
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005488 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5489 psa_set_key_algorithm( &base_attributes, alg );
5490 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005491 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005492 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005493
5494 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005495 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5496 input1->x, input1->len,
5497 input2->x, input2->len,
5498 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005499 goto exit;
5500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005502 output_buffer,
5503 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005505
5506 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005507 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5508 input1->x, input1->len,
5509 input2->x, input2->len,
5510 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005511 goto exit;
5512
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005513 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5514 psa_set_key_algorithm( &derived_attributes, 0 );
5515 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005516 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005518 &derived_key ) );
5519 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005520 export_buffer, bytes1,
5521 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005522 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005523 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005524 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005526 &derived_key ) );
5527 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005528 export_buffer + bytes1, bytes2,
5529 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005530 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005531
5532 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005533 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5534 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005535
5536exit:
5537 mbedtls_free( output_buffer );
5538 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005539 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005540 psa_destroy_key( base_key );
5541 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005542 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005543}
5544/* END_CASE */
5545
5546/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005547void derive_key( int alg_arg,
5548 data_t *key_data, data_t *input1, data_t *input2,
5549 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005550 int expected_status_arg,
5551 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005552{
Ronald Cron5425a212020-08-04 14:58:35 +02005553 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5554 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005555 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005556 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005557 size_t bits = bits_arg;
5558 psa_status_t expected_status = expected_status_arg;
5559 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5560 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5561 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5562
5563 PSA_ASSERT( psa_crypto_init( ) );
5564
5565 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5566 psa_set_key_algorithm( &base_attributes, alg );
5567 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5568 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005569 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005570
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005571 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5572 input1->x, input1->len,
5573 input2->x, input2->len,
5574 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005575 goto exit;
5576
5577 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5578 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005579 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005580 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005581
5582 psa_status_t status =
5583 psa_key_derivation_output_key( &derived_attributes,
5584 &operation,
5585 &derived_key );
5586 if( is_large_output > 0 )
5587 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5588 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005589
5590exit:
5591 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005592 psa_destroy_key( base_key );
5593 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005594 PSA_DONE( );
5595}
5596/* END_CASE */
5597
5598/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005599void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005600 int our_key_type_arg, int our_key_alg_arg,
5601 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005602 int expected_status_arg )
5603{
Ronald Cron5425a212020-08-04 14:58:35 +02005604 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005605 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005606 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005607 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005608 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005610 psa_status_t expected_status = expected_status_arg;
5611 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005612
Gilles Peskine8817f612018-12-18 00:18:46 +01005613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005614
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005615 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005616 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005617 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005618 PSA_ASSERT( psa_import_key( &attributes,
5619 our_key_data->x, our_key_data->len,
5620 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005621
Gilles Peskine77f40d82019-04-11 21:27:06 +02005622 /* The tests currently include inputs that should fail at either step.
5623 * Test cases that fail at the setup step should be changed to call
5624 * key_derivation_setup instead, and this function should be renamed
5625 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005627 if( status == PSA_SUCCESS )
5628 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005629 TEST_EQUAL( psa_key_derivation_key_agreement(
5630 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5631 our_key,
5632 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005633 expected_status );
5634 }
5635 else
5636 {
5637 TEST_ASSERT( status == expected_status );
5638 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005639
5640exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005642 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005643 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005644}
5645/* END_CASE */
5646
5647/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005648void raw_key_agreement( int alg_arg,
5649 int our_key_type_arg, data_t *our_key_data,
5650 data_t *peer_key_data,
5651 data_t *expected_output )
5652{
Ronald Cron5425a212020-08-04 14:58:35 +02005653 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005654 psa_algorithm_t alg = alg_arg;
5655 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005657 unsigned char *output = NULL;
5658 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005659 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005660
5661 ASSERT_ALLOC( output, expected_output->len );
5662 PSA_ASSERT( psa_crypto_init( ) );
5663
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005664 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5665 psa_set_key_algorithm( &attributes, alg );
5666 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005667 PSA_ASSERT( psa_import_key( &attributes,
5668 our_key_data->x, our_key_data->len,
5669 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005670
gabor-mezei-armceface22021-01-21 12:26:17 +01005671 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5672 key_bits = psa_get_key_bits( &attributes );
5673
Gilles Peskinebe697d82019-05-16 18:00:41 +02005674 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5675 peer_key_data->x, peer_key_data->len,
5676 output, expected_output->len,
5677 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005678 ASSERT_COMPARE( output, output_length,
5679 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005680 TEST_ASSERT( output_length <=
5681 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5682 TEST_ASSERT( output_length <=
5683 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005684
5685exit:
5686 mbedtls_free( output );
5687 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005688 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005689}
5690/* END_CASE */
5691
5692/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005693void key_agreement_capacity( int alg_arg,
5694 int our_key_type_arg, data_t *our_key_data,
5695 data_t *peer_key_data,
5696 int expected_capacity_arg )
5697{
Ronald Cron5425a212020-08-04 14:58:35 +02005698 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005699 psa_algorithm_t alg = alg_arg;
5700 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005701 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005703 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005704 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005705
Gilles Peskine8817f612018-12-18 00:18:46 +01005706 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005707
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005708 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5709 psa_set_key_algorithm( &attributes, alg );
5710 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005711 PSA_ASSERT( psa_import_key( &attributes,
5712 our_key_data->x, our_key_data->len,
5713 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005714
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005715 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005716 PSA_ASSERT( psa_key_derivation_key_agreement(
5717 &operation,
5718 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5719 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005720 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5721 {
5722 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005724 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005725 NULL, 0 ) );
5726 }
Gilles Peskine59685592018-09-18 12:11:34 +02005727
Gilles Peskinebf491972018-10-25 22:36:12 +02005728 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005729 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005731 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005732
Gilles Peskinebf491972018-10-25 22:36:12 +02005733 /* Test the actual capacity by reading the output. */
5734 while( actual_capacity > sizeof( output ) )
5735 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005736 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005737 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005738 actual_capacity -= sizeof( output );
5739 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005741 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005742 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005743 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005744
Gilles Peskine59685592018-09-18 12:11:34 +02005745exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005746 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005747 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005748 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005749}
5750/* END_CASE */
5751
5752/* BEGIN_CASE */
5753void key_agreement_output( int alg_arg,
5754 int our_key_type_arg, data_t *our_key_data,
5755 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005756 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005757{
Ronald Cron5425a212020-08-04 14:58:35 +02005758 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005759 psa_algorithm_t alg = alg_arg;
5760 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005761 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005763 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005764
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005765 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5766 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005767
Gilles Peskine8817f612018-12-18 00:18:46 +01005768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005769
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5771 psa_set_key_algorithm( &attributes, alg );
5772 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005773 PSA_ASSERT( psa_import_key( &attributes,
5774 our_key_data->x, our_key_data->len,
5775 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005776
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005777 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005778 PSA_ASSERT( psa_key_derivation_key_agreement(
5779 &operation,
5780 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5781 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005782 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5783 {
5784 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005785 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005786 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005787 NULL, 0 ) );
5788 }
Gilles Peskine59685592018-09-18 12:11:34 +02005789
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005790 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005791 actual_output,
5792 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005793 ASSERT_COMPARE( actual_output, expected_output1->len,
5794 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005795 if( expected_output2->len != 0 )
5796 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005797 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005798 actual_output,
5799 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005800 ASSERT_COMPARE( actual_output, expected_output2->len,
5801 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005802 }
Gilles Peskine59685592018-09-18 12:11:34 +02005803
5804exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005805 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005806 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005807 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005808 mbedtls_free( actual_output );
5809}
5810/* END_CASE */
5811
5812/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005813void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005814{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005815 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005816 unsigned char *output = NULL;
5817 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005818 size_t i;
5819 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005820
Simon Butcher49f8e312020-03-03 15:51:50 +00005821 TEST_ASSERT( bytes_arg >= 0 );
5822
Gilles Peskine91892022021-02-08 19:50:26 +01005823 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005824 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005825
Gilles Peskine8817f612018-12-18 00:18:46 +01005826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005827
Gilles Peskinea50d7392018-06-21 10:22:13 +02005828 /* Run several times, to ensure that every output byte will be
5829 * nonzero at least once with overwhelming probability
5830 * (2^(-8*number_of_runs)). */
5831 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005832 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005833 if( bytes != 0 )
5834 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005835 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005836
Gilles Peskinea50d7392018-06-21 10:22:13 +02005837 for( i = 0; i < bytes; i++ )
5838 {
5839 if( output[i] != 0 )
5840 ++changed[i];
5841 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005842 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005843
5844 /* Check that every byte was changed to nonzero at least once. This
5845 * validates that psa_generate_random is overwriting every byte of
5846 * the output buffer. */
5847 for( i = 0; i < bytes; i++ )
5848 {
5849 TEST_ASSERT( changed[i] != 0 );
5850 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005851
5852exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005853 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005854 mbedtls_free( output );
5855 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005856}
5857/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005858
5859/* BEGIN_CASE */
5860void generate_key( int type_arg,
5861 int bits_arg,
5862 int usage_arg,
5863 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005864 int expected_status_arg,
5865 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005866{
Ronald Cron5425a212020-08-04 14:58:35 +02005867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005868 psa_key_type_t type = type_arg;
5869 psa_key_usage_t usage = usage_arg;
5870 size_t bits = bits_arg;
5871 psa_algorithm_t alg = alg_arg;
5872 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005874 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005875
Gilles Peskine8817f612018-12-18 00:18:46 +01005876 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005877
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005878 psa_set_key_usage_flags( &attributes, usage );
5879 psa_set_key_algorithm( &attributes, alg );
5880 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005881 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005882
5883 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005884 psa_status_t status = psa_generate_key( &attributes, &key );
5885
5886 if( is_large_key > 0 )
5887 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5888 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005889 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005890 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005891
5892 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005893 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005894 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5895 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005896
Gilles Peskine818ca122018-06-20 18:16:48 +02005897 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005898 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005899 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005900
5901exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005902 /*
5903 * Key attributes may have been returned by psa_get_key_attributes()
5904 * thus reset them as required.
5905 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005906 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005907
Ronald Cron5425a212020-08-04 14:58:35 +02005908 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005909 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005910}
5911/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005912
Ronald Cronee414c72021-03-18 18:50:08 +01005913/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005914void generate_key_rsa( int bits_arg,
5915 data_t *e_arg,
5916 int expected_status_arg )
5917{
Ronald Cron5425a212020-08-04 14:58:35 +02005918 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005919 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005920 size_t bits = bits_arg;
5921 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5922 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5923 psa_status_t expected_status = expected_status_arg;
5924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5925 uint8_t *exported = NULL;
5926 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005927 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005928 size_t exported_length = SIZE_MAX;
5929 uint8_t *e_read_buffer = NULL;
5930 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005931 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005932 size_t e_read_length = SIZE_MAX;
5933
5934 if( e_arg->len == 0 ||
5935 ( e_arg->len == 3 &&
5936 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5937 {
5938 is_default_public_exponent = 1;
5939 e_read_size = 0;
5940 }
5941 ASSERT_ALLOC( e_read_buffer, e_read_size );
5942 ASSERT_ALLOC( exported, exported_size );
5943
5944 PSA_ASSERT( psa_crypto_init( ) );
5945
5946 psa_set_key_usage_flags( &attributes, usage );
5947 psa_set_key_algorithm( &attributes, alg );
5948 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5949 e_arg->x, e_arg->len ) );
5950 psa_set_key_bits( &attributes, bits );
5951
5952 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005953 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005954 if( expected_status != PSA_SUCCESS )
5955 goto exit;
5956
5957 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005958 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005959 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5960 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5961 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5962 e_read_buffer, e_read_size,
5963 &e_read_length ) );
5964 if( is_default_public_exponent )
5965 TEST_EQUAL( e_read_length, 0 );
5966 else
5967 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5968
5969 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005970 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005971 goto exit;
5972
5973 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005974 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005975 exported, exported_size,
5976 &exported_length ) );
5977 {
5978 uint8_t *p = exported;
5979 uint8_t *end = exported + exported_length;
5980 size_t len;
5981 /* RSAPublicKey ::= SEQUENCE {
5982 * modulus INTEGER, -- n
5983 * publicExponent INTEGER } -- e
5984 */
5985 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005986 MBEDTLS_ASN1_SEQUENCE |
5987 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005988 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005989 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5990 MBEDTLS_ASN1_INTEGER ) );
5991 if( len >= 1 && p[0] == 0 )
5992 {
5993 ++p;
5994 --len;
5995 }
5996 if( e_arg->len == 0 )
5997 {
5998 TEST_EQUAL( len, 3 );
5999 TEST_EQUAL( p[0], 1 );
6000 TEST_EQUAL( p[1], 0 );
6001 TEST_EQUAL( p[2], 1 );
6002 }
6003 else
6004 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6005 }
6006
6007exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006008 /*
6009 * Key attributes may have been returned by psa_get_key_attributes() or
6010 * set by psa_set_key_domain_parameters() thus reset them as required.
6011 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006012 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006013
Ronald Cron5425a212020-08-04 14:58:35 +02006014 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006015 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006016 mbedtls_free( e_read_buffer );
6017 mbedtls_free( exported );
6018}
6019/* END_CASE */
6020
Darryl Greend49a4992018-06-18 17:27:26 +01006021/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006022void persistent_key_load_key_from_storage( data_t *data,
6023 int type_arg, int bits_arg,
6024 int usage_flags_arg, int alg_arg,
6025 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006026{
Ronald Cron71016a92020-08-28 19:01:50 +02006027 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006029 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6030 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006031 psa_key_type_t type = type_arg;
6032 size_t bits = bits_arg;
6033 psa_key_usage_t usage_flags = usage_flags_arg;
6034 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006035 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006036 unsigned char *first_export = NULL;
6037 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006038 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006039 size_t first_exported_length;
6040 size_t second_exported_length;
6041
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006042 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6043 {
6044 ASSERT_ALLOC( first_export, export_size );
6045 ASSERT_ALLOC( second_export, export_size );
6046 }
Darryl Greend49a4992018-06-18 17:27:26 +01006047
Gilles Peskine8817f612018-12-18 00:18:46 +01006048 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006049
Gilles Peskinec87af662019-05-15 16:12:22 +02006050 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006051 psa_set_key_usage_flags( &attributes, usage_flags );
6052 psa_set_key_algorithm( &attributes, alg );
6053 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006054 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006055
Darryl Green0c6575a2018-11-07 16:05:30 +00006056 switch( generation_method )
6057 {
6058 case IMPORT_KEY:
6059 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006060 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006061 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006062 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006063
Darryl Green0c6575a2018-11-07 16:05:30 +00006064 case GENERATE_KEY:
6065 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006066 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006067 break;
6068
6069 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006070#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006071 {
6072 /* Create base key */
6073 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6074 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6075 psa_set_key_usage_flags( &base_attributes,
6076 PSA_KEY_USAGE_DERIVE );
6077 psa_set_key_algorithm( &base_attributes, derive_alg );
6078 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006079 PSA_ASSERT( psa_import_key( &base_attributes,
6080 data->x, data->len,
6081 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006082 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006083 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006084 PSA_ASSERT( psa_key_derivation_input_key(
6085 &operation,
6086 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006087 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006088 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006089 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006090 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6091 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006092 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006093 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006094 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006095 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006096 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006097#else
6098 TEST_ASSUME( ! "KDF not supported in this configuration" );
6099#endif
6100 break;
6101
6102 default:
6103 TEST_ASSERT( ! "generation_method not implemented in test" );
6104 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006105 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006106 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006107
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006108 /* Export the key if permitted by the key policy. */
6109 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6110 {
Ronald Cron5425a212020-08-04 14:58:35 +02006111 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006112 first_export, export_size,
6113 &first_exported_length ) );
6114 if( generation_method == IMPORT_KEY )
6115 ASSERT_COMPARE( data->x, data->len,
6116 first_export, first_exported_length );
6117 }
Darryl Greend49a4992018-06-18 17:27:26 +01006118
6119 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006120 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006121 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006122 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006123
Darryl Greend49a4992018-06-18 17:27:26 +01006124 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006125 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006126 TEST_ASSERT( mbedtls_svc_key_id_equal(
6127 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006128 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6129 PSA_KEY_LIFETIME_PERSISTENT );
6130 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6131 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6132 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6133 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006134
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006135 /* Export the key again if permitted by the key policy. */
6136 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006137 {
Ronald Cron5425a212020-08-04 14:58:35 +02006138 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006139 second_export, export_size,
6140 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006141 ASSERT_COMPARE( first_export, first_exported_length,
6142 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006143 }
6144
6145 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006146 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006147 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006148
6149exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006150 /*
6151 * Key attributes may have been returned by psa_get_key_attributes()
6152 * thus reset them as required.
6153 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006154 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006155
Darryl Greend49a4992018-06-18 17:27:26 +01006156 mbedtls_free( first_export );
6157 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006158 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006159 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006160 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006161 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006162}
6163/* END_CASE */