blob: ac58b6edf3189f907036080163ad086e4dc38ba6 [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,
Paul Elliott5e3bb132021-06-22 16:22:13 +0100275 int test_set_lengths_arg,
Paul Elliottd3f82412021-06-16 16:52:21 +0100276 data_t *expected_result )
277{
278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
279 psa_key_type_t key_type = key_type_arg;
280 psa_algorithm_t alg = alg_arg;
281 psa_aead_operation_t operation;
282 unsigned char *output_data = NULL;
283 unsigned char *part_data = NULL;
284 unsigned char *final_data = NULL;
285 size_t output_size = 0;
286 size_t finish_output_size;
287 size_t part_data_size = 0;
288 size_t output_length = 0;
289 size_t key_bits = 0;
290 size_t tag_length = 0;
291 size_t tag_size = 0;
292 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
293 uint32_t part_offset = 0;
294 size_t part_length = 0;
295 size_t output_part_length = 0;
296 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
297 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
298
299 PSA_ASSERT( psa_crypto_init( ) );
300
301 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
302 psa_set_key_algorithm( &attributes, alg );
303 psa_set_key_type( &attributes, key_type );
304
305 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
306 &key ) );
307
308 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
309 key_bits = psa_get_key_bits( &attributes );
310
311 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
312
313 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
314
315 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
316 ( input_data->len +
317 tag_length ) );
318
319 ASSERT_ALLOC( output_data, output_size );
320
321 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
322
323 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
324
325 ASSERT_ALLOC( final_data, finish_output_size );
326
327 operation = psa_aead_operation_init( );
328
329 status = psa_aead_encrypt_setup( &operation, key, alg );
330
331 /* If the operation is not supported, just skip and not fail in case the
332 * encryption involves a common limitation of cryptography hardwares and
333 * an alternative implementation. */
334 if( status == PSA_ERROR_NOT_SUPPORTED )
335 {
336 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
337 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
338 }
339
340 PSA_ASSERT( status );
341
342 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
343
Paul Elliott5e3bb132021-06-22 16:22:13 +0100344 if( test_set_lengths_arg )
Paul Elliottd3f82412021-06-16 16:52:21 +0100345 {
346 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
347 input_data->len ) );
348 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100349
350 if( ad_part_len != -1 )
351 {
352 /* Pass additional data in parts */
353 part_offset = 0;
354
355 while( part_offset < additional_data->len )
356 {
357 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
358 {
359 part_length = additional_data->len - part_offset;
360 }
361 else
362 {
363 part_length = ad_part_len;
364 }
365
366 PSA_ASSERT( psa_aead_update_ad( &operation,
367 additional_data->x + part_offset,
368 part_length ) );
369
370 part_offset += part_length;
371 }
372 }
373 else
374 {
375 /* Pass additional data in one go. */
376 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
377 additional_data->len ) );
378 }
379
380 if( data_part_len != -1 )
381 {
382 /* Pass data in parts */
383 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
384 ( size_t ) data_part_len );
385
386 ASSERT_ALLOC( part_data, part_data_size );
387
388 part_offset = 0;
389
390 while( part_offset < input_data->len )
391 {
392 if( input_data->len - part_offset < ( uint32_t ) data_part_len )
393 {
394 part_length = input_data->len - part_offset;
395 }
396 else
397 {
398 part_length = data_part_len;
399 }
400
401 PSA_ASSERT( psa_aead_update( &operation,
402 ( input_data->x + part_offset ),
403 part_length, part_data,
404 part_data_size,
405 &output_part_length ) );
406
407 if( output_data && output_part_length )
408 {
409 memcpy( ( output_data + part_offset ), part_data,
410 output_part_length );
411 }
412
413 part_offset += part_length;
414 output_length += output_part_length;
415 }
416 }
417 else
418 {
419 /* Pass whole data in one go */
420 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
421 input_data->len, output_data,
422 output_size, &output_length ) );
423 }
424
425 PSA_ASSERT( psa_aead_finish( &operation, final_data,
426 finish_output_size,
427 &output_part_length,
428 tag_buffer, tag_length,
429 &tag_size ) );
430
431 if( output_data && output_part_length )
432 {
433 memcpy( ( output_data + output_length ), final_data,
434 output_part_length );
435 }
436
437 TEST_EQUAL( tag_length, tag_size );
438
439 output_length += output_part_length;
440
441 if( output_data && tag_length )
442 {
443 memcpy( ( output_data + output_length ), tag_buffer, tag_length );
444 }
445
446 output_length += tag_length;
447
448 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
449 * should be exact. */
450 TEST_EQUAL( output_length,
451 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
452 input_data->len ) );
453 TEST_ASSERT( output_length <=
454 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
455
456 ASSERT_COMPARE( expected_result->x, expected_result->len,
457 output_data, output_length );
458
459exit:
460 psa_destroy_key( key );
461 psa_aead_abort( &operation );
462 mbedtls_free( output_data );
463 mbedtls_free( part_data );
464 mbedtls_free( final_data );
465 PSA_DONE( );
466
467 return( status );
468}
469
Paul Elliott87c909a2021-06-24 18:07:39 +0100470static void aead_multipart_decrypt_internal( int key_type_arg, data_t *key_data,
471 int alg_arg,
472 data_t *nonce,
473 data_t *additional_data,
474 int ad_part_len,
475 data_t *input_data,
476 int data_part_len,
477 int test_set_lengths_arg,
478 data_t *expected_data,
479 int expected_result_arg )
Paul Elliottd3f82412021-06-16 16:52:21 +0100480{
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
Paul Elliott5e3bb132021-06-22 16:22:13 +0100552 if( test_set_lengths_arg )
Paul Elliottd3f82412021-06-16 16:52:21 +0100553 {
554 status = psa_aead_set_lengths( &operation, additional_data->len,
555 ( input_data->len - tag_length ) );
556
557 if( status != PSA_SUCCESS )
558 {
559 TEST_EQUAL( status, expected_result_arg );
560 goto exit;
561 }
562 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100563
564 if( ad_part_len != -1 )
565 {
566 part_offset = 0;
567
568 while( part_offset < additional_data->len )
569 {
570 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
571 {
572 part_length = additional_data->len - part_offset;
573 }
574 else
575 {
576 part_length = ad_part_len;
577 }
578
579 status = psa_aead_update_ad( &operation,
580 additional_data->x + part_offset,
581 part_length );
582
583 if( status != PSA_SUCCESS )
584 {
585 TEST_EQUAL( status, expected_result_arg );
586 goto exit;
587 }
588
589 part_offset += part_length;
590 }
591 }
592 else
593 {
594 status = psa_aead_update_ad( &operation, additional_data->x,
595 additional_data->len );
596
597 if( status != PSA_SUCCESS )
598 {
599 TEST_EQUAL( status, expected_result_arg );
600 goto exit;
601 }
602 }
603
604 if( data_part_len != -1 )
605 {
606 /* Pass data in parts */
607 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
608 ( size_t ) data_part_len );
609
610 ASSERT_ALLOC( part_data, part_data_size );
611
612 part_offset = 0;
613
614 while( part_offset < ( input_data->len - tag_length) )
615 {
616 if( (input_data->len - tag_length - part_offset ) <
617 ( uint32_t ) data_part_len )
618 {
619 part_length = ( input_data->len - tag_length - part_offset );
620 }
621 else
622 {
623 part_length = data_part_len;
624 }
625
626 status = psa_aead_update( &operation,
627 ( input_data->x + part_offset ),
628 part_length, part_data,
629 part_data_size, &output_part_length );
630
631 if( status != PSA_SUCCESS )
632 {
633 TEST_EQUAL( status, expected_result_arg );
634 goto exit;
635 }
636
637 if( output_data && output_part_length )
638 {
639 memcpy( ( output_data + part_offset ), part_data,
640 output_part_length );
641 }
642
643 part_offset += part_length;
644 output_length += output_part_length;
645 }
646 }
647 else
648 {
649 status = psa_aead_update( &operation, input_data->x,
650 ( input_data->len - tag_length ), output_data,
651 output_size, &output_length );
652
653 if( status != PSA_SUCCESS )
654 {
655 TEST_EQUAL( status, expected_result_arg );
656 goto exit;
657 }
658 }
659
660 status = psa_aead_verify( &operation, final_data,
661 verify_output_size,
662 &output_part_length,
663 ( input_data->x + input_data->len - tag_length ),
664 tag_length );
665
666 if( status != PSA_SUCCESS )
667 {
668 TEST_EQUAL( status, expected_result_arg );
669 goto exit;
670 }
671
672 if( output_data && output_part_length )
673 {
674 memcpy( ( output_data + output_length ), final_data,
675 output_part_length );
676 }
677
678 output_length += output_part_length;
679
680 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
681 {
682 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
683 * should be exact. */
684 TEST_EQUAL( output_length,
685 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
686 input_data->len ) );
687 TEST_ASSERT( output_length <=
688 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
689 }
690
691 if( expected_result == PSA_SUCCESS )
692 {
693 ASSERT_COMPARE( expected_data->x, expected_data->len,
694 output_data, output_length );
695 }
696
697exit:
698 psa_destroy_key( key );
699 psa_aead_abort( &operation );
700 mbedtls_free( output_data );
701 mbedtls_free( part_data );
702 mbedtls_free( final_data );
703 PSA_DONE( );
704}
705
Gilles Peskinee59236f2018-01-27 23:32:46 +0100706/* END_HEADER */
707
708/* BEGIN_DEPENDENCIES
709 * depends_on:MBEDTLS_PSA_CRYPTO_C
710 * END_DEPENDENCIES
711 */
712
713/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200714void static_checks( )
715{
716 size_t max_truncated_mac_size =
717 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
718
719 /* Check that the length for a truncated MAC always fits in the algorithm
720 * encoding. The shifted mask is the maximum truncated value. The
721 * untruncated algorithm may be one byte larger. */
722 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
723}
724/* END_CASE */
725
726/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200727void import_with_policy( int type_arg,
728 int usage_arg, int alg_arg,
729 int expected_status_arg )
730{
731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
732 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200733 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200734 psa_key_type_t type = type_arg;
735 psa_key_usage_t usage = usage_arg;
736 psa_algorithm_t alg = alg_arg;
737 psa_status_t expected_status = expected_status_arg;
738 const uint8_t key_material[16] = {0};
739 psa_status_t status;
740
741 PSA_ASSERT( psa_crypto_init( ) );
742
743 psa_set_key_type( &attributes, type );
744 psa_set_key_usage_flags( &attributes, usage );
745 psa_set_key_algorithm( &attributes, alg );
746
747 status = psa_import_key( &attributes,
748 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200749 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200750 TEST_EQUAL( status, expected_status );
751 if( status != PSA_SUCCESS )
752 goto exit;
753
Ronald Cron5425a212020-08-04 14:58:35 +0200754 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200755 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
756 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
757 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200758 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200759
Ronald Cron5425a212020-08-04 14:58:35 +0200760 PSA_ASSERT( psa_destroy_key( key ) );
761 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200762
763exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100764 /*
765 * Key attributes may have been returned by psa_get_key_attributes()
766 * thus reset them as required.
767 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200768 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100769
770 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200771 PSA_DONE( );
772}
773/* END_CASE */
774
775/* BEGIN_CASE */
776void import_with_data( data_t *data, int type_arg,
777 int attr_bits_arg,
778 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200779{
780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
781 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200783 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200784 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200785 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787
Gilles Peskine8817f612018-12-18 00:18:46 +0100788 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789
Gilles Peskine4747d192019-04-17 15:05:45 +0200790 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200791 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200792
Ronald Cron5425a212020-08-04 14:58:35 +0200793 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100794 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200795 if( status != PSA_SUCCESS )
796 goto exit;
797
Ronald Cron5425a212020-08-04 14:58:35 +0200798 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200799 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200800 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200801 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200802 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200803
Ronald Cron5425a212020-08-04 14:58:35 +0200804 PSA_ASSERT( psa_destroy_key( key ) );
805 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806
807exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100808 /*
809 * Key attributes may have been returned by psa_get_key_attributes()
810 * thus reset them as required.
811 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200812 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100813
814 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200815 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816}
817/* END_CASE */
818
819/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200820void import_large_key( int type_arg, int byte_size_arg,
821 int expected_status_arg )
822{
823 psa_key_type_t type = type_arg;
824 size_t byte_size = byte_size_arg;
825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
826 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200828 psa_status_t status;
829 uint8_t *buffer = NULL;
830 size_t buffer_size = byte_size + 1;
831 size_t n;
832
Steven Cooreman69967ce2021-01-18 18:01:08 +0100833 /* Skip the test case if the target running the test cannot
834 * accomodate large keys due to heap size constraints */
835 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200836 memset( buffer, 'K', byte_size );
837
838 PSA_ASSERT( psa_crypto_init( ) );
839
840 /* Try importing the key */
841 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
842 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200843 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100844 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200845 TEST_EQUAL( status, expected_status );
846
847 if( status == PSA_SUCCESS )
848 {
Ronald Cron5425a212020-08-04 14:58:35 +0200849 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200850 TEST_EQUAL( psa_get_key_type( &attributes ), type );
851 TEST_EQUAL( psa_get_key_bits( &attributes ),
852 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200853 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200854 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200855 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200856 for( n = 0; n < byte_size; n++ )
857 TEST_EQUAL( buffer[n], 'K' );
858 for( n = byte_size; n < buffer_size; n++ )
859 TEST_EQUAL( buffer[n], 0 );
860 }
861
862exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100863 /*
864 * Key attributes may have been returned by psa_get_key_attributes()
865 * thus reset them as required.
866 */
867 psa_reset_key_attributes( &attributes );
868
Ronald Cron5425a212020-08-04 14:58:35 +0200869 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200870 PSA_DONE( );
871 mbedtls_free( buffer );
872}
873/* END_CASE */
874
875/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200876void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
877{
Ronald Cron5425a212020-08-04 14:58:35 +0200878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200879 size_t bits = bits_arg;
880 psa_status_t expected_status = expected_status_arg;
881 psa_status_t status;
882 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200883 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200884 size_t buffer_size = /* Slight overapproximations */
885 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200886 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200887 unsigned char *p;
888 int ret;
889 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200891
Gilles Peskine8817f612018-12-18 00:18:46 +0100892 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200893 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200894
895 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
896 bits, keypair ) ) >= 0 );
897 length = ret;
898
899 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200900 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200901 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100902 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200903
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200904 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200905 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200906
907exit:
908 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200909 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200910}
911/* END_CASE */
912
913/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300914void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300915 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200916 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100917 int expected_bits,
918 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200919 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100920 int canonical_input )
921{
Ronald Cron5425a212020-08-04 14:58:35 +0200922 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100923 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200924 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200925 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100927 unsigned char *exported = NULL;
928 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100929 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100930 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100931 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200933 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100934
Moran Pekercb088e72018-07-17 17:36:59 +0300935 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200936 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200938 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100939 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940
Gilles Peskine4747d192019-04-17 15:05:45 +0200941 psa_set_key_usage_flags( &attributes, usage_arg );
942 psa_set_key_algorithm( &attributes, alg );
943 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700944
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200946 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947
948 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200949 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200950 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
951 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200952 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953
954 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200955 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100956 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100957
958 /* The exported length must be set by psa_export_key() to a value between 0
959 * and export_size. On errors, the exported length must be 0. */
960 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
961 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
962 TEST_ASSERT( exported_length <= export_size );
963
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200964 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200965 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200967 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100968 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200970 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971
Gilles Peskineea38a922021-02-13 00:05:16 +0100972 /* Run sanity checks on the exported key. For non-canonical inputs,
973 * this validates the canonical representations. For canonical inputs,
974 * this doesn't directly validate the implementation, but it still helps
975 * by cross-validating the test data with the sanity check code. */
976 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200977 goto exit;
978
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200980 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 else
982 {
Ronald Cron5425a212020-08-04 14:58:35 +0200983 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200984 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200985 &key2 ) );
986 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100987 reexported,
988 export_size,
989 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200990 ASSERT_COMPARE( exported, exported_length,
991 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200992 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100994 TEST_ASSERT( exported_length <=
995 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
996 psa_get_key_bits( &got_attributes ) ) );
997 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998
999destroy:
1000 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001001 PSA_ASSERT( psa_destroy_key( key ) );
1002 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003
1004exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001005 /*
1006 * Key attributes may have been returned by psa_get_key_attributes()
1007 * thus reset them as required.
1008 */
1009 psa_reset_key_attributes( &got_attributes );
1010
itayzafrir3e02b3b2018-06-12 17:06:52 +03001011 mbedtls_free( exported );
1012 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001013 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014}
1015/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001016
Moran Pekerf709f4a2018-06-06 17:26:04 +03001017/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001018void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001019 int type_arg,
1020 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001021 int export_size_delta,
1022 int expected_export_status_arg,
1023 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001024{
Ronald Cron5425a212020-08-04 14:58:35 +02001025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001026 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001027 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001028 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001029 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001030 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001031 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001032 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001034
Gilles Peskine8817f612018-12-18 00:18:46 +01001035 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001036
Gilles Peskine4747d192019-04-17 15:05:45 +02001037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1038 psa_set_key_algorithm( &attributes, alg );
1039 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001040
1041 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001042 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001043
Gilles Peskine49c25912018-10-29 15:15:31 +01001044 /* Export the public key */
1045 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001046 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001047 exported, export_size,
1048 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001049 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001050 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001051 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001052 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001053 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001054 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001055 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001056 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001057 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01001058 TEST_ASSERT( expected_public_key->len <=
1059 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1060 TEST_ASSERT( expected_public_key->len <=
1061 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001062 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1063 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001064 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001065
1066exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001067 /*
1068 * Key attributes may have been returned by psa_get_key_attributes()
1069 * thus reset them as required.
1070 */
1071 psa_reset_key_attributes( &attributes );
1072
itayzafrir3e02b3b2018-06-12 17:06:52 +03001073 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001074 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001075 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001076}
1077/* END_CASE */
1078
Gilles Peskine20035e32018-02-03 22:44:14 +01001079/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001080void import_and_exercise_key( data_t *data,
1081 int type_arg,
1082 int bits_arg,
1083 int alg_arg )
1084{
Ronald Cron5425a212020-08-04 14:58:35 +02001085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001086 psa_key_type_t type = type_arg;
1087 size_t bits = bits_arg;
1088 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001089 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001091 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001092
Gilles Peskine8817f612018-12-18 00:18:46 +01001093 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001094
Gilles Peskine4747d192019-04-17 15:05:45 +02001095 psa_set_key_usage_flags( &attributes, usage );
1096 psa_set_key_algorithm( &attributes, alg );
1097 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001098
1099 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001100 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001101
1102 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001103 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001104 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1105 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001106
1107 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001108 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001109 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001110
Ronald Cron5425a212020-08-04 14:58:35 +02001111 PSA_ASSERT( psa_destroy_key( key ) );
1112 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001113
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001114exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001115 /*
1116 * Key attributes may have been returned by psa_get_key_attributes()
1117 * thus reset them as required.
1118 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001119 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001120
1121 psa_reset_key_attributes( &attributes );
1122 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001123 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001124}
1125/* END_CASE */
1126
1127/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001128void effective_key_attributes( int type_arg, int expected_type_arg,
1129 int bits_arg, int expected_bits_arg,
1130 int usage_arg, int expected_usage_arg,
1131 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001132{
Ronald Cron5425a212020-08-04 14:58:35 +02001133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001134 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001135 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001136 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001137 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001138 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001139 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001140 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001141 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
Gilles Peskine8817f612018-12-18 00:18:46 +01001144 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001145
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001146 psa_set_key_usage_flags( &attributes, usage );
1147 psa_set_key_algorithm( &attributes, alg );
1148 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001149 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001150
Ronald Cron5425a212020-08-04 14:58:35 +02001151 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001152 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001153
Ronald Cron5425a212020-08-04 14:58:35 +02001154 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001155 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1156 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1157 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1158 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001159
1160exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001161 /*
1162 * Key attributes may have been returned by psa_get_key_attributes()
1163 * thus reset them as required.
1164 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001165 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001166
1167 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001168 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001169}
1170/* END_CASE */
1171
1172/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001173void check_key_policy( int type_arg, int bits_arg,
1174 int usage_arg, int alg_arg )
1175{
1176 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1177 usage_arg, usage_arg, alg_arg, alg_arg );
1178 goto exit;
1179}
1180/* END_CASE */
1181
1182/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001183void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001184{
1185 /* Test each valid way of initializing the object, except for `= {0}`, as
1186 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1187 * though it's OK by the C standard. We could test for this, but we'd need
1188 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001189 psa_key_attributes_t func = psa_key_attributes_init( );
1190 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1191 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001192
1193 memset( &zero, 0, sizeof( zero ) );
1194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001195 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1196 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1197 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001198
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001199 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1200 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1201 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1202
1203 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1204 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1205 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1206
1207 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1208 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1209 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1210
1211 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1212 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1213 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001214}
1215/* END_CASE */
1216
1217/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218void mac_key_policy( int policy_usage,
1219 int policy_alg,
1220 int key_type,
1221 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001222 int exercise_alg,
1223 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001224{
Ronald Cron5425a212020-08-04 14:58:35 +02001225 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001227 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001228 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001229 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001231
Gilles Peskine8817f612018-12-18 00:18:46 +01001232 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001233
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001234 psa_set_key_usage_flags( &attributes, policy_usage );
1235 psa_set_key_algorithm( &attributes, policy_alg );
1236 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001237
Gilles Peskine049c7532019-05-15 20:22:09 +02001238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001239 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001240
Ronald Cron5425a212020-08-04 14:58:35 +02001241 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001242 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001243 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001244 else
1245 TEST_EQUAL( status, expected_status );
1246
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001247 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001248
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001250 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001251 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001252 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001253 else
1254 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001255
1256exit:
1257 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001258 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001259 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260}
1261/* END_CASE */
1262
1263/* BEGIN_CASE */
1264void cipher_key_policy( int policy_usage,
1265 int policy_alg,
1266 int key_type,
1267 data_t *key_data,
1268 int exercise_alg )
1269{
Ronald Cron5425a212020-08-04 14:58:35 +02001270 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001272 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273 psa_status_t status;
1274
Gilles Peskine8817f612018-12-18 00:18:46 +01001275 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001276
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001277 psa_set_key_usage_flags( &attributes, policy_usage );
1278 psa_set_key_algorithm( &attributes, policy_alg );
1279 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001280
Gilles Peskine049c7532019-05-15 20:22:09 +02001281 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001282 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283
Ronald Cron5425a212020-08-04 14:58:35 +02001284 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001285 if( policy_alg == exercise_alg &&
1286 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001287 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001288 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001289 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001290 psa_cipher_abort( &operation );
1291
Ronald Cron5425a212020-08-04 14:58:35 +02001292 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293 if( policy_alg == exercise_alg &&
1294 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001295 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001297 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001298
1299exit:
1300 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001301 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001302 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001303}
1304/* END_CASE */
1305
1306/* BEGIN_CASE */
1307void aead_key_policy( int policy_usage,
1308 int policy_alg,
1309 int key_type,
1310 data_t *key_data,
1311 int nonce_length_arg,
1312 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001313 int exercise_alg,
1314 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001315{
Ronald Cron5425a212020-08-04 14:58:35 +02001316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001318 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001319 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 unsigned char nonce[16] = {0};
1321 size_t nonce_length = nonce_length_arg;
1322 unsigned char tag[16];
1323 size_t tag_length = tag_length_arg;
1324 size_t output_length;
1325
1326 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1327 TEST_ASSERT( tag_length <= sizeof( tag ) );
1328
Gilles Peskine8817f612018-12-18 00:18:46 +01001329 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001331 psa_set_key_usage_flags( &attributes, policy_usage );
1332 psa_set_key_algorithm( &attributes, policy_alg );
1333 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334
Gilles Peskine049c7532019-05-15 20:22:09 +02001335 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001336 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001337
Ronald Cron5425a212020-08-04 14:58:35 +02001338 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339 nonce, nonce_length,
1340 NULL, 0,
1341 NULL, 0,
1342 tag, tag_length,
1343 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001344 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1345 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001347 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348
1349 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351 nonce, nonce_length,
1352 NULL, 0,
1353 tag, tag_length,
1354 NULL, 0,
1355 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001356 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1357 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1358 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001359 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001360 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001361 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001362
1363exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001364 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001365 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001366}
1367/* END_CASE */
1368
1369/* BEGIN_CASE */
1370void asymmetric_encryption_key_policy( int policy_usage,
1371 int policy_alg,
1372 int key_type,
1373 data_t *key_data,
1374 int exercise_alg )
1375{
Ronald Cron5425a212020-08-04 14:58:35 +02001376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001378 psa_status_t status;
1379 size_t key_bits;
1380 size_t buffer_length;
1381 unsigned char *buffer = NULL;
1382 size_t output_length;
1383
Gilles Peskine8817f612018-12-18 00:18:46 +01001384 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001385
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001386 psa_set_key_usage_flags( &attributes, policy_usage );
1387 psa_set_key_algorithm( &attributes, policy_alg );
1388 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001389
Gilles Peskine049c7532019-05-15 20:22:09 +02001390 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001391 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001392
Ronald Cron5425a212020-08-04 14:58:35 +02001393 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001394 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001395 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1396 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001397 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001398
Ronald Cron5425a212020-08-04 14:58:35 +02001399 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001400 NULL, 0,
1401 NULL, 0,
1402 buffer, buffer_length,
1403 &output_length );
1404 if( policy_alg == exercise_alg &&
1405 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001406 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001407 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001408 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001409
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001410 if( buffer_length != 0 )
1411 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001412 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001413 buffer, buffer_length,
1414 NULL, 0,
1415 buffer, buffer_length,
1416 &output_length );
1417 if( policy_alg == exercise_alg &&
1418 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001419 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001420 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001421 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001422
1423exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001424 /*
1425 * Key attributes may have been returned by psa_get_key_attributes()
1426 * thus reset them as required.
1427 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001428 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001429
1430 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001431 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001432 mbedtls_free( buffer );
1433}
1434/* END_CASE */
1435
1436/* BEGIN_CASE */
1437void asymmetric_signature_key_policy( int policy_usage,
1438 int policy_alg,
1439 int key_type,
1440 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001441 int exercise_alg,
1442 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001443{
Ronald Cron5425a212020-08-04 14:58:35 +02001444 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001446 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001447 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1448 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1449 * compatible with the policy and `payload_length_arg` is supposed to be
1450 * a valid input length to sign. If `payload_length_arg <= 0`,
1451 * `exercise_alg` is supposed to be forbidden by the policy. */
1452 int compatible_alg = payload_length_arg > 0;
1453 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001454 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001455 size_t signature_length;
1456
Gilles Peskine8817f612018-12-18 00:18:46 +01001457 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001459 psa_set_key_usage_flags( &attributes, policy_usage );
1460 psa_set_key_algorithm( &attributes, policy_alg );
1461 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001462
Gilles Peskine049c7532019-05-15 20:22:09 +02001463 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001464 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001465
Ronald Cron5425a212020-08-04 14:58:35 +02001466 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001467 payload, payload_length,
1468 signature, sizeof( signature ),
1469 &signature_length );
1470 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001471 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001472 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001473 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001474
1475 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001476 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001477 payload, payload_length,
1478 signature, sizeof( signature ) );
1479 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001480 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001481 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001482 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001483
1484exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001485 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001486 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001487}
1488/* END_CASE */
1489
Janos Follathba3fab92019-06-11 14:50:16 +01001490/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001491void derive_key_policy( int policy_usage,
1492 int policy_alg,
1493 int key_type,
1494 data_t *key_data,
1495 int exercise_alg )
1496{
Ronald Cron5425a212020-08-04 14:58:35 +02001497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001499 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001500 psa_status_t status;
1501
Gilles Peskine8817f612018-12-18 00:18:46 +01001502 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001503
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001504 psa_set_key_usage_flags( &attributes, policy_usage );
1505 psa_set_key_algorithm( &attributes, policy_alg );
1506 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001507
Gilles Peskine049c7532019-05-15 20:22:09 +02001508 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001509 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001510
Janos Follathba3fab92019-06-11 14:50:16 +01001511 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1512
1513 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1514 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001515 {
Janos Follathba3fab92019-06-11 14:50:16 +01001516 PSA_ASSERT( psa_key_derivation_input_bytes(
1517 &operation,
1518 PSA_KEY_DERIVATION_INPUT_SEED,
1519 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001520 }
Janos Follathba3fab92019-06-11 14:50:16 +01001521
1522 status = psa_key_derivation_input_key( &operation,
1523 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001524 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001525
Gilles Peskineea0fb492018-07-12 17:17:20 +02001526 if( policy_alg == exercise_alg &&
1527 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001528 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001529 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001530 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001531
1532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001533 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001534 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001535 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001540void agreement_key_policy( int policy_usage,
1541 int policy_alg,
1542 int key_type_arg,
1543 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001544 int exercise_alg,
1545 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001546{
Ronald Cron5425a212020-08-04 14:58:35 +02001547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001548 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001549 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001550 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001551 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001552 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001555
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001556 psa_set_key_usage_flags( &attributes, policy_usage );
1557 psa_set_key_algorithm( &attributes, policy_alg );
1558 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001559
Gilles Peskine049c7532019-05-15 20:22:09 +02001560 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001561 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001562
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001563 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001564 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001565
Steven Cooremance48e852020-10-05 16:02:45 +02001566 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001567
1568exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001569 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001570 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001571 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001572}
1573/* END_CASE */
1574
1575/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001576void key_policy_alg2( int key_type_arg, data_t *key_data,
1577 int usage_arg, int alg_arg, int alg2_arg )
1578{
Ronald Cron5425a212020-08-04 14:58:35 +02001579 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001580 psa_key_type_t key_type = key_type_arg;
1581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1582 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1583 psa_key_usage_t usage = usage_arg;
1584 psa_algorithm_t alg = alg_arg;
1585 psa_algorithm_t alg2 = alg2_arg;
1586
1587 PSA_ASSERT( psa_crypto_init( ) );
1588
1589 psa_set_key_usage_flags( &attributes, usage );
1590 psa_set_key_algorithm( &attributes, alg );
1591 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1592 psa_set_key_type( &attributes, key_type );
1593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001594 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001595
Ronald Cron5425a212020-08-04 14:58:35 +02001596 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001597 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1598 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1599 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1600
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001601 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001602 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001603 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001604 goto exit;
1605
1606exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001607 /*
1608 * Key attributes may have been returned by psa_get_key_attributes()
1609 * thus reset them as required.
1610 */
1611 psa_reset_key_attributes( &got_attributes );
1612
Ronald Cron5425a212020-08-04 14:58:35 +02001613 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001614 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001615}
1616/* END_CASE */
1617
1618/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001619void raw_agreement_key_policy( int policy_usage,
1620 int policy_alg,
1621 int key_type_arg,
1622 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001623 int exercise_alg,
1624 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001625{
Ronald Cron5425a212020-08-04 14:58:35 +02001626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001628 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001629 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001630 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001631 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001632
1633 PSA_ASSERT( psa_crypto_init( ) );
1634
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001635 psa_set_key_usage_flags( &attributes, policy_usage );
1636 psa_set_key_algorithm( &attributes, policy_alg );
1637 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001638
Gilles Peskine049c7532019-05-15 20:22:09 +02001639 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001640 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001641
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001642 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001643
Steven Cooremance48e852020-10-05 16:02:45 +02001644 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001645
1646exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001647 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001648 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001649 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001650}
1651/* END_CASE */
1652
1653/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001654void copy_success( int source_usage_arg,
1655 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001656 int type_arg, data_t *material,
1657 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001658 int target_usage_arg,
1659 int target_alg_arg, int target_alg2_arg,
1660 int expected_usage_arg,
1661 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001662{
Gilles Peskineca25db92019-04-19 11:43:08 +02001663 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1664 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001665 psa_key_usage_t expected_usage = expected_usage_arg;
1666 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001667 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001668 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1669 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001670 uint8_t *export_buffer = NULL;
1671
Gilles Peskine57ab7212019-01-28 13:03:09 +01001672 PSA_ASSERT( psa_crypto_init( ) );
1673
Gilles Peskineca25db92019-04-19 11:43:08 +02001674 /* Prepare the source key. */
1675 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1676 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001677 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001678 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001679 PSA_ASSERT( psa_import_key( &source_attributes,
1680 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001681 &source_key ) );
1682 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001683
Gilles Peskineca25db92019-04-19 11:43:08 +02001684 /* Prepare the target attributes. */
1685 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001686 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001687 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001688 /* Set volatile lifetime to reset the key identifier to 0. */
1689 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1690 }
1691
Gilles Peskineca25db92019-04-19 11:43:08 +02001692 if( target_usage_arg != -1 )
1693 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1694 if( target_alg_arg != -1 )
1695 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001696 if( target_alg2_arg != -1 )
1697 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001698
1699 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001700 PSA_ASSERT( psa_copy_key( source_key,
1701 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001702
1703 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001704 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001705
1706 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001707 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001708 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1709 psa_get_key_type( &target_attributes ) );
1710 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1711 psa_get_key_bits( &target_attributes ) );
1712 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1713 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001714 TEST_EQUAL( expected_alg2,
1715 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001716 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1717 {
1718 size_t length;
1719 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001720 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001721 material->len, &length ) );
1722 ASSERT_COMPARE( material->x, material->len,
1723 export_buffer, length );
1724 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001725
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001726 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001727 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001728 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001729 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001730
Ronald Cron5425a212020-08-04 14:58:35 +02001731 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001732
1733exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001734 /*
1735 * Source and target key attributes may have been returned by
1736 * psa_get_key_attributes() thus reset them as required.
1737 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001738 psa_reset_key_attributes( &source_attributes );
1739 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001740
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001741 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001742 mbedtls_free( export_buffer );
1743}
1744/* END_CASE */
1745
1746/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001747void copy_fail( int source_usage_arg,
1748 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001749 int type_arg, data_t *material,
1750 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001751 int target_usage_arg,
1752 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001753 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001754 int expected_status_arg )
1755{
1756 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1757 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001758 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1759 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001760 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001761
1762 PSA_ASSERT( psa_crypto_init( ) );
1763
1764 /* Prepare the source key. */
1765 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1766 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001767 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001768 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001769 PSA_ASSERT( psa_import_key( &source_attributes,
1770 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001771 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001772
1773 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001774 psa_set_key_id( &target_attributes, key_id );
1775 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001776 psa_set_key_type( &target_attributes, target_type_arg );
1777 psa_set_key_bits( &target_attributes, target_bits_arg );
1778 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1779 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001780 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001781
1782 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001783 TEST_EQUAL( psa_copy_key( source_key,
1784 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001785 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001786
Ronald Cron5425a212020-08-04 14:58:35 +02001787 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001788
Gilles Peskine4a644642019-05-03 17:14:08 +02001789exit:
1790 psa_reset_key_attributes( &source_attributes );
1791 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001792 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001793}
1794/* END_CASE */
1795
1796/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001797void hash_operation_init( )
1798{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001799 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001800 /* Test each valid way of initializing the object, except for `= {0}`, as
1801 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1802 * though it's OK by the C standard. We could test for this, but we'd need
1803 * to supress the Clang warning for the test. */
1804 psa_hash_operation_t func = psa_hash_operation_init( );
1805 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1806 psa_hash_operation_t zero;
1807
1808 memset( &zero, 0, sizeof( zero ) );
1809
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001810 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001811 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1812 PSA_ERROR_BAD_STATE );
1813 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1814 PSA_ERROR_BAD_STATE );
1815 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1816 PSA_ERROR_BAD_STATE );
1817
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001818 /* A default hash operation should be abortable without error. */
1819 PSA_ASSERT( psa_hash_abort( &func ) );
1820 PSA_ASSERT( psa_hash_abort( &init ) );
1821 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001822}
1823/* END_CASE */
1824
1825/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001826void hash_setup( int alg_arg,
1827 int expected_status_arg )
1828{
1829 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001830 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001831 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001832 psa_status_t status;
1833
Gilles Peskine8817f612018-12-18 00:18:46 +01001834 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001835
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001836 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001837 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001838
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001839 /* Whether setup succeeded or failed, abort must succeed. */
1840 PSA_ASSERT( psa_hash_abort( &operation ) );
1841
1842 /* If setup failed, reproduce the failure, so as to
1843 * test the resulting state of the operation object. */
1844 if( status != PSA_SUCCESS )
1845 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1846
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001847 /* Now the operation object should be reusable. */
1848#if defined(KNOWN_SUPPORTED_HASH_ALG)
1849 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1850 PSA_ASSERT( psa_hash_abort( &operation ) );
1851#endif
1852
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001853exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001854 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001855}
1856/* END_CASE */
1857
1858/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001859void hash_compute_fail( int alg_arg, data_t *input,
1860 int output_size_arg, int expected_status_arg )
1861{
1862 psa_algorithm_t alg = alg_arg;
1863 uint8_t *output = NULL;
1864 size_t output_size = output_size_arg;
1865 size_t output_length = INVALID_EXPORT_LENGTH;
1866 psa_status_t expected_status = expected_status_arg;
1867 psa_status_t status;
1868
1869 ASSERT_ALLOC( output, output_size );
1870
1871 PSA_ASSERT( psa_crypto_init( ) );
1872
1873 status = psa_hash_compute( alg, input->x, input->len,
1874 output, output_size, &output_length );
1875 TEST_EQUAL( status, expected_status );
1876 TEST_ASSERT( output_length <= output_size );
1877
1878exit:
1879 mbedtls_free( output );
1880 PSA_DONE( );
1881}
1882/* END_CASE */
1883
1884/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001885void hash_compare_fail( int alg_arg, data_t *input,
1886 data_t *reference_hash,
1887 int expected_status_arg )
1888{
1889 psa_algorithm_t alg = alg_arg;
1890 psa_status_t expected_status = expected_status_arg;
1891 psa_status_t status;
1892
1893 PSA_ASSERT( psa_crypto_init( ) );
1894
1895 status = psa_hash_compare( alg, input->x, input->len,
1896 reference_hash->x, reference_hash->len );
1897 TEST_EQUAL( status, expected_status );
1898
1899exit:
1900 PSA_DONE( );
1901}
1902/* END_CASE */
1903
1904/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001905void hash_compute_compare( int alg_arg, data_t *input,
1906 data_t *expected_output )
1907{
1908 psa_algorithm_t alg = alg_arg;
1909 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1910 size_t output_length = INVALID_EXPORT_LENGTH;
1911 size_t i;
1912
1913 PSA_ASSERT( psa_crypto_init( ) );
1914
1915 /* Compute with tight buffer */
1916 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001917 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001918 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001919 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001920 ASSERT_COMPARE( output, output_length,
1921 expected_output->x, expected_output->len );
1922
1923 /* Compute with larger buffer */
1924 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1925 output, sizeof( output ),
1926 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001927 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001928 ASSERT_COMPARE( output, output_length,
1929 expected_output->x, expected_output->len );
1930
1931 /* Compare with correct hash */
1932 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1933 output, output_length ) );
1934
1935 /* Compare with trailing garbage */
1936 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1937 output, output_length + 1 ),
1938 PSA_ERROR_INVALID_SIGNATURE );
1939
1940 /* Compare with truncated hash */
1941 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1942 output, output_length - 1 ),
1943 PSA_ERROR_INVALID_SIGNATURE );
1944
1945 /* Compare with corrupted value */
1946 for( i = 0; i < output_length; i++ )
1947 {
Chris Jones9634bb12021-01-20 15:56:42 +00001948 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001949 output[i] ^= 1;
1950 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1951 output, output_length ),
1952 PSA_ERROR_INVALID_SIGNATURE );
1953 output[i] ^= 1;
1954 }
1955
1956exit:
1957 PSA_DONE( );
1958}
1959/* END_CASE */
1960
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001961/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001962void hash_bad_order( )
1963{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001964 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001965 unsigned char input[] = "";
1966 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001967 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001968 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1969 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1970 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001971 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001972 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001973 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001974
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001976
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001977 /* Call setup twice in a row. */
1978 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1979 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1980 PSA_ERROR_BAD_STATE );
1981 PSA_ASSERT( psa_hash_abort( &operation ) );
1982
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001983 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001984 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001985 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001986 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001987
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001988 /* Call update after finish. */
1989 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1990 PSA_ASSERT( psa_hash_finish( &operation,
1991 hash, sizeof( hash ), &hash_len ) );
1992 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001993 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001994 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001995
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001996 /* Call verify without calling setup beforehand. */
1997 TEST_EQUAL( psa_hash_verify( &operation,
1998 valid_hash, sizeof( valid_hash ) ),
1999 PSA_ERROR_BAD_STATE );
2000 PSA_ASSERT( psa_hash_abort( &operation ) );
2001
2002 /* Call verify after finish. */
2003 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2004 PSA_ASSERT( psa_hash_finish( &operation,
2005 hash, sizeof( hash ), &hash_len ) );
2006 TEST_EQUAL( psa_hash_verify( &operation,
2007 valid_hash, sizeof( valid_hash ) ),
2008 PSA_ERROR_BAD_STATE );
2009 PSA_ASSERT( psa_hash_abort( &operation ) );
2010
2011 /* Call verify twice in a row. */
2012 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2013 PSA_ASSERT( psa_hash_verify( &operation,
2014 valid_hash, sizeof( valid_hash ) ) );
2015 TEST_EQUAL( psa_hash_verify( &operation,
2016 valid_hash, sizeof( valid_hash ) ),
2017 PSA_ERROR_BAD_STATE );
2018 PSA_ASSERT( psa_hash_abort( &operation ) );
2019
2020 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002021 TEST_EQUAL( psa_hash_finish( &operation,
2022 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002023 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002024 PSA_ASSERT( psa_hash_abort( &operation ) );
2025
2026 /* Call finish twice in a row. */
2027 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2028 PSA_ASSERT( psa_hash_finish( &operation,
2029 hash, sizeof( hash ), &hash_len ) );
2030 TEST_EQUAL( psa_hash_finish( &operation,
2031 hash, sizeof( hash ), &hash_len ),
2032 PSA_ERROR_BAD_STATE );
2033 PSA_ASSERT( psa_hash_abort( &operation ) );
2034
2035 /* Call finish after calling verify. */
2036 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2037 PSA_ASSERT( psa_hash_verify( &operation,
2038 valid_hash, sizeof( valid_hash ) ) );
2039 TEST_EQUAL( psa_hash_finish( &operation,
2040 hash, sizeof( hash ), &hash_len ),
2041 PSA_ERROR_BAD_STATE );
2042 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002043
2044exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002045 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002046}
2047/* END_CASE */
2048
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002049/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002050void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002051{
2052 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002053 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2054 * appended to it */
2055 unsigned char hash[] = {
2056 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2057 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2058 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002059 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002060 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002061
Gilles Peskine8817f612018-12-18 00:18:46 +01002062 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002063
itayzafrir27e69452018-11-01 14:26:34 +02002064 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002065 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002066 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002067 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002068
itayzafrir27e69452018-11-01 14:26:34 +02002069 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002070 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002071 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002072 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002073
itayzafrir27e69452018-11-01 14:26:34 +02002074 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002075 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002076 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002077 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002078
itayzafrirec93d302018-10-18 18:01:10 +03002079exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002080 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002081}
2082/* END_CASE */
2083
Ronald Cronee414c72021-03-18 18:50:08 +01002084/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002085void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002086{
2087 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002088 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002089 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002090 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002091 size_t hash_len;
2092
Gilles Peskine8817f612018-12-18 00:18:46 +01002093 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002094
itayzafrir58028322018-10-25 10:22:01 +03002095 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002096 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002097 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002098 hash, expected_size - 1, &hash_len ),
2099 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002100
2101exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002102 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002103}
2104/* END_CASE */
2105
Ronald Cronee414c72021-03-18 18:50:08 +01002106/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002107void hash_clone_source_state( )
2108{
2109 psa_algorithm_t alg = PSA_ALG_SHA_256;
2110 unsigned char hash[PSA_HASH_MAX_SIZE];
2111 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2112 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2113 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2114 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2115 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2116 size_t hash_len;
2117
2118 PSA_ASSERT( psa_crypto_init( ) );
2119 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2120
2121 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2122 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2123 PSA_ASSERT( psa_hash_finish( &op_finished,
2124 hash, sizeof( hash ), &hash_len ) );
2125 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2126 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2127
2128 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2129 PSA_ERROR_BAD_STATE );
2130
2131 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2132 PSA_ASSERT( psa_hash_finish( &op_init,
2133 hash, sizeof( hash ), &hash_len ) );
2134 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2135 PSA_ASSERT( psa_hash_finish( &op_finished,
2136 hash, sizeof( hash ), &hash_len ) );
2137 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2138 PSA_ASSERT( psa_hash_finish( &op_aborted,
2139 hash, sizeof( hash ), &hash_len ) );
2140
2141exit:
2142 psa_hash_abort( &op_source );
2143 psa_hash_abort( &op_init );
2144 psa_hash_abort( &op_setup );
2145 psa_hash_abort( &op_finished );
2146 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002147 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002148}
2149/* END_CASE */
2150
Ronald Cronee414c72021-03-18 18:50:08 +01002151/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002152void hash_clone_target_state( )
2153{
2154 psa_algorithm_t alg = PSA_ALG_SHA_256;
2155 unsigned char hash[PSA_HASH_MAX_SIZE];
2156 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2157 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2158 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2159 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2160 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2161 size_t hash_len;
2162
2163 PSA_ASSERT( psa_crypto_init( ) );
2164
2165 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2166 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2167 PSA_ASSERT( psa_hash_finish( &op_finished,
2168 hash, sizeof( hash ), &hash_len ) );
2169 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2170 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2171
2172 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2173 PSA_ASSERT( psa_hash_finish( &op_target,
2174 hash, sizeof( hash ), &hash_len ) );
2175
2176 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2177 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2178 PSA_ERROR_BAD_STATE );
2179 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2180 PSA_ERROR_BAD_STATE );
2181
2182exit:
2183 psa_hash_abort( &op_target );
2184 psa_hash_abort( &op_init );
2185 psa_hash_abort( &op_setup );
2186 psa_hash_abort( &op_finished );
2187 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002188 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002189}
2190/* END_CASE */
2191
itayzafrir58028322018-10-25 10:22:01 +03002192/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002193void mac_operation_init( )
2194{
Jaeden Amero252ef282019-02-15 14:05:35 +00002195 const uint8_t input[1] = { 0 };
2196
Jaeden Amero769ce272019-01-04 11:48:03 +00002197 /* Test each valid way of initializing the object, except for `= {0}`, as
2198 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2199 * though it's OK by the C standard. We could test for this, but we'd need
2200 * to supress the Clang warning for the test. */
2201 psa_mac_operation_t func = psa_mac_operation_init( );
2202 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2203 psa_mac_operation_t zero;
2204
2205 memset( &zero, 0, sizeof( zero ) );
2206
Jaeden Amero252ef282019-02-15 14:05:35 +00002207 /* A freshly-initialized MAC operation should not be usable. */
2208 TEST_EQUAL( psa_mac_update( &func,
2209 input, sizeof( input ) ),
2210 PSA_ERROR_BAD_STATE );
2211 TEST_EQUAL( psa_mac_update( &init,
2212 input, sizeof( input ) ),
2213 PSA_ERROR_BAD_STATE );
2214 TEST_EQUAL( psa_mac_update( &zero,
2215 input, sizeof( input ) ),
2216 PSA_ERROR_BAD_STATE );
2217
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002218 /* A default MAC operation should be abortable without error. */
2219 PSA_ASSERT( psa_mac_abort( &func ) );
2220 PSA_ASSERT( psa_mac_abort( &init ) );
2221 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002222}
2223/* END_CASE */
2224
2225/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002226void mac_setup( int key_type_arg,
2227 data_t *key,
2228 int alg_arg,
2229 int expected_status_arg )
2230{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002231 psa_key_type_t key_type = key_type_arg;
2232 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002233 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002234 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002235 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2236#if defined(KNOWN_SUPPORTED_MAC_ALG)
2237 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2238#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002239
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002242 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2243 &operation, &status ) )
2244 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002245 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002246
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002247 /* The operation object should be reusable. */
2248#if defined(KNOWN_SUPPORTED_MAC_ALG)
2249 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2250 smoke_test_key_data,
2251 sizeof( smoke_test_key_data ),
2252 KNOWN_SUPPORTED_MAC_ALG,
2253 &operation, &status ) )
2254 goto exit;
2255 TEST_EQUAL( status, PSA_SUCCESS );
2256#endif
2257
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002258exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002259 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002260}
2261/* END_CASE */
2262
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002263/* 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 +00002264void mac_bad_order( )
2265{
Ronald Cron5425a212020-08-04 14:58:35 +02002266 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002267 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2268 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002269 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002270 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2271 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2272 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002274 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2275 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2276 size_t sign_mac_length = 0;
2277 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2278 const uint8_t verify_mac[] = {
2279 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2280 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2281 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2282
2283 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002285 psa_set_key_algorithm( &attributes, alg );
2286 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002287
Ronald Cron5425a212020-08-04 14:58:35 +02002288 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2289 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002290
Jaeden Amero252ef282019-02-15 14:05:35 +00002291 /* Call update without calling setup beforehand. */
2292 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2293 PSA_ERROR_BAD_STATE );
2294 PSA_ASSERT( psa_mac_abort( &operation ) );
2295
2296 /* Call sign finish without calling setup beforehand. */
2297 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2298 &sign_mac_length),
2299 PSA_ERROR_BAD_STATE );
2300 PSA_ASSERT( psa_mac_abort( &operation ) );
2301
2302 /* Call verify finish without calling setup beforehand. */
2303 TEST_EQUAL( psa_mac_verify_finish( &operation,
2304 verify_mac, sizeof( verify_mac ) ),
2305 PSA_ERROR_BAD_STATE );
2306 PSA_ASSERT( psa_mac_abort( &operation ) );
2307
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002308 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002309 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2310 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002311 PSA_ERROR_BAD_STATE );
2312 PSA_ASSERT( psa_mac_abort( &operation ) );
2313
Jaeden Amero252ef282019-02-15 14:05:35 +00002314 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002315 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002316 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2317 PSA_ASSERT( psa_mac_sign_finish( &operation,
2318 sign_mac, sizeof( sign_mac ),
2319 &sign_mac_length ) );
2320 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2321 PSA_ERROR_BAD_STATE );
2322 PSA_ASSERT( psa_mac_abort( &operation ) );
2323
2324 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002325 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002326 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2327 PSA_ASSERT( psa_mac_verify_finish( &operation,
2328 verify_mac, sizeof( verify_mac ) ) );
2329 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2330 PSA_ERROR_BAD_STATE );
2331 PSA_ASSERT( psa_mac_abort( &operation ) );
2332
2333 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002334 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002335 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2336 PSA_ASSERT( psa_mac_sign_finish( &operation,
2337 sign_mac, sizeof( sign_mac ),
2338 &sign_mac_length ) );
2339 TEST_EQUAL( psa_mac_sign_finish( &operation,
2340 sign_mac, sizeof( sign_mac ),
2341 &sign_mac_length ),
2342 PSA_ERROR_BAD_STATE );
2343 PSA_ASSERT( psa_mac_abort( &operation ) );
2344
2345 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002346 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002347 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2348 PSA_ASSERT( psa_mac_verify_finish( &operation,
2349 verify_mac, sizeof( verify_mac ) ) );
2350 TEST_EQUAL( psa_mac_verify_finish( &operation,
2351 verify_mac, sizeof( verify_mac ) ),
2352 PSA_ERROR_BAD_STATE );
2353 PSA_ASSERT( psa_mac_abort( &operation ) );
2354
2355 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002356 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002357 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2358 TEST_EQUAL( psa_mac_verify_finish( &operation,
2359 verify_mac, sizeof( verify_mac ) ),
2360 PSA_ERROR_BAD_STATE );
2361 PSA_ASSERT( psa_mac_abort( &operation ) );
2362
2363 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002364 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002365 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2366 TEST_EQUAL( psa_mac_sign_finish( &operation,
2367 sign_mac, sizeof( sign_mac ),
2368 &sign_mac_length ),
2369 PSA_ERROR_BAD_STATE );
2370 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002371
Ronald Cron5425a212020-08-04 14:58:35 +02002372 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002373
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002374exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002375 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002376}
2377/* END_CASE */
2378
2379/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002380void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002381 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002382 int alg_arg,
2383 data_t *input,
2384 data_t *expected_mac )
2385{
Ronald Cron5425a212020-08-04 14:58:35 +02002386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002387 psa_key_type_t key_type = key_type_arg;
2388 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002389 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002391 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002392 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002393 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002394 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002395 const size_t output_sizes_to_test[] = {
2396 0,
2397 1,
2398 expected_mac->len - 1,
2399 expected_mac->len,
2400 expected_mac->len + 1,
2401 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002402
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002403 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002404 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002405 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002406
Gilles Peskine8817f612018-12-18 00:18:46 +01002407 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002408
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002409 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002410 psa_set_key_algorithm( &attributes, alg );
2411 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002412
Ronald Cron5425a212020-08-04 14:58:35 +02002413 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2414 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002415
Gilles Peskine8b356b52020-08-25 23:44:59 +02002416 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2417 {
2418 const size_t output_size = output_sizes_to_test[i];
2419 psa_status_t expected_status =
2420 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2421 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002422
Chris Jones9634bb12021-01-20 15:56:42 +00002423 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002424 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002425
Gilles Peskine8b356b52020-08-25 23:44:59 +02002426 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002427 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002428 PSA_ASSERT( psa_mac_update( &operation,
2429 input->x, input->len ) );
2430 TEST_EQUAL( psa_mac_sign_finish( &operation,
2431 actual_mac, output_size,
2432 &mac_length ),
2433 expected_status );
2434 PSA_ASSERT( psa_mac_abort( &operation ) );
2435
2436 if( expected_status == PSA_SUCCESS )
2437 {
2438 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2439 actual_mac, mac_length );
2440 }
2441 mbedtls_free( actual_mac );
2442 actual_mac = NULL;
2443 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002444
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002445exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002446 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002447 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002448 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002449 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002450}
2451/* END_CASE */
2452
2453/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002454void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002455 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002456 int alg_arg,
2457 data_t *input,
2458 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459{
Ronald Cron5425a212020-08-04 14:58:35 +02002460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461 psa_key_type_t key_type = key_type_arg;
2462 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002463 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002465 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002466
Gilles Peskine69c12672018-06-28 00:07:19 +02002467 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2468
Gilles Peskine8817f612018-12-18 00:18:46 +01002469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002471 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002472 psa_set_key_algorithm( &attributes, alg );
2473 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002474
Ronald Cron5425a212020-08-04 14:58:35 +02002475 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2476 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002477
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002478 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002479 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002480 PSA_ASSERT( psa_mac_update( &operation,
2481 input->x, input->len ) );
2482 PSA_ASSERT( psa_mac_verify_finish( &operation,
2483 expected_mac->x,
2484 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002486 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002487 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002488 PSA_ASSERT( psa_mac_update( &operation,
2489 input->x, input->len ) );
2490 TEST_EQUAL( psa_mac_verify_finish( &operation,
2491 expected_mac->x,
2492 expected_mac->len - 1 ),
2493 PSA_ERROR_INVALID_SIGNATURE );
2494
2495 /* Test a MAC that's too long. */
2496 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2497 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002498 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002499 PSA_ASSERT( psa_mac_update( &operation,
2500 input->x, input->len ) );
2501 TEST_EQUAL( psa_mac_verify_finish( &operation,
2502 perturbed_mac,
2503 expected_mac->len + 1 ),
2504 PSA_ERROR_INVALID_SIGNATURE );
2505
2506 /* Test changing one byte. */
2507 for( size_t i = 0; i < expected_mac->len; i++ )
2508 {
Chris Jones9634bb12021-01-20 15:56:42 +00002509 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002510 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002511 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002512 PSA_ASSERT( psa_mac_update( &operation,
2513 input->x, input->len ) );
2514 TEST_EQUAL( psa_mac_verify_finish( &operation,
2515 perturbed_mac,
2516 expected_mac->len ),
2517 PSA_ERROR_INVALID_SIGNATURE );
2518 perturbed_mac[i] ^= 1;
2519 }
2520
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002522 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002523 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002524 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002525 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526}
2527/* END_CASE */
2528
2529/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002530void cipher_operation_init( )
2531{
Jaeden Ameroab439972019-02-15 14:12:05 +00002532 const uint8_t input[1] = { 0 };
2533 unsigned char output[1] = { 0 };
2534 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002535 /* Test each valid way of initializing the object, except for `= {0}`, as
2536 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2537 * though it's OK by the C standard. We could test for this, but we'd need
2538 * to supress the Clang warning for the test. */
2539 psa_cipher_operation_t func = psa_cipher_operation_init( );
2540 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2541 psa_cipher_operation_t zero;
2542
2543 memset( &zero, 0, sizeof( zero ) );
2544
Jaeden Ameroab439972019-02-15 14:12:05 +00002545 /* A freshly-initialized cipher operation should not be usable. */
2546 TEST_EQUAL( psa_cipher_update( &func,
2547 input, sizeof( input ),
2548 output, sizeof( output ),
2549 &output_length ),
2550 PSA_ERROR_BAD_STATE );
2551 TEST_EQUAL( psa_cipher_update( &init,
2552 input, sizeof( input ),
2553 output, sizeof( output ),
2554 &output_length ),
2555 PSA_ERROR_BAD_STATE );
2556 TEST_EQUAL( psa_cipher_update( &zero,
2557 input, sizeof( input ),
2558 output, sizeof( output ),
2559 &output_length ),
2560 PSA_ERROR_BAD_STATE );
2561
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002562 /* A default cipher operation should be abortable without error. */
2563 PSA_ASSERT( psa_cipher_abort( &func ) );
2564 PSA_ASSERT( psa_cipher_abort( &init ) );
2565 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002566}
2567/* END_CASE */
2568
2569/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002570void cipher_setup( int key_type_arg,
2571 data_t *key,
2572 int alg_arg,
2573 int expected_status_arg )
2574{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002575 psa_key_type_t key_type = key_type_arg;
2576 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002577 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002578 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002579 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002580#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002581 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2582#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002583
Gilles Peskine8817f612018-12-18 00:18:46 +01002584 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002586 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2587 &operation, &status ) )
2588 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002589 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002590
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002591 /* The operation object should be reusable. */
2592#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2593 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2594 smoke_test_key_data,
2595 sizeof( smoke_test_key_data ),
2596 KNOWN_SUPPORTED_CIPHER_ALG,
2597 &operation, &status ) )
2598 goto exit;
2599 TEST_EQUAL( status, PSA_SUCCESS );
2600#endif
2601
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002602exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002603 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002604 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002605}
2606/* END_CASE */
2607
Ronald Cronee414c72021-03-18 18:50:08 +01002608/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002609void cipher_bad_order( )
2610{
Ronald Cron5425a212020-08-04 14:58:35 +02002611 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002612 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2613 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002615 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002616 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002617 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002618 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2619 0xaa, 0xaa, 0xaa, 0xaa };
2620 const uint8_t text[] = {
2621 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2622 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002623 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002624 size_t length = 0;
2625
2626 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002627 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2628 psa_set_key_algorithm( &attributes, alg );
2629 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002630 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2631 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002632
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002633 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002634 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2635 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002636 PSA_ERROR_BAD_STATE );
2637 PSA_ASSERT( psa_cipher_abort( &operation ) );
2638
2639 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002640 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2641 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002642 PSA_ERROR_BAD_STATE );
2643 PSA_ASSERT( psa_cipher_abort( &operation ) );
2644
Jaeden Ameroab439972019-02-15 14:12:05 +00002645 /* Generate an IV without calling setup beforehand. */
2646 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2647 buffer, sizeof( buffer ),
2648 &length ),
2649 PSA_ERROR_BAD_STATE );
2650 PSA_ASSERT( psa_cipher_abort( &operation ) );
2651
2652 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002653 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002654 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2655 buffer, sizeof( buffer ),
2656 &length ) );
2657 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2658 buffer, sizeof( buffer ),
2659 &length ),
2660 PSA_ERROR_BAD_STATE );
2661 PSA_ASSERT( psa_cipher_abort( &operation ) );
2662
2663 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002664 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002665 PSA_ASSERT( psa_cipher_set_iv( &operation,
2666 iv, sizeof( iv ) ) );
2667 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2668 buffer, sizeof( buffer ),
2669 &length ),
2670 PSA_ERROR_BAD_STATE );
2671 PSA_ASSERT( psa_cipher_abort( &operation ) );
2672
2673 /* Set an IV without calling setup beforehand. */
2674 TEST_EQUAL( psa_cipher_set_iv( &operation,
2675 iv, sizeof( iv ) ),
2676 PSA_ERROR_BAD_STATE );
2677 PSA_ASSERT( psa_cipher_abort( &operation ) );
2678
2679 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002681 PSA_ASSERT( psa_cipher_set_iv( &operation,
2682 iv, sizeof( iv ) ) );
2683 TEST_EQUAL( psa_cipher_set_iv( &operation,
2684 iv, sizeof( iv ) ),
2685 PSA_ERROR_BAD_STATE );
2686 PSA_ASSERT( psa_cipher_abort( &operation ) );
2687
2688 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002689 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002690 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2691 buffer, sizeof( buffer ),
2692 &length ) );
2693 TEST_EQUAL( psa_cipher_set_iv( &operation,
2694 iv, sizeof( iv ) ),
2695 PSA_ERROR_BAD_STATE );
2696 PSA_ASSERT( psa_cipher_abort( &operation ) );
2697
2698 /* Call update without calling setup beforehand. */
2699 TEST_EQUAL( psa_cipher_update( &operation,
2700 text, sizeof( text ),
2701 buffer, sizeof( buffer ),
2702 &length ),
2703 PSA_ERROR_BAD_STATE );
2704 PSA_ASSERT( psa_cipher_abort( &operation ) );
2705
2706 /* Call update without an IV where an IV is required. */
2707 TEST_EQUAL( psa_cipher_update( &operation,
2708 text, sizeof( text ),
2709 buffer, sizeof( buffer ),
2710 &length ),
2711 PSA_ERROR_BAD_STATE );
2712 PSA_ASSERT( psa_cipher_abort( &operation ) );
2713
2714 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002715 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002716 PSA_ASSERT( psa_cipher_set_iv( &operation,
2717 iv, sizeof( iv ) ) );
2718 PSA_ASSERT( psa_cipher_finish( &operation,
2719 buffer, sizeof( buffer ), &length ) );
2720 TEST_EQUAL( psa_cipher_update( &operation,
2721 text, sizeof( text ),
2722 buffer, sizeof( buffer ),
2723 &length ),
2724 PSA_ERROR_BAD_STATE );
2725 PSA_ASSERT( psa_cipher_abort( &operation ) );
2726
2727 /* Call finish without calling setup beforehand. */
2728 TEST_EQUAL( psa_cipher_finish( &operation,
2729 buffer, sizeof( buffer ), &length ),
2730 PSA_ERROR_BAD_STATE );
2731 PSA_ASSERT( psa_cipher_abort( &operation ) );
2732
2733 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002734 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002735 /* Not calling update means we are encrypting an empty buffer, which is OK
2736 * for cipher modes with padding. */
2737 TEST_EQUAL( psa_cipher_finish( &operation,
2738 buffer, sizeof( buffer ), &length ),
2739 PSA_ERROR_BAD_STATE );
2740 PSA_ASSERT( psa_cipher_abort( &operation ) );
2741
2742 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002744 PSA_ASSERT( psa_cipher_set_iv( &operation,
2745 iv, sizeof( iv ) ) );
2746 PSA_ASSERT( psa_cipher_finish( &operation,
2747 buffer, sizeof( buffer ), &length ) );
2748 TEST_EQUAL( psa_cipher_finish( &operation,
2749 buffer, sizeof( buffer ), &length ),
2750 PSA_ERROR_BAD_STATE );
2751 PSA_ASSERT( psa_cipher_abort( &operation ) );
2752
Ronald Cron5425a212020-08-04 14:58:35 +02002753 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002754
Jaeden Ameroab439972019-02-15 14:12:05 +00002755exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002756 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002757 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002758}
2759/* END_CASE */
2760
2761/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002762void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002763 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002764 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002765 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002766{
Ronald Cron5425a212020-08-04 14:58:35 +02002767 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002768 psa_status_t status;
2769 psa_key_type_t key_type = key_type_arg;
2770 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002771 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002772 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002773 size_t output_buffer_size = 0;
2774 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002775 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002776 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002778
Gilles Peskine8817f612018-12-18 00:18:46 +01002779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002780
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2782 psa_set_key_algorithm( &attributes, alg );
2783 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002784
Ronald Cron5425a212020-08-04 14:58:35 +02002785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2786 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002787
Ronald Cron5425a212020-08-04 14:58:35 +02002788 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002790 if( iv->len > 0 )
2791 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002792 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002793 }
2794
gabor-mezei-armceface22021-01-21 12:26:17 +01002795 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2796 TEST_ASSERT( output_buffer_size <=
2797 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002798 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799
Gilles Peskine8817f612018-12-18 00:18:46 +01002800 PSA_ASSERT( psa_cipher_update( &operation,
2801 input->x, input->len,
2802 output, output_buffer_size,
2803 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002804 TEST_ASSERT( function_output_length <=
2805 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2806 TEST_ASSERT( function_output_length <=
2807 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002808 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002809
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002811 ( output_buffer_size == 0 ? NULL :
2812 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002813 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002814 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002815 TEST_ASSERT( function_output_length <=
2816 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2817 TEST_ASSERT( function_output_length <=
2818 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002819 total_output_length += function_output_length;
2820
Gilles Peskinefe11b722018-12-18 00:24:04 +01002821 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822 if( expected_status == PSA_SUCCESS )
2823 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002825 ASSERT_COMPARE( expected_output->x, expected_output->len,
2826 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002828
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002830 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002831 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002832 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002833 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834}
2835/* END_CASE */
2836
2837/* BEGIN_CASE */
2838void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002839 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002840 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002841 int first_part_size_arg,
2842 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002843 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844{
Ronald Cron5425a212020-08-04 14:58:35 +02002845 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846 psa_key_type_t key_type = key_type_arg;
2847 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002848 size_t first_part_size = first_part_size_arg;
2849 size_t output1_length = output1_length_arg;
2850 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002851 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002852 size_t output_buffer_size = 0;
2853 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002854 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002855 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002857
Gilles Peskine8817f612018-12-18 00:18:46 +01002858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002860 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2861 psa_set_key_algorithm( &attributes, alg );
2862 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002863
Ronald Cron5425a212020-08-04 14:58:35 +02002864 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2865 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866
Ronald Cron5425a212020-08-04 14:58:35 +02002867 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002869 if( iv->len > 0 )
2870 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002871 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002872 }
2873
gabor-mezei-armceface22021-01-21 12:26:17 +01002874 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2875 TEST_ASSERT( output_buffer_size <=
2876 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002877 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002878
Gilles Peskinee0866522019-02-19 19:44:00 +01002879 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002880 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2881 output, output_buffer_size,
2882 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002883 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002884 TEST_ASSERT( function_output_length <=
2885 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2886 TEST_ASSERT( function_output_length <=
2887 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002888 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002889
Gilles Peskine8817f612018-12-18 00:18:46 +01002890 PSA_ASSERT( psa_cipher_update( &operation,
2891 input->x + first_part_size,
2892 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002893 ( output_buffer_size == 0 ? NULL :
2894 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002895 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002897 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002898 TEST_ASSERT( function_output_length <=
2899 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2900 alg,
2901 input->len - first_part_size ) );
2902 TEST_ASSERT( function_output_length <=
2903 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002904 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002905
Gilles Peskine8817f612018-12-18 00:18:46 +01002906 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002907 ( output_buffer_size == 0 ? NULL :
2908 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002909 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002911 TEST_ASSERT( function_output_length <=
2912 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2913 TEST_ASSERT( function_output_length <=
2914 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002915 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002916 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002917
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002918 ASSERT_COMPARE( expected_output->x, expected_output->len,
2919 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
2921exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002922 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002923 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002924 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002925 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926}
2927/* END_CASE */
2928
2929/* BEGIN_CASE */
2930void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002931 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002932 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002933 int first_part_size_arg,
2934 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002935 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002936{
Ronald Cron5425a212020-08-04 14:58:35 +02002937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002938 psa_key_type_t key_type = key_type_arg;
2939 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002940 size_t first_part_size = first_part_size_arg;
2941 size_t output1_length = output1_length_arg;
2942 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002943 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944 size_t output_buffer_size = 0;
2945 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002946 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002947 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949
Gilles Peskine8817f612018-12-18 00:18:46 +01002950 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002952 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2953 psa_set_key_algorithm( &attributes, alg );
2954 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002955
Ronald Cron5425a212020-08-04 14:58:35 +02002956 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2957 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958
Ronald Cron5425a212020-08-04 14:58:35 +02002959 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960
Steven Cooreman177deba2020-09-07 17:14:14 +02002961 if( iv->len > 0 )
2962 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002963 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002964 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002965
gabor-mezei-armceface22021-01-21 12:26:17 +01002966 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2967 TEST_ASSERT( output_buffer_size <=
2968 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002969 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970
Gilles Peskinee0866522019-02-19 19:44:00 +01002971 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002972 PSA_ASSERT( psa_cipher_update( &operation,
2973 input->x, first_part_size,
2974 output, output_buffer_size,
2975 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002976 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002977 TEST_ASSERT( function_output_length <=
2978 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2979 TEST_ASSERT( function_output_length <=
2980 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002981 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002982
Gilles Peskine8817f612018-12-18 00:18:46 +01002983 PSA_ASSERT( psa_cipher_update( &operation,
2984 input->x + first_part_size,
2985 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002986 ( output_buffer_size == 0 ? NULL :
2987 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002988 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002989 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002990 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002991 TEST_ASSERT( function_output_length <=
2992 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2993 alg,
2994 input->len - first_part_size ) );
2995 TEST_ASSERT( function_output_length <=
2996 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002997 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002998
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003000 ( output_buffer_size == 0 ? NULL :
3001 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003002 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003004 TEST_ASSERT( function_output_length <=
3005 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3006 TEST_ASSERT( function_output_length <=
3007 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003008 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003010
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003011 ASSERT_COMPARE( expected_output->x, expected_output->len,
3012 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013
3014exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003015 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003016 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003017 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003018 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003019}
3020/* END_CASE */
3021
Gilles Peskine50e586b2018-06-08 14:28:46 +02003022/* BEGIN_CASE */
3023void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003024 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003025 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003026 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027{
Ronald Cron5425a212020-08-04 14:58:35 +02003028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003029 psa_status_t status;
3030 psa_key_type_t key_type = key_type_arg;
3031 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003032 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003033 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003034 size_t output_buffer_size = 0;
3035 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003036 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003037 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003039
Gilles Peskine8817f612018-12-18 00:18:46 +01003040 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003041
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003042 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3043 psa_set_key_algorithm( &attributes, alg );
3044 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003045
Ronald Cron5425a212020-08-04 14:58:35 +02003046 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3047 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003048
Ronald Cron5425a212020-08-04 14:58:35 +02003049 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003050
Steven Cooreman177deba2020-09-07 17:14:14 +02003051 if( iv->len > 0 )
3052 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003053 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003054 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003055
gabor-mezei-armceface22021-01-21 12:26:17 +01003056 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3057 TEST_ASSERT( output_buffer_size <=
3058 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003059 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003060
Gilles Peskine8817f612018-12-18 00:18:46 +01003061 PSA_ASSERT( psa_cipher_update( &operation,
3062 input->x, input->len,
3063 output, output_buffer_size,
3064 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003065 TEST_ASSERT( function_output_length <=
3066 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3067 TEST_ASSERT( function_output_length <=
3068 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003069 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003070
Gilles Peskine50e586b2018-06-08 14:28:46 +02003071 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003072 ( output_buffer_size == 0 ? NULL :
3073 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003074 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003075 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003076 TEST_ASSERT( function_output_length <=
3077 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3078 TEST_ASSERT( function_output_length <=
3079 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003080 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003081 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003082
3083 if( expected_status == PSA_SUCCESS )
3084 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003086 ASSERT_COMPARE( expected_output->x, expected_output->len,
3087 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003088 }
3089
Gilles Peskine50e586b2018-06-08 14:28:46 +02003090exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003091 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003092 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003093 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003094 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003095}
3096/* END_CASE */
3097
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098/* BEGIN_CASE */
3099void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003100 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003101 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003102{
Ronald Cron5425a212020-08-04 14:58:35 +02003103 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003104 psa_key_type_t key_type = key_type_arg;
3105 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003106 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003107 size_t iv_size = 16;
3108 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003109 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003110 size_t output1_size = 0;
3111 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003112 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003113 size_t output2_size = 0;
3114 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003115 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003116 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3117 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003119
Gilles Peskine8817f612018-12-18 00:18:46 +01003120 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003122 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3123 psa_set_key_algorithm( &attributes, alg );
3124 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003125
Ronald Cron5425a212020-08-04 14:58:35 +02003126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3127 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003128
Ronald Cron5425a212020-08-04 14:58:35 +02003129 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3130 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003131
Steven Cooreman177deba2020-09-07 17:14:14 +02003132 if( alg != PSA_ALG_ECB_NO_PADDING )
3133 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003134 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3135 iv, iv_size,
3136 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003137 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003138 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3139 TEST_ASSERT( output1_size <=
3140 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003141 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003142
Gilles Peskine8817f612018-12-18 00:18:46 +01003143 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3144 output1, output1_size,
3145 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003146 TEST_ASSERT( output1_length <=
3147 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3148 TEST_ASSERT( output1_length <=
3149 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3150
Gilles Peskine8817f612018-12-18 00:18:46 +01003151 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003152 output1 + output1_length,
3153 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3157 TEST_ASSERT( function_output_length <=
3158 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003159
Gilles Peskine048b7f02018-06-08 14:20:49 +02003160 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003161
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003163
3164 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003165 TEST_ASSERT( output2_size <=
3166 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3167 TEST_ASSERT( output2_size <=
3168 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003169 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003170
Steven Cooreman177deba2020-09-07 17:14:14 +02003171 if( iv_length > 0 )
3172 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003173 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3174 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003175 }
3176
Gilles Peskine8817f612018-12-18 00:18:46 +01003177 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3178 output2, output2_size,
3179 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003180 TEST_ASSERT( output2_length <=
3181 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3182 TEST_ASSERT( output2_length <=
3183 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3184
Gilles Peskine048b7f02018-06-08 14:20:49 +02003185 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_cipher_finish( &operation2,
3187 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003188 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003189 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003190 TEST_ASSERT( function_output_length <=
3191 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3192 TEST_ASSERT( function_output_length <=
3193 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003194
Gilles Peskine048b7f02018-06-08 14:20:49 +02003195 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003198
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003199 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003200
3201exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003202 psa_cipher_abort( &operation1 );
3203 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003204 mbedtls_free( output1 );
3205 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003206 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003207 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003208}
3209/* END_CASE */
3210
3211/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003212void cipher_verify_output_multipart( int alg_arg,
3213 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003214 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003215 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003216 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003217{
Ronald Cron5425a212020-08-04 14:58:35 +02003218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003219 psa_key_type_t key_type = key_type_arg;
3220 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003221 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003222 unsigned char iv[16] = {0};
3223 size_t iv_size = 16;
3224 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003225 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003226 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003227 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003228 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003229 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003230 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003231 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003232 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3233 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003235
Gilles Peskine8817f612018-12-18 00:18:46 +01003236 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003237
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003238 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3239 psa_set_key_algorithm( &attributes, alg );
3240 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003241
Ronald Cron5425a212020-08-04 14:58:35 +02003242 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3243 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003244
Ronald Cron5425a212020-08-04 14:58:35 +02003245 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3246 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003247
Steven Cooreman177deba2020-09-07 17:14:14 +02003248 if( alg != PSA_ALG_ECB_NO_PADDING )
3249 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003250 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3251 iv, iv_size,
3252 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003253 }
3254
gabor-mezei-armceface22021-01-21 12:26:17 +01003255 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3256 TEST_ASSERT( output1_buffer_size <=
3257 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003258 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003259
Gilles Peskinee0866522019-02-19 19:44:00 +01003260 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003261
Gilles Peskine8817f612018-12-18 00:18:46 +01003262 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3263 output1, output1_buffer_size,
3264 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003265 TEST_ASSERT( function_output_length <=
3266 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3267 TEST_ASSERT( function_output_length <=
3268 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003269 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003270
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_cipher_update( &operation1,
3272 input->x + first_part_size,
3273 input->len - first_part_size,
3274 output1, output1_buffer_size,
3275 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003276 TEST_ASSERT( function_output_length <=
3277 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3278 alg,
3279 input->len - first_part_size ) );
3280 TEST_ASSERT( function_output_length <=
3281 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003282 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003283
Gilles Peskine8817f612018-12-18 00:18:46 +01003284 PSA_ASSERT( psa_cipher_finish( &operation1,
3285 output1 + output1_length,
3286 output1_buffer_size - output1_length,
3287 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003288 TEST_ASSERT( function_output_length <=
3289 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3290 TEST_ASSERT( function_output_length <=
3291 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003292 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003293
Gilles Peskine8817f612018-12-18 00:18:46 +01003294 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003295
Gilles Peskine048b7f02018-06-08 14:20:49 +02003296 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003297 TEST_ASSERT( output2_buffer_size <=
3298 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3299 TEST_ASSERT( output2_buffer_size <=
3300 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003301 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003302
Steven Cooreman177deba2020-09-07 17:14:14 +02003303 if( iv_length > 0 )
3304 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003305 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3306 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003307 }
Moran Pekerded84402018-06-06 16:36:50 +03003308
Gilles Peskine8817f612018-12-18 00:18:46 +01003309 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3310 output2, output2_buffer_size,
3311 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003312 TEST_ASSERT( function_output_length <=
3313 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3314 TEST_ASSERT( function_output_length <=
3315 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003316 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003317
Gilles Peskine8817f612018-12-18 00:18:46 +01003318 PSA_ASSERT( psa_cipher_update( &operation2,
3319 output1 + first_part_size,
3320 output1_length - first_part_size,
3321 output2, output2_buffer_size,
3322 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003323 TEST_ASSERT( function_output_length <=
3324 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3325 alg,
3326 output1_length - first_part_size ) );
3327 TEST_ASSERT( function_output_length <=
3328 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003329 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003330
Gilles Peskine8817f612018-12-18 00:18:46 +01003331 PSA_ASSERT( psa_cipher_finish( &operation2,
3332 output2 + output2_length,
3333 output2_buffer_size - output2_length,
3334 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003335 TEST_ASSERT( function_output_length <=
3336 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3337 TEST_ASSERT( function_output_length <=
3338 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003339 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003340
Gilles Peskine8817f612018-12-18 00:18:46 +01003341 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003342
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003343 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003344
3345exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003346 psa_cipher_abort( &operation1 );
3347 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003348 mbedtls_free( output1 );
3349 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003350 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003351 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003352}
3353/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003354
Gilles Peskine20035e32018-02-03 22:44:14 +01003355/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003356void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003357 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003358 data_t *nonce,
3359 data_t *additional_data,
3360 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003361 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362{
Ronald Cron5425a212020-08-04 14:58:35 +02003363 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364 psa_key_type_t key_type = key_type_arg;
3365 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003366 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367 unsigned char *output_data = NULL;
3368 size_t output_size = 0;
3369 size_t output_length = 0;
3370 unsigned char *output_data2 = NULL;
3371 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003372 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003373 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003375
Gilles Peskine8817f612018-12-18 00:18:46 +01003376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003378 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3379 psa_set_key_algorithm( &attributes, alg );
3380 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381
Gilles Peskine049c7532019-05-15 20:22:09 +02003382 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003383 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003384 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3385 key_bits = psa_get_key_bits( &attributes );
3386
3387 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3388 alg );
3389 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3390 * should be exact. */
3391 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3392 expected_result != PSA_ERROR_NOT_SUPPORTED )
3393 {
3394 TEST_EQUAL( output_size,
3395 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3396 TEST_ASSERT( output_size <=
3397 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3398 }
3399 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400
Steven Cooremanf49478b2021-02-15 15:19:25 +01003401 status = psa_aead_encrypt( key, alg,
3402 nonce->x, nonce->len,
3403 additional_data->x,
3404 additional_data->len,
3405 input_data->x, input_data->len,
3406 output_data, output_size,
3407 &output_length );
3408
3409 /* If the operation is not supported, just skip and not fail in case the
3410 * encryption involves a common limitation of cryptography hardwares and
3411 * an alternative implementation. */
3412 if( status == PSA_ERROR_NOT_SUPPORTED )
3413 {
3414 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3415 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3416 }
3417
3418 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419
3420 if( PSA_SUCCESS == expected_result )
3421 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003422 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423
Gilles Peskine003a4a92019-05-14 16:09:40 +02003424 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3425 * should be exact. */
3426 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003427 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003428
gabor-mezei-armceface22021-01-21 12:26:17 +01003429 TEST_ASSERT( input_data->len <=
3430 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3431
Ronald Cron5425a212020-08-04 14:58:35 +02003432 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003433 nonce->x, nonce->len,
3434 additional_data->x,
3435 additional_data->len,
3436 output_data, output_length,
3437 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003438 &output_length2 ),
3439 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003440
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003441 ASSERT_COMPARE( input_data->x, input_data->len,
3442 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003443 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003444
Gilles Peskinea1cac842018-06-11 19:33:02 +02003445exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003446 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003447 mbedtls_free( output_data );
3448 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003449 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003450}
3451/* END_CASE */
3452
3453/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003454void aead_encrypt( int key_type_arg, data_t *key_data,
3455 int alg_arg,
3456 data_t *nonce,
3457 data_t *additional_data,
3458 data_t *input_data,
3459 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460{
Ronald Cron5425a212020-08-04 14:58:35 +02003461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462 psa_key_type_t key_type = key_type_arg;
3463 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003464 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465 unsigned char *output_data = NULL;
3466 size_t output_size = 0;
3467 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003469 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003472
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003473 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3474 psa_set_key_algorithm( &attributes, alg );
3475 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003476
Gilles Peskine049c7532019-05-15 20:22:09 +02003477 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003478 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003479 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3480 key_bits = psa_get_key_bits( &attributes );
3481
3482 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3483 alg );
3484 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3485 * should be exact. */
3486 TEST_EQUAL( output_size,
3487 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3488 TEST_ASSERT( output_size <=
3489 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3490 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003491
Steven Cooremand588ea12021-01-11 19:36:04 +01003492 status = psa_aead_encrypt( key, alg,
3493 nonce->x, nonce->len,
3494 additional_data->x, additional_data->len,
3495 input_data->x, input_data->len,
3496 output_data, output_size,
3497 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003498
Ronald Cron28a45ed2021-02-09 20:35:42 +01003499 /* If the operation is not supported, just skip and not fail in case the
3500 * encryption involves a common limitation of cryptography hardwares and
3501 * an alternative implementation. */
3502 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003503 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003504 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3505 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003506 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003507
3508 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003509 ASSERT_COMPARE( expected_result->x, expected_result->len,
3510 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003511
Gilles Peskinea1cac842018-06-11 19:33:02 +02003512exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003513 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003514 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003515 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003516}
3517/* END_CASE */
3518
3519/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003520void aead_decrypt( int key_type_arg, data_t *key_data,
3521 int alg_arg,
3522 data_t *nonce,
3523 data_t *additional_data,
3524 data_t *input_data,
3525 data_t *expected_data,
3526 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003527{
Ronald Cron5425a212020-08-04 14:58:35 +02003528 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003529 psa_key_type_t key_type = key_type_arg;
3530 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003531 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003532 unsigned char *output_data = NULL;
3533 size_t output_size = 0;
3534 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003536 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003537 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003538
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003540
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003541 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3542 psa_set_key_algorithm( &attributes, alg );
3543 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003544
Gilles Peskine049c7532019-05-15 20:22:09 +02003545 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003546 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003547 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3548 key_bits = psa_get_key_bits( &attributes );
3549
3550 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3551 alg );
3552 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3553 expected_result != PSA_ERROR_NOT_SUPPORTED )
3554 {
3555 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3556 * should be exact. */
3557 TEST_EQUAL( output_size,
3558 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3559 TEST_ASSERT( output_size <=
3560 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3561 }
3562 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003563
Steven Cooremand588ea12021-01-11 19:36:04 +01003564 status = psa_aead_decrypt( key, alg,
3565 nonce->x, nonce->len,
3566 additional_data->x,
3567 additional_data->len,
3568 input_data->x, input_data->len,
3569 output_data, output_size,
3570 &output_length );
3571
Ronald Cron28a45ed2021-02-09 20:35:42 +01003572 /* If the operation is not supported, just skip and not fail in case the
3573 * decryption involves a common limitation of cryptography hardwares and
3574 * an alternative implementation. */
3575 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003576 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003577 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3578 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003579 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003580
3581 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003582
Gilles Peskine2d277862018-06-18 15:41:12 +02003583 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003584 ASSERT_COMPARE( expected_data->x, expected_data->len,
3585 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003586
Gilles Peskinea1cac842018-06-11 19:33:02 +02003587exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003588 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003589 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003590 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003591}
3592/* END_CASE */
3593
3594/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003595void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3596 int alg_arg,
3597 data_t *nonce,
3598 data_t *additional_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003599 int test_ad_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003600 data_t *input_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003601 int test_data_mp_arg,
Paul Elliott5e3bb132021-06-22 16:22:13 +01003602 int test_set_lengths_arg,
Paul Elliottd3f82412021-06-16 16:52:21 +01003603 data_t *expected_result_arg )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003604{
Paul Elliottd3f82412021-06-16 16:52:21 +01003605 size_t ad_part_len = 0;
3606 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003607
Paul Elliottd3f82412021-06-16 16:52:21 +01003608 if( test_ad_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003609 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003610 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3611 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003612 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003613 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003614
Paul Elliottd3f82412021-06-16 16:52:21 +01003615 aead_multipart_encrypt_internal( key_type_arg, key_data,
3616 alg_arg,nonce,
3617 additional_data,
3618 ad_part_len,
3619 input_data, -1,
Paul Elliott5e3bb132021-06-22 16:22:13 +01003620 test_set_lengths_arg,
Paul Elliottd3f82412021-06-16 16:52:21 +01003621 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,
Paul Elliott5e3bb132021-06-22 16:22:13 +01003634 test_set_lengths_arg,
Paul Elliottd3f82412021-06-16 16:52:21 +01003635 expected_result_arg );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003636 }
3637 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003638
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639
Paul Elliott8fc45162021-06-23 16:06:01 +01003640 /* Goto is required to silence warnings about unused labels, as we
3641 * don't actually do any test assertions in this function. */
3642 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003643}
3644/* END_CASE */
3645
3646/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003647void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3648 int alg_arg,
3649 data_t *nonce,
3650 data_t *additional_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003651 int test_ad_mp_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003652 data_t *input_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003653 int test_data_mp_arg,
Paul Elliott5e3bb132021-06-22 16:22:13 +01003654 int test_set_lengths_arg,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003655 data_t *expected_data,
Paul Elliottd3f82412021-06-16 16:52:21 +01003656 int expected_status )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003657{
Paul Elliottd3f82412021-06-16 16:52:21 +01003658 size_t ad_part_len = 0;
3659 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003660
Paul Elliottd3f82412021-06-16 16:52:21 +01003661 if( test_ad_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003662 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003663 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3664 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003665 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003666 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003667
Paul Elliottd3f82412021-06-16 16:52:21 +01003668 aead_multipart_decrypt_internal( key_type_arg, key_data,
3669 alg_arg, nonce,
3670 additional_data,
3671 ad_part_len,
3672 input_data, -1,
Paul Elliott5e3bb132021-06-22 16:22:13 +01003673 test_set_lengths_arg,
Paul Elliottd3f82412021-06-16 16:52:21 +01003674 expected_data, expected_status );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003675 }
3676 }
3677
Paul Elliottd3f82412021-06-16 16:52:21 +01003678 if( test_data_mp_arg == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003679 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003680 for( data_part_len = 1; data_part_len <= input_data->len;
3681 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003682 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003683 aead_multipart_decrypt_internal( key_type_arg, key_data,
3684 alg_arg, nonce,
3685 additional_data, -1,
3686 input_data, data_part_len,
Paul Elliott5e3bb132021-06-22 16:22:13 +01003687 test_set_lengths_arg,
Paul Elliottd3f82412021-06-16 16:52:21 +01003688 expected_data, expected_status );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003689 }
3690 }
3691
Paul Elliott8fc45162021-06-23 16:06:01 +01003692 /* Goto is required to silence warnings about unused labels, as we
3693 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003694 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003695}
3696/* END_CASE */
3697
3698/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003699void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3700 int alg_arg,
3701 int nonce_len,
Paul Elliottd85f5472021-07-16 18:20:16 +01003702 int expected_generated_len_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003703 data_t *additional_data,
3704 data_t *input_data,
3705 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003706{
3707
3708 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3709 psa_key_type_t key_type = key_type_arg;
3710 psa_algorithm_t alg = alg_arg;
3711 psa_aead_operation_t operation;
3712 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3713 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3714 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3715 size_t nonce_generated_len = 0;
Paul Elliottd85f5472021-07-16 18:20:16 +01003716 size_t expected_generated_len = expected_generated_len_arg;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003717 unsigned char *output_data = NULL;
3718 unsigned char *final_data = NULL;
3719 size_t output_size = 0;
3720 size_t finish_output_size = 0;
3721 size_t output_length = 0;
3722 size_t tag_length = 0;
3723 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003724
3725 PSA_ASSERT( psa_crypto_init( ) );
3726
3727 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3728 psa_set_key_algorithm( & attributes, alg );
3729 psa_set_key_type( & attributes, key_type );
3730
3731 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3732 &key ) );
3733
3734 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3735
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003736 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3737
3738 ASSERT_ALLOC( output_data, output_size );
3739
3740 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3741
3742 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3743
3744 ASSERT_ALLOC( final_data, finish_output_size );
3745
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003746 operation = psa_aead_operation_init( );
3747
3748 status = psa_aead_encrypt_setup( &operation, key, alg );
3749
3750 /* If the operation is not supported, just skip and not fail in case the
3751 * encryption involves a common limitation of cryptography hardwares and
3752 * an alternative implementation. */
3753 if( status == PSA_ERROR_NOT_SUPPORTED )
3754 {
3755 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3756 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len );
3757 }
3758
3759 PSA_ASSERT( status );
3760
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003761 status = psa_aead_generate_nonce( &operation, nonce_buffer,
3762 nonce_len,
3763 &nonce_generated_len );
3764
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003765 TEST_ASSERT( status == expected_status_arg );
3766
Paul Elliottd85f5472021-07-16 18:20:16 +01003767 TEST_EQUAL( nonce_generated_len, expected_generated_len );
3768
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003769 TEST_ASSERT( nonce_generated_len < PSA_AEAD_NONCE_MAX_SIZE );
3770
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003771 if( expected_status_arg == PSA_SUCCESS )
3772 {
3773
3774 /* Ensure we can still complete operation. */
3775
3776 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3777 additional_data->len ) );
3778
3779 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
3780 output_data, output_size, &output_length ) );
3781
3782 PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size,
3783 &output_length, tag_buffer,
3784 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3785 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003786
3787exit:
3788 psa_destroy_key( key );
Paul Elliott16906f92021-06-24 09:57:01 +01003789 mbedtls_free( output_data );
3790 mbedtls_free( final_data );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003791 psa_aead_abort( &operation );
3792 PSA_DONE( );
3793}
3794/* END_CASE */
3795
3796/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01003797void aead_multipart_state_test( int key_type_arg, data_t *key_data,
3798 int alg_arg,
3799 data_t *nonce,
3800 data_t *additional_data,
3801 data_t *input_data )
3802{
3803 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3804 psa_key_type_t key_type = key_type_arg;
3805 psa_algorithm_t alg = alg_arg;
3806 psa_aead_operation_t operation;
3807 unsigned char *output_data = NULL;
3808 unsigned char *final_data = NULL;
3809 size_t output_size = 0;
3810 size_t finish_output_size = 0;
3811 size_t output_length = 0;
3812 size_t key_bits = 0;
3813 size_t tag_length = 0;
3814 size_t tag_size = 0;
3815 size_t nonce_length = 0;
3816 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3817 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3818 size_t output_part_length = 0;
3819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3820
3821 PSA_ASSERT( psa_crypto_init( ) );
3822
3823 psa_set_key_usage_flags( & attributes,
3824 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3825 psa_set_key_algorithm( & attributes, alg );
3826 psa_set_key_type( & attributes, key_type );
3827
3828 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3829 &key ) );
3830
3831 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3832 key_bits = psa_get_key_bits( &attributes );
3833
3834 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3835
3836 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
3837
3838 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3839
3840 ASSERT_ALLOC( output_data, output_size );
3841
3842 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3843
3844 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3845
3846 ASSERT_ALLOC( final_data, finish_output_size );
3847
3848 /* Test all operations error without calling setup first. */
3849
3850 operation = psa_aead_operation_init( );
3851
3852 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3853 PSA_ERROR_BAD_STATE );
3854
3855 psa_aead_abort( &operation );
3856
3857 operation = psa_aead_operation_init( );
3858
3859 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3860 PSA_AEAD_NONCE_MAX_SIZE,
3861 &nonce_length ),
3862 PSA_ERROR_BAD_STATE );
3863
3864 psa_aead_abort( &operation );
3865
Paul Elliott481be342021-07-16 17:38:47 +01003866 /* ------------------------------------------------------- */
3867
Paul Elliottc23a9a02021-06-21 18:32:46 +01003868 operation = psa_aead_operation_init( );
3869
3870 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3871 input_data->len ),
3872 PSA_ERROR_BAD_STATE );
3873
3874 psa_aead_abort( &operation );
3875
Paul Elliott481be342021-07-16 17:38:47 +01003876 /* ------------------------------------------------------- */
3877
Paul Elliottc23a9a02021-06-21 18:32:46 +01003878 operation = psa_aead_operation_init( );
3879
3880 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3881 additional_data->len ),
3882 PSA_ERROR_BAD_STATE );
3883
3884 psa_aead_abort( &operation );
3885
Paul Elliott481be342021-07-16 17:38:47 +01003886 /* ------------------------------------------------------- */
3887
Paul Elliottc23a9a02021-06-21 18:32:46 +01003888 operation = psa_aead_operation_init( );
3889
3890 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
3891 input_data->len, output_data,
3892 output_size, &output_length ),
3893 PSA_ERROR_BAD_STATE );
3894
3895 psa_aead_abort( &operation );
3896
Paul Elliott481be342021-07-16 17:38:47 +01003897 /* ------------------------------------------------------- */
3898
Paul Elliottc23a9a02021-06-21 18:32:46 +01003899 operation = psa_aead_operation_init( );
3900
3901 TEST_EQUAL( psa_aead_finish( &operation, final_data,
3902 finish_output_size,
3903 &output_part_length,
3904 tag_buffer, tag_length,
3905 &tag_size ),
3906 PSA_ERROR_BAD_STATE );
3907
3908 psa_aead_abort( &operation );
3909
Paul Elliott481be342021-07-16 17:38:47 +01003910 /* ------------------------------------------------------- */
3911
Paul Elliottc23a9a02021-06-21 18:32:46 +01003912 operation = psa_aead_operation_init( );
3913
3914 TEST_EQUAL( psa_aead_verify( &operation, final_data,
3915 finish_output_size,
3916 &output_part_length,
3917 tag_buffer,
3918 tag_length ),
3919 PSA_ERROR_BAD_STATE );
3920
3921 psa_aead_abort( &operation );
3922
3923 /* Test for double setups. */
3924
3925 operation = psa_aead_operation_init( );
3926
3927 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3928
3929 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3930 PSA_ERROR_BAD_STATE );
3931
3932 psa_aead_abort( &operation );
3933
Paul Elliott481be342021-07-16 17:38:47 +01003934 /* ------------------------------------------------------- */
3935
Paul Elliottc23a9a02021-06-21 18:32:46 +01003936 operation = psa_aead_operation_init( );
3937
3938 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3939
3940 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3941 PSA_ERROR_BAD_STATE );
3942
3943 psa_aead_abort( &operation );
3944
Paul Elliott374a2be2021-07-16 17:53:40 +01003945 /* ------------------------------------------------------- */
3946
3947 operation = psa_aead_operation_init( );
3948
3949 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3950
3951 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3952 PSA_ERROR_BAD_STATE );
3953
3954 psa_aead_abort( &operation );
3955
3956 /* ------------------------------------------------------- */
3957
3958 operation = psa_aead_operation_init( );
3959
3960 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3961
3962 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3963 PSA_ERROR_BAD_STATE );
3964
3965 psa_aead_abort( &operation );
3966
Paul Elliottc23a9a02021-06-21 18:32:46 +01003967 /* Test for not setting a nonce. */
3968
3969 operation = psa_aead_operation_init( );
3970
3971 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3972
3973 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3974 additional_data->len ),
3975 PSA_ERROR_BAD_STATE );
3976
3977 psa_aead_abort( &operation );
3978
3979 /* Test for double setting nonce. */
3980
3981 operation = psa_aead_operation_init( );
3982
3983 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3984
3985 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3986
3987 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3988 PSA_ERROR_BAD_STATE );
3989
3990 psa_aead_abort( &operation );
3991
Paul Elliott374a2be2021-07-16 17:53:40 +01003992 /* Test for double generating nonce. */
3993
3994 operation = psa_aead_operation_init( );
3995
3996 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3997
3998 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3999 PSA_AEAD_NONCE_MAX_SIZE,
4000 &nonce_length ) );
4001
4002 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4003 PSA_AEAD_NONCE_MAX_SIZE,
4004 &nonce_length ),
4005 PSA_ERROR_BAD_STATE );
4006
4007
4008 psa_aead_abort( &operation );
4009
4010 /* Test for generate nonce then set and vice versa */
4011
4012 operation = psa_aead_operation_init( );
4013
4014 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4015
4016 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4017 PSA_AEAD_NONCE_MAX_SIZE,
4018 &nonce_length ) );
4019
4020 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4021 PSA_ERROR_BAD_STATE );
4022
4023 psa_aead_abort( &operation );
4024
4025 /* ------------------------------------------------------- */
4026
4027 operation = psa_aead_operation_init( );
4028
4029 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4030
4031 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4032
4033 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4034 PSA_AEAD_NONCE_MAX_SIZE,
4035 &nonce_length ),
4036 PSA_ERROR_BAD_STATE );
4037
4038 psa_aead_abort( &operation );
4039
Paul Elliott7220cae2021-06-22 17:25:57 +01004040 /* Test for generating nonce in decrypt setup. */
4041
4042 operation = psa_aead_operation_init( );
4043
4044 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4045
4046 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4047 PSA_AEAD_NONCE_MAX_SIZE,
4048 &nonce_length ),
4049 PSA_ERROR_BAD_STATE );
4050
4051 psa_aead_abort( &operation );
4052
Paul Elliottc23a9a02021-06-21 18:32:46 +01004053 /* Test for setting lengths twice. */
4054
4055 operation = psa_aead_operation_init( );
4056
4057 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4058
4059 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4060
4061 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4062 input_data->len ) );
4063
4064 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4065 input_data->len ),
4066 PSA_ERROR_BAD_STATE );
4067
4068 psa_aead_abort( &operation );
4069
Paul Elliott01876512021-06-23 18:13:04 +01004070 /* Test that generate/set nonce and set lengths are interchangeable (we
4071 * already tested set nonce followed by set lengths above). */
4072
4073 operation = psa_aead_operation_init( );
4074
4075 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4076
4077 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4078 input_data->len ) );
4079
4080 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4081
4082 psa_aead_abort( &operation );
4083
Paul Elliott481be342021-07-16 17:38:47 +01004084 /* ------------------------------------------------------- */
4085
Paul Elliott01876512021-06-23 18:13:04 +01004086 operation = psa_aead_operation_init( );
4087
4088 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4089
4090 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4091 input_data->len ) );
4092
4093 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4094 PSA_AEAD_NONCE_MAX_SIZE,
4095 &nonce_length ) );
4096
4097 psa_aead_abort( &operation );
4098
Paul Elliott481be342021-07-16 17:38:47 +01004099 /* ------------------------------------------------------- */
4100
Paul Elliott01876512021-06-23 18:13:04 +01004101 operation = psa_aead_operation_init( );
4102
4103 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4104
4105 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4106 PSA_AEAD_NONCE_MAX_SIZE,
4107 &nonce_length ) );
4108
4109 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4110 input_data->len ) );
4111
4112 psa_aead_abort( &operation );
4113
Paul Elliottc23a9a02021-06-21 18:32:46 +01004114 /* Test for setting lengths after already starting data. */
4115
4116 operation = psa_aead_operation_init( );
4117
4118 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4119
4120 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4121
4122 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4123 input_data->len, output_data,
4124 output_size, &output_length ) );
4125
4126 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4127 input_data->len ),
4128 PSA_ERROR_BAD_STATE );
4129
4130 psa_aead_abort( &operation );
4131
4132 /* Test for not sending any additional data or data (encrypt) */
4133
4134 operation = psa_aead_operation_init( );
4135
4136 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4137
4138 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4139
4140 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4141 input_data->len ) );
4142
4143 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4144 finish_output_size,
4145 &output_part_length,
4146 tag_buffer, tag_length,
4147 &tag_size ),
4148 PSA_ERROR_INVALID_ARGUMENT );
4149
4150 psa_aead_abort( &operation );
4151
4152 /* Test for not sending any additional data or data (decrypt) */
4153
4154 operation = psa_aead_operation_init( );
4155
4156 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4157
4158 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4159
4160 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4161 input_data->len ) );
4162
4163 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4164 finish_output_size,
4165 &output_part_length,
4166 tag_buffer,
4167 tag_length ),
4168 PSA_ERROR_INVALID_ARGUMENT );
4169
4170 psa_aead_abort( &operation );
4171
4172 /* Test for not sending any additional data. */
4173
4174 operation = psa_aead_operation_init( );
4175
4176 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4177
4178 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4179
4180 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4181 input_data->len ) );
4182
4183 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4184 input_data->len, output_data,
4185 output_size, &output_length ),
4186 PSA_ERROR_INVALID_ARGUMENT );
4187
4188 psa_aead_abort( &operation );
4189
4190 /* Test sending additional data after data. */
4191
4192 operation = psa_aead_operation_init( );
4193
4194 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4195
4196 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4197
4198 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4199 input_data->len, output_data,
4200 output_size, &output_length ) );
4201
4202 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4203 additional_data->len ),
4204 PSA_ERROR_BAD_STATE );
4205
4206 psa_aead_abort( &operation );
4207
Paul Elliott534d0b42021-06-22 19:15:20 +01004208 /* Test calling finish on decryption. */
4209
4210 operation = psa_aead_operation_init( );
4211
4212 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4213
4214 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4215
4216 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4217 finish_output_size,
4218 &output_part_length,
4219 tag_buffer, tag_length,
4220 &tag_size ),
4221 PSA_ERROR_BAD_STATE );
4222
4223 psa_aead_abort( &operation );
4224
4225 /* Test calling verify on encryption. */
4226
4227 operation = psa_aead_operation_init( );
4228
4229 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4230
4231 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4232
4233 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4234 finish_output_size,
4235 &output_part_length,
4236 tag_buffer,
4237 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004238 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004239
4240 psa_aead_abort( &operation );
4241
4242
Paul Elliottc23a9a02021-06-21 18:32:46 +01004243exit:
4244 psa_destroy_key( key );
4245 psa_aead_abort( &operation );
4246 mbedtls_free( output_data );
4247 mbedtls_free( final_data );
4248 PSA_DONE( );
4249}
4250/* END_CASE */
4251
4252/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004253void signature_size( int type_arg,
4254 int bits,
4255 int alg_arg,
4256 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004257{
4258 psa_key_type_t type = type_arg;
4259 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004260 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004261
Gilles Peskinefe11b722018-12-18 00:24:04 +01004262 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004263
Gilles Peskinee59236f2018-01-27 23:32:46 +01004264exit:
4265 ;
4266}
4267/* END_CASE */
4268
4269/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004270void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4271 int alg_arg, data_t *input_data,
4272 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004273{
Ronald Cron5425a212020-08-04 14:58:35 +02004274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004275 psa_key_type_t key_type = key_type_arg;
4276 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004277 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004278 unsigned char *signature = NULL;
4279 size_t signature_size;
4280 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004282
Gilles Peskine8817f612018-12-18 00:18:46 +01004283 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004284
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004286 psa_set_key_algorithm( &attributes, alg );
4287 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004288
Gilles Peskine049c7532019-05-15 20:22:09 +02004289 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004290 &key ) );
4291 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004292 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004293
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004294 /* Allocate a buffer which has the size advertized by the
4295 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004296 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004297 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004298 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004299 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004300 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004301
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004302 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004303 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004304 input_data->x, input_data->len,
4305 signature, signature_size,
4306 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004307 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004308 ASSERT_COMPARE( output_data->x, output_data->len,
4309 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004310
4311exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004312 /*
4313 * Key attributes may have been returned by psa_get_key_attributes()
4314 * thus reset them as required.
4315 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004316 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004317
Ronald Cron5425a212020-08-04 14:58:35 +02004318 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004319 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004320 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004321}
4322/* END_CASE */
4323
4324/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004325void sign_hash_fail( int key_type_arg, data_t *key_data,
4326 int alg_arg, data_t *input_data,
4327 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004328{
Ronald Cron5425a212020-08-04 14:58:35 +02004329 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004330 psa_key_type_t key_type = key_type_arg;
4331 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004332 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004333 psa_status_t actual_status;
4334 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004335 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004336 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004338
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004339 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004340
Gilles Peskine8817f612018-12-18 00:18:46 +01004341 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004342
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004343 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004344 psa_set_key_algorithm( &attributes, alg );
4345 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004346
Gilles Peskine049c7532019-05-15 20:22:09 +02004347 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004348 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004349
Ronald Cron5425a212020-08-04 14:58:35 +02004350 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004351 input_data->x, input_data->len,
4352 signature, signature_size,
4353 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004354 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004355 /* The value of *signature_length is unspecified on error, but
4356 * whatever it is, it should be less than signature_size, so that
4357 * if the caller tries to read *signature_length bytes without
4358 * checking the error code then they don't overflow a buffer. */
4359 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004360
4361exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004362 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004363 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004364 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004365 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004366}
4367/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004368
4369/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004370void sign_verify_hash( int key_type_arg, data_t *key_data,
4371 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004372{
Ronald Cron5425a212020-08-04 14:58:35 +02004373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004374 psa_key_type_t key_type = key_type_arg;
4375 psa_algorithm_t alg = alg_arg;
4376 size_t key_bits;
4377 unsigned char *signature = NULL;
4378 size_t signature_size;
4379 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004381
Gilles Peskine8817f612018-12-18 00:18:46 +01004382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004383
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004384 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004385 psa_set_key_algorithm( &attributes, alg );
4386 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004387
Gilles Peskine049c7532019-05-15 20:22:09 +02004388 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004389 &key ) );
4390 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004391 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004392
4393 /* Allocate a buffer which has the size advertized by the
4394 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004395 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004396 key_bits, alg );
4397 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004398 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004399 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004400
4401 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004402 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004403 input_data->x, input_data->len,
4404 signature, signature_size,
4405 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004406 /* Check that the signature length looks sensible. */
4407 TEST_ASSERT( signature_length <= signature_size );
4408 TEST_ASSERT( signature_length > 0 );
4409
4410 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004411 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004412 input_data->x, input_data->len,
4413 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004414
4415 if( input_data->len != 0 )
4416 {
4417 /* Flip a bit in the input and verify that the signature is now
4418 * detected as invalid. Flip a bit at the beginning, not at the end,
4419 * because ECDSA may ignore the last few bits of the input. */
4420 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004421 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004422 input_data->x, input_data->len,
4423 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004424 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004425 }
4426
4427exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004428 /*
4429 * Key attributes may have been returned by psa_get_key_attributes()
4430 * thus reset them as required.
4431 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004432 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004433
Ronald Cron5425a212020-08-04 14:58:35 +02004434 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004435 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004436 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004437}
4438/* END_CASE */
4439
4440/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004441void verify_hash( int key_type_arg, data_t *key_data,
4442 int alg_arg, data_t *hash_data,
4443 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004444{
Ronald Cron5425a212020-08-04 14:58:35 +02004445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004446 psa_key_type_t key_type = key_type_arg;
4447 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004449
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004450 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004451
Gilles Peskine8817f612018-12-18 00:18:46 +01004452 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004453
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004454 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004455 psa_set_key_algorithm( &attributes, alg );
4456 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004457
Gilles Peskine049c7532019-05-15 20:22:09 +02004458 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004459 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004460
Ronald Cron5425a212020-08-04 14:58:35 +02004461 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004462 hash_data->x, hash_data->len,
4463 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004464
itayzafrir5c753392018-05-08 11:18:38 +03004465exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004466 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004467 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004468 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004469}
4470/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004471
4472/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004473void verify_hash_fail( int key_type_arg, data_t *key_data,
4474 int alg_arg, data_t *hash_data,
4475 data_t *signature_data,
4476 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004477{
Ronald Cron5425a212020-08-04 14:58:35 +02004478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004479 psa_key_type_t key_type = key_type_arg;
4480 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004481 psa_status_t actual_status;
4482 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004484
Gilles Peskine8817f612018-12-18 00:18:46 +01004485 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004486
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004487 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004488 psa_set_key_algorithm( &attributes, alg );
4489 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004490
Gilles Peskine049c7532019-05-15 20:22:09 +02004491 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004492 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004493
Ronald Cron5425a212020-08-04 14:58:35 +02004494 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004495 hash_data->x, hash_data->len,
4496 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004497 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004498
4499exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004500 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004501 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004502 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004503}
4504/* END_CASE */
4505
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004506/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004507void sign_message_deterministic( int key_type_arg,
4508 data_t *key_data,
4509 int alg_arg,
4510 data_t *input_data,
4511 data_t *output_data )
4512{
4513 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4514 psa_key_type_t key_type = key_type_arg;
4515 psa_algorithm_t alg = alg_arg;
4516 size_t key_bits;
4517 unsigned char *signature = NULL;
4518 size_t signature_size;
4519 size_t signature_length = 0xdeadbeef;
4520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4521
4522 PSA_ASSERT( psa_crypto_init( ) );
4523
4524 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4525 psa_set_key_algorithm( &attributes, alg );
4526 psa_set_key_type( &attributes, key_type );
4527
4528 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4529 &key ) );
4530 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4531 key_bits = psa_get_key_bits( &attributes );
4532
4533 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4534 TEST_ASSERT( signature_size != 0 );
4535 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4536 ASSERT_ALLOC( signature, signature_size );
4537
4538 PSA_ASSERT( psa_sign_message( key, alg,
4539 input_data->x, input_data->len,
4540 signature, signature_size,
4541 &signature_length ) );
4542
4543 ASSERT_COMPARE( output_data->x, output_data->len,
4544 signature, signature_length );
4545
4546exit:
4547 psa_reset_key_attributes( &attributes );
4548
4549 psa_destroy_key( key );
4550 mbedtls_free( signature );
4551 PSA_DONE( );
4552
4553}
4554/* END_CASE */
4555
4556/* BEGIN_CASE */
4557void sign_message_fail( int key_type_arg,
4558 data_t *key_data,
4559 int alg_arg,
4560 data_t *input_data,
4561 int signature_size_arg,
4562 int expected_status_arg )
4563{
4564 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4565 psa_key_type_t key_type = key_type_arg;
4566 psa_algorithm_t alg = alg_arg;
4567 size_t signature_size = signature_size_arg;
4568 psa_status_t actual_status;
4569 psa_status_t expected_status = expected_status_arg;
4570 unsigned char *signature = NULL;
4571 size_t signature_length = 0xdeadbeef;
4572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4573
4574 ASSERT_ALLOC( signature, signature_size );
4575
4576 PSA_ASSERT( psa_crypto_init( ) );
4577
4578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4579 psa_set_key_algorithm( &attributes, alg );
4580 psa_set_key_type( &attributes, key_type );
4581
4582 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4583 &key ) );
4584
4585 actual_status = psa_sign_message( key, alg,
4586 input_data->x, input_data->len,
4587 signature, signature_size,
4588 &signature_length );
4589 TEST_EQUAL( actual_status, expected_status );
4590 /* The value of *signature_length is unspecified on error, but
4591 * whatever it is, it should be less than signature_size, so that
4592 * if the caller tries to read *signature_length bytes without
4593 * checking the error code then they don't overflow a buffer. */
4594 TEST_ASSERT( signature_length <= signature_size );
4595
4596exit:
4597 psa_reset_key_attributes( &attributes );
4598 psa_destroy_key( key );
4599 mbedtls_free( signature );
4600 PSA_DONE( );
4601}
4602/* END_CASE */
4603
4604/* BEGIN_CASE */
4605void sign_verify_message( int key_type_arg,
4606 data_t *key_data,
4607 int alg_arg,
4608 data_t *input_data )
4609{
4610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4611 psa_key_type_t key_type = key_type_arg;
4612 psa_algorithm_t alg = alg_arg;
4613 size_t key_bits;
4614 unsigned char *signature = NULL;
4615 size_t signature_size;
4616 size_t signature_length = 0xdeadbeef;
4617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4618
4619 PSA_ASSERT( psa_crypto_init( ) );
4620
4621 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4622 PSA_KEY_USAGE_VERIFY_MESSAGE );
4623 psa_set_key_algorithm( &attributes, alg );
4624 psa_set_key_type( &attributes, key_type );
4625
4626 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4627 &key ) );
4628 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4629 key_bits = psa_get_key_bits( &attributes );
4630
4631 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4632 TEST_ASSERT( signature_size != 0 );
4633 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4634 ASSERT_ALLOC( signature, signature_size );
4635
4636 PSA_ASSERT( psa_sign_message( key, alg,
4637 input_data->x, input_data->len,
4638 signature, signature_size,
4639 &signature_length ) );
4640 TEST_ASSERT( signature_length <= signature_size );
4641 TEST_ASSERT( signature_length > 0 );
4642
4643 PSA_ASSERT( psa_verify_message( key, alg,
4644 input_data->x, input_data->len,
4645 signature, signature_length ) );
4646
4647 if( input_data->len != 0 )
4648 {
4649 /* Flip a bit in the input and verify that the signature is now
4650 * detected as invalid. Flip a bit at the beginning, not at the end,
4651 * because ECDSA may ignore the last few bits of the input. */
4652 input_data->x[0] ^= 1;
4653 TEST_EQUAL( psa_verify_message( key, alg,
4654 input_data->x, input_data->len,
4655 signature, signature_length ),
4656 PSA_ERROR_INVALID_SIGNATURE );
4657 }
4658
4659exit:
4660 psa_reset_key_attributes( &attributes );
4661
4662 psa_destroy_key( key );
4663 mbedtls_free( signature );
4664 PSA_DONE( );
4665}
4666/* END_CASE */
4667
4668/* BEGIN_CASE */
4669void verify_message( int key_type_arg,
4670 data_t *key_data,
4671 int alg_arg,
4672 data_t *input_data,
4673 data_t *signature_data )
4674{
4675 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4676 psa_key_type_t key_type = key_type_arg;
4677 psa_algorithm_t alg = alg_arg;
4678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4679
4680 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4681
4682 PSA_ASSERT( psa_crypto_init( ) );
4683
4684 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4685 psa_set_key_algorithm( &attributes, alg );
4686 psa_set_key_type( &attributes, key_type );
4687
4688 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4689 &key ) );
4690
4691 PSA_ASSERT( psa_verify_message( key, alg,
4692 input_data->x, input_data->len,
4693 signature_data->x, signature_data->len ) );
4694
4695exit:
4696 psa_reset_key_attributes( &attributes );
4697 psa_destroy_key( key );
4698 PSA_DONE( );
4699}
4700/* END_CASE */
4701
4702/* BEGIN_CASE */
4703void verify_message_fail( int key_type_arg,
4704 data_t *key_data,
4705 int alg_arg,
4706 data_t *hash_data,
4707 data_t *signature_data,
4708 int expected_status_arg )
4709{
4710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4711 psa_key_type_t key_type = key_type_arg;
4712 psa_algorithm_t alg = alg_arg;
4713 psa_status_t actual_status;
4714 psa_status_t expected_status = expected_status_arg;
4715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4716
4717 PSA_ASSERT( psa_crypto_init( ) );
4718
4719 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4720 psa_set_key_algorithm( &attributes, alg );
4721 psa_set_key_type( &attributes, key_type );
4722
4723 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4724 &key ) );
4725
4726 actual_status = psa_verify_message( key, alg,
4727 hash_data->x, hash_data->len,
4728 signature_data->x,
4729 signature_data->len );
4730 TEST_EQUAL( actual_status, expected_status );
4731
4732exit:
4733 psa_reset_key_attributes( &attributes );
4734 psa_destroy_key( key );
4735 PSA_DONE( );
4736}
4737/* END_CASE */
4738
4739/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004740void asymmetric_encrypt( int key_type_arg,
4741 data_t *key_data,
4742 int alg_arg,
4743 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004744 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004745 int expected_output_length_arg,
4746 int expected_status_arg )
4747{
Ronald Cron5425a212020-08-04 14:58:35 +02004748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004749 psa_key_type_t key_type = key_type_arg;
4750 psa_algorithm_t alg = alg_arg;
4751 size_t expected_output_length = expected_output_length_arg;
4752 size_t key_bits;
4753 unsigned char *output = NULL;
4754 size_t output_size;
4755 size_t output_length = ~0;
4756 psa_status_t actual_status;
4757 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004759
Gilles Peskine8817f612018-12-18 00:18:46 +01004760 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004761
Gilles Peskine656896e2018-06-29 19:12:28 +02004762 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004763 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4764 psa_set_key_algorithm( &attributes, alg );
4765 psa_set_key_type( &attributes, key_type );
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 ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004768
4769 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004770 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004771 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004772
Gilles Peskine656896e2018-06-29 19:12:28 +02004773 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004774 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004775 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004776
4777 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004778 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004779 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004780 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004781 output, output_size,
4782 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004783 TEST_EQUAL( actual_status, expected_status );
4784 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004785
Gilles Peskine68428122018-06-30 18:42:41 +02004786 /* If the label is empty, the test framework puts a non-null pointer
4787 * in label->x. Test that a null pointer works as well. */
4788 if( label->len == 0 )
4789 {
4790 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004791 if( output_size != 0 )
4792 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004793 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004794 input_data->x, input_data->len,
4795 NULL, label->len,
4796 output, output_size,
4797 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004798 TEST_EQUAL( actual_status, expected_status );
4799 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004800 }
4801
Gilles Peskine656896e2018-06-29 19:12:28 +02004802exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004803 /*
4804 * Key attributes may have been returned by psa_get_key_attributes()
4805 * thus reset them as required.
4806 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004807 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004808
Ronald Cron5425a212020-08-04 14:58:35 +02004809 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004810 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004811 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004812}
4813/* END_CASE */
4814
4815/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004816void asymmetric_encrypt_decrypt( int key_type_arg,
4817 data_t *key_data,
4818 int alg_arg,
4819 data_t *input_data,
4820 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004821{
Ronald Cron5425a212020-08-04 14:58:35 +02004822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004823 psa_key_type_t key_type = key_type_arg;
4824 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004825 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004826 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004827 size_t output_size;
4828 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004829 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004830 size_t output2_size;
4831 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004833
Gilles Peskine8817f612018-12-18 00:18:46 +01004834 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004835
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004836 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4837 psa_set_key_algorithm( &attributes, alg );
4838 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004839
Gilles Peskine049c7532019-05-15 20:22:09 +02004840 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004841 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004842
4843 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004844 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004845 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004846
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004847 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004848 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004849 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004850
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004851 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004852 TEST_ASSERT( output2_size <=
4853 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4854 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004855 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004856
Gilles Peskineeebd7382018-06-08 18:11:54 +02004857 /* We test encryption by checking that encrypt-then-decrypt gives back
4858 * the original plaintext because of the non-optional random
4859 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004860 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004861 input_data->x, input_data->len,
4862 label->x, label->len,
4863 output, output_size,
4864 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004865 /* We don't know what ciphertext length to expect, but check that
4866 * it looks sensible. */
4867 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004868
Ronald Cron5425a212020-08-04 14:58:35 +02004869 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004870 output, output_length,
4871 label->x, label->len,
4872 output2, output2_size,
4873 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004874 ASSERT_COMPARE( input_data->x, input_data->len,
4875 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004876
4877exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004878 /*
4879 * Key attributes may have been returned by psa_get_key_attributes()
4880 * thus reset them as required.
4881 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004882 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004883
Ronald Cron5425a212020-08-04 14:58:35 +02004884 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004885 mbedtls_free( output );
4886 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004887 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004888}
4889/* END_CASE */
4890
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004891/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004892void asymmetric_decrypt( int key_type_arg,
4893 data_t *key_data,
4894 int alg_arg,
4895 data_t *input_data,
4896 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004897 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004898{
Ronald Cron5425a212020-08-04 14:58:35 +02004899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004900 psa_key_type_t key_type = key_type_arg;
4901 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004902 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004903 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004904 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004905 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004907
Gilles Peskine8817f612018-12-18 00:18:46 +01004908 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004909
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004910 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4911 psa_set_key_algorithm( &attributes, alg );
4912 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004913
Gilles Peskine049c7532019-05-15 20:22:09 +02004914 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004915 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004916
gabor-mezei-armceface22021-01-21 12:26:17 +01004917 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4918 key_bits = psa_get_key_bits( &attributes );
4919
4920 /* Determine the maximum ciphertext length */
4921 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4922 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4923 ASSERT_ALLOC( output, output_size );
4924
Ronald Cron5425a212020-08-04 14:58:35 +02004925 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004926 input_data->x, input_data->len,
4927 label->x, label->len,
4928 output,
4929 output_size,
4930 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004931 ASSERT_COMPARE( expected_data->x, expected_data->len,
4932 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004933
Gilles Peskine68428122018-06-30 18:42:41 +02004934 /* If the label is empty, the test framework puts a non-null pointer
4935 * in label->x. Test that a null pointer works as well. */
4936 if( label->len == 0 )
4937 {
4938 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004939 if( output_size != 0 )
4940 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004941 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004942 input_data->x, input_data->len,
4943 NULL, label->len,
4944 output,
4945 output_size,
4946 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004947 ASSERT_COMPARE( expected_data->x, expected_data->len,
4948 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004949 }
4950
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004951exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004952 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004953 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004954 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004955 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004956}
4957/* END_CASE */
4958
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004959/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004960void asymmetric_decrypt_fail( int key_type_arg,
4961 data_t *key_data,
4962 int alg_arg,
4963 data_t *input_data,
4964 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004965 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004966 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004967{
Ronald Cron5425a212020-08-04 14:58:35 +02004968 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004969 psa_key_type_t key_type = key_type_arg;
4970 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004971 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004972 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004973 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004974 psa_status_t actual_status;
4975 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004976 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004977
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004978 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004979
Gilles Peskine8817f612018-12-18 00:18:46 +01004980 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004981
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004982 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4983 psa_set_key_algorithm( &attributes, alg );
4984 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004985
Gilles Peskine049c7532019-05-15 20:22:09 +02004986 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004987 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004988
Ronald Cron5425a212020-08-04 14:58:35 +02004989 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004990 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004991 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004992 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004993 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004994 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004995 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004996
Gilles Peskine68428122018-06-30 18:42:41 +02004997 /* If the label is empty, the test framework puts a non-null pointer
4998 * in label->x. Test that a null pointer works as well. */
4999 if( label->len == 0 )
5000 {
5001 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005002 if( output_size != 0 )
5003 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005004 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005005 input_data->x, input_data->len,
5006 NULL, label->len,
5007 output, output_size,
5008 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005009 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005010 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005011 }
5012
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005013exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005014 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005015 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005016 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005017 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005018}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005019/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005020
5021/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005022void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005023{
5024 /* Test each valid way of initializing the object, except for `= {0}`, as
5025 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5026 * though it's OK by the C standard. We could test for this, but we'd need
5027 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005028 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005029 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5030 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5031 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005032
5033 memset( &zero, 0, sizeof( zero ) );
5034
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005036 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005037 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005038 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005039 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005040 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005041 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005042
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005043 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005044 PSA_ASSERT( psa_key_derivation_abort(&func) );
5045 PSA_ASSERT( psa_key_derivation_abort(&init) );
5046 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005047}
5048/* END_CASE */
5049
Janos Follath16de4a42019-06-13 16:32:24 +01005050/* BEGIN_CASE */
5051void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005052{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005053 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005054 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005056
Gilles Peskine8817f612018-12-18 00:18:46 +01005057 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005058
Janos Follath16de4a42019-06-13 16:32:24 +01005059 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005060 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005061
5062exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005064 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005065}
5066/* END_CASE */
5067
Janos Follathaf3c2a02019-06-12 12:34:34 +01005068/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005069void derive_set_capacity( int alg_arg, int capacity_arg,
5070 int expected_status_arg )
5071{
5072 psa_algorithm_t alg = alg_arg;
5073 size_t capacity = capacity_arg;
5074 psa_status_t expected_status = expected_status_arg;
5075 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5076
5077 PSA_ASSERT( psa_crypto_init( ) );
5078
5079 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5080
5081 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5082 expected_status );
5083
5084exit:
5085 psa_key_derivation_abort( &operation );
5086 PSA_DONE( );
5087}
5088/* END_CASE */
5089
5090/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005091void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005092 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005093 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005094 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005095 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005096 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005097 int expected_status_arg3,
5098 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005099{
5100 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005101 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5102 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005103 psa_status_t expected_statuses[] = {expected_status_arg1,
5104 expected_status_arg2,
5105 expected_status_arg3};
5106 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005107 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5108 MBEDTLS_SVC_KEY_ID_INIT,
5109 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005110 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5112 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005113 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005114 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005115 psa_status_t expected_output_status = expected_output_status_arg;
5116 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005117
5118 PSA_ASSERT( psa_crypto_init( ) );
5119
5120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5121 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005122
5123 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5124
5125 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5126 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005127 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005128 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005129 psa_set_key_type( &attributes, key_types[i] );
5130 PSA_ASSERT( psa_import_key( &attributes,
5131 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005132 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005133 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5134 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5135 {
5136 // When taking a private key as secret input, use key agreement
5137 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005138 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5139 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005140 expected_statuses[i] );
5141 }
5142 else
5143 {
5144 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005145 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005146 expected_statuses[i] );
5147 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005148 }
5149 else
5150 {
5151 TEST_EQUAL( psa_key_derivation_input_bytes(
5152 &operation, steps[i],
5153 inputs[i]->x, inputs[i]->len ),
5154 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005155 }
5156 }
5157
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005158 if( output_key_type != PSA_KEY_TYPE_NONE )
5159 {
5160 psa_reset_key_attributes( &attributes );
5161 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5162 psa_set_key_bits( &attributes, 8 );
5163 actual_output_status =
5164 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005165 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005166 }
5167 else
5168 {
5169 uint8_t buffer[1];
5170 actual_output_status =
5171 psa_key_derivation_output_bytes( &operation,
5172 buffer, sizeof( buffer ) );
5173 }
5174 TEST_EQUAL( actual_output_status, expected_output_status );
5175
Janos Follathaf3c2a02019-06-12 12:34:34 +01005176exit:
5177 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005178 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5179 psa_destroy_key( keys[i] );
5180 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005181 PSA_DONE( );
5182}
5183/* END_CASE */
5184
Janos Follathd958bb72019-07-03 15:02:16 +01005185/* BEGIN_CASE */
5186void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005187{
Janos Follathd958bb72019-07-03 15:02:16 +01005188 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005189 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005190 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005191 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005192 unsigned char input1[] = "Input 1";
5193 size_t input1_length = sizeof( input1 );
5194 unsigned char input2[] = "Input 2";
5195 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005196 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005197 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005198 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5199 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5200 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005202
Gilles Peskine8817f612018-12-18 00:18:46 +01005203 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005204
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005205 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5206 psa_set_key_algorithm( &attributes, alg );
5207 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005208
Gilles Peskine73676cb2019-05-15 20:15:10 +02005209 PSA_ASSERT( psa_import_key( &attributes,
5210 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005211 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005212
5213 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005214 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5215 input1, input1_length,
5216 input2, input2_length,
5217 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005218 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005219
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005221 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005222 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005223
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005224 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005225
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005227 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005228
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005229exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005231 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005232 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005233}
5234/* END_CASE */
5235
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005236/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005237void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005238{
5239 uint8_t output_buffer[16];
5240 size_t buffer_size = 16;
5241 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005243
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005244 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5245 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005246 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005247
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005249 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005250
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005252
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005253 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5254 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005255 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005256
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005258 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005259
5260exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005262}
5263/* END_CASE */
5264
5265/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005266void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005267 int step1_arg, data_t *input1,
5268 int step2_arg, data_t *input2,
5269 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005270 int requested_capacity_arg,
5271 data_t *expected_output1,
5272 data_t *expected_output2 )
5273{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005274 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005275 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5276 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005277 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5278 MBEDTLS_SVC_KEY_ID_INIT,
5279 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005280 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005282 uint8_t *expected_outputs[2] =
5283 {expected_output1->x, expected_output2->x};
5284 size_t output_sizes[2] =
5285 {expected_output1->len, expected_output2->len};
5286 size_t output_buffer_size = 0;
5287 uint8_t *output_buffer = NULL;
5288 size_t expected_capacity;
5289 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005291 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005292 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005293
5294 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5295 {
5296 if( output_sizes[i] > output_buffer_size )
5297 output_buffer_size = output_sizes[i];
5298 if( output_sizes[i] == 0 )
5299 expected_outputs[i] = NULL;
5300 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005301 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005302 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005303
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5305 psa_set_key_algorithm( &attributes, alg );
5306 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005307
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005308 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005309 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5310 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5311 requested_capacity ) );
5312 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005313 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005314 switch( steps[i] )
5315 {
5316 case 0:
5317 break;
5318 case PSA_KEY_DERIVATION_INPUT_SECRET:
5319 PSA_ASSERT( psa_import_key( &attributes,
5320 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005321 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005322
5323 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5324 {
5325 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5326 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5327 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5328 }
5329
Gilles Peskine1468da72019-05-29 17:35:49 +02005330 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005331 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005332 break;
5333 default:
5334 PSA_ASSERT( psa_key_derivation_input_bytes(
5335 &operation, steps[i],
5336 inputs[i]->x, inputs[i]->len ) );
5337 break;
5338 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005339 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005340
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005342 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005343 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005344 expected_capacity = requested_capacity;
5345
5346 /* Expansion phase. */
5347 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5348 {
5349 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005351 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005352 if( expected_capacity == 0 && output_sizes[i] == 0 )
5353 {
5354 /* Reading 0 bytes when 0 bytes are available can go either way. */
5355 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005356 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005357 continue;
5358 }
5359 else if( expected_capacity == 0 ||
5360 output_sizes[i] > expected_capacity )
5361 {
5362 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005363 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005364 expected_capacity = 0;
5365 continue;
5366 }
5367 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005368 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005369 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005370 ASSERT_COMPARE( output_buffer, output_sizes[i],
5371 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005372 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005373 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005375 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005376 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005377 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005379
5380exit:
5381 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005382 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005383 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5384 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005385 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005386}
5387/* END_CASE */
5388
5389/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005390void derive_full( int alg_arg,
5391 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005392 data_t *input1,
5393 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005394 int requested_capacity_arg )
5395{
Ronald Cron5425a212020-08-04 14:58:35 +02005396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005397 psa_algorithm_t alg = alg_arg;
5398 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005400 unsigned char output_buffer[16];
5401 size_t expected_capacity = requested_capacity;
5402 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005404
Gilles Peskine8817f612018-12-18 00:18:46 +01005405 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005406
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005407 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5408 psa_set_key_algorithm( &attributes, alg );
5409 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005410
Gilles Peskine049c7532019-05-15 20:22:09 +02005411 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005412 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005413
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005414 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5415 input1->x, input1->len,
5416 input2->x, input2->len,
5417 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005418 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005421 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005422 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005423
5424 /* Expansion phase. */
5425 while( current_capacity > 0 )
5426 {
5427 size_t read_size = sizeof( output_buffer );
5428 if( read_size > current_capacity )
5429 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005431 output_buffer,
5432 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005433 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005434 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005435 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005436 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005437 }
5438
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 /* Check that the operation refuses to go over capacity. */
5440 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005441 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005442
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005444
5445exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005446 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005447 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005448 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005449}
5450/* END_CASE */
5451
Janos Follathe60c9052019-07-03 13:51:30 +01005452/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005453void derive_key_exercise( int alg_arg,
5454 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005455 data_t *input1,
5456 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005457 int derived_type_arg,
5458 int derived_bits_arg,
5459 int derived_usage_arg,
5460 int derived_alg_arg )
5461{
Ronald Cron5425a212020-08-04 14:58:35 +02005462 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5463 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005464 psa_algorithm_t alg = alg_arg;
5465 psa_key_type_t derived_type = derived_type_arg;
5466 size_t derived_bits = derived_bits_arg;
5467 psa_key_usage_t derived_usage = derived_usage_arg;
5468 psa_algorithm_t derived_alg = derived_alg_arg;
5469 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005472 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005473
Gilles Peskine8817f612018-12-18 00:18:46 +01005474 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005475
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005476 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5477 psa_set_key_algorithm( &attributes, alg );
5478 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005479 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005480 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005481
5482 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005483 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5484 input1->x, input1->len,
5485 input2->x, input2->len,
5486 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005487 goto exit;
5488
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005489 psa_set_key_usage_flags( &attributes, derived_usage );
5490 psa_set_key_algorithm( &attributes, derived_alg );
5491 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005492 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005493 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005494 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005495
5496 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005497 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005498 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5499 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005500
5501 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005502 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005503 goto exit;
5504
5505exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005506 /*
5507 * Key attributes may have been returned by psa_get_key_attributes()
5508 * thus reset them as required.
5509 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005510 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005511
5512 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005513 psa_destroy_key( base_key );
5514 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005515 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005516}
5517/* END_CASE */
5518
Janos Follath42fd8882019-07-03 14:17:09 +01005519/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005520void derive_key_export( int alg_arg,
5521 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005522 data_t *input1,
5523 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005524 int bytes1_arg,
5525 int bytes2_arg )
5526{
Ronald Cron5425a212020-08-04 14:58:35 +02005527 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5528 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005529 psa_algorithm_t alg = alg_arg;
5530 size_t bytes1 = bytes1_arg;
5531 size_t bytes2 = bytes2_arg;
5532 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005534 uint8_t *output_buffer = NULL;
5535 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005536 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5537 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005538 size_t length;
5539
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005540 ASSERT_ALLOC( output_buffer, capacity );
5541 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005542 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005543
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5545 psa_set_key_algorithm( &base_attributes, alg );
5546 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005547 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005548 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005549
5550 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005551 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5552 input1->x, input1->len,
5553 input2->x, input2->len,
5554 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005555 goto exit;
5556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005557 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005558 output_buffer,
5559 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005561
5562 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005563 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5564 input1->x, input1->len,
5565 input2->x, input2->len,
5566 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005567 goto exit;
5568
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005569 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5570 psa_set_key_algorithm( &derived_attributes, 0 );
5571 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005572 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005573 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005574 &derived_key ) );
5575 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005576 export_buffer, bytes1,
5577 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005578 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005579 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005580 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005581 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005582 &derived_key ) );
5583 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005584 export_buffer + bytes1, bytes2,
5585 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005586 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005587
5588 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005589 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5590 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005591
5592exit:
5593 mbedtls_free( output_buffer );
5594 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005595 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005596 psa_destroy_key( base_key );
5597 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005598 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005599}
5600/* END_CASE */
5601
5602/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005603void derive_key( int alg_arg,
5604 data_t *key_data, data_t *input1, data_t *input2,
5605 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005606 int expected_status_arg,
5607 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005608{
Ronald Cron5425a212020-08-04 14:58:35 +02005609 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5610 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005611 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005612 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005613 size_t bits = bits_arg;
5614 psa_status_t expected_status = expected_status_arg;
5615 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5616 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5617 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5618
5619 PSA_ASSERT( psa_crypto_init( ) );
5620
5621 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5622 psa_set_key_algorithm( &base_attributes, alg );
5623 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5624 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005625 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005626
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005627 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5628 input1->x, input1->len,
5629 input2->x, input2->len,
5630 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005631 goto exit;
5632
5633 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5634 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005635 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005636 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005637
5638 psa_status_t status =
5639 psa_key_derivation_output_key( &derived_attributes,
5640 &operation,
5641 &derived_key );
5642 if( is_large_output > 0 )
5643 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5644 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005645
5646exit:
5647 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005648 psa_destroy_key( base_key );
5649 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005650 PSA_DONE( );
5651}
5652/* END_CASE */
5653
5654/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005655void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005656 int our_key_type_arg, int our_key_alg_arg,
5657 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005658 int expected_status_arg )
5659{
Ronald Cron5425a212020-08-04 14:58:35 +02005660 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005661 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005662 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005663 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005664 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005666 psa_status_t expected_status = expected_status_arg;
5667 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005668
Gilles Peskine8817f612018-12-18 00:18:46 +01005669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005670
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005672 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005673 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005674 PSA_ASSERT( psa_import_key( &attributes,
5675 our_key_data->x, our_key_data->len,
5676 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005677
Gilles Peskine77f40d82019-04-11 21:27:06 +02005678 /* The tests currently include inputs that should fail at either step.
5679 * Test cases that fail at the setup step should be changed to call
5680 * key_derivation_setup instead, and this function should be renamed
5681 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005683 if( status == PSA_SUCCESS )
5684 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005685 TEST_EQUAL( psa_key_derivation_key_agreement(
5686 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5687 our_key,
5688 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005689 expected_status );
5690 }
5691 else
5692 {
5693 TEST_ASSERT( status == expected_status );
5694 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005695
5696exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005698 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005699 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005700}
5701/* END_CASE */
5702
5703/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005704void raw_key_agreement( int alg_arg,
5705 int our_key_type_arg, data_t *our_key_data,
5706 data_t *peer_key_data,
5707 data_t *expected_output )
5708{
Ronald Cron5425a212020-08-04 14:58:35 +02005709 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005710 psa_algorithm_t alg = alg_arg;
5711 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005713 unsigned char *output = NULL;
5714 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005715 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005716
5717 ASSERT_ALLOC( output, expected_output->len );
5718 PSA_ASSERT( psa_crypto_init( ) );
5719
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5721 psa_set_key_algorithm( &attributes, alg );
5722 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005723 PSA_ASSERT( psa_import_key( &attributes,
5724 our_key_data->x, our_key_data->len,
5725 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005726
gabor-mezei-armceface22021-01-21 12:26:17 +01005727 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5728 key_bits = psa_get_key_bits( &attributes );
5729
Gilles Peskinebe697d82019-05-16 18:00:41 +02005730 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5731 peer_key_data->x, peer_key_data->len,
5732 output, expected_output->len,
5733 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005734 ASSERT_COMPARE( output, output_length,
5735 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005736 TEST_ASSERT( output_length <=
5737 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5738 TEST_ASSERT( output_length <=
5739 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005740
5741exit:
5742 mbedtls_free( output );
5743 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005744 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005745}
5746/* END_CASE */
5747
5748/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005749void key_agreement_capacity( int alg_arg,
5750 int our_key_type_arg, data_t *our_key_data,
5751 data_t *peer_key_data,
5752 int expected_capacity_arg )
5753{
Ronald Cron5425a212020-08-04 14:58:35 +02005754 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005755 psa_algorithm_t alg = alg_arg;
5756 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005757 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005759 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005760 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005761
Gilles Peskine8817f612018-12-18 00:18:46 +01005762 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005763
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005764 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5765 psa_set_key_algorithm( &attributes, alg );
5766 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005767 PSA_ASSERT( psa_import_key( &attributes,
5768 our_key_data->x, our_key_data->len,
5769 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005770
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005771 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005772 PSA_ASSERT( psa_key_derivation_key_agreement(
5773 &operation,
5774 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5775 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005776 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5777 {
5778 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005779 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005780 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005781 NULL, 0 ) );
5782 }
Gilles Peskine59685592018-09-18 12:11:34 +02005783
Gilles Peskinebf491972018-10-25 22:36:12 +02005784 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005785 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005786 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005787 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005788
Gilles Peskinebf491972018-10-25 22:36:12 +02005789 /* Test the actual capacity by reading the output. */
5790 while( actual_capacity > sizeof( output ) )
5791 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005792 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005793 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005794 actual_capacity -= sizeof( output );
5795 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005796 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005797 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005798 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005799 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005800
Gilles Peskine59685592018-09-18 12:11:34 +02005801exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005803 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005804 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005805}
5806/* END_CASE */
5807
5808/* BEGIN_CASE */
5809void key_agreement_output( int alg_arg,
5810 int our_key_type_arg, data_t *our_key_data,
5811 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005812 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005813{
Ronald Cron5425a212020-08-04 14:58:35 +02005814 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005815 psa_algorithm_t alg = alg_arg;
5816 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005817 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005819 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005820
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005821 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5822 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005823
Gilles Peskine8817f612018-12-18 00:18:46 +01005824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005825
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5827 psa_set_key_algorithm( &attributes, alg );
5828 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005829 PSA_ASSERT( psa_import_key( &attributes,
5830 our_key_data->x, our_key_data->len,
5831 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005832
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005833 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005834 PSA_ASSERT( psa_key_derivation_key_agreement(
5835 &operation,
5836 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5837 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005838 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5839 {
5840 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005841 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005842 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005843 NULL, 0 ) );
5844 }
Gilles Peskine59685592018-09-18 12:11:34 +02005845
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005846 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005847 actual_output,
5848 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005849 ASSERT_COMPARE( actual_output, expected_output1->len,
5850 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005851 if( expected_output2->len != 0 )
5852 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005853 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005854 actual_output,
5855 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005856 ASSERT_COMPARE( actual_output, expected_output2->len,
5857 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005858 }
Gilles Peskine59685592018-09-18 12:11:34 +02005859
5860exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005861 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005862 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005863 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005864 mbedtls_free( actual_output );
5865}
5866/* END_CASE */
5867
5868/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005869void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005870{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005871 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005872 unsigned char *output = NULL;
5873 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005874 size_t i;
5875 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005876
Simon Butcher49f8e312020-03-03 15:51:50 +00005877 TEST_ASSERT( bytes_arg >= 0 );
5878
Gilles Peskine91892022021-02-08 19:50:26 +01005879 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005880 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005881
Gilles Peskine8817f612018-12-18 00:18:46 +01005882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005883
Gilles Peskinea50d7392018-06-21 10:22:13 +02005884 /* Run several times, to ensure that every output byte will be
5885 * nonzero at least once with overwhelming probability
5886 * (2^(-8*number_of_runs)). */
5887 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005888 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005889 if( bytes != 0 )
5890 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005891 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005892
Gilles Peskinea50d7392018-06-21 10:22:13 +02005893 for( i = 0; i < bytes; i++ )
5894 {
5895 if( output[i] != 0 )
5896 ++changed[i];
5897 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005898 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005899
5900 /* Check that every byte was changed to nonzero at least once. This
5901 * validates that psa_generate_random is overwriting every byte of
5902 * the output buffer. */
5903 for( i = 0; i < bytes; i++ )
5904 {
5905 TEST_ASSERT( changed[i] != 0 );
5906 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005907
5908exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005909 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005910 mbedtls_free( output );
5911 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005912}
5913/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005914
5915/* BEGIN_CASE */
5916void generate_key( int type_arg,
5917 int bits_arg,
5918 int usage_arg,
5919 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005920 int expected_status_arg,
5921 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005922{
Ronald Cron5425a212020-08-04 14:58:35 +02005923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005924 psa_key_type_t type = type_arg;
5925 psa_key_usage_t usage = usage_arg;
5926 size_t bits = bits_arg;
5927 psa_algorithm_t alg = alg_arg;
5928 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005930 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005931
Gilles Peskine8817f612018-12-18 00:18:46 +01005932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005933
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005934 psa_set_key_usage_flags( &attributes, usage );
5935 psa_set_key_algorithm( &attributes, alg );
5936 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005937 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005938
5939 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005940 psa_status_t status = psa_generate_key( &attributes, &key );
5941
5942 if( is_large_key > 0 )
5943 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5944 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005945 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005946 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005947
5948 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005949 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005950 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5951 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005952
Gilles Peskine818ca122018-06-20 18:16:48 +02005953 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005954 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005955 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005956
5957exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005958 /*
5959 * Key attributes may have been returned by psa_get_key_attributes()
5960 * thus reset them as required.
5961 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005962 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005963
Ronald Cron5425a212020-08-04 14:58:35 +02005964 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005965 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005966}
5967/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005968
Ronald Cronee414c72021-03-18 18:50:08 +01005969/* 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 +02005970void generate_key_rsa( int bits_arg,
5971 data_t *e_arg,
5972 int expected_status_arg )
5973{
Ronald Cron5425a212020-08-04 14:58:35 +02005974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005975 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005976 size_t bits = bits_arg;
5977 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5978 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5979 psa_status_t expected_status = expected_status_arg;
5980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5981 uint8_t *exported = NULL;
5982 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005983 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005984 size_t exported_length = SIZE_MAX;
5985 uint8_t *e_read_buffer = NULL;
5986 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005987 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005988 size_t e_read_length = SIZE_MAX;
5989
5990 if( e_arg->len == 0 ||
5991 ( e_arg->len == 3 &&
5992 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5993 {
5994 is_default_public_exponent = 1;
5995 e_read_size = 0;
5996 }
5997 ASSERT_ALLOC( e_read_buffer, e_read_size );
5998 ASSERT_ALLOC( exported, exported_size );
5999
6000 PSA_ASSERT( psa_crypto_init( ) );
6001
6002 psa_set_key_usage_flags( &attributes, usage );
6003 psa_set_key_algorithm( &attributes, alg );
6004 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6005 e_arg->x, e_arg->len ) );
6006 psa_set_key_bits( &attributes, bits );
6007
6008 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006009 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006010 if( expected_status != PSA_SUCCESS )
6011 goto exit;
6012
6013 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006014 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006015 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6016 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6017 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6018 e_read_buffer, e_read_size,
6019 &e_read_length ) );
6020 if( is_default_public_exponent )
6021 TEST_EQUAL( e_read_length, 0 );
6022 else
6023 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6024
6025 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006026 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006027 goto exit;
6028
6029 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006030 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006031 exported, exported_size,
6032 &exported_length ) );
6033 {
6034 uint8_t *p = exported;
6035 uint8_t *end = exported + exported_length;
6036 size_t len;
6037 /* RSAPublicKey ::= SEQUENCE {
6038 * modulus INTEGER, -- n
6039 * publicExponent INTEGER } -- e
6040 */
6041 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006042 MBEDTLS_ASN1_SEQUENCE |
6043 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006044 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006045 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6046 MBEDTLS_ASN1_INTEGER ) );
6047 if( len >= 1 && p[0] == 0 )
6048 {
6049 ++p;
6050 --len;
6051 }
6052 if( e_arg->len == 0 )
6053 {
6054 TEST_EQUAL( len, 3 );
6055 TEST_EQUAL( p[0], 1 );
6056 TEST_EQUAL( p[1], 0 );
6057 TEST_EQUAL( p[2], 1 );
6058 }
6059 else
6060 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6061 }
6062
6063exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006064 /*
6065 * Key attributes may have been returned by psa_get_key_attributes() or
6066 * set by psa_set_key_domain_parameters() thus reset them as required.
6067 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006068 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006069
Ronald Cron5425a212020-08-04 14:58:35 +02006070 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006071 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006072 mbedtls_free( e_read_buffer );
6073 mbedtls_free( exported );
6074}
6075/* END_CASE */
6076
Darryl Greend49a4992018-06-18 17:27:26 +01006077/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006078void persistent_key_load_key_from_storage( data_t *data,
6079 int type_arg, int bits_arg,
6080 int usage_flags_arg, int alg_arg,
6081 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006082{
Ronald Cron71016a92020-08-28 19:01:50 +02006083 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6086 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006087 psa_key_type_t type = type_arg;
6088 size_t bits = bits_arg;
6089 psa_key_usage_t usage_flags = usage_flags_arg;
6090 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006091 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006092 unsigned char *first_export = NULL;
6093 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006094 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006095 size_t first_exported_length;
6096 size_t second_exported_length;
6097
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006098 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6099 {
6100 ASSERT_ALLOC( first_export, export_size );
6101 ASSERT_ALLOC( second_export, export_size );
6102 }
Darryl Greend49a4992018-06-18 17:27:26 +01006103
Gilles Peskine8817f612018-12-18 00:18:46 +01006104 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006105
Gilles Peskinec87af662019-05-15 16:12:22 +02006106 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006107 psa_set_key_usage_flags( &attributes, usage_flags );
6108 psa_set_key_algorithm( &attributes, alg );
6109 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006110 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006111
Darryl Green0c6575a2018-11-07 16:05:30 +00006112 switch( generation_method )
6113 {
6114 case IMPORT_KEY:
6115 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006116 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006117 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006118 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006119
Darryl Green0c6575a2018-11-07 16:05:30 +00006120 case GENERATE_KEY:
6121 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006122 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006123 break;
6124
6125 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006126#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006127 {
6128 /* Create base key */
6129 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6130 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6131 psa_set_key_usage_flags( &base_attributes,
6132 PSA_KEY_USAGE_DERIVE );
6133 psa_set_key_algorithm( &base_attributes, derive_alg );
6134 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006135 PSA_ASSERT( psa_import_key( &base_attributes,
6136 data->x, data->len,
6137 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006138 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006139 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006140 PSA_ASSERT( psa_key_derivation_input_key(
6141 &operation,
6142 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006143 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006144 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006145 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006146 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6147 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006148 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006149 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006150 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006151 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006152 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006153#else
6154 TEST_ASSUME( ! "KDF not supported in this configuration" );
6155#endif
6156 break;
6157
6158 default:
6159 TEST_ASSERT( ! "generation_method not implemented in test" );
6160 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006161 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006162 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006163
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006164 /* Export the key if permitted by the key policy. */
6165 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6166 {
Ronald Cron5425a212020-08-04 14:58:35 +02006167 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006168 first_export, export_size,
6169 &first_exported_length ) );
6170 if( generation_method == IMPORT_KEY )
6171 ASSERT_COMPARE( data->x, data->len,
6172 first_export, first_exported_length );
6173 }
Darryl Greend49a4992018-06-18 17:27:26 +01006174
6175 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006176 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006177 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006178 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006179
Darryl Greend49a4992018-06-18 17:27:26 +01006180 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006181 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006182 TEST_ASSERT( mbedtls_svc_key_id_equal(
6183 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006184 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6185 PSA_KEY_LIFETIME_PERSISTENT );
6186 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6187 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6188 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6189 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006190
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006191 /* Export the key again if permitted by the key policy. */
6192 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006193 {
Ronald Cron5425a212020-08-04 14:58:35 +02006194 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006195 second_export, export_size,
6196 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006197 ASSERT_COMPARE( first_export, first_exported_length,
6198 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006199 }
6200
6201 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006202 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006203 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006204
6205exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006206 /*
6207 * Key attributes may have been returned by psa_get_key_attributes()
6208 * thus reset them as required.
6209 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006210 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006211
Darryl Greend49a4992018-06-18 17:27:26 +01006212 mbedtls_free( first_export );
6213 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006214 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006215 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006216 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006217 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006218}
6219/* END_CASE */