blob: 1a5c23e8e01a2d34261aaac3e5cd784dfdcd00e2 [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
Gilles Peskinee59236f2018-01-27 23:32:46 +0100708/* END_HEADER */
709
710/* BEGIN_DEPENDENCIES
711 * depends_on:MBEDTLS_PSA_CRYPTO_C
712 * END_DEPENDENCIES
713 */
714
715/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200716void static_checks( )
717{
718 size_t max_truncated_mac_size =
719 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
720
721 /* Check that the length for a truncated MAC always fits in the algorithm
722 * encoding. The shifted mask is the maximum truncated value. The
723 * untruncated algorithm may be one byte larger. */
724 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
725}
726/* END_CASE */
727
728/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200729void import_with_policy( int type_arg,
730 int usage_arg, int alg_arg,
731 int expected_status_arg )
732{
733 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
734 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200735 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200736 psa_key_type_t type = type_arg;
737 psa_key_usage_t usage = usage_arg;
738 psa_algorithm_t alg = alg_arg;
739 psa_status_t expected_status = expected_status_arg;
740 const uint8_t key_material[16] = {0};
741 psa_status_t status;
742
743 PSA_ASSERT( psa_crypto_init( ) );
744
745 psa_set_key_type( &attributes, type );
746 psa_set_key_usage_flags( &attributes, usage );
747 psa_set_key_algorithm( &attributes, alg );
748
749 status = psa_import_key( &attributes,
750 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200751 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200752 TEST_EQUAL( status, expected_status );
753 if( status != PSA_SUCCESS )
754 goto exit;
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200757 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
758 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
759 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200760 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200761
Ronald Cron5425a212020-08-04 14:58:35 +0200762 PSA_ASSERT( psa_destroy_key( key ) );
763 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200764
765exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100766 /*
767 * Key attributes may have been returned by psa_get_key_attributes()
768 * thus reset them as required.
769 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200770 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100771
772 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200773 PSA_DONE( );
774}
775/* END_CASE */
776
777/* BEGIN_CASE */
778void import_with_data( data_t *data, int type_arg,
779 int attr_bits_arg,
780 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200781{
782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
783 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200784 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200785 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200786 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200787 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789
Gilles Peskine8817f612018-12-18 00:18:46 +0100790 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791
Gilles Peskine4747d192019-04-17 15:05:45 +0200792 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200793 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200794
Ronald Cron5425a212020-08-04 14:58:35 +0200795 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100796 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200797 if( status != PSA_SUCCESS )
798 goto exit;
799
Ronald Cron5425a212020-08-04 14:58:35 +0200800 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200801 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200802 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200803 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200804 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200805
Ronald Cron5425a212020-08-04 14:58:35 +0200806 PSA_ASSERT( psa_destroy_key( key ) );
807 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808
809exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100810 /*
811 * Key attributes may have been returned by psa_get_key_attributes()
812 * thus reset them as required.
813 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200814 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100815
816 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200817 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818}
819/* END_CASE */
820
821/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200822void import_large_key( int type_arg, int byte_size_arg,
823 int expected_status_arg )
824{
825 psa_key_type_t type = type_arg;
826 size_t byte_size = byte_size_arg;
827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
828 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200830 psa_status_t status;
831 uint8_t *buffer = NULL;
832 size_t buffer_size = byte_size + 1;
833 size_t n;
834
Steven Cooreman69967ce2021-01-18 18:01:08 +0100835 /* Skip the test case if the target running the test cannot
836 * accomodate large keys due to heap size constraints */
837 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200838 memset( buffer, 'K', byte_size );
839
840 PSA_ASSERT( psa_crypto_init( ) );
841
842 /* Try importing the key */
843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
844 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100846 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200847 TEST_EQUAL( status, expected_status );
848
849 if( status == PSA_SUCCESS )
850 {
Ronald Cron5425a212020-08-04 14:58:35 +0200851 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200852 TEST_EQUAL( psa_get_key_type( &attributes ), type );
853 TEST_EQUAL( psa_get_key_bits( &attributes ),
854 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200855 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200856 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200857 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200858 for( n = 0; n < byte_size; n++ )
859 TEST_EQUAL( buffer[n], 'K' );
860 for( n = byte_size; n < buffer_size; n++ )
861 TEST_EQUAL( buffer[n], 0 );
862 }
863
864exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100865 /*
866 * Key attributes may have been returned by psa_get_key_attributes()
867 * thus reset them as required.
868 */
869 psa_reset_key_attributes( &attributes );
870
Ronald Cron5425a212020-08-04 14:58:35 +0200871 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200872 PSA_DONE( );
873 mbedtls_free( buffer );
874}
875/* END_CASE */
876
877/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200878void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
879{
Ronald Cron5425a212020-08-04 14:58:35 +0200880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200881 size_t bits = bits_arg;
882 psa_status_t expected_status = expected_status_arg;
883 psa_status_t status;
884 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200885 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200886 size_t buffer_size = /* Slight overapproximations */
887 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200888 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200889 unsigned char *p;
890 int ret;
891 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200893
Gilles Peskine8817f612018-12-18 00:18:46 +0100894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200895 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200896
897 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
898 bits, keypair ) ) >= 0 );
899 length = ret;
900
901 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200902 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200903 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100904 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200905
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200906 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200907 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200908
909exit:
910 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200911 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200912}
913/* END_CASE */
914
915/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300916void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300917 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200918 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100919 int expected_bits,
920 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200921 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100922 int canonical_input )
923{
Ronald Cron5425a212020-08-04 14:58:35 +0200924 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100925 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200926 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200927 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100929 unsigned char *exported = NULL;
930 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100931 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100932 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100933 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200934 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200935 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936
Moran Pekercb088e72018-07-17 17:36:59 +0300937 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200938 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200940 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100941 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942
Gilles Peskine4747d192019-04-17 15:05:45 +0200943 psa_set_key_usage_flags( &attributes, usage_arg );
944 psa_set_key_algorithm( &attributes, alg );
945 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700946
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200948 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949
950 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200951 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200952 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
953 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200954 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955
956 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200957 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100958 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100959
960 /* The exported length must be set by psa_export_key() to a value between 0
961 * and export_size. On errors, the exported length must be 0. */
962 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
963 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
964 TEST_ASSERT( exported_length <= export_size );
965
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200966 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200967 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200969 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100970 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200972 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973
Gilles Peskineea38a922021-02-13 00:05:16 +0100974 /* Run sanity checks on the exported key. For non-canonical inputs,
975 * this validates the canonical representations. For canonical inputs,
976 * this doesn't directly validate the implementation, but it still helps
977 * by cross-validating the test data with the sanity check code. */
978 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200979 goto exit;
980
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200982 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 else
984 {
Ronald Cron5425a212020-08-04 14:58:35 +0200985 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200986 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200987 &key2 ) );
988 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100989 reexported,
990 export_size,
991 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200992 ASSERT_COMPARE( exported, exported_length,
993 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200994 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100996 TEST_ASSERT( exported_length <=
997 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
998 psa_get_key_bits( &got_attributes ) ) );
999 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000
1001destroy:
1002 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001003 PSA_ASSERT( psa_destroy_key( key ) );
1004 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005
1006exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001007 /*
1008 * Key attributes may have been returned by psa_get_key_attributes()
1009 * thus reset them as required.
1010 */
1011 psa_reset_key_attributes( &got_attributes );
1012
itayzafrir3e02b3b2018-06-12 17:06:52 +03001013 mbedtls_free( exported );
1014 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001015 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016}
1017/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001018
Moran Pekerf709f4a2018-06-06 17:26:04 +03001019/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001020void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001021 int type_arg,
1022 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001023 int export_size_delta,
1024 int expected_export_status_arg,
1025 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001026{
Ronald Cron5425a212020-08-04 14:58:35 +02001027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001028 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001029 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001030 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001031 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001032 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001033 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001034 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001036
Gilles Peskine8817f612018-12-18 00:18:46 +01001037 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001038
Gilles Peskine4747d192019-04-17 15:05:45 +02001039 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1040 psa_set_key_algorithm( &attributes, alg );
1041 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001042
1043 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001044 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045
Gilles Peskine49c25912018-10-29 15:15:31 +01001046 /* Export the public key */
1047 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001048 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001049 exported, export_size,
1050 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001051 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001052 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001053 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001054 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001055 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001056 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001057 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001058 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001059 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01001060 TEST_ASSERT( expected_public_key->len <=
1061 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1062 TEST_ASSERT( expected_public_key->len <=
1063 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001064 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1065 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001066 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001067
1068exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001069 /*
1070 * Key attributes may have been returned by psa_get_key_attributes()
1071 * thus reset them as required.
1072 */
1073 psa_reset_key_attributes( &attributes );
1074
itayzafrir3e02b3b2018-06-12 17:06:52 +03001075 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001076 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001077 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001078}
1079/* END_CASE */
1080
Gilles Peskine20035e32018-02-03 22:44:14 +01001081/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001082void import_and_exercise_key( data_t *data,
1083 int type_arg,
1084 int bits_arg,
1085 int alg_arg )
1086{
Ronald Cron5425a212020-08-04 14:58:35 +02001087 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001088 psa_key_type_t type = type_arg;
1089 size_t bits = bits_arg;
1090 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001091 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001093 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001094
Gilles Peskine8817f612018-12-18 00:18:46 +01001095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001096
Gilles Peskine4747d192019-04-17 15:05:45 +02001097 psa_set_key_usage_flags( &attributes, usage );
1098 psa_set_key_algorithm( &attributes, alg );
1099 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001100
1101 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001102 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001103
1104 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001105 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001106 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1107 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001108
1109 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001110 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001111 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001112
Ronald Cron5425a212020-08-04 14:58:35 +02001113 PSA_ASSERT( psa_destroy_key( key ) );
1114 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001115
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001116exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001117 /*
1118 * Key attributes may have been returned by psa_get_key_attributes()
1119 * thus reset them as required.
1120 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001121 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001122
1123 psa_reset_key_attributes( &attributes );
1124 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001125 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001126}
1127/* END_CASE */
1128
1129/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001130void effective_key_attributes( int type_arg, int expected_type_arg,
1131 int bits_arg, int expected_bits_arg,
1132 int usage_arg, int expected_usage_arg,
1133 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001134{
Ronald Cron5425a212020-08-04 14:58:35 +02001135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001136 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001137 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001138 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001139 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001140 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001141 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001142 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001143 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001145
Gilles Peskine8817f612018-12-18 00:18:46 +01001146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001147
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001148 psa_set_key_usage_flags( &attributes, usage );
1149 psa_set_key_algorithm( &attributes, alg );
1150 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001151 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001152
Ronald Cron5425a212020-08-04 14:58:35 +02001153 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001154 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001155
Ronald Cron5425a212020-08-04 14:58:35 +02001156 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001157 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1158 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1159 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1160 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001161
1162exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001163 /*
1164 * Key attributes may have been returned by psa_get_key_attributes()
1165 * thus reset them as required.
1166 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001167 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001168
1169 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001170 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001171}
1172/* END_CASE */
1173
1174/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001175void check_key_policy( int type_arg, int bits_arg,
1176 int usage_arg, int alg_arg )
1177{
1178 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1179 usage_arg, usage_arg, alg_arg, alg_arg );
1180 goto exit;
1181}
1182/* END_CASE */
1183
1184/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001185void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001186{
1187 /* Test each valid way of initializing the object, except for `= {0}`, as
1188 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1189 * though it's OK by the C standard. We could test for this, but we'd need
1190 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_key_attributes_t func = psa_key_attributes_init( );
1192 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1193 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001194
1195 memset( &zero, 0, sizeof( zero ) );
1196
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001197 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1198 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1199 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001200
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001201 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1202 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1203 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1204
1205 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1206 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1207 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1208
1209 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1210 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1211 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1212
1213 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1214 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1215 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001216}
1217/* END_CASE */
1218
1219/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220void mac_key_policy( int policy_usage,
1221 int policy_alg,
1222 int key_type,
1223 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001224 int exercise_alg,
1225 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001226{
Ronald Cron5425a212020-08-04 14:58:35 +02001227 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001229 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001231 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001232 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001233
Gilles Peskine8817f612018-12-18 00:18:46 +01001234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001235
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001236 psa_set_key_usage_flags( &attributes, policy_usage );
1237 psa_set_key_algorithm( &attributes, policy_alg );
1238 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001239
Gilles Peskine049c7532019-05-15 20:22:09 +02001240 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001241 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001242
Ronald Cron5425a212020-08-04 14:58:35 +02001243 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001244 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001245 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001246 else
1247 TEST_EQUAL( status, expected_status );
1248
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001250
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001251 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001252 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001253 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001254 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001255 else
1256 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257
1258exit:
1259 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001260 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001261 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262}
1263/* END_CASE */
1264
1265/* BEGIN_CASE */
1266void cipher_key_policy( int policy_usage,
1267 int policy_alg,
1268 int key_type,
1269 data_t *key_data,
1270 int exercise_alg )
1271{
Ronald Cron5425a212020-08-04 14:58:35 +02001272 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001274 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275 psa_status_t status;
1276
Gilles Peskine8817f612018-12-18 00:18:46 +01001277 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001279 psa_set_key_usage_flags( &attributes, policy_usage );
1280 psa_set_key_algorithm( &attributes, policy_alg );
1281 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001282
Gilles Peskine049c7532019-05-15 20:22:09 +02001283 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001284 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001285
Ronald Cron5425a212020-08-04 14:58:35 +02001286 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 if( policy_alg == exercise_alg &&
1288 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001289 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001290 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001291 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292 psa_cipher_abort( &operation );
1293
Ronald Cron5425a212020-08-04 14:58:35 +02001294 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001295 if( policy_alg == exercise_alg &&
1296 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001297 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001298 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001299 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001300
1301exit:
1302 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001303 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001304 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001305}
1306/* END_CASE */
1307
1308/* BEGIN_CASE */
1309void aead_key_policy( int policy_usage,
1310 int policy_alg,
1311 int key_type,
1312 data_t *key_data,
1313 int nonce_length_arg,
1314 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001315 int exercise_alg,
1316 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317{
Ronald Cron5425a212020-08-04 14:58:35 +02001318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001321 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001322 unsigned char nonce[16] = {0};
1323 size_t nonce_length = nonce_length_arg;
1324 unsigned char tag[16];
1325 size_t tag_length = tag_length_arg;
1326 size_t output_length;
1327
1328 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1329 TEST_ASSERT( tag_length <= sizeof( tag ) );
1330
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_set_key_usage_flags( &attributes, policy_usage );
1334 psa_set_key_algorithm( &attributes, policy_alg );
1335 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Gilles Peskine049c7532019-05-15 20:22:09 +02001337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001338 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339
Ronald Cron5425a212020-08-04 14:58:35 +02001340 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341 nonce, nonce_length,
1342 NULL, 0,
1343 NULL, 0,
1344 tag, tag_length,
1345 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001346 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1347 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001349 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001350
1351 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001352 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353 nonce, nonce_length,
1354 NULL, 0,
1355 tag, tag_length,
1356 NULL, 0,
1357 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001358 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1359 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1360 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001361 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001362 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001363 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364
1365exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001366 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001367 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001368}
1369/* END_CASE */
1370
1371/* BEGIN_CASE */
1372void asymmetric_encryption_key_policy( int policy_usage,
1373 int policy_alg,
1374 int key_type,
1375 data_t *key_data,
1376 int exercise_alg )
1377{
Ronald Cron5425a212020-08-04 14:58:35 +02001378 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001380 psa_status_t status;
1381 size_t key_bits;
1382 size_t buffer_length;
1383 unsigned char *buffer = NULL;
1384 size_t output_length;
1385
Gilles Peskine8817f612018-12-18 00:18:46 +01001386 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001387
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001388 psa_set_key_usage_flags( &attributes, policy_usage );
1389 psa_set_key_algorithm( &attributes, policy_alg );
1390 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001391
Gilles Peskine049c7532019-05-15 20:22:09 +02001392 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001393 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001394
Ronald Cron5425a212020-08-04 14:58:35 +02001395 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001396 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001397 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1398 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001399 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001400
Ronald Cron5425a212020-08-04 14:58:35 +02001401 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001402 NULL, 0,
1403 NULL, 0,
1404 buffer, buffer_length,
1405 &output_length );
1406 if( policy_alg == exercise_alg &&
1407 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001408 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001409 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001410 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001411
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001412 if( buffer_length != 0 )
1413 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001414 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001415 buffer, buffer_length,
1416 NULL, 0,
1417 buffer, buffer_length,
1418 &output_length );
1419 if( policy_alg == exercise_alg &&
1420 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001421 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001422 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001423 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001424
1425exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001426 /*
1427 * Key attributes may have been returned by psa_get_key_attributes()
1428 * thus reset them as required.
1429 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001430 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001431
1432 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001433 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001434 mbedtls_free( buffer );
1435}
1436/* END_CASE */
1437
1438/* BEGIN_CASE */
1439void asymmetric_signature_key_policy( int policy_usage,
1440 int policy_alg,
1441 int key_type,
1442 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001443 int exercise_alg,
1444 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001445{
Ronald Cron5425a212020-08-04 14:58:35 +02001446 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001449 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1450 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1451 * compatible with the policy and `payload_length_arg` is supposed to be
1452 * a valid input length to sign. If `payload_length_arg <= 0`,
1453 * `exercise_alg` is supposed to be forbidden by the policy. */
1454 int compatible_alg = payload_length_arg > 0;
1455 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001456 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001457 size_t signature_length;
1458
Gilles Peskine8817f612018-12-18 00:18:46 +01001459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001460
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001461 psa_set_key_usage_flags( &attributes, policy_usage );
1462 psa_set_key_algorithm( &attributes, policy_alg );
1463 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464
Gilles Peskine049c7532019-05-15 20:22:09 +02001465 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001466 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467
Ronald Cron5425a212020-08-04 14:58:35 +02001468 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001469 payload, payload_length,
1470 signature, sizeof( signature ),
1471 &signature_length );
1472 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001473 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001474 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001475 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476
1477 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001478 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001479 payload, payload_length,
1480 signature, sizeof( signature ) );
1481 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001482 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001483 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001484 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001485
1486exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001487 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001488 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001489}
1490/* END_CASE */
1491
Janos Follathba3fab92019-06-11 14:50:16 +01001492/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001493void derive_key_policy( int policy_usage,
1494 int policy_alg,
1495 int key_type,
1496 data_t *key_data,
1497 int exercise_alg )
1498{
Ronald Cron5425a212020-08-04 14:58:35 +02001499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001501 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001502 psa_status_t status;
1503
Gilles Peskine8817f612018-12-18 00:18:46 +01001504 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001505
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001506 psa_set_key_usage_flags( &attributes, policy_usage );
1507 psa_set_key_algorithm( &attributes, policy_alg );
1508 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001509
Gilles Peskine049c7532019-05-15 20:22:09 +02001510 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001511 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001512
Janos Follathba3fab92019-06-11 14:50:16 +01001513 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1514
1515 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1516 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001517 {
Janos Follathba3fab92019-06-11 14:50:16 +01001518 PSA_ASSERT( psa_key_derivation_input_bytes(
1519 &operation,
1520 PSA_KEY_DERIVATION_INPUT_SEED,
1521 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001522 }
Janos Follathba3fab92019-06-11 14:50:16 +01001523
1524 status = psa_key_derivation_input_key( &operation,
1525 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001526 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001527
Gilles Peskineea0fb492018-07-12 17:17:20 +02001528 if( policy_alg == exercise_alg &&
1529 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001530 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001531 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001532 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001533
1534exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001535 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001536 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001537 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001538}
1539/* END_CASE */
1540
1541/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001542void agreement_key_policy( int policy_usage,
1543 int policy_alg,
1544 int key_type_arg,
1545 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001546 int exercise_alg,
1547 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001548{
Ronald Cron5425a212020-08-04 14:58:35 +02001549 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001551 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001552 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001553 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001554 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001555
Gilles Peskine8817f612018-12-18 00:18:46 +01001556 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001557
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001558 psa_set_key_usage_flags( &attributes, policy_usage );
1559 psa_set_key_algorithm( &attributes, policy_alg );
1560 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001561
Gilles Peskine049c7532019-05-15 20:22:09 +02001562 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001563 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001564
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001565 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001566 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001567
Steven Cooremance48e852020-10-05 16:02:45 +02001568 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001569
1570exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001571 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001572 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001573 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001574}
1575/* END_CASE */
1576
1577/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001578void key_policy_alg2( int key_type_arg, data_t *key_data,
1579 int usage_arg, int alg_arg, int alg2_arg )
1580{
Ronald Cron5425a212020-08-04 14:58:35 +02001581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001582 psa_key_type_t key_type = key_type_arg;
1583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1584 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1585 psa_key_usage_t usage = usage_arg;
1586 psa_algorithm_t alg = alg_arg;
1587 psa_algorithm_t alg2 = alg2_arg;
1588
1589 PSA_ASSERT( psa_crypto_init( ) );
1590
1591 psa_set_key_usage_flags( &attributes, usage );
1592 psa_set_key_algorithm( &attributes, alg );
1593 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1594 psa_set_key_type( &attributes, key_type );
1595 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001596 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001597
Ronald Cron5425a212020-08-04 14:58:35 +02001598 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001599 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1600 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1601 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1602
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001603 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001604 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001605 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001606 goto exit;
1607
1608exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001609 /*
1610 * Key attributes may have been returned by psa_get_key_attributes()
1611 * thus reset them as required.
1612 */
1613 psa_reset_key_attributes( &got_attributes );
1614
Ronald Cron5425a212020-08-04 14:58:35 +02001615 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001616 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001617}
1618/* END_CASE */
1619
1620/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001621void raw_agreement_key_policy( int policy_usage,
1622 int policy_alg,
1623 int key_type_arg,
1624 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001625 int exercise_alg,
1626 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001627{
Ronald Cron5425a212020-08-04 14:58:35 +02001628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001629 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001630 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001631 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001632 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001633 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001634
1635 PSA_ASSERT( psa_crypto_init( ) );
1636
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001637 psa_set_key_usage_flags( &attributes, policy_usage );
1638 psa_set_key_algorithm( &attributes, policy_alg );
1639 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001640
Gilles Peskine049c7532019-05-15 20:22:09 +02001641 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001642 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001643
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001644 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001645
Steven Cooremance48e852020-10-05 16:02:45 +02001646 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001647
1648exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001649 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001650 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001651 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001652}
1653/* END_CASE */
1654
1655/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001656void copy_success( int source_usage_arg,
1657 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001658 int type_arg, data_t *material,
1659 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001660 int target_usage_arg,
1661 int target_alg_arg, int target_alg2_arg,
1662 int expected_usage_arg,
1663 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001664{
Gilles Peskineca25db92019-04-19 11:43:08 +02001665 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1666 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001667 psa_key_usage_t expected_usage = expected_usage_arg;
1668 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001669 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001670 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1671 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001672 uint8_t *export_buffer = NULL;
1673
Gilles Peskine57ab7212019-01-28 13:03:09 +01001674 PSA_ASSERT( psa_crypto_init( ) );
1675
Gilles Peskineca25db92019-04-19 11:43:08 +02001676 /* Prepare the source key. */
1677 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1678 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001679 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001680 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001681 PSA_ASSERT( psa_import_key( &source_attributes,
1682 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001683 &source_key ) );
1684 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001685
Gilles Peskineca25db92019-04-19 11:43:08 +02001686 /* Prepare the target attributes. */
1687 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001688 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001689 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001690 /* Set volatile lifetime to reset the key identifier to 0. */
1691 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1692 }
1693
Gilles Peskineca25db92019-04-19 11:43:08 +02001694 if( target_usage_arg != -1 )
1695 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1696 if( target_alg_arg != -1 )
1697 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001698 if( target_alg2_arg != -1 )
1699 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001700
1701 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001702 PSA_ASSERT( psa_copy_key( source_key,
1703 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001704
1705 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001706 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001707
1708 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001709 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001710 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1711 psa_get_key_type( &target_attributes ) );
1712 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1713 psa_get_key_bits( &target_attributes ) );
1714 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1715 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001716 TEST_EQUAL( expected_alg2,
1717 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001718 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1719 {
1720 size_t length;
1721 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001722 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001723 material->len, &length ) );
1724 ASSERT_COMPARE( material->x, material->len,
1725 export_buffer, length );
1726 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001727
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001728 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001729 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001730 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001731 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001732
Ronald Cron5425a212020-08-04 14:58:35 +02001733 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001734
1735exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001736 /*
1737 * Source and target key attributes may have been returned by
1738 * psa_get_key_attributes() thus reset them as required.
1739 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001740 psa_reset_key_attributes( &source_attributes );
1741 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001742
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001743 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001744 mbedtls_free( export_buffer );
1745}
1746/* END_CASE */
1747
1748/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001749void copy_fail( int source_usage_arg,
1750 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001751 int type_arg, data_t *material,
1752 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001753 int target_usage_arg,
1754 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001755 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001756 int expected_status_arg )
1757{
1758 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1759 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001760 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1761 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001762 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001763
1764 PSA_ASSERT( psa_crypto_init( ) );
1765
1766 /* Prepare the source key. */
1767 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1768 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001769 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001770 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001771 PSA_ASSERT( psa_import_key( &source_attributes,
1772 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001773 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001774
1775 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001776 psa_set_key_id( &target_attributes, key_id );
1777 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001778 psa_set_key_type( &target_attributes, target_type_arg );
1779 psa_set_key_bits( &target_attributes, target_bits_arg );
1780 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1781 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001782 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001783
1784 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001785 TEST_EQUAL( psa_copy_key( source_key,
1786 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001787 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001788
Ronald Cron5425a212020-08-04 14:58:35 +02001789 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001790
Gilles Peskine4a644642019-05-03 17:14:08 +02001791exit:
1792 psa_reset_key_attributes( &source_attributes );
1793 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001794 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001795}
1796/* END_CASE */
1797
1798/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001799void hash_operation_init( )
1800{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001801 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001802 /* Test each valid way of initializing the object, except for `= {0}`, as
1803 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1804 * though it's OK by the C standard. We could test for this, but we'd need
1805 * to supress the Clang warning for the test. */
1806 psa_hash_operation_t func = psa_hash_operation_init( );
1807 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1808 psa_hash_operation_t zero;
1809
1810 memset( &zero, 0, sizeof( zero ) );
1811
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001812 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001813 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1814 PSA_ERROR_BAD_STATE );
1815 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1816 PSA_ERROR_BAD_STATE );
1817 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1818 PSA_ERROR_BAD_STATE );
1819
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001820 /* A default hash operation should be abortable without error. */
1821 PSA_ASSERT( psa_hash_abort( &func ) );
1822 PSA_ASSERT( psa_hash_abort( &init ) );
1823 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001824}
1825/* END_CASE */
1826
1827/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001828void hash_setup( int alg_arg,
1829 int expected_status_arg )
1830{
1831 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001832 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001833 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001834 psa_status_t status;
1835
Gilles Peskine8817f612018-12-18 00:18:46 +01001836 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001837
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001838 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001839 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001840
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001841 /* Whether setup succeeded or failed, abort must succeed. */
1842 PSA_ASSERT( psa_hash_abort( &operation ) );
1843
1844 /* If setup failed, reproduce the failure, so as to
1845 * test the resulting state of the operation object. */
1846 if( status != PSA_SUCCESS )
1847 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1848
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001849 /* Now the operation object should be reusable. */
1850#if defined(KNOWN_SUPPORTED_HASH_ALG)
1851 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1852 PSA_ASSERT( psa_hash_abort( &operation ) );
1853#endif
1854
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001855exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001856 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001857}
1858/* END_CASE */
1859
1860/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001861void hash_compute_fail( int alg_arg, data_t *input,
1862 int output_size_arg, int expected_status_arg )
1863{
1864 psa_algorithm_t alg = alg_arg;
1865 uint8_t *output = NULL;
1866 size_t output_size = output_size_arg;
1867 size_t output_length = INVALID_EXPORT_LENGTH;
1868 psa_status_t expected_status = expected_status_arg;
1869 psa_status_t status;
1870
1871 ASSERT_ALLOC( output, output_size );
1872
1873 PSA_ASSERT( psa_crypto_init( ) );
1874
1875 status = psa_hash_compute( alg, input->x, input->len,
1876 output, output_size, &output_length );
1877 TEST_EQUAL( status, expected_status );
1878 TEST_ASSERT( output_length <= output_size );
1879
1880exit:
1881 mbedtls_free( output );
1882 PSA_DONE( );
1883}
1884/* END_CASE */
1885
1886/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001887void hash_compare_fail( int alg_arg, data_t *input,
1888 data_t *reference_hash,
1889 int expected_status_arg )
1890{
1891 psa_algorithm_t alg = alg_arg;
1892 psa_status_t expected_status = expected_status_arg;
1893 psa_status_t status;
1894
1895 PSA_ASSERT( psa_crypto_init( ) );
1896
1897 status = psa_hash_compare( alg, input->x, input->len,
1898 reference_hash->x, reference_hash->len );
1899 TEST_EQUAL( status, expected_status );
1900
1901exit:
1902 PSA_DONE( );
1903}
1904/* END_CASE */
1905
1906/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001907void hash_compute_compare( int alg_arg, data_t *input,
1908 data_t *expected_output )
1909{
1910 psa_algorithm_t alg = alg_arg;
1911 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1912 size_t output_length = INVALID_EXPORT_LENGTH;
1913 size_t i;
1914
1915 PSA_ASSERT( psa_crypto_init( ) );
1916
1917 /* Compute with tight buffer */
1918 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001919 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001920 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001921 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001922 ASSERT_COMPARE( output, output_length,
1923 expected_output->x, expected_output->len );
1924
1925 /* Compute with larger buffer */
1926 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1927 output, sizeof( output ),
1928 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001929 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001930 ASSERT_COMPARE( output, output_length,
1931 expected_output->x, expected_output->len );
1932
1933 /* Compare with correct hash */
1934 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1935 output, output_length ) );
1936
1937 /* Compare with trailing garbage */
1938 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1939 output, output_length + 1 ),
1940 PSA_ERROR_INVALID_SIGNATURE );
1941
1942 /* Compare with truncated hash */
1943 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1944 output, output_length - 1 ),
1945 PSA_ERROR_INVALID_SIGNATURE );
1946
1947 /* Compare with corrupted value */
1948 for( i = 0; i < output_length; i++ )
1949 {
Chris Jones9634bb12021-01-20 15:56:42 +00001950 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001951 output[i] ^= 1;
1952 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1953 output, output_length ),
1954 PSA_ERROR_INVALID_SIGNATURE );
1955 output[i] ^= 1;
1956 }
1957
1958exit:
1959 PSA_DONE( );
1960}
1961/* END_CASE */
1962
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001963/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001964void hash_bad_order( )
1965{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001966 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001967 unsigned char input[] = "";
1968 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001969 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001970 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1971 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1972 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001973 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001974 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001975 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001976
Gilles Peskine8817f612018-12-18 00:18:46 +01001977 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001978
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001979 /* Call setup twice in a row. */
1980 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1981 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1982 PSA_ERROR_BAD_STATE );
1983 PSA_ASSERT( psa_hash_abort( &operation ) );
1984
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001985 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001986 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001987 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001988 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001989
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001990 /* Call update after finish. */
1991 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1992 PSA_ASSERT( psa_hash_finish( &operation,
1993 hash, sizeof( hash ), &hash_len ) );
1994 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001995 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001996 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001997
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001998 /* Call verify without calling setup beforehand. */
1999 TEST_EQUAL( psa_hash_verify( &operation,
2000 valid_hash, sizeof( valid_hash ) ),
2001 PSA_ERROR_BAD_STATE );
2002 PSA_ASSERT( psa_hash_abort( &operation ) );
2003
2004 /* Call verify after finish. */
2005 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2006 PSA_ASSERT( psa_hash_finish( &operation,
2007 hash, sizeof( hash ), &hash_len ) );
2008 TEST_EQUAL( psa_hash_verify( &operation,
2009 valid_hash, sizeof( valid_hash ) ),
2010 PSA_ERROR_BAD_STATE );
2011 PSA_ASSERT( psa_hash_abort( &operation ) );
2012
2013 /* Call verify twice in a row. */
2014 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2015 PSA_ASSERT( psa_hash_verify( &operation,
2016 valid_hash, sizeof( valid_hash ) ) );
2017 TEST_EQUAL( psa_hash_verify( &operation,
2018 valid_hash, sizeof( valid_hash ) ),
2019 PSA_ERROR_BAD_STATE );
2020 PSA_ASSERT( psa_hash_abort( &operation ) );
2021
2022 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002023 TEST_EQUAL( psa_hash_finish( &operation,
2024 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002025 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002026 PSA_ASSERT( psa_hash_abort( &operation ) );
2027
2028 /* Call finish twice in a row. */
2029 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2030 PSA_ASSERT( psa_hash_finish( &operation,
2031 hash, sizeof( hash ), &hash_len ) );
2032 TEST_EQUAL( psa_hash_finish( &operation,
2033 hash, sizeof( hash ), &hash_len ),
2034 PSA_ERROR_BAD_STATE );
2035 PSA_ASSERT( psa_hash_abort( &operation ) );
2036
2037 /* Call finish after calling verify. */
2038 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2039 PSA_ASSERT( psa_hash_verify( &operation,
2040 valid_hash, sizeof( valid_hash ) ) );
2041 TEST_EQUAL( psa_hash_finish( &operation,
2042 hash, sizeof( hash ), &hash_len ),
2043 PSA_ERROR_BAD_STATE );
2044 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002045
2046exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002047 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002048}
2049/* END_CASE */
2050
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002051/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002052void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002053{
2054 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002055 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2056 * appended to it */
2057 unsigned char hash[] = {
2058 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2059 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2060 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002061 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002062 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002063
Gilles Peskine8817f612018-12-18 00:18:46 +01002064 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002065
itayzafrir27e69452018-11-01 14:26:34 +02002066 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002067 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002068 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002069 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002070
itayzafrir27e69452018-11-01 14:26:34 +02002071 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002072 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002073 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002074 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002075
itayzafrir27e69452018-11-01 14:26:34 +02002076 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002077 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002078 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002079 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002080
itayzafrirec93d302018-10-18 18:01:10 +03002081exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002082 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002083}
2084/* END_CASE */
2085
Ronald Cronee414c72021-03-18 18:50:08 +01002086/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002087void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002088{
2089 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002090 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002091 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002092 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002093 size_t hash_len;
2094
Gilles Peskine8817f612018-12-18 00:18:46 +01002095 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002096
itayzafrir58028322018-10-25 10:22:01 +03002097 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002099 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002100 hash, expected_size - 1, &hash_len ),
2101 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002102
2103exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002104 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002105}
2106/* END_CASE */
2107
Ronald Cronee414c72021-03-18 18:50:08 +01002108/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002109void hash_clone_source_state( )
2110{
2111 psa_algorithm_t alg = PSA_ALG_SHA_256;
2112 unsigned char hash[PSA_HASH_MAX_SIZE];
2113 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2114 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2115 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2116 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2117 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2118 size_t hash_len;
2119
2120 PSA_ASSERT( psa_crypto_init( ) );
2121 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2122
2123 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2124 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2125 PSA_ASSERT( psa_hash_finish( &op_finished,
2126 hash, sizeof( hash ), &hash_len ) );
2127 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2128 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2129
2130 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2131 PSA_ERROR_BAD_STATE );
2132
2133 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2134 PSA_ASSERT( psa_hash_finish( &op_init,
2135 hash, sizeof( hash ), &hash_len ) );
2136 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2137 PSA_ASSERT( psa_hash_finish( &op_finished,
2138 hash, sizeof( hash ), &hash_len ) );
2139 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2140 PSA_ASSERT( psa_hash_finish( &op_aborted,
2141 hash, sizeof( hash ), &hash_len ) );
2142
2143exit:
2144 psa_hash_abort( &op_source );
2145 psa_hash_abort( &op_init );
2146 psa_hash_abort( &op_setup );
2147 psa_hash_abort( &op_finished );
2148 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002149 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002150}
2151/* END_CASE */
2152
Ronald Cronee414c72021-03-18 18:50:08 +01002153/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002154void hash_clone_target_state( )
2155{
2156 psa_algorithm_t alg = PSA_ALG_SHA_256;
2157 unsigned char hash[PSA_HASH_MAX_SIZE];
2158 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2159 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2160 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2161 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2162 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2163 size_t hash_len;
2164
2165 PSA_ASSERT( psa_crypto_init( ) );
2166
2167 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2168 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2169 PSA_ASSERT( psa_hash_finish( &op_finished,
2170 hash, sizeof( hash ), &hash_len ) );
2171 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2172 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2173
2174 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2175 PSA_ASSERT( psa_hash_finish( &op_target,
2176 hash, sizeof( hash ), &hash_len ) );
2177
2178 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2179 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2180 PSA_ERROR_BAD_STATE );
2181 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2182 PSA_ERROR_BAD_STATE );
2183
2184exit:
2185 psa_hash_abort( &op_target );
2186 psa_hash_abort( &op_init );
2187 psa_hash_abort( &op_setup );
2188 psa_hash_abort( &op_finished );
2189 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002190 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002191}
2192/* END_CASE */
2193
itayzafrir58028322018-10-25 10:22:01 +03002194/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002195void mac_operation_init( )
2196{
Jaeden Amero252ef282019-02-15 14:05:35 +00002197 const uint8_t input[1] = { 0 };
2198
Jaeden Amero769ce272019-01-04 11:48:03 +00002199 /* Test each valid way of initializing the object, except for `= {0}`, as
2200 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2201 * though it's OK by the C standard. We could test for this, but we'd need
2202 * to supress the Clang warning for the test. */
2203 psa_mac_operation_t func = psa_mac_operation_init( );
2204 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2205 psa_mac_operation_t zero;
2206
2207 memset( &zero, 0, sizeof( zero ) );
2208
Jaeden Amero252ef282019-02-15 14:05:35 +00002209 /* A freshly-initialized MAC operation should not be usable. */
2210 TEST_EQUAL( psa_mac_update( &func,
2211 input, sizeof( input ) ),
2212 PSA_ERROR_BAD_STATE );
2213 TEST_EQUAL( psa_mac_update( &init,
2214 input, sizeof( input ) ),
2215 PSA_ERROR_BAD_STATE );
2216 TEST_EQUAL( psa_mac_update( &zero,
2217 input, sizeof( input ) ),
2218 PSA_ERROR_BAD_STATE );
2219
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002220 /* A default MAC operation should be abortable without error. */
2221 PSA_ASSERT( psa_mac_abort( &func ) );
2222 PSA_ASSERT( psa_mac_abort( &init ) );
2223 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002224}
2225/* END_CASE */
2226
2227/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002228void mac_setup( int key_type_arg,
2229 data_t *key,
2230 int alg_arg,
2231 int expected_status_arg )
2232{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002233 psa_key_type_t key_type = key_type_arg;
2234 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002235 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002236 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002237 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2238#if defined(KNOWN_SUPPORTED_MAC_ALG)
2239 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2240#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241
Gilles Peskine8817f612018-12-18 00:18:46 +01002242 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002243
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002244 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2245 &operation, &status ) )
2246 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002247 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002249 /* The operation object should be reusable. */
2250#if defined(KNOWN_SUPPORTED_MAC_ALG)
2251 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2252 smoke_test_key_data,
2253 sizeof( smoke_test_key_data ),
2254 KNOWN_SUPPORTED_MAC_ALG,
2255 &operation, &status ) )
2256 goto exit;
2257 TEST_EQUAL( status, PSA_SUCCESS );
2258#endif
2259
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002260exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002261 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002262}
2263/* END_CASE */
2264
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002265/* 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 +00002266void mac_bad_order( )
2267{
Ronald Cron5425a212020-08-04 14:58:35 +02002268 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002269 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2270 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002271 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002272 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2273 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2274 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002276 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2277 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2278 size_t sign_mac_length = 0;
2279 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2280 const uint8_t verify_mac[] = {
2281 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2282 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2283 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2284
2285 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002287 psa_set_key_algorithm( &attributes, alg );
2288 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002289
Ronald Cron5425a212020-08-04 14:58:35 +02002290 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2291 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002292
Jaeden Amero252ef282019-02-15 14:05:35 +00002293 /* Call update without calling setup beforehand. */
2294 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2295 PSA_ERROR_BAD_STATE );
2296 PSA_ASSERT( psa_mac_abort( &operation ) );
2297
2298 /* Call sign finish without calling setup beforehand. */
2299 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2300 &sign_mac_length),
2301 PSA_ERROR_BAD_STATE );
2302 PSA_ASSERT( psa_mac_abort( &operation ) );
2303
2304 /* Call verify finish without calling setup beforehand. */
2305 TEST_EQUAL( psa_mac_verify_finish( &operation,
2306 verify_mac, sizeof( verify_mac ) ),
2307 PSA_ERROR_BAD_STATE );
2308 PSA_ASSERT( psa_mac_abort( &operation ) );
2309
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002310 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002311 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2312 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002313 PSA_ERROR_BAD_STATE );
2314 PSA_ASSERT( psa_mac_abort( &operation ) );
2315
Jaeden Amero252ef282019-02-15 14:05:35 +00002316 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002317 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002318 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2319 PSA_ASSERT( psa_mac_sign_finish( &operation,
2320 sign_mac, sizeof( sign_mac ),
2321 &sign_mac_length ) );
2322 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2323 PSA_ERROR_BAD_STATE );
2324 PSA_ASSERT( psa_mac_abort( &operation ) );
2325
2326 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002327 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002328 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2329 PSA_ASSERT( psa_mac_verify_finish( &operation,
2330 verify_mac, sizeof( verify_mac ) ) );
2331 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2332 PSA_ERROR_BAD_STATE );
2333 PSA_ASSERT( psa_mac_abort( &operation ) );
2334
2335 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002336 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002337 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2338 PSA_ASSERT( psa_mac_sign_finish( &operation,
2339 sign_mac, sizeof( sign_mac ),
2340 &sign_mac_length ) );
2341 TEST_EQUAL( psa_mac_sign_finish( &operation,
2342 sign_mac, sizeof( sign_mac ),
2343 &sign_mac_length ),
2344 PSA_ERROR_BAD_STATE );
2345 PSA_ASSERT( psa_mac_abort( &operation ) );
2346
2347 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002348 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002349 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2350 PSA_ASSERT( psa_mac_verify_finish( &operation,
2351 verify_mac, sizeof( verify_mac ) ) );
2352 TEST_EQUAL( psa_mac_verify_finish( &operation,
2353 verify_mac, sizeof( verify_mac ) ),
2354 PSA_ERROR_BAD_STATE );
2355 PSA_ASSERT( psa_mac_abort( &operation ) );
2356
2357 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002359 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2360 TEST_EQUAL( psa_mac_verify_finish( &operation,
2361 verify_mac, sizeof( verify_mac ) ),
2362 PSA_ERROR_BAD_STATE );
2363 PSA_ASSERT( psa_mac_abort( &operation ) );
2364
2365 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002366 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002367 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2368 TEST_EQUAL( psa_mac_sign_finish( &operation,
2369 sign_mac, sizeof( sign_mac ),
2370 &sign_mac_length ),
2371 PSA_ERROR_BAD_STATE );
2372 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002373
Ronald Cron5425a212020-08-04 14:58:35 +02002374 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002375
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002376exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002377 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002378}
2379/* END_CASE */
2380
2381/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002382void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002383 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002384 int alg_arg,
2385 data_t *input,
2386 data_t *expected_mac )
2387{
Ronald Cron5425a212020-08-04 14:58:35 +02002388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002389 psa_key_type_t key_type = key_type_arg;
2390 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002391 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002393 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002394 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002395 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002396 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002397 const size_t output_sizes_to_test[] = {
2398 0,
2399 1,
2400 expected_mac->len - 1,
2401 expected_mac->len,
2402 expected_mac->len + 1,
2403 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002404
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002405 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002406 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002407 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002408
Gilles Peskine8817f612018-12-18 00:18:46 +01002409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002410
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002412 psa_set_key_algorithm( &attributes, alg );
2413 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002414
Ronald Cron5425a212020-08-04 14:58:35 +02002415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2416 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002417
Gilles Peskine8b356b52020-08-25 23:44:59 +02002418 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2419 {
2420 const size_t output_size = output_sizes_to_test[i];
2421 psa_status_t expected_status =
2422 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2423 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002424
Chris Jones9634bb12021-01-20 15:56:42 +00002425 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002426 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002427
Gilles Peskine8b356b52020-08-25 23:44:59 +02002428 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002429 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002430 PSA_ASSERT( psa_mac_update( &operation,
2431 input->x, input->len ) );
2432 TEST_EQUAL( psa_mac_sign_finish( &operation,
2433 actual_mac, output_size,
2434 &mac_length ),
2435 expected_status );
2436 PSA_ASSERT( psa_mac_abort( &operation ) );
2437
2438 if( expected_status == PSA_SUCCESS )
2439 {
2440 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2441 actual_mac, mac_length );
2442 }
2443 mbedtls_free( actual_mac );
2444 actual_mac = NULL;
2445 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002446
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002447exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002448 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002450 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002451 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002452}
2453/* END_CASE */
2454
2455/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002456void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002457 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002458 int alg_arg,
2459 data_t *input,
2460 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461{
Ronald Cron5425a212020-08-04 14:58:35 +02002462 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463 psa_key_type_t key_type = key_type_arg;
2464 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002465 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002467 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002468
Gilles Peskine69c12672018-06-28 00:07:19 +02002469 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2470
Gilles Peskine8817f612018-12-18 00:18:46 +01002471 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002473 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002474 psa_set_key_algorithm( &attributes, alg );
2475 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002476
Ronald Cron5425a212020-08-04 14:58:35 +02002477 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2478 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002479
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002480 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002481 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002482 PSA_ASSERT( psa_mac_update( &operation,
2483 input->x, input->len ) );
2484 PSA_ASSERT( psa_mac_verify_finish( &operation,
2485 expected_mac->x,
2486 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002488 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002489 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002490 PSA_ASSERT( psa_mac_update( &operation,
2491 input->x, input->len ) );
2492 TEST_EQUAL( psa_mac_verify_finish( &operation,
2493 expected_mac->x,
2494 expected_mac->len - 1 ),
2495 PSA_ERROR_INVALID_SIGNATURE );
2496
2497 /* Test a MAC that's too long. */
2498 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2499 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002500 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002501 PSA_ASSERT( psa_mac_update( &operation,
2502 input->x, input->len ) );
2503 TEST_EQUAL( psa_mac_verify_finish( &operation,
2504 perturbed_mac,
2505 expected_mac->len + 1 ),
2506 PSA_ERROR_INVALID_SIGNATURE );
2507
2508 /* Test changing one byte. */
2509 for( size_t i = 0; i < expected_mac->len; i++ )
2510 {
Chris Jones9634bb12021-01-20 15:56:42 +00002511 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002512 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002513 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002514 PSA_ASSERT( psa_mac_update( &operation,
2515 input->x, input->len ) );
2516 TEST_EQUAL( psa_mac_verify_finish( &operation,
2517 perturbed_mac,
2518 expected_mac->len ),
2519 PSA_ERROR_INVALID_SIGNATURE );
2520 perturbed_mac[i] ^= 1;
2521 }
2522
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002524 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002525 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002526 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002527 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528}
2529/* END_CASE */
2530
2531/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002532void cipher_operation_init( )
2533{
Jaeden Ameroab439972019-02-15 14:12:05 +00002534 const uint8_t input[1] = { 0 };
2535 unsigned char output[1] = { 0 };
2536 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002537 /* Test each valid way of initializing the object, except for `= {0}`, as
2538 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2539 * though it's OK by the C standard. We could test for this, but we'd need
2540 * to supress the Clang warning for the test. */
2541 psa_cipher_operation_t func = psa_cipher_operation_init( );
2542 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2543 psa_cipher_operation_t zero;
2544
2545 memset( &zero, 0, sizeof( zero ) );
2546
Jaeden Ameroab439972019-02-15 14:12:05 +00002547 /* A freshly-initialized cipher operation should not be usable. */
2548 TEST_EQUAL( psa_cipher_update( &func,
2549 input, sizeof( input ),
2550 output, sizeof( output ),
2551 &output_length ),
2552 PSA_ERROR_BAD_STATE );
2553 TEST_EQUAL( psa_cipher_update( &init,
2554 input, sizeof( input ),
2555 output, sizeof( output ),
2556 &output_length ),
2557 PSA_ERROR_BAD_STATE );
2558 TEST_EQUAL( psa_cipher_update( &zero,
2559 input, sizeof( input ),
2560 output, sizeof( output ),
2561 &output_length ),
2562 PSA_ERROR_BAD_STATE );
2563
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002564 /* A default cipher operation should be abortable without error. */
2565 PSA_ASSERT( psa_cipher_abort( &func ) );
2566 PSA_ASSERT( psa_cipher_abort( &init ) );
2567 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002568}
2569/* END_CASE */
2570
2571/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002572void cipher_setup( int key_type_arg,
2573 data_t *key,
2574 int alg_arg,
2575 int expected_status_arg )
2576{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002577 psa_key_type_t key_type = key_type_arg;
2578 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002579 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002580 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002582#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002583 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2584#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585
Gilles Peskine8817f612018-12-18 00:18:46 +01002586 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002587
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002588 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2589 &operation, &status ) )
2590 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002591 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002592
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002593 /* The operation object should be reusable. */
2594#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2595 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2596 smoke_test_key_data,
2597 sizeof( smoke_test_key_data ),
2598 KNOWN_SUPPORTED_CIPHER_ALG,
2599 &operation, &status ) )
2600 goto exit;
2601 TEST_EQUAL( status, PSA_SUCCESS );
2602#endif
2603
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002604exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002605 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002606 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607}
2608/* END_CASE */
2609
Ronald Cronee414c72021-03-18 18:50:08 +01002610/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002611void cipher_bad_order( )
2612{
Ronald Cron5425a212020-08-04 14:58:35 +02002613 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002614 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2615 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002617 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002618 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002619 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002620 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2621 0xaa, 0xaa, 0xaa, 0xaa };
2622 const uint8_t text[] = {
2623 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2624 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002625 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002626 size_t length = 0;
2627
2628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002629 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2630 psa_set_key_algorithm( &attributes, alg );
2631 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002632 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2633 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002634
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002635 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002636 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2637 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002638 PSA_ERROR_BAD_STATE );
2639 PSA_ASSERT( psa_cipher_abort( &operation ) );
2640
2641 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002642 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2643 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002644 PSA_ERROR_BAD_STATE );
2645 PSA_ASSERT( psa_cipher_abort( &operation ) );
2646
Jaeden Ameroab439972019-02-15 14:12:05 +00002647 /* Generate an IV without calling setup beforehand. */
2648 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2649 buffer, sizeof( buffer ),
2650 &length ),
2651 PSA_ERROR_BAD_STATE );
2652 PSA_ASSERT( psa_cipher_abort( &operation ) );
2653
2654 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002655 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002656 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2657 buffer, sizeof( buffer ),
2658 &length ) );
2659 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2660 buffer, sizeof( buffer ),
2661 &length ),
2662 PSA_ERROR_BAD_STATE );
2663 PSA_ASSERT( psa_cipher_abort( &operation ) );
2664
2665 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002666 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002667 PSA_ASSERT( psa_cipher_set_iv( &operation,
2668 iv, sizeof( iv ) ) );
2669 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2670 buffer, sizeof( buffer ),
2671 &length ),
2672 PSA_ERROR_BAD_STATE );
2673 PSA_ASSERT( psa_cipher_abort( &operation ) );
2674
2675 /* Set an IV without calling setup beforehand. */
2676 TEST_EQUAL( psa_cipher_set_iv( &operation,
2677 iv, sizeof( iv ) ),
2678 PSA_ERROR_BAD_STATE );
2679 PSA_ASSERT( psa_cipher_abort( &operation ) );
2680
2681 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002682 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002683 PSA_ASSERT( psa_cipher_set_iv( &operation,
2684 iv, sizeof( iv ) ) );
2685 TEST_EQUAL( psa_cipher_set_iv( &operation,
2686 iv, sizeof( iv ) ),
2687 PSA_ERROR_BAD_STATE );
2688 PSA_ASSERT( psa_cipher_abort( &operation ) );
2689
2690 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002691 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002692 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2693 buffer, sizeof( buffer ),
2694 &length ) );
2695 TEST_EQUAL( psa_cipher_set_iv( &operation,
2696 iv, sizeof( iv ) ),
2697 PSA_ERROR_BAD_STATE );
2698 PSA_ASSERT( psa_cipher_abort( &operation ) );
2699
2700 /* Call update without calling setup beforehand. */
2701 TEST_EQUAL( psa_cipher_update( &operation,
2702 text, sizeof( text ),
2703 buffer, sizeof( buffer ),
2704 &length ),
2705 PSA_ERROR_BAD_STATE );
2706 PSA_ASSERT( psa_cipher_abort( &operation ) );
2707
2708 /* Call update without an IV where an IV is required. */
2709 TEST_EQUAL( psa_cipher_update( &operation,
2710 text, sizeof( text ),
2711 buffer, sizeof( buffer ),
2712 &length ),
2713 PSA_ERROR_BAD_STATE );
2714 PSA_ASSERT( psa_cipher_abort( &operation ) );
2715
2716 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002717 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002718 PSA_ASSERT( psa_cipher_set_iv( &operation,
2719 iv, sizeof( iv ) ) );
2720 PSA_ASSERT( psa_cipher_finish( &operation,
2721 buffer, sizeof( buffer ), &length ) );
2722 TEST_EQUAL( psa_cipher_update( &operation,
2723 text, sizeof( text ),
2724 buffer, sizeof( buffer ),
2725 &length ),
2726 PSA_ERROR_BAD_STATE );
2727 PSA_ASSERT( psa_cipher_abort( &operation ) );
2728
2729 /* Call finish without calling setup beforehand. */
2730 TEST_EQUAL( psa_cipher_finish( &operation,
2731 buffer, sizeof( buffer ), &length ),
2732 PSA_ERROR_BAD_STATE );
2733 PSA_ASSERT( psa_cipher_abort( &operation ) );
2734
2735 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002736 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002737 /* Not calling update means we are encrypting an empty buffer, which is OK
2738 * for cipher modes with padding. */
2739 TEST_EQUAL( psa_cipher_finish( &operation,
2740 buffer, sizeof( buffer ), &length ),
2741 PSA_ERROR_BAD_STATE );
2742 PSA_ASSERT( psa_cipher_abort( &operation ) );
2743
2744 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002745 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002746 PSA_ASSERT( psa_cipher_set_iv( &operation,
2747 iv, sizeof( iv ) ) );
2748 PSA_ASSERT( psa_cipher_finish( &operation,
2749 buffer, sizeof( buffer ), &length ) );
2750 TEST_EQUAL( psa_cipher_finish( &operation,
2751 buffer, sizeof( buffer ), &length ),
2752 PSA_ERROR_BAD_STATE );
2753 PSA_ASSERT( psa_cipher_abort( &operation ) );
2754
Ronald Cron5425a212020-08-04 14:58:35 +02002755 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002756
Jaeden Ameroab439972019-02-15 14:12:05 +00002757exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002758 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002759 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002760}
2761/* END_CASE */
2762
2763/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002764void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002765 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002766 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002767 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002768{
Ronald Cron5425a212020-08-04 14:58:35 +02002769 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770 psa_status_t status;
2771 psa_key_type_t key_type = key_type_arg;
2772 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002773 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002774 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775 size_t output_buffer_size = 0;
2776 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002777 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002778 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002780
Gilles Peskine8817f612018-12-18 00:18:46 +01002781 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002782
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002783 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2784 psa_set_key_algorithm( &attributes, alg );
2785 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002786
Ronald Cron5425a212020-08-04 14:58:35 +02002787 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2788 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789
Ronald Cron5425a212020-08-04 14:58:35 +02002790 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002792 if( iv->len > 0 )
2793 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002794 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002795 }
2796
gabor-mezei-armceface22021-01-21 12:26:17 +01002797 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2798 TEST_ASSERT( output_buffer_size <=
2799 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002800 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_cipher_update( &operation,
2803 input->x, input->len,
2804 output, output_buffer_size,
2805 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002806 TEST_ASSERT( function_output_length <=
2807 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2808 TEST_ASSERT( function_output_length <=
2809 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002810 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002811
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002813 ( output_buffer_size == 0 ? NULL :
2814 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002815 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002816 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002817 TEST_ASSERT( function_output_length <=
2818 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2819 TEST_ASSERT( function_output_length <=
2820 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002821 total_output_length += function_output_length;
2822
Gilles Peskinefe11b722018-12-18 00:24:04 +01002823 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002824 if( expected_status == PSA_SUCCESS )
2825 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002827 ASSERT_COMPARE( expected_output->x, expected_output->len,
2828 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002830
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002832 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002833 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002834 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002835 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836}
2837/* END_CASE */
2838
2839/* BEGIN_CASE */
2840void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002841 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002842 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002843 int first_part_size_arg,
2844 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002845 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846{
Ronald Cron5425a212020-08-04 14:58:35 +02002847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002848 psa_key_type_t key_type = key_type_arg;
2849 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 size_t first_part_size = first_part_size_arg;
2851 size_t output1_length = output1_length_arg;
2852 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002853 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002854 size_t output_buffer_size = 0;
2855 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002856 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002857 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002861
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2863 psa_set_key_algorithm( &attributes, alg );
2864 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002865
Ronald Cron5425a212020-08-04 14:58:35 +02002866 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2867 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868
Ronald Cron5425a212020-08-04 14:58:35 +02002869 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002870
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002871 if( iv->len > 0 )
2872 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002873 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002874 }
2875
gabor-mezei-armceface22021-01-21 12:26:17 +01002876 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2877 TEST_ASSERT( output_buffer_size <=
2878 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002879 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880
Gilles Peskinee0866522019-02-19 19:44:00 +01002881 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2883 output, output_buffer_size,
2884 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002885 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002886 TEST_ASSERT( function_output_length <=
2887 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2888 TEST_ASSERT( function_output_length <=
2889 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002890 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002891
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_cipher_update( &operation,
2893 input->x + first_part_size,
2894 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002895 ( output_buffer_size == 0 ? NULL :
2896 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002897 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002898 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002899 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002900 TEST_ASSERT( function_output_length <=
2901 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2902 alg,
2903 input->len - first_part_size ) );
2904 TEST_ASSERT( function_output_length <=
2905 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002906 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002907
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002909 ( output_buffer_size == 0 ? NULL :
2910 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002911 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002912 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002913 TEST_ASSERT( function_output_length <=
2914 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2915 TEST_ASSERT( function_output_length <=
2916 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002917 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002918 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002919
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002920 ASSERT_COMPARE( expected_output->x, expected_output->len,
2921 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922
2923exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002924 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002925 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002926 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002927 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928}
2929/* END_CASE */
2930
2931/* BEGIN_CASE */
2932void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002933 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002934 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002935 int first_part_size_arg,
2936 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002937 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002938{
Ronald Cron5425a212020-08-04 14:58:35 +02002939 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940 psa_key_type_t key_type = key_type_arg;
2941 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002942 size_t first_part_size = first_part_size_arg;
2943 size_t output1_length = output1_length_arg;
2944 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002945 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946 size_t output_buffer_size = 0;
2947 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002948 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002949 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951
Gilles Peskine8817f612018-12-18 00:18:46 +01002952 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002953
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2955 psa_set_key_algorithm( &attributes, alg );
2956 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002957
Ronald Cron5425a212020-08-04 14:58:35 +02002958 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2959 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960
Ronald Cron5425a212020-08-04 14:58:35 +02002961 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962
Steven Cooreman177deba2020-09-07 17:14:14 +02002963 if( iv->len > 0 )
2964 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002965 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002966 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967
gabor-mezei-armceface22021-01-21 12:26:17 +01002968 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2969 TEST_ASSERT( output_buffer_size <=
2970 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002971 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972
Gilles Peskinee0866522019-02-19 19:44:00 +01002973 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002974 PSA_ASSERT( psa_cipher_update( &operation,
2975 input->x, first_part_size,
2976 output, output_buffer_size,
2977 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002978 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002979 TEST_ASSERT( function_output_length <=
2980 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2981 TEST_ASSERT( function_output_length <=
2982 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002983 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002984
Gilles Peskine8817f612018-12-18 00:18:46 +01002985 PSA_ASSERT( psa_cipher_update( &operation,
2986 input->x + first_part_size,
2987 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002988 ( output_buffer_size == 0 ? NULL :
2989 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002990 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002991 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002992 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002993 TEST_ASSERT( function_output_length <=
2994 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2995 alg,
2996 input->len - first_part_size ) );
2997 TEST_ASSERT( function_output_length <=
2998 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002999 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003000
Gilles Peskine8817f612018-12-18 00:18:46 +01003001 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003002 ( output_buffer_size == 0 ? NULL :
3003 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003004 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003005 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003006 TEST_ASSERT( function_output_length <=
3007 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3008 TEST_ASSERT( function_output_length <=
3009 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003010 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003012
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003013 ASSERT_COMPARE( expected_output->x, expected_output->len,
3014 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015
3016exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003017 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003018 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003019 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003020 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003021}
3022/* END_CASE */
3023
Gilles Peskine50e586b2018-06-08 14:28:46 +02003024/* BEGIN_CASE */
3025void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003026 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003027 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003028 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003029{
Ronald Cron5425a212020-08-04 14:58:35 +02003030 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003031 psa_status_t status;
3032 psa_key_type_t key_type = key_type_arg;
3033 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003034 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003035 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003036 size_t output_buffer_size = 0;
3037 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003038 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003039 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003041
Gilles Peskine8817f612018-12-18 00:18:46 +01003042 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003043
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003044 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3045 psa_set_key_algorithm( &attributes, alg );
3046 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003047
Ronald Cron5425a212020-08-04 14:58:35 +02003048 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3049 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003050
Ronald Cron5425a212020-08-04 14:58:35 +02003051 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003052
Steven Cooreman177deba2020-09-07 17:14:14 +02003053 if( iv->len > 0 )
3054 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003055 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003056 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003057
gabor-mezei-armceface22021-01-21 12:26:17 +01003058 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3059 TEST_ASSERT( output_buffer_size <=
3060 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003061 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003062
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_cipher_update( &operation,
3064 input->x, input->len,
3065 output, output_buffer_size,
3066 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003067 TEST_ASSERT( function_output_length <=
3068 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3069 TEST_ASSERT( function_output_length <=
3070 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003071 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003072
Gilles Peskine50e586b2018-06-08 14:28:46 +02003073 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003074 ( output_buffer_size == 0 ? NULL :
3075 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003076 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003077 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003078 TEST_ASSERT( function_output_length <=
3079 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3080 TEST_ASSERT( function_output_length <=
3081 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003082 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003083 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084
3085 if( expected_status == PSA_SUCCESS )
3086 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003088 ASSERT_COMPARE( expected_output->x, expected_output->len,
3089 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003090 }
3091
Gilles Peskine50e586b2018-06-08 14:28:46 +02003092exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003093 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003094 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003095 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003096 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003097}
3098/* END_CASE */
3099
Gilles Peskine50e586b2018-06-08 14:28:46 +02003100/* BEGIN_CASE */
3101void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003102 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003103 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003104{
Ronald Cron5425a212020-08-04 14:58:35 +02003105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003106 psa_key_type_t key_type = key_type_arg;
3107 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003108 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003109 size_t iv_size = 16;
3110 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003111 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003112 size_t output1_size = 0;
3113 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003114 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003115 size_t output2_size = 0;
3116 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003117 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003118 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3119 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003121
Gilles Peskine8817f612018-12-18 00:18:46 +01003122 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003124 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3125 psa_set_key_algorithm( &attributes, alg );
3126 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003127
Ronald Cron5425a212020-08-04 14:58:35 +02003128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3129 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003130
Ronald Cron5425a212020-08-04 14:58:35 +02003131 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3132 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003133
Steven Cooreman177deba2020-09-07 17:14:14 +02003134 if( alg != PSA_ALG_ECB_NO_PADDING )
3135 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003136 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3137 iv, iv_size,
3138 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003139 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003140 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3141 TEST_ASSERT( output1_size <=
3142 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003143 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3146 output1, output1_size,
3147 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003148 TEST_ASSERT( output1_length <=
3149 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3150 TEST_ASSERT( output1_length <=
3151 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3152
Gilles Peskine8817f612018-12-18 00:18:46 +01003153 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003154 output1 + output1_length,
3155 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003156 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003157 TEST_ASSERT( function_output_length <=
3158 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003161
Gilles Peskine048b7f02018-06-08 14:20:49 +02003162 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003165
3166 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003167 TEST_ASSERT( output2_size <=
3168 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3169 TEST_ASSERT( output2_size <=
3170 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003171 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003172
Steven Cooreman177deba2020-09-07 17:14:14 +02003173 if( iv_length > 0 )
3174 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003175 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3176 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003177 }
3178
Gilles Peskine8817f612018-12-18 00:18:46 +01003179 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3180 output2, output2_size,
3181 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003182 TEST_ASSERT( output2_length <=
3183 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3184 TEST_ASSERT( output2_length <=
3185 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3186
Gilles Peskine048b7f02018-06-08 14:20:49 +02003187 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_cipher_finish( &operation2,
3189 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003190 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003191 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003192 TEST_ASSERT( function_output_length <=
3193 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3194 TEST_ASSERT( function_output_length <=
3195 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine048b7f02018-06-08 14:20:49 +02003197 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003198
Gilles Peskine8817f612018-12-18 00:18:46 +01003199 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003201 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003202
3203exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003204 psa_cipher_abort( &operation1 );
3205 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003206 mbedtls_free( output1 );
3207 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003208 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003209 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003210}
3211/* END_CASE */
3212
3213/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003214void cipher_verify_output_multipart( int alg_arg,
3215 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003216 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003217 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003218 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003219{
Ronald Cron5425a212020-08-04 14:58:35 +02003220 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003221 psa_key_type_t key_type = key_type_arg;
3222 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003223 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003224 unsigned char iv[16] = {0};
3225 size_t iv_size = 16;
3226 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003227 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003228 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003229 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003230 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003231 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003232 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003233 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003234 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3235 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003237
Gilles Peskine8817f612018-12-18 00:18:46 +01003238 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003239
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003240 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3241 psa_set_key_algorithm( &attributes, alg );
3242 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003243
Ronald Cron5425a212020-08-04 14:58:35 +02003244 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3245 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003246
Ronald Cron5425a212020-08-04 14:58:35 +02003247 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3248 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003249
Steven Cooreman177deba2020-09-07 17:14:14 +02003250 if( alg != PSA_ALG_ECB_NO_PADDING )
3251 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003252 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3253 iv, iv_size,
3254 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003255 }
3256
gabor-mezei-armceface22021-01-21 12:26:17 +01003257 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3258 TEST_ASSERT( output1_buffer_size <=
3259 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003260 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003261
Gilles Peskinee0866522019-02-19 19:44:00 +01003262 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3265 output1, output1_buffer_size,
3266 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003267 TEST_ASSERT( function_output_length <=
3268 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3269 TEST_ASSERT( function_output_length <=
3270 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003271 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003272
Gilles Peskine8817f612018-12-18 00:18:46 +01003273 PSA_ASSERT( psa_cipher_update( &operation1,
3274 input->x + first_part_size,
3275 input->len - first_part_size,
3276 output1, output1_buffer_size,
3277 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003278 TEST_ASSERT( function_output_length <=
3279 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3280 alg,
3281 input->len - first_part_size ) );
3282 TEST_ASSERT( function_output_length <=
3283 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003284 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003285
Gilles Peskine8817f612018-12-18 00:18:46 +01003286 PSA_ASSERT( psa_cipher_finish( &operation1,
3287 output1 + output1_length,
3288 output1_buffer_size - output1_length,
3289 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003290 TEST_ASSERT( function_output_length <=
3291 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3292 TEST_ASSERT( function_output_length <=
3293 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003294 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003295
Gilles Peskine8817f612018-12-18 00:18:46 +01003296 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003297
Gilles Peskine048b7f02018-06-08 14:20:49 +02003298 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003299 TEST_ASSERT( output2_buffer_size <=
3300 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3301 TEST_ASSERT( output2_buffer_size <=
3302 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003303 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003304
Steven Cooreman177deba2020-09-07 17:14:14 +02003305 if( iv_length > 0 )
3306 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003307 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3308 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003309 }
Moran Pekerded84402018-06-06 16:36:50 +03003310
Gilles Peskine8817f612018-12-18 00:18:46 +01003311 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3312 output2, output2_buffer_size,
3313 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003314 TEST_ASSERT( function_output_length <=
3315 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3316 TEST_ASSERT( function_output_length <=
3317 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003318 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003319
Gilles Peskine8817f612018-12-18 00:18:46 +01003320 PSA_ASSERT( psa_cipher_update( &operation2,
3321 output1 + first_part_size,
3322 output1_length - first_part_size,
3323 output2, output2_buffer_size,
3324 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003325 TEST_ASSERT( function_output_length <=
3326 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3327 alg,
3328 output1_length - first_part_size ) );
3329 TEST_ASSERT( function_output_length <=
3330 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003331 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003332
Gilles Peskine8817f612018-12-18 00:18:46 +01003333 PSA_ASSERT( psa_cipher_finish( &operation2,
3334 output2 + output2_length,
3335 output2_buffer_size - output2_length,
3336 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003337 TEST_ASSERT( function_output_length <=
3338 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3339 TEST_ASSERT( function_output_length <=
3340 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003341 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003342
Gilles Peskine8817f612018-12-18 00:18:46 +01003343 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003344
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003345 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003346
3347exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003348 psa_cipher_abort( &operation1 );
3349 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003350 mbedtls_free( output1 );
3351 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003352 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003353 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003354}
3355/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003356
Gilles Peskine20035e32018-02-03 22:44:14 +01003357/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003358void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003359 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003360 data_t *nonce,
3361 data_t *additional_data,
3362 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003363 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364{
Ronald Cron5425a212020-08-04 14:58:35 +02003365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003366 psa_key_type_t key_type = key_type_arg;
3367 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003368 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003369 unsigned char *output_data = NULL;
3370 size_t output_size = 0;
3371 size_t output_length = 0;
3372 unsigned char *output_data2 = NULL;
3373 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003374 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003375 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003380 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3381 psa_set_key_algorithm( &attributes, alg );
3382 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383
Gilles Peskine049c7532019-05-15 20:22:09 +02003384 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003385 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003386 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3387 key_bits = psa_get_key_bits( &attributes );
3388
3389 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3390 alg );
3391 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3392 * should be exact. */
3393 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3394 expected_result != PSA_ERROR_NOT_SUPPORTED )
3395 {
3396 TEST_EQUAL( output_size,
3397 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3398 TEST_ASSERT( output_size <=
3399 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3400 }
3401 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402
Steven Cooremanf49478b2021-02-15 15:19:25 +01003403 status = psa_aead_encrypt( key, alg,
3404 nonce->x, nonce->len,
3405 additional_data->x,
3406 additional_data->len,
3407 input_data->x, input_data->len,
3408 output_data, output_size,
3409 &output_length );
3410
3411 /* If the operation is not supported, just skip and not fail in case the
3412 * encryption involves a common limitation of cryptography hardwares and
3413 * an alternative implementation. */
3414 if( status == PSA_ERROR_NOT_SUPPORTED )
3415 {
3416 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3417 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3418 }
3419
3420 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421
3422 if( PSA_SUCCESS == expected_result )
3423 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003424 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003425
Gilles Peskine003a4a92019-05-14 16:09:40 +02003426 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3427 * should be exact. */
3428 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003429 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003430
gabor-mezei-armceface22021-01-21 12:26:17 +01003431 TEST_ASSERT( input_data->len <=
3432 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3433
Ronald Cron5425a212020-08-04 14:58:35 +02003434 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003435 nonce->x, nonce->len,
3436 additional_data->x,
3437 additional_data->len,
3438 output_data, output_length,
3439 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003440 &output_length2 ),
3441 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003442
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003443 ASSERT_COMPARE( input_data->x, input_data->len,
3444 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003445 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003446
Gilles Peskinea1cac842018-06-11 19:33:02 +02003447exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003448 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003449 mbedtls_free( output_data );
3450 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003451 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003452}
3453/* END_CASE */
3454
3455/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003456void aead_encrypt( int key_type_arg, data_t *key_data,
3457 int alg_arg,
3458 data_t *nonce,
3459 data_t *additional_data,
3460 data_t *input_data,
3461 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462{
Ronald Cron5425a212020-08-04 14:58:35 +02003463 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003464 psa_key_type_t key_type = key_type_arg;
3465 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003466 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003467 unsigned char *output_data = NULL;
3468 size_t output_size = 0;
3469 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003470 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003471 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003472
Gilles Peskine8817f612018-12-18 00:18:46 +01003473 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003475 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3476 psa_set_key_algorithm( &attributes, alg );
3477 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478
Gilles Peskine049c7532019-05-15 20:22:09 +02003479 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003480 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003481 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3482 key_bits = psa_get_key_bits( &attributes );
3483
3484 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3485 alg );
3486 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3487 * should be exact. */
3488 TEST_EQUAL( output_size,
3489 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3490 TEST_ASSERT( output_size <=
3491 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3492 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003493
Steven Cooremand588ea12021-01-11 19:36:04 +01003494 status = psa_aead_encrypt( key, alg,
3495 nonce->x, nonce->len,
3496 additional_data->x, additional_data->len,
3497 input_data->x, input_data->len,
3498 output_data, output_size,
3499 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003500
Ronald Cron28a45ed2021-02-09 20:35:42 +01003501 /* If the operation is not supported, just skip and not fail in case the
3502 * encryption involves a common limitation of cryptography hardwares and
3503 * an alternative implementation. */
3504 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003505 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003506 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3507 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003508 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003509
3510 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003511 ASSERT_COMPARE( expected_result->x, expected_result->len,
3512 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003513
Gilles Peskinea1cac842018-06-11 19:33:02 +02003514exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003515 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003516 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003517 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003518}
3519/* END_CASE */
3520
3521/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003522void aead_decrypt( int key_type_arg, data_t *key_data,
3523 int alg_arg,
3524 data_t *nonce,
3525 data_t *additional_data,
3526 data_t *input_data,
3527 data_t *expected_data,
3528 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003529{
Ronald Cron5425a212020-08-04 14:58:35 +02003530 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003531 psa_key_type_t key_type = key_type_arg;
3532 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003533 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003534 unsigned char *output_data = NULL;
3535 size_t output_size = 0;
3536 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003538 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003539 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003540
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003542
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3544 psa_set_key_algorithm( &attributes, alg );
3545 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003546
Gilles Peskine049c7532019-05-15 20:22:09 +02003547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003548 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003549 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3550 key_bits = psa_get_key_bits( &attributes );
3551
3552 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3553 alg );
3554 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3555 expected_result != PSA_ERROR_NOT_SUPPORTED )
3556 {
3557 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3558 * should be exact. */
3559 TEST_EQUAL( output_size,
3560 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3561 TEST_ASSERT( output_size <=
3562 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3563 }
3564 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003565
Steven Cooremand588ea12021-01-11 19:36:04 +01003566 status = psa_aead_decrypt( key, alg,
3567 nonce->x, nonce->len,
3568 additional_data->x,
3569 additional_data->len,
3570 input_data->x, input_data->len,
3571 output_data, output_size,
3572 &output_length );
3573
Ronald Cron28a45ed2021-02-09 20:35:42 +01003574 /* If the operation is not supported, just skip and not fail in case the
3575 * decryption involves a common limitation of cryptography hardwares and
3576 * an alternative implementation. */
3577 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003578 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003579 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3580 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003581 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003582
3583 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003584
Gilles Peskine2d277862018-06-18 15:41:12 +02003585 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003586 ASSERT_COMPARE( expected_data->x, expected_data->len,
3587 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003588
Gilles Peskinea1cac842018-06-11 19:33:02 +02003589exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003590 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003591 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003592 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003593}
3594/* END_CASE */
3595
3596/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003597void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3598 int alg_arg,
3599 data_t *nonce,
3600 data_t *additional_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003601 int test_ad_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602 data_t *input_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003603 int test_data_mp_arg,
3604 data_t *expected_result_arg )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605{
Paul Elliottd3f82412021-06-16 16:52:21 +01003606 size_t ad_part_len = 0;
3607 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003608
Paul Elliottd3f82412021-06-16 16:52:21 +01003609 if( test_ad_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003610 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003611 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3612 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003613 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003614 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003615
Paul Elliottd3f82412021-06-16 16:52:21 +01003616 aead_multipart_encrypt_internal( key_type_arg, key_data,
3617 alg_arg,nonce,
3618 additional_data,
3619 ad_part_len,
3620 input_data, -1,
3621 expected_result_arg );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003622 }
3623 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003624
3625 if( test_data_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003626 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003627 for( data_part_len = 1; data_part_len <= input_data->len;
3628 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003629 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003630 aead_multipart_encrypt_internal( key_type_arg, key_data,
3631 alg_arg, nonce,
3632 additional_data, -1,
3633 input_data, data_part_len,
3634 expected_result_arg );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003635 }
3636 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003637
Paul Elliottd3f82412021-06-16 16:52:21 +01003638 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639
3640exit:
Paul Elliott0023e0a2021-04-27 10:06:22 +01003641}
3642/* END_CASE */
3643
3644/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003645void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3646 int alg_arg,
3647 data_t *nonce,
3648 data_t *additional_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003649 int test_ad_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003650 data_t *input_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003651 int test_data_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003652 data_t *expected_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003653 int expected_status )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003654{
Paul Elliottd3f82412021-06-16 16:52:21 +01003655 size_t ad_part_len = 0;
3656 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003657
Paul Elliottd3f82412021-06-16 16:52:21 +01003658 if( test_ad_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003659 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003660 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3661 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003662 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003663 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003664
Paul Elliottd3f82412021-06-16 16:52:21 +01003665 aead_multipart_decrypt_internal( key_type_arg, key_data,
3666 alg_arg, nonce,
3667 additional_data,
3668 ad_part_len,
3669 input_data, -1,
3670 expected_data, expected_status );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003671 }
3672 }
3673
Paul Elliottd3f82412021-06-16 16:52:21 +01003674 if( test_data_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003675 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003676 for( data_part_len = 1; data_part_len <= input_data->len;
3677 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003678 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003679 aead_multipart_decrypt_internal( key_type_arg, key_data,
3680 alg_arg, nonce,
3681 additional_data, -1,
3682 input_data, data_part_len,
3683 expected_data, expected_status );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003684 }
3685 }
3686
Paul Elliottd3f82412021-06-16 16:52:21 +01003687 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003688
3689exit:
Paul Elliott0023e0a2021-04-27 10:06:22 +01003690}
3691/* END_CASE */
3692
3693/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003694void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3695 int alg_arg,
3696 int nonce_len,
3697 int expected_result_arg )
3698{
3699
3700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3701 psa_key_type_t key_type = key_type_arg;
3702 psa_algorithm_t alg = alg_arg;
3703 psa_aead_operation_t operation;
3704 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3707 size_t nonce_generated_len = 0;
3708
3709 PSA_ASSERT( psa_crypto_init( ) );
3710
3711 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3712 psa_set_key_algorithm( & attributes, alg );
3713 psa_set_key_type( & attributes, key_type );
3714
3715 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3716 &key ) );
3717
3718 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3719
3720 operation = psa_aead_operation_init( );
3721
3722 status = psa_aead_encrypt_setup( &operation, key, alg );
3723
3724 /* If the operation is not supported, just skip and not fail in case the
3725 * encryption involves a common limitation of cryptography hardwares and
3726 * an alternative implementation. */
3727 if( status == PSA_ERROR_NOT_SUPPORTED )
3728 {
3729 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3730 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len );
3731 }
3732
3733 PSA_ASSERT( status );
3734
3735 TEST_ASSERT( nonce_len < PSA_AEAD_NONCE_MAX_SIZE );
3736
3737 status = psa_aead_generate_nonce( &operation, nonce_buffer,
3738 nonce_len,
3739 &nonce_generated_len );
3740
3741 TEST_ASSERT( status == expected_result_arg );
3742
3743exit:
3744 psa_destroy_key( key );
3745 psa_aead_abort( &operation );
3746 PSA_DONE( );
3747}
3748/* END_CASE */
3749
3750/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01003751void aead_multipart_state_test( int key_type_arg, data_t *key_data,
3752 int alg_arg,
3753 data_t *nonce,
3754 data_t *additional_data,
3755 data_t *input_data )
3756{
3757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3758 psa_key_type_t key_type = key_type_arg;
3759 psa_algorithm_t alg = alg_arg;
3760 psa_aead_operation_t operation;
3761 unsigned char *output_data = NULL;
3762 unsigned char *final_data = NULL;
3763 size_t output_size = 0;
3764 size_t finish_output_size = 0;
3765 size_t output_length = 0;
3766 size_t key_bits = 0;
3767 size_t tag_length = 0;
3768 size_t tag_size = 0;
3769 size_t nonce_length = 0;
3770 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3771 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3772 size_t output_part_length = 0;
3773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3774
3775 PSA_ASSERT( psa_crypto_init( ) );
3776
3777 psa_set_key_usage_flags( & attributes,
3778 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3779 psa_set_key_algorithm( & attributes, alg );
3780 psa_set_key_type( & attributes, key_type );
3781
3782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3783 &key ) );
3784
3785 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3786 key_bits = psa_get_key_bits( &attributes );
3787
3788 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3789
3790 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
3791
3792 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3793
3794 ASSERT_ALLOC( output_data, output_size );
3795
3796 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3797
3798 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3799
3800 ASSERT_ALLOC( final_data, finish_output_size );
3801
3802 /* Test all operations error without calling setup first. */
3803
3804 operation = psa_aead_operation_init( );
3805
3806 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3807 PSA_ERROR_BAD_STATE );
3808
3809 psa_aead_abort( &operation );
3810
3811 operation = psa_aead_operation_init( );
3812
3813 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3814 PSA_AEAD_NONCE_MAX_SIZE,
3815 &nonce_length ),
3816 PSA_ERROR_BAD_STATE );
3817
3818 psa_aead_abort( &operation );
3819
3820 operation = psa_aead_operation_init( );
3821
3822 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3823 input_data->len ),
3824 PSA_ERROR_BAD_STATE );
3825
3826 psa_aead_abort( &operation );
3827
3828 operation = psa_aead_operation_init( );
3829
3830 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3831 additional_data->len ),
3832 PSA_ERROR_BAD_STATE );
3833
3834 psa_aead_abort( &operation );
3835
3836 operation = psa_aead_operation_init( );
3837
3838 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
3839 input_data->len, output_data,
3840 output_size, &output_length ),
3841 PSA_ERROR_BAD_STATE );
3842
3843 psa_aead_abort( &operation );
3844
3845 operation = psa_aead_operation_init( );
3846
3847 TEST_EQUAL( psa_aead_finish( &operation, final_data,
3848 finish_output_size,
3849 &output_part_length,
3850 tag_buffer, tag_length,
3851 &tag_size ),
3852 PSA_ERROR_BAD_STATE );
3853
3854 psa_aead_abort( &operation );
3855
3856 operation = psa_aead_operation_init( );
3857
3858 TEST_EQUAL( psa_aead_verify( &operation, final_data,
3859 finish_output_size,
3860 &output_part_length,
3861 tag_buffer,
3862 tag_length ),
3863 PSA_ERROR_BAD_STATE );
3864
3865 psa_aead_abort( &operation );
3866
3867 /* Test for double setups. */
3868
3869 operation = psa_aead_operation_init( );
3870
3871 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3872
3873 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3874 PSA_ERROR_BAD_STATE );
3875
3876 psa_aead_abort( &operation );
3877
3878 operation = psa_aead_operation_init( );
3879
3880 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3881
3882 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3883 PSA_ERROR_BAD_STATE );
3884
3885 psa_aead_abort( &operation );
3886
3887 /* Test for not setting a nonce. */
3888
3889 operation = psa_aead_operation_init( );
3890
3891 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3892
3893 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3894 additional_data->len ),
3895 PSA_ERROR_BAD_STATE );
3896
3897 psa_aead_abort( &operation );
3898
3899 /* Test for double setting nonce. */
3900
3901 operation = psa_aead_operation_init( );
3902
3903 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3904
3905 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3906
3907 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3908 PSA_ERROR_BAD_STATE );
3909
3910 psa_aead_abort( &operation );
3911
3912 /* Test for setting lengths twice. */
3913
3914 operation = psa_aead_operation_init( );
3915
3916 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3917
3918 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3919
3920 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3921 input_data->len ) );
3922
3923 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3924 input_data->len ),
3925 PSA_ERROR_BAD_STATE );
3926
3927 psa_aead_abort( &operation );
3928
3929 /* Test for setting lengths after already starting data. */
3930
3931 operation = psa_aead_operation_init( );
3932
3933 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3934
3935 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3936
3937 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
3938 input_data->len, output_data,
3939 output_size, &output_length ) );
3940
3941 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3942 input_data->len ),
3943 PSA_ERROR_BAD_STATE );
3944
3945 psa_aead_abort( &operation );
3946
3947 /* Test for not sending any additional data or data (encrypt) */
3948
3949 operation = psa_aead_operation_init( );
3950
3951 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3952
3953 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3954
3955 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3956 input_data->len ) );
3957
3958 TEST_EQUAL( psa_aead_finish( &operation, final_data,
3959 finish_output_size,
3960 &output_part_length,
3961 tag_buffer, tag_length,
3962 &tag_size ),
3963 PSA_ERROR_INVALID_ARGUMENT );
3964
3965 psa_aead_abort( &operation );
3966
3967 /* Test for not sending any additional data or data (decrypt) */
3968
3969 operation = psa_aead_operation_init( );
3970
3971 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3972
3973 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3974
3975 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3976 input_data->len ) );
3977
3978 TEST_EQUAL( psa_aead_verify( &operation, final_data,
3979 finish_output_size,
3980 &output_part_length,
3981 tag_buffer,
3982 tag_length ),
3983 PSA_ERROR_INVALID_ARGUMENT );
3984
3985 psa_aead_abort( &operation );
3986
3987 /* Test for not sending any additional data. */
3988
3989 operation = psa_aead_operation_init( );
3990
3991 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3992
3993 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3994
3995 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3996 input_data->len ) );
3997
3998 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
3999 input_data->len, output_data,
4000 output_size, &output_length ),
4001 PSA_ERROR_INVALID_ARGUMENT );
4002
4003 psa_aead_abort( &operation );
4004
4005 /* Test sending additional data after data. */
4006
4007 operation = psa_aead_operation_init( );
4008
4009 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4010
4011 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4012
4013 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4014 input_data->len, output_data,
4015 output_size, &output_length ) );
4016
4017 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4018 additional_data->len ),
4019 PSA_ERROR_BAD_STATE );
4020
4021 psa_aead_abort( &operation );
4022
4023exit:
4024 psa_destroy_key( key );
4025 psa_aead_abort( &operation );
4026 mbedtls_free( output_data );
4027 mbedtls_free( final_data );
4028 PSA_DONE( );
4029}
4030/* END_CASE */
4031
4032/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004033void signature_size( int type_arg,
4034 int bits,
4035 int alg_arg,
4036 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004037{
4038 psa_key_type_t type = type_arg;
4039 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004040 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004041
Gilles Peskinefe11b722018-12-18 00:24:04 +01004042 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004043
Gilles Peskinee59236f2018-01-27 23:32:46 +01004044exit:
4045 ;
4046}
4047/* END_CASE */
4048
4049/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004050void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4051 int alg_arg, data_t *input_data,
4052 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004053{
Ronald Cron5425a212020-08-04 14:58:35 +02004054 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004055 psa_key_type_t key_type = key_type_arg;
4056 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004057 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004058 unsigned char *signature = NULL;
4059 size_t signature_size;
4060 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004062
Gilles Peskine8817f612018-12-18 00:18:46 +01004063 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004064
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004065 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004066 psa_set_key_algorithm( &attributes, alg );
4067 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004068
Gilles Peskine049c7532019-05-15 20:22:09 +02004069 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004070 &key ) );
4071 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004072 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004073
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004074 /* Allocate a buffer which has the size advertized by the
4075 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004076 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004077 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004078 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004079 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004080 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004081
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004082 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004083 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004084 input_data->x, input_data->len,
4085 signature, signature_size,
4086 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004087 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004088 ASSERT_COMPARE( output_data->x, output_data->len,
4089 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004090
4091exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004092 /*
4093 * Key attributes may have been returned by psa_get_key_attributes()
4094 * thus reset them as required.
4095 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004096 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004097
Ronald Cron5425a212020-08-04 14:58:35 +02004098 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004099 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004100 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004101}
4102/* END_CASE */
4103
4104/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004105void sign_hash_fail( int key_type_arg, data_t *key_data,
4106 int alg_arg, data_t *input_data,
4107 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004108{
Ronald Cron5425a212020-08-04 14:58:35 +02004109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004110 psa_key_type_t key_type = key_type_arg;
4111 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004112 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004113 psa_status_t actual_status;
4114 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004115 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004116 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004118
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004119 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004120
Gilles Peskine8817f612018-12-18 00:18:46 +01004121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004122
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004123 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004124 psa_set_key_algorithm( &attributes, alg );
4125 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004126
Gilles Peskine049c7532019-05-15 20:22:09 +02004127 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004128 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004129
Ronald Cron5425a212020-08-04 14:58:35 +02004130 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004131 input_data->x, input_data->len,
4132 signature, signature_size,
4133 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004134 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004135 /* The value of *signature_length is unspecified on error, but
4136 * whatever it is, it should be less than signature_size, so that
4137 * if the caller tries to read *signature_length bytes without
4138 * checking the error code then they don't overflow a buffer. */
4139 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004140
4141exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004142 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004143 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004144 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004145 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004146}
4147/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004148
4149/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004150void sign_verify_hash( int key_type_arg, data_t *key_data,
4151 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004152{
Ronald Cron5425a212020-08-04 14:58:35 +02004153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004154 psa_key_type_t key_type = key_type_arg;
4155 psa_algorithm_t alg = alg_arg;
4156 size_t key_bits;
4157 unsigned char *signature = NULL;
4158 size_t signature_size;
4159 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004161
Gilles Peskine8817f612018-12-18 00:18:46 +01004162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004163
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004164 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004165 psa_set_key_algorithm( &attributes, alg );
4166 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004167
Gilles Peskine049c7532019-05-15 20:22:09 +02004168 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004169 &key ) );
4170 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004171 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004172
4173 /* Allocate a buffer which has the size advertized by the
4174 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004175 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004176 key_bits, alg );
4177 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004178 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004179 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004180
4181 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004182 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004183 input_data->x, input_data->len,
4184 signature, signature_size,
4185 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004186 /* Check that the signature length looks sensible. */
4187 TEST_ASSERT( signature_length <= signature_size );
4188 TEST_ASSERT( signature_length > 0 );
4189
4190 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004191 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004192 input_data->x, input_data->len,
4193 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004194
4195 if( input_data->len != 0 )
4196 {
4197 /* Flip a bit in the input and verify that the signature is now
4198 * detected as invalid. Flip a bit at the beginning, not at the end,
4199 * because ECDSA may ignore the last few bits of the input. */
4200 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004201 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004202 input_data->x, input_data->len,
4203 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004204 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004205 }
4206
4207exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004208 /*
4209 * Key attributes may have been returned by psa_get_key_attributes()
4210 * thus reset them as required.
4211 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004212 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004213
Ronald Cron5425a212020-08-04 14:58:35 +02004214 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004215 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004216 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004217}
4218/* END_CASE */
4219
4220/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004221void verify_hash( int key_type_arg, data_t *key_data,
4222 int alg_arg, data_t *hash_data,
4223 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004224{
Ronald Cron5425a212020-08-04 14:58:35 +02004225 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004226 psa_key_type_t key_type = key_type_arg;
4227 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004229
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004230 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004231
Gilles Peskine8817f612018-12-18 00:18:46 +01004232 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004233
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004235 psa_set_key_algorithm( &attributes, alg );
4236 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004237
Gilles Peskine049c7532019-05-15 20:22:09 +02004238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004239 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004240
Ronald Cron5425a212020-08-04 14:58:35 +02004241 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004242 hash_data->x, hash_data->len,
4243 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004244
itayzafrir5c753392018-05-08 11:18:38 +03004245exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004246 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004247 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004248 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004249}
4250/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004251
4252/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004253void verify_hash_fail( int key_type_arg, data_t *key_data,
4254 int alg_arg, data_t *hash_data,
4255 data_t *signature_data,
4256 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257{
Ronald Cron5425a212020-08-04 14:58:35 +02004258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259 psa_key_type_t key_type = key_type_arg;
4260 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004261 psa_status_t actual_status;
4262 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004264
Gilles Peskine8817f612018-12-18 00:18:46 +01004265 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004266
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004267 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004268 psa_set_key_algorithm( &attributes, alg );
4269 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004270
Gilles Peskine049c7532019-05-15 20:22:09 +02004271 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004272 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004273
Ronald Cron5425a212020-08-04 14:58:35 +02004274 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004275 hash_data->x, hash_data->len,
4276 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004277 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004278
4279exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004280 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004281 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004282 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004283}
4284/* END_CASE */
4285
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004286/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004287void sign_message_deterministic( int key_type_arg,
4288 data_t *key_data,
4289 int alg_arg,
4290 data_t *input_data,
4291 data_t *output_data )
4292{
4293 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4294 psa_key_type_t key_type = key_type_arg;
4295 psa_algorithm_t alg = alg_arg;
4296 size_t key_bits;
4297 unsigned char *signature = NULL;
4298 size_t signature_size;
4299 size_t signature_length = 0xdeadbeef;
4300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4301
4302 PSA_ASSERT( psa_crypto_init( ) );
4303
4304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4305 psa_set_key_algorithm( &attributes, alg );
4306 psa_set_key_type( &attributes, key_type );
4307
4308 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4309 &key ) );
4310 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4311 key_bits = psa_get_key_bits( &attributes );
4312
4313 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4314 TEST_ASSERT( signature_size != 0 );
4315 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4316 ASSERT_ALLOC( signature, signature_size );
4317
4318 PSA_ASSERT( psa_sign_message( key, alg,
4319 input_data->x, input_data->len,
4320 signature, signature_size,
4321 &signature_length ) );
4322
4323 ASSERT_COMPARE( output_data->x, output_data->len,
4324 signature, signature_length );
4325
4326exit:
4327 psa_reset_key_attributes( &attributes );
4328
4329 psa_destroy_key( key );
4330 mbedtls_free( signature );
4331 PSA_DONE( );
4332
4333}
4334/* END_CASE */
4335
4336/* BEGIN_CASE */
4337void sign_message_fail( int key_type_arg,
4338 data_t *key_data,
4339 int alg_arg,
4340 data_t *input_data,
4341 int signature_size_arg,
4342 int expected_status_arg )
4343{
4344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4345 psa_key_type_t key_type = key_type_arg;
4346 psa_algorithm_t alg = alg_arg;
4347 size_t signature_size = signature_size_arg;
4348 psa_status_t actual_status;
4349 psa_status_t expected_status = expected_status_arg;
4350 unsigned char *signature = NULL;
4351 size_t signature_length = 0xdeadbeef;
4352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4353
4354 ASSERT_ALLOC( signature, signature_size );
4355
4356 PSA_ASSERT( psa_crypto_init( ) );
4357
4358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4359 psa_set_key_algorithm( &attributes, alg );
4360 psa_set_key_type( &attributes, key_type );
4361
4362 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4363 &key ) );
4364
4365 actual_status = psa_sign_message( key, alg,
4366 input_data->x, input_data->len,
4367 signature, signature_size,
4368 &signature_length );
4369 TEST_EQUAL( actual_status, expected_status );
4370 /* The value of *signature_length is unspecified on error, but
4371 * whatever it is, it should be less than signature_size, so that
4372 * if the caller tries to read *signature_length bytes without
4373 * checking the error code then they don't overflow a buffer. */
4374 TEST_ASSERT( signature_length <= signature_size );
4375
4376exit:
4377 psa_reset_key_attributes( &attributes );
4378 psa_destroy_key( key );
4379 mbedtls_free( signature );
4380 PSA_DONE( );
4381}
4382/* END_CASE */
4383
4384/* BEGIN_CASE */
4385void sign_verify_message( int key_type_arg,
4386 data_t *key_data,
4387 int alg_arg,
4388 data_t *input_data )
4389{
4390 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4391 psa_key_type_t key_type = key_type_arg;
4392 psa_algorithm_t alg = alg_arg;
4393 size_t key_bits;
4394 unsigned char *signature = NULL;
4395 size_t signature_size;
4396 size_t signature_length = 0xdeadbeef;
4397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4398
4399 PSA_ASSERT( psa_crypto_init( ) );
4400
4401 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4402 PSA_KEY_USAGE_VERIFY_MESSAGE );
4403 psa_set_key_algorithm( &attributes, alg );
4404 psa_set_key_type( &attributes, key_type );
4405
4406 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4407 &key ) );
4408 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4409 key_bits = psa_get_key_bits( &attributes );
4410
4411 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4412 TEST_ASSERT( signature_size != 0 );
4413 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4414 ASSERT_ALLOC( signature, signature_size );
4415
4416 PSA_ASSERT( psa_sign_message( key, alg,
4417 input_data->x, input_data->len,
4418 signature, signature_size,
4419 &signature_length ) );
4420 TEST_ASSERT( signature_length <= signature_size );
4421 TEST_ASSERT( signature_length > 0 );
4422
4423 PSA_ASSERT( psa_verify_message( key, alg,
4424 input_data->x, input_data->len,
4425 signature, signature_length ) );
4426
4427 if( input_data->len != 0 )
4428 {
4429 /* Flip a bit in the input and verify that the signature is now
4430 * detected as invalid. Flip a bit at the beginning, not at the end,
4431 * because ECDSA may ignore the last few bits of the input. */
4432 input_data->x[0] ^= 1;
4433 TEST_EQUAL( psa_verify_message( key, alg,
4434 input_data->x, input_data->len,
4435 signature, signature_length ),
4436 PSA_ERROR_INVALID_SIGNATURE );
4437 }
4438
4439exit:
4440 psa_reset_key_attributes( &attributes );
4441
4442 psa_destroy_key( key );
4443 mbedtls_free( signature );
4444 PSA_DONE( );
4445}
4446/* END_CASE */
4447
4448/* BEGIN_CASE */
4449void verify_message( int key_type_arg,
4450 data_t *key_data,
4451 int alg_arg,
4452 data_t *input_data,
4453 data_t *signature_data )
4454{
4455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4456 psa_key_type_t key_type = key_type_arg;
4457 psa_algorithm_t alg = alg_arg;
4458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4459
4460 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4461
4462 PSA_ASSERT( psa_crypto_init( ) );
4463
4464 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4465 psa_set_key_algorithm( &attributes, alg );
4466 psa_set_key_type( &attributes, key_type );
4467
4468 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4469 &key ) );
4470
4471 PSA_ASSERT( psa_verify_message( key, alg,
4472 input_data->x, input_data->len,
4473 signature_data->x, signature_data->len ) );
4474
4475exit:
4476 psa_reset_key_attributes( &attributes );
4477 psa_destroy_key( key );
4478 PSA_DONE( );
4479}
4480/* END_CASE */
4481
4482/* BEGIN_CASE */
4483void verify_message_fail( int key_type_arg,
4484 data_t *key_data,
4485 int alg_arg,
4486 data_t *hash_data,
4487 data_t *signature_data,
4488 int expected_status_arg )
4489{
4490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4491 psa_key_type_t key_type = key_type_arg;
4492 psa_algorithm_t alg = alg_arg;
4493 psa_status_t actual_status;
4494 psa_status_t expected_status = expected_status_arg;
4495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4496
4497 PSA_ASSERT( psa_crypto_init( ) );
4498
4499 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4500 psa_set_key_algorithm( &attributes, alg );
4501 psa_set_key_type( &attributes, key_type );
4502
4503 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4504 &key ) );
4505
4506 actual_status = psa_verify_message( key, alg,
4507 hash_data->x, hash_data->len,
4508 signature_data->x,
4509 signature_data->len );
4510 TEST_EQUAL( actual_status, expected_status );
4511
4512exit:
4513 psa_reset_key_attributes( &attributes );
4514 psa_destroy_key( key );
4515 PSA_DONE( );
4516}
4517/* END_CASE */
4518
4519/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004520void asymmetric_encrypt( int key_type_arg,
4521 data_t *key_data,
4522 int alg_arg,
4523 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004524 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004525 int expected_output_length_arg,
4526 int expected_status_arg )
4527{
Ronald Cron5425a212020-08-04 14:58:35 +02004528 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004529 psa_key_type_t key_type = key_type_arg;
4530 psa_algorithm_t alg = alg_arg;
4531 size_t expected_output_length = expected_output_length_arg;
4532 size_t key_bits;
4533 unsigned char *output = NULL;
4534 size_t output_size;
4535 size_t output_length = ~0;
4536 psa_status_t actual_status;
4537 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004539
Gilles Peskine8817f612018-12-18 00:18:46 +01004540 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004541
Gilles Peskine656896e2018-06-29 19:12:28 +02004542 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4544 psa_set_key_algorithm( &attributes, alg );
4545 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004546 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004547 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004548
4549 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004550 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004551 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004552
Gilles Peskine656896e2018-06-29 19:12:28 +02004553 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004554 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004555 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004556
4557 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004558 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004559 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004560 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004561 output, output_size,
4562 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004563 TEST_EQUAL( actual_status, expected_status );
4564 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004565
Gilles Peskine68428122018-06-30 18:42:41 +02004566 /* If the label is empty, the test framework puts a non-null pointer
4567 * in label->x. Test that a null pointer works as well. */
4568 if( label->len == 0 )
4569 {
4570 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004571 if( output_size != 0 )
4572 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004573 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004574 input_data->x, input_data->len,
4575 NULL, label->len,
4576 output, output_size,
4577 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004578 TEST_EQUAL( actual_status, expected_status );
4579 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004580 }
4581
Gilles Peskine656896e2018-06-29 19:12:28 +02004582exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004583 /*
4584 * Key attributes may have been returned by psa_get_key_attributes()
4585 * thus reset them as required.
4586 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004587 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004588
Ronald Cron5425a212020-08-04 14:58:35 +02004589 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004590 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004591 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004592}
4593/* END_CASE */
4594
4595/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004596void asymmetric_encrypt_decrypt( int key_type_arg,
4597 data_t *key_data,
4598 int alg_arg,
4599 data_t *input_data,
4600 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004601{
Ronald Cron5425a212020-08-04 14:58:35 +02004602 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004603 psa_key_type_t key_type = key_type_arg;
4604 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004605 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004606 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004607 size_t output_size;
4608 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004609 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004610 size_t output2_size;
4611 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004613
Gilles Peskine8817f612018-12-18 00:18:46 +01004614 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004615
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004616 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4617 psa_set_key_algorithm( &attributes, alg );
4618 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004619
Gilles Peskine049c7532019-05-15 20:22:09 +02004620 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004621 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004622
4623 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004624 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004625 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004626
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004627 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004628 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004629 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004630
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004631 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004632 TEST_ASSERT( output2_size <=
4633 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4634 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004635 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004636
Gilles Peskineeebd7382018-06-08 18:11:54 +02004637 /* We test encryption by checking that encrypt-then-decrypt gives back
4638 * the original plaintext because of the non-optional random
4639 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004640 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004641 input_data->x, input_data->len,
4642 label->x, label->len,
4643 output, output_size,
4644 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004645 /* We don't know what ciphertext length to expect, but check that
4646 * it looks sensible. */
4647 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004648
Ronald Cron5425a212020-08-04 14:58:35 +02004649 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004650 output, output_length,
4651 label->x, label->len,
4652 output2, output2_size,
4653 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004654 ASSERT_COMPARE( input_data->x, input_data->len,
4655 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004656
4657exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004658 /*
4659 * Key attributes may have been returned by psa_get_key_attributes()
4660 * thus reset them as required.
4661 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004662 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004663
Ronald Cron5425a212020-08-04 14:58:35 +02004664 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004665 mbedtls_free( output );
4666 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004667 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004668}
4669/* END_CASE */
4670
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004671/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004672void asymmetric_decrypt( int key_type_arg,
4673 data_t *key_data,
4674 int alg_arg,
4675 data_t *input_data,
4676 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004677 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004678{
Ronald Cron5425a212020-08-04 14:58:35 +02004679 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004680 psa_key_type_t key_type = key_type_arg;
4681 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004682 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004683 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004684 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004685 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004686 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004687
Gilles Peskine8817f612018-12-18 00:18:46 +01004688 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004689
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004690 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4691 psa_set_key_algorithm( &attributes, alg );
4692 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004693
Gilles Peskine049c7532019-05-15 20:22:09 +02004694 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004695 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004696
gabor-mezei-armceface22021-01-21 12:26:17 +01004697 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4698 key_bits = psa_get_key_bits( &attributes );
4699
4700 /* Determine the maximum ciphertext length */
4701 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4702 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4703 ASSERT_ALLOC( output, output_size );
4704
Ronald Cron5425a212020-08-04 14:58:35 +02004705 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004706 input_data->x, input_data->len,
4707 label->x, label->len,
4708 output,
4709 output_size,
4710 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004711 ASSERT_COMPARE( expected_data->x, expected_data->len,
4712 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004713
Gilles Peskine68428122018-06-30 18:42:41 +02004714 /* If the label is empty, the test framework puts a non-null pointer
4715 * in label->x. Test that a null pointer works as well. */
4716 if( label->len == 0 )
4717 {
4718 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004719 if( output_size != 0 )
4720 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004721 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004722 input_data->x, input_data->len,
4723 NULL, label->len,
4724 output,
4725 output_size,
4726 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004727 ASSERT_COMPARE( expected_data->x, expected_data->len,
4728 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004729 }
4730
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004731exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004732 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004733 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004734 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004735 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004736}
4737/* END_CASE */
4738
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004739/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004740void asymmetric_decrypt_fail( int key_type_arg,
4741 data_t *key_data,
4742 int alg_arg,
4743 data_t *input_data,
4744 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004745 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004746 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004747{
Ronald Cron5425a212020-08-04 14:58:35 +02004748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004749 psa_key_type_t key_type = key_type_arg;
4750 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004751 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004752 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004753 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004754 psa_status_t actual_status;
4755 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004757
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004758 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004759
Gilles Peskine8817f612018-12-18 00:18:46 +01004760 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004761
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004762 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4763 psa_set_key_algorithm( &attributes, alg );
4764 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004765
Gilles Peskine049c7532019-05-15 20:22:09 +02004766 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004767 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004768
Ronald Cron5425a212020-08-04 14:58:35 +02004769 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004770 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004771 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004772 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004773 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004774 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004775 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004776
Gilles Peskine68428122018-06-30 18:42:41 +02004777 /* If the label is empty, the test framework puts a non-null pointer
4778 * in label->x. Test that a null pointer works as well. */
4779 if( label->len == 0 )
4780 {
4781 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004782 if( output_size != 0 )
4783 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004784 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004785 input_data->x, input_data->len,
4786 NULL, label->len,
4787 output, output_size,
4788 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004789 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004790 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004791 }
4792
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004793exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004794 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004795 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004796 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004797 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004798}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004799/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004800
4801/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004802void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004803{
4804 /* Test each valid way of initializing the object, except for `= {0}`, as
4805 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4806 * though it's OK by the C standard. We could test for this, but we'd need
4807 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004808 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004809 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4810 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4811 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004812
4813 memset( &zero, 0, sizeof( zero ) );
4814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004816 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004817 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004818 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004819 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004820 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004821 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004822
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004824 PSA_ASSERT( psa_key_derivation_abort(&func) );
4825 PSA_ASSERT( psa_key_derivation_abort(&init) );
4826 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004827}
4828/* END_CASE */
4829
Janos Follath16de4a42019-06-13 16:32:24 +01004830/* BEGIN_CASE */
4831void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004832{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004833 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004834 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004836
Gilles Peskine8817f612018-12-18 00:18:46 +01004837 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004838
Janos Follath16de4a42019-06-13 16:32:24 +01004839 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004840 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004841
4842exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004844 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004845}
4846/* END_CASE */
4847
Janos Follathaf3c2a02019-06-12 12:34:34 +01004848/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004849void derive_set_capacity( int alg_arg, int capacity_arg,
4850 int expected_status_arg )
4851{
4852 psa_algorithm_t alg = alg_arg;
4853 size_t capacity = capacity_arg;
4854 psa_status_t expected_status = expected_status_arg;
4855 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4856
4857 PSA_ASSERT( psa_crypto_init( ) );
4858
4859 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4860
4861 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4862 expected_status );
4863
4864exit:
4865 psa_key_derivation_abort( &operation );
4866 PSA_DONE( );
4867}
4868/* END_CASE */
4869
4870/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004871void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004872 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004873 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004874 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004875 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004876 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004877 int expected_status_arg3,
4878 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004879{
4880 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004881 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4882 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004883 psa_status_t expected_statuses[] = {expected_status_arg1,
4884 expected_status_arg2,
4885 expected_status_arg3};
4886 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004887 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4888 MBEDTLS_SVC_KEY_ID_INIT,
4889 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004890 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4892 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004893 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004894 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004895 psa_status_t expected_output_status = expected_output_status_arg;
4896 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004897
4898 PSA_ASSERT( psa_crypto_init( ) );
4899
4900 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4901 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004902
4903 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4904
4905 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4906 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004907 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004908 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004909 psa_set_key_type( &attributes, key_types[i] );
4910 PSA_ASSERT( psa_import_key( &attributes,
4911 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004912 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004913 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4914 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4915 {
4916 // When taking a private key as secret input, use key agreement
4917 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004918 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4919 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004920 expected_statuses[i] );
4921 }
4922 else
4923 {
4924 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004925 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004926 expected_statuses[i] );
4927 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004928 }
4929 else
4930 {
4931 TEST_EQUAL( psa_key_derivation_input_bytes(
4932 &operation, steps[i],
4933 inputs[i]->x, inputs[i]->len ),
4934 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004935 }
4936 }
4937
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004938 if( output_key_type != PSA_KEY_TYPE_NONE )
4939 {
4940 psa_reset_key_attributes( &attributes );
4941 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4942 psa_set_key_bits( &attributes, 8 );
4943 actual_output_status =
4944 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004945 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004946 }
4947 else
4948 {
4949 uint8_t buffer[1];
4950 actual_output_status =
4951 psa_key_derivation_output_bytes( &operation,
4952 buffer, sizeof( buffer ) );
4953 }
4954 TEST_EQUAL( actual_output_status, expected_output_status );
4955
Janos Follathaf3c2a02019-06-12 12:34:34 +01004956exit:
4957 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004958 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4959 psa_destroy_key( keys[i] );
4960 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004961 PSA_DONE( );
4962}
4963/* END_CASE */
4964
Janos Follathd958bb72019-07-03 15:02:16 +01004965/* BEGIN_CASE */
4966void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004967{
Janos Follathd958bb72019-07-03 15:02:16 +01004968 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004969 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004970 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004972 unsigned char input1[] = "Input 1";
4973 size_t input1_length = sizeof( input1 );
4974 unsigned char input2[] = "Input 2";
4975 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004976 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004977 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004978 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4979 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4980 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004982
Gilles Peskine8817f612018-12-18 00:18:46 +01004983 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004984
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004985 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4986 psa_set_key_algorithm( &attributes, alg );
4987 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004988
Gilles Peskine73676cb2019-05-15 20:15:10 +02004989 PSA_ASSERT( psa_import_key( &attributes,
4990 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004991 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004992
4993 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004994 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4995 input1, input1_length,
4996 input2, input2_length,
4997 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004998 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005000 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005001 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005002 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005003
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005005
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005006 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005007 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005008
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005009exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005010 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005011 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005012 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005013}
5014/* END_CASE */
5015
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005016/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005017void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005018{
5019 uint8_t output_buffer[16];
5020 size_t buffer_size = 16;
5021 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005023
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005024 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5025 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005026 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005027
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005029 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005030
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005031 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005032
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005033 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5034 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005035 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005036
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005038 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005039
5040exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005042}
5043/* END_CASE */
5044
5045/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005046void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005047 int step1_arg, data_t *input1,
5048 int step2_arg, data_t *input2,
5049 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005050 int requested_capacity_arg,
5051 data_t *expected_output1,
5052 data_t *expected_output2 )
5053{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005054 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005055 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5056 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005057 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5058 MBEDTLS_SVC_KEY_ID_INIT,
5059 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005060 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005061 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005062 uint8_t *expected_outputs[2] =
5063 {expected_output1->x, expected_output2->x};
5064 size_t output_sizes[2] =
5065 {expected_output1->len, expected_output2->len};
5066 size_t output_buffer_size = 0;
5067 uint8_t *output_buffer = NULL;
5068 size_t expected_capacity;
5069 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005071 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005072 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005073
5074 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5075 {
5076 if( output_sizes[i] > output_buffer_size )
5077 output_buffer_size = output_sizes[i];
5078 if( output_sizes[i] == 0 )
5079 expected_outputs[i] = NULL;
5080 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005081 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005082 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005083
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5085 psa_set_key_algorithm( &attributes, alg );
5086 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005087
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005088 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005089 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5090 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5091 requested_capacity ) );
5092 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005093 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005094 switch( steps[i] )
5095 {
5096 case 0:
5097 break;
5098 case PSA_KEY_DERIVATION_INPUT_SECRET:
5099 PSA_ASSERT( psa_import_key( &attributes,
5100 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005101 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005102
5103 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5104 {
5105 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5106 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5107 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5108 }
5109
Gilles Peskine1468da72019-05-29 17:35:49 +02005110 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005111 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005112 break;
5113 default:
5114 PSA_ASSERT( psa_key_derivation_input_bytes(
5115 &operation, steps[i],
5116 inputs[i]->x, inputs[i]->len ) );
5117 break;
5118 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005119 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005120
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005122 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005123 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005124 expected_capacity = requested_capacity;
5125
5126 /* Expansion phase. */
5127 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5128 {
5129 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005130 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005131 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005132 if( expected_capacity == 0 && output_sizes[i] == 0 )
5133 {
5134 /* Reading 0 bytes when 0 bytes are available can go either way. */
5135 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005136 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005137 continue;
5138 }
5139 else if( expected_capacity == 0 ||
5140 output_sizes[i] > expected_capacity )
5141 {
5142 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005143 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005144 expected_capacity = 0;
5145 continue;
5146 }
5147 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005148 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005149 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005150 ASSERT_COMPARE( output_buffer, output_sizes[i],
5151 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005153 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005155 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005156 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005157 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005158 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005159
5160exit:
5161 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005163 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5164 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005165 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005166}
5167/* END_CASE */
5168
5169/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005170void derive_full( int alg_arg,
5171 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005172 data_t *input1,
5173 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005174 int requested_capacity_arg )
5175{
Ronald Cron5425a212020-08-04 14:58:35 +02005176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005177 psa_algorithm_t alg = alg_arg;
5178 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005179 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005180 unsigned char output_buffer[16];
5181 size_t expected_capacity = requested_capacity;
5182 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005184
Gilles Peskine8817f612018-12-18 00:18:46 +01005185 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005186
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5188 psa_set_key_algorithm( &attributes, alg );
5189 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005190
Gilles Peskine049c7532019-05-15 20:22:09 +02005191 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005192 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005193
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005194 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5195 input1->x, input1->len,
5196 input2->x, input2->len,
5197 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005198 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005199
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005201 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005202 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005203
5204 /* Expansion phase. */
5205 while( current_capacity > 0 )
5206 {
5207 size_t read_size = sizeof( output_buffer );
5208 if( read_size > current_capacity )
5209 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005211 output_buffer,
5212 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005213 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005215 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005216 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005217 }
5218
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 /* Check that the operation refuses to go over capacity. */
5220 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005221 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005222
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005224
5225exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005227 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005228 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005229}
5230/* END_CASE */
5231
Janos Follathe60c9052019-07-03 13:51:30 +01005232/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005233void derive_key_exercise( int alg_arg,
5234 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005235 data_t *input1,
5236 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005237 int derived_type_arg,
5238 int derived_bits_arg,
5239 int derived_usage_arg,
5240 int derived_alg_arg )
5241{
Ronald Cron5425a212020-08-04 14:58:35 +02005242 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5243 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005244 psa_algorithm_t alg = alg_arg;
5245 psa_key_type_t derived_type = derived_type_arg;
5246 size_t derived_bits = derived_bits_arg;
5247 psa_key_usage_t derived_usage = derived_usage_arg;
5248 psa_algorithm_t derived_alg = derived_alg_arg;
5249 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005252 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005253
Gilles Peskine8817f612018-12-18 00:18:46 +01005254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005255
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005256 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5257 psa_set_key_algorithm( &attributes, alg );
5258 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005259 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005260 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005261
5262 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005263 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5264 input1->x, input1->len,
5265 input2->x, input2->len,
5266 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005267 goto exit;
5268
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005269 psa_set_key_usage_flags( &attributes, derived_usage );
5270 psa_set_key_algorithm( &attributes, derived_alg );
5271 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005272 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005274 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005275
5276 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005277 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005278 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5279 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005280
5281 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005282 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005283 goto exit;
5284
5285exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005286 /*
5287 * Key attributes may have been returned by psa_get_key_attributes()
5288 * thus reset them as required.
5289 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005290 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005291
5292 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005293 psa_destroy_key( base_key );
5294 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005295 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005296}
5297/* END_CASE */
5298
Janos Follath42fd8882019-07-03 14:17:09 +01005299/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005300void derive_key_export( int alg_arg,
5301 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005302 data_t *input1,
5303 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005304 int bytes1_arg,
5305 int bytes2_arg )
5306{
Ronald Cron5425a212020-08-04 14:58:35 +02005307 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5308 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005309 psa_algorithm_t alg = alg_arg;
5310 size_t bytes1 = bytes1_arg;
5311 size_t bytes2 = bytes2_arg;
5312 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005313 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005314 uint8_t *output_buffer = NULL;
5315 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005316 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5317 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005318 size_t length;
5319
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005320 ASSERT_ALLOC( output_buffer, capacity );
5321 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005323
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005324 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5325 psa_set_key_algorithm( &base_attributes, alg );
5326 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005327 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005328 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005329
5330 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005331 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5332 input1->x, input1->len,
5333 input2->x, input2->len,
5334 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005335 goto exit;
5336
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005338 output_buffer,
5339 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005341
5342 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005343 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5344 input1->x, input1->len,
5345 input2->x, input2->len,
5346 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005347 goto exit;
5348
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005349 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5350 psa_set_key_algorithm( &derived_attributes, 0 );
5351 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005352 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005353 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005354 &derived_key ) );
5355 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005356 export_buffer, bytes1,
5357 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005358 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005359 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005360 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005361 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005362 &derived_key ) );
5363 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005364 export_buffer + bytes1, bytes2,
5365 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005366 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005367
5368 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005369 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5370 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005371
5372exit:
5373 mbedtls_free( output_buffer );
5374 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005375 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005376 psa_destroy_key( base_key );
5377 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005378 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005379}
5380/* END_CASE */
5381
5382/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005383void derive_key( int alg_arg,
5384 data_t *key_data, data_t *input1, data_t *input2,
5385 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005386 int expected_status_arg,
5387 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005388{
Ronald Cron5425a212020-08-04 14:58:35 +02005389 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5390 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005391 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005392 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005393 size_t bits = bits_arg;
5394 psa_status_t expected_status = expected_status_arg;
5395 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5396 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5397 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5398
5399 PSA_ASSERT( psa_crypto_init( ) );
5400
5401 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5402 psa_set_key_algorithm( &base_attributes, alg );
5403 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5404 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005405 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005406
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005407 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5408 input1->x, input1->len,
5409 input2->x, input2->len,
5410 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005411 goto exit;
5412
5413 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5414 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005415 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005416 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005417
5418 psa_status_t status =
5419 psa_key_derivation_output_key( &derived_attributes,
5420 &operation,
5421 &derived_key );
5422 if( is_large_output > 0 )
5423 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5424 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005425
5426exit:
5427 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005428 psa_destroy_key( base_key );
5429 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005430 PSA_DONE( );
5431}
5432/* END_CASE */
5433
5434/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005435void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005436 int our_key_type_arg, int our_key_alg_arg,
5437 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005438 int expected_status_arg )
5439{
Ronald Cron5425a212020-08-04 14:58:35 +02005440 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005441 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005442 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005443 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005446 psa_status_t expected_status = expected_status_arg;
5447 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005448
Gilles Peskine8817f612018-12-18 00:18:46 +01005449 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005450
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005452 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005453 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005454 PSA_ASSERT( psa_import_key( &attributes,
5455 our_key_data->x, our_key_data->len,
5456 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005457
Gilles Peskine77f40d82019-04-11 21:27:06 +02005458 /* The tests currently include inputs that should fail at either step.
5459 * Test cases that fail at the setup step should be changed to call
5460 * key_derivation_setup instead, and this function should be renamed
5461 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005462 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005463 if( status == PSA_SUCCESS )
5464 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005465 TEST_EQUAL( psa_key_derivation_key_agreement(
5466 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5467 our_key,
5468 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005469 expected_status );
5470 }
5471 else
5472 {
5473 TEST_ASSERT( status == expected_status );
5474 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005475
5476exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005478 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005479 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005480}
5481/* END_CASE */
5482
5483/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005484void raw_key_agreement( int alg_arg,
5485 int our_key_type_arg, data_t *our_key_data,
5486 data_t *peer_key_data,
5487 data_t *expected_output )
5488{
Ronald Cron5425a212020-08-04 14:58:35 +02005489 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005490 psa_algorithm_t alg = alg_arg;
5491 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005493 unsigned char *output = NULL;
5494 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005495 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005496
5497 ASSERT_ALLOC( output, expected_output->len );
5498 PSA_ASSERT( psa_crypto_init( ) );
5499
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005500 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5501 psa_set_key_algorithm( &attributes, alg );
5502 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005503 PSA_ASSERT( psa_import_key( &attributes,
5504 our_key_data->x, our_key_data->len,
5505 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005506
gabor-mezei-armceface22021-01-21 12:26:17 +01005507 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5508 key_bits = psa_get_key_bits( &attributes );
5509
Gilles Peskinebe697d82019-05-16 18:00:41 +02005510 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5511 peer_key_data->x, peer_key_data->len,
5512 output, expected_output->len,
5513 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005514 ASSERT_COMPARE( output, output_length,
5515 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005516 TEST_ASSERT( output_length <=
5517 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5518 TEST_ASSERT( output_length <=
5519 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005520
5521exit:
5522 mbedtls_free( output );
5523 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005524 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005525}
5526/* END_CASE */
5527
5528/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005529void key_agreement_capacity( int alg_arg,
5530 int our_key_type_arg, data_t *our_key_data,
5531 data_t *peer_key_data,
5532 int expected_capacity_arg )
5533{
Ronald Cron5425a212020-08-04 14:58:35 +02005534 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005535 psa_algorithm_t alg = alg_arg;
5536 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005539 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005540 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005541
Gilles Peskine8817f612018-12-18 00:18:46 +01005542 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005543
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5545 psa_set_key_algorithm( &attributes, alg );
5546 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005547 PSA_ASSERT( psa_import_key( &attributes,
5548 our_key_data->x, our_key_data->len,
5549 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005550
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005551 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005552 PSA_ASSERT( psa_key_derivation_key_agreement(
5553 &operation,
5554 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5555 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005556 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5557 {
5558 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005559 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005560 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005561 NULL, 0 ) );
5562 }
Gilles Peskine59685592018-09-18 12:11:34 +02005563
Gilles Peskinebf491972018-10-25 22:36:12 +02005564 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005565 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005566 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005567 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005568
Gilles Peskinebf491972018-10-25 22:36:12 +02005569 /* Test the actual capacity by reading the output. */
5570 while( actual_capacity > sizeof( output ) )
5571 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005573 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005574 actual_capacity -= sizeof( output );
5575 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005576 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005577 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005578 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005579 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005580
Gilles Peskine59685592018-09-18 12:11:34 +02005581exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005582 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005583 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005584 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005585}
5586/* END_CASE */
5587
5588/* BEGIN_CASE */
5589void key_agreement_output( int alg_arg,
5590 int our_key_type_arg, data_t *our_key_data,
5591 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005592 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005593{
Ronald Cron5425a212020-08-04 14:58:35 +02005594 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005595 psa_algorithm_t alg = alg_arg;
5596 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005597 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005599 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005600
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005601 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5602 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005603
Gilles Peskine8817f612018-12-18 00:18:46 +01005604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005605
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005606 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5607 psa_set_key_algorithm( &attributes, alg );
5608 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005609 PSA_ASSERT( psa_import_key( &attributes,
5610 our_key_data->x, our_key_data->len,
5611 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005612
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005613 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005614 PSA_ASSERT( psa_key_derivation_key_agreement(
5615 &operation,
5616 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5617 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005618 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5619 {
5620 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005621 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005622 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005623 NULL, 0 ) );
5624 }
Gilles Peskine59685592018-09-18 12:11:34 +02005625
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005627 actual_output,
5628 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005629 ASSERT_COMPARE( actual_output, expected_output1->len,
5630 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005631 if( expected_output2->len != 0 )
5632 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005634 actual_output,
5635 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005636 ASSERT_COMPARE( actual_output, expected_output2->len,
5637 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005638 }
Gilles Peskine59685592018-09-18 12:11:34 +02005639
5640exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005642 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005643 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005644 mbedtls_free( actual_output );
5645}
5646/* END_CASE */
5647
5648/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005649void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005650{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005651 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005652 unsigned char *output = NULL;
5653 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005654 size_t i;
5655 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005656
Simon Butcher49f8e312020-03-03 15:51:50 +00005657 TEST_ASSERT( bytes_arg >= 0 );
5658
Gilles Peskine91892022021-02-08 19:50:26 +01005659 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005660 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005661
Gilles Peskine8817f612018-12-18 00:18:46 +01005662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005663
Gilles Peskinea50d7392018-06-21 10:22:13 +02005664 /* Run several times, to ensure that every output byte will be
5665 * nonzero at least once with overwhelming probability
5666 * (2^(-8*number_of_runs)). */
5667 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005668 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005669 if( bytes != 0 )
5670 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005671 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005672
Gilles Peskinea50d7392018-06-21 10:22:13 +02005673 for( i = 0; i < bytes; i++ )
5674 {
5675 if( output[i] != 0 )
5676 ++changed[i];
5677 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005678 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005679
5680 /* Check that every byte was changed to nonzero at least once. This
5681 * validates that psa_generate_random is overwriting every byte of
5682 * the output buffer. */
5683 for( i = 0; i < bytes; i++ )
5684 {
5685 TEST_ASSERT( changed[i] != 0 );
5686 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005687
5688exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005689 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005690 mbedtls_free( output );
5691 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005692}
5693/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005694
5695/* BEGIN_CASE */
5696void generate_key( int type_arg,
5697 int bits_arg,
5698 int usage_arg,
5699 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005700 int expected_status_arg,
5701 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005702{
Ronald Cron5425a212020-08-04 14:58:35 +02005703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005704 psa_key_type_t type = type_arg;
5705 psa_key_usage_t usage = usage_arg;
5706 size_t bits = bits_arg;
5707 psa_algorithm_t alg = alg_arg;
5708 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005710 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005711
Gilles Peskine8817f612018-12-18 00:18:46 +01005712 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005713
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005714 psa_set_key_usage_flags( &attributes, usage );
5715 psa_set_key_algorithm( &attributes, alg );
5716 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005717 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005718
5719 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005720 psa_status_t status = psa_generate_key( &attributes, &key );
5721
5722 if( is_large_key > 0 )
5723 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5724 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005725 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005726 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005727
5728 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005729 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005730 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5731 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005732
Gilles Peskine818ca122018-06-20 18:16:48 +02005733 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005734 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005735 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005736
5737exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005738 /*
5739 * Key attributes may have been returned by psa_get_key_attributes()
5740 * thus reset them as required.
5741 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005742 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005743
Ronald Cron5425a212020-08-04 14:58:35 +02005744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005745 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005746}
5747/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005748
Ronald Cronee414c72021-03-18 18:50:08 +01005749/* 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 +02005750void generate_key_rsa( int bits_arg,
5751 data_t *e_arg,
5752 int expected_status_arg )
5753{
Ronald Cron5425a212020-08-04 14:58:35 +02005754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005755 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005756 size_t bits = bits_arg;
5757 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5758 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5759 psa_status_t expected_status = expected_status_arg;
5760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5761 uint8_t *exported = NULL;
5762 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005763 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005764 size_t exported_length = SIZE_MAX;
5765 uint8_t *e_read_buffer = NULL;
5766 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005767 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005768 size_t e_read_length = SIZE_MAX;
5769
5770 if( e_arg->len == 0 ||
5771 ( e_arg->len == 3 &&
5772 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5773 {
5774 is_default_public_exponent = 1;
5775 e_read_size = 0;
5776 }
5777 ASSERT_ALLOC( e_read_buffer, e_read_size );
5778 ASSERT_ALLOC( exported, exported_size );
5779
5780 PSA_ASSERT( psa_crypto_init( ) );
5781
5782 psa_set_key_usage_flags( &attributes, usage );
5783 psa_set_key_algorithm( &attributes, alg );
5784 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5785 e_arg->x, e_arg->len ) );
5786 psa_set_key_bits( &attributes, bits );
5787
5788 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005789 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005790 if( expected_status != PSA_SUCCESS )
5791 goto exit;
5792
5793 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005794 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005795 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5796 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5797 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5798 e_read_buffer, e_read_size,
5799 &e_read_length ) );
5800 if( is_default_public_exponent )
5801 TEST_EQUAL( e_read_length, 0 );
5802 else
5803 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5804
5805 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005806 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005807 goto exit;
5808
5809 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005810 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005811 exported, exported_size,
5812 &exported_length ) );
5813 {
5814 uint8_t *p = exported;
5815 uint8_t *end = exported + exported_length;
5816 size_t len;
5817 /* RSAPublicKey ::= SEQUENCE {
5818 * modulus INTEGER, -- n
5819 * publicExponent INTEGER } -- e
5820 */
5821 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005822 MBEDTLS_ASN1_SEQUENCE |
5823 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005824 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005825 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5826 MBEDTLS_ASN1_INTEGER ) );
5827 if( len >= 1 && p[0] == 0 )
5828 {
5829 ++p;
5830 --len;
5831 }
5832 if( e_arg->len == 0 )
5833 {
5834 TEST_EQUAL( len, 3 );
5835 TEST_EQUAL( p[0], 1 );
5836 TEST_EQUAL( p[1], 0 );
5837 TEST_EQUAL( p[2], 1 );
5838 }
5839 else
5840 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5841 }
5842
5843exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005844 /*
5845 * Key attributes may have been returned by psa_get_key_attributes() or
5846 * set by psa_set_key_domain_parameters() thus reset them as required.
5847 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005848 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005849
Ronald Cron5425a212020-08-04 14:58:35 +02005850 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005851 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005852 mbedtls_free( e_read_buffer );
5853 mbedtls_free( exported );
5854}
5855/* END_CASE */
5856
Darryl Greend49a4992018-06-18 17:27:26 +01005857/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005858void persistent_key_load_key_from_storage( data_t *data,
5859 int type_arg, int bits_arg,
5860 int usage_flags_arg, int alg_arg,
5861 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005862{
Ronald Cron71016a92020-08-28 19:01:50 +02005863 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005865 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5866 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005867 psa_key_type_t type = type_arg;
5868 size_t bits = bits_arg;
5869 psa_key_usage_t usage_flags = usage_flags_arg;
5870 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005871 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005872 unsigned char *first_export = NULL;
5873 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005874 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005875 size_t first_exported_length;
5876 size_t second_exported_length;
5877
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005878 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5879 {
5880 ASSERT_ALLOC( first_export, export_size );
5881 ASSERT_ALLOC( second_export, export_size );
5882 }
Darryl Greend49a4992018-06-18 17:27:26 +01005883
Gilles Peskine8817f612018-12-18 00:18:46 +01005884 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005885
Gilles Peskinec87af662019-05-15 16:12:22 +02005886 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005887 psa_set_key_usage_flags( &attributes, usage_flags );
5888 psa_set_key_algorithm( &attributes, alg );
5889 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005890 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005891
Darryl Green0c6575a2018-11-07 16:05:30 +00005892 switch( generation_method )
5893 {
5894 case IMPORT_KEY:
5895 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005896 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005897 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005898 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005899
Darryl Green0c6575a2018-11-07 16:05:30 +00005900 case GENERATE_KEY:
5901 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005902 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005903 break;
5904
5905 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005906#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005907 {
5908 /* Create base key */
5909 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5910 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5911 psa_set_key_usage_flags( &base_attributes,
5912 PSA_KEY_USAGE_DERIVE );
5913 psa_set_key_algorithm( &base_attributes, derive_alg );
5914 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005915 PSA_ASSERT( psa_import_key( &base_attributes,
5916 data->x, data->len,
5917 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005918 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005919 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005920 PSA_ASSERT( psa_key_derivation_input_key(
5921 &operation,
5922 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005923 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005924 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005925 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005926 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5927 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005928 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005929 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005930 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005931 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005932 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005933#else
5934 TEST_ASSUME( ! "KDF not supported in this configuration" );
5935#endif
5936 break;
5937
5938 default:
5939 TEST_ASSERT( ! "generation_method not implemented in test" );
5940 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005941 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005942 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005943
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005944 /* Export the key if permitted by the key policy. */
5945 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5946 {
Ronald Cron5425a212020-08-04 14:58:35 +02005947 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005948 first_export, export_size,
5949 &first_exported_length ) );
5950 if( generation_method == IMPORT_KEY )
5951 ASSERT_COMPARE( data->x, data->len,
5952 first_export, first_exported_length );
5953 }
Darryl Greend49a4992018-06-18 17:27:26 +01005954
5955 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005956 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005957 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005958 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005959
Darryl Greend49a4992018-06-18 17:27:26 +01005960 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005961 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005962 TEST_ASSERT( mbedtls_svc_key_id_equal(
5963 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005964 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5965 PSA_KEY_LIFETIME_PERSISTENT );
5966 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5967 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5968 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5969 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005970
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005971 /* Export the key again if permitted by the key policy. */
5972 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005973 {
Ronald Cron5425a212020-08-04 14:58:35 +02005974 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005975 second_export, export_size,
5976 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005977 ASSERT_COMPARE( first_export, first_exported_length,
5978 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005979 }
5980
5981 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005982 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005983 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005984
5985exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005986 /*
5987 * Key attributes may have been returned by psa_get_key_attributes()
5988 * thus reset them as required.
5989 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005990 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005991
Darryl Greend49a4992018-06-18 17:27:26 +01005992 mbedtls_free( first_export );
5993 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005994 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005995 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005996 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005997 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005998}
5999/* END_CASE */