blob: 0e9917a432d20d824f52718ad0cc03e9503ea40a [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 Elliott97fd1ba2021-07-21 18:46:06 +0100267/*!
268 * \brief Internal Function for AEAD multipart tests.
269 *
270 * \param key_type_arg Type of key passed in
271 * \param key_data The encryption / decryption key data
272 * \param alg_arg The type of algorithm used
273 * \param nonce Nonce data
274 * \param additional_data Additional data
275 * \param ad_part_len If not -1, the length of chunks to
276 * feed additional data in to be encrypted /
277 * decrypted. If -1, no chunking.
278 * \param input_data Data to encrypt / decrypt
279 * \param data_part_len If not -1, the length of chunks to feed the
280 * data in to be encrypted / decrypted. If -1,
281 * no chunking
282 * \param do_set_lengths If non-zero, then set lengths prior to
283 * calling encryption / decryption.
284 * \param expected_output Expected output
285 * \param expected_status_arg Expected status
286 * \param is_encrypt If non-zero this is an encryption operation.
287 *
288 * \return int Zero on failure, non-zero on success.
289 *
290 */
291static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
292 int alg_arg,
293 data_t *nonce,
294 data_t *additional_data,
295 int ad_part_len,
296 data_t *input_data,
297 int data_part_len,
298 int do_set_lengths,
299 data_t *expected_output,
300 int expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +0100301 int is_encrypt,
Paul Elliottebf91632021-07-22 17:54:42 +0100302 int do_zero_parts,
303 int swap_set_functions )
Paul Elliottd3f82412021-06-16 16:52:21 +0100304{
305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
306 psa_key_type_t key_type = key_type_arg;
307 psa_algorithm_t alg = alg_arg;
308 psa_aead_operation_t operation;
309 unsigned char *output_data = NULL;
310 unsigned char *part_data = NULL;
311 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100313 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 size_t output_size = 0;
315 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100316 size_t output_length = 0;
317 size_t key_bits = 0;
318 size_t tag_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100319 uint32_t part_offset = 0;
320 size_t part_length = 0;
321 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100322 size_t tag_size = 0;
323 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
325 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
326
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 int test_ok = 0;
Paul Elliott329d5382021-07-22 17:10:45 +0100328 uint32_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100329
Paul Elliottd3f82412021-06-16 16:52:21 +0100330 PSA_ASSERT( psa_crypto_init( ) );
331
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100332 if( is_encrypt )
333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
334 else
335 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
336
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_set_key_algorithm( &attributes, alg );
338 psa_set_key_type( &attributes, key_type );
339
340 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
341 &key ) );
342
343 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
344 key_bits = psa_get_key_bits( &attributes );
345
346 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
347
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 if( is_encrypt )
349 {
350 /* Tag gets written at end of buffer. */
351 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
352 ( input_data->len +
353 tag_length ) );
354 data_true_size = input_data->len;
355 }
356 else
357 {
358 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
359 ( input_data->len -
360 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100361
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100362 /* Do not want to attempt to decrypt tag. */
363 data_true_size = input_data->len - tag_length;
364 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100365
366 ASSERT_ALLOC( output_data, output_size );
367
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100368 if( is_encrypt )
369 {
370 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
371 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
372 }
373 else
374 {
375 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
376 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100380
381 operation = psa_aead_operation_init( );
382
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383
384 if( is_encrypt )
385 status = psa_aead_encrypt_setup( &operation, key, alg );
386 else
387 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100388
389 /* If the operation is not supported, just skip and not fail in case the
390 * encryption involves a common limitation of cryptography hardwares and
391 * an alternative implementation. */
392 if( status == PSA_ERROR_NOT_SUPPORTED )
393 {
394 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
395 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
396 }
397
398 PSA_ASSERT( status );
399
Paul Elliottebf91632021-07-22 17:54:42 +0100400 if( swap_set_functions )
Paul Elliottd3f82412021-06-16 16:52:21 +0100401 {
Paul Elliottebf91632021-07-22 17:54:42 +0100402 if( do_set_lengths )
403 {
404 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
405 data_true_size ) );
406 }
407
408 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
409 }
410 else
411 {
412 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
413
414 if( do_set_lengths )
415 {
416 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
417 data_true_size ) );
418 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100419 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100420
421 if( ad_part_len != -1 )
422 {
423 /* Pass additional data in parts */
424 part_offset = 0;
425
426 while( part_offset < additional_data->len )
427 {
Paul Elliott329d5382021-07-22 17:10:45 +0100428 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100429 {
Paul Elliott329d5382021-07-22 17:10:45 +0100430 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100431 }
432 else
433 {
Paul Elliott329d5382021-07-22 17:10:45 +0100434 if( additional_data->len - part_offset <
435 ( uint32_t ) ad_part_len )
436 {
437 part_length = additional_data->len - part_offset;
438 }
439 else
440 {
441 part_length = ad_part_len;
442 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100443 }
444
445 PSA_ASSERT( psa_aead_update_ad( &operation,
446 additional_data->x + part_offset,
447 part_length ) );
448
449 part_offset += part_length;
450 }
451 }
452 else
453 {
454 /* Pass additional data in one go. */
455 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
456 additional_data->len ) );
457 }
458
459 if( data_part_len != -1 )
460 {
461 /* Pass data in parts */
462 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100463 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100464
465 ASSERT_ALLOC( part_data, part_data_size );
466
467 part_offset = 0;
468
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100469 while( part_offset < data_true_size )
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 {
Paul Elliott329d5382021-07-22 17:10:45 +0100471 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100472 {
Paul Elliott329d5382021-07-22 17:10:45 +0100473 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100474 }
475 else
476 {
Paul Elliott329d5382021-07-22 17:10:45 +0100477 if( ( data_true_size - part_offset ) < ( uint32_t ) data_part_len )
478 {
479 part_length = ( data_true_size - part_offset );
480 }
481 else
482 {
483 part_length = data_part_len;
484 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
486
487 PSA_ASSERT( psa_aead_update( &operation,
488 ( input_data->x + part_offset ),
489 part_length, part_data,
490 part_data_size,
491 &output_part_length ) );
492
493 if( output_data && output_part_length )
494 {
495 memcpy( ( output_data + part_offset ), part_data,
496 output_part_length );
497 }
498
499 part_offset += part_length;
500 output_length += output_part_length;
501 }
502 }
503 else
504 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100505 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100506 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 output_size, &output_length ) );
509 }
510
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100511 if( is_encrypt )
512 PSA_ASSERT( psa_aead_finish( &operation, final_data,
513 final_output_size,
514 &output_part_length,
515 tag_buffer, tag_length,
516 &tag_size ) );
517 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 status = psa_aead_verify( &operation, final_data,
520 final_output_size,
521 &output_part_length,
522 ( input_data->x + data_true_size ),
523 tag_length );
524
525 if( status != PSA_SUCCESS )
526 {
527 if( !expect_valid_signature )
528 {
529 /* Expected failure. */
530 test_ok = 1;
531 goto exit;
532 }
533 else
534 PSA_ASSERT( status );
535 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100536 }
537
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100538 if( output_data && output_part_length )
539 memcpy( ( output_data + output_length ), final_data,
540 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100541
542 output_length += output_part_length;
543
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100544
545 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
546 * should be exact.*/
547 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100548 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100549 TEST_EQUAL( tag_length, tag_size );
550
551 if( output_data && tag_length )
552 memcpy( ( output_data + output_length ), tag_buffer,
553 tag_length );
554
555 output_length += tag_length;
556
557 TEST_EQUAL( output_length,
558 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
559 input_data->len ) );
560 TEST_ASSERT( output_length <=
561 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
562 }
563 else
564 {
565 TEST_EQUAL( output_length,
566 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
567 input_data->len ) );
568 TEST_ASSERT( output_length <=
569 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100570 }
571
Paul Elliottd3f82412021-06-16 16:52:21 +0100572
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100573 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100574 output_data, output_length );
575
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100577 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
579exit:
580 psa_destroy_key( key );
581 psa_aead_abort( &operation );
582 mbedtls_free( output_data );
583 mbedtls_free( part_data );
584 mbedtls_free( final_data );
585 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100586
587 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100588}
589
Gilles Peskinee59236f2018-01-27 23:32:46 +0100590/* END_HEADER */
591
592/* BEGIN_DEPENDENCIES
593 * depends_on:MBEDTLS_PSA_CRYPTO_C
594 * END_DEPENDENCIES
595 */
596
597/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200598void static_checks( )
599{
600 size_t max_truncated_mac_size =
601 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
602
603 /* Check that the length for a truncated MAC always fits in the algorithm
604 * encoding. The shifted mask is the maximum truncated value. The
605 * untruncated algorithm may be one byte larger. */
606 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
607}
608/* END_CASE */
609
610/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200611void import_with_policy( int type_arg,
612 int usage_arg, int alg_arg,
613 int expected_status_arg )
614{
615 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
616 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200618 psa_key_type_t type = type_arg;
619 psa_key_usage_t usage = usage_arg;
620 psa_algorithm_t alg = alg_arg;
621 psa_status_t expected_status = expected_status_arg;
622 const uint8_t key_material[16] = {0};
623 psa_status_t status;
624
625 PSA_ASSERT( psa_crypto_init( ) );
626
627 psa_set_key_type( &attributes, type );
628 psa_set_key_usage_flags( &attributes, usage );
629 psa_set_key_algorithm( &attributes, alg );
630
631 status = psa_import_key( &attributes,
632 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200633 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200634 TEST_EQUAL( status, expected_status );
635 if( status != PSA_SUCCESS )
636 goto exit;
637
Ronald Cron5425a212020-08-04 14:58:35 +0200638 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200639 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
640 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
641 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200642 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200643
Ronald Cron5425a212020-08-04 14:58:35 +0200644 PSA_ASSERT( psa_destroy_key( key ) );
645 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200646
647exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100648 /*
649 * Key attributes may have been returned by psa_get_key_attributes()
650 * thus reset them as required.
651 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200652 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100653
654 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655 PSA_DONE( );
656}
657/* END_CASE */
658
659/* BEGIN_CASE */
660void import_with_data( data_t *data, int type_arg,
661 int attr_bits_arg,
662 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200663{
664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
665 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200666 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200667 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200668 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200669 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671
Gilles Peskine8817f612018-12-18 00:18:46 +0100672 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Gilles Peskine4747d192019-04-17 15:05:45 +0200674 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200675 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200676
Ronald Cron5425a212020-08-04 14:58:35 +0200677 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100678 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200679 if( status != PSA_SUCCESS )
680 goto exit;
681
Ronald Cron5425a212020-08-04 14:58:35 +0200682 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200683 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200684 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200685 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200686 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200687
Ronald Cron5425a212020-08-04 14:58:35 +0200688 PSA_ASSERT( psa_destroy_key( key ) );
689 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690
691exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100692 /*
693 * Key attributes may have been returned by psa_get_key_attributes()
694 * thus reset them as required.
695 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200696 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100697
698 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200699 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700}
701/* END_CASE */
702
703/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200704void import_large_key( int type_arg, int byte_size_arg,
705 int expected_status_arg )
706{
707 psa_key_type_t type = type_arg;
708 size_t byte_size = byte_size_arg;
709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
710 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200712 psa_status_t status;
713 uint8_t *buffer = NULL;
714 size_t buffer_size = byte_size + 1;
715 size_t n;
716
Steven Cooreman69967ce2021-01-18 18:01:08 +0100717 /* Skip the test case if the target running the test cannot
718 * accomodate large keys due to heap size constraints */
719 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200720 memset( buffer, 'K', byte_size );
721
722 PSA_ASSERT( psa_crypto_init( ) );
723
724 /* Try importing the key */
725 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
726 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200727 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100728 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200729 TEST_EQUAL( status, expected_status );
730
731 if( status == PSA_SUCCESS )
732 {
Ronald Cron5425a212020-08-04 14:58:35 +0200733 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200734 TEST_EQUAL( psa_get_key_type( &attributes ), type );
735 TEST_EQUAL( psa_get_key_bits( &attributes ),
736 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200737 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200738 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200739 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200740 for( n = 0; n < byte_size; n++ )
741 TEST_EQUAL( buffer[n], 'K' );
742 for( n = byte_size; n < buffer_size; n++ )
743 TEST_EQUAL( buffer[n], 0 );
744 }
745
746exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100747 /*
748 * Key attributes may have been returned by psa_get_key_attributes()
749 * thus reset them as required.
750 */
751 psa_reset_key_attributes( &attributes );
752
Ronald Cron5425a212020-08-04 14:58:35 +0200753 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 PSA_DONE( );
755 mbedtls_free( buffer );
756}
757/* END_CASE */
758
759/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200760void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
761{
Ronald Cron5425a212020-08-04 14:58:35 +0200762 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763 size_t bits = bits_arg;
764 psa_status_t expected_status = expected_status_arg;
765 psa_status_t status;
766 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200767 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768 size_t buffer_size = /* Slight overapproximations */
769 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200770 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 unsigned char *p;
772 int ret;
773 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200775
Gilles Peskine8817f612018-12-18 00:18:46 +0100776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200777 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778
779 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
780 bits, keypair ) ) >= 0 );
781 length = ret;
782
783 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200784 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200785 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100786 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200787
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200788 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200789 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200790
791exit:
792 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200793 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200794}
795/* END_CASE */
796
797/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300798void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300799 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200800 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 int expected_bits,
802 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200803 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100804 int canonical_input )
805{
Ronald Cron5425a212020-08-04 14:58:35 +0200806 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100807 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200808 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200809 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 unsigned char *exported = NULL;
812 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100814 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200817 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818
Moran Pekercb088e72018-07-17 17:36:59 +0300819 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200820 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200822 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100823 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824
Gilles Peskine4747d192019-04-17 15:05:45 +0200825 psa_set_key_usage_flags( &attributes, usage_arg );
826 psa_set_key_algorithm( &attributes, alg );
827 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700828
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200830 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100831
832 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200833 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200834 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
835 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200836 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100840 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100841
842 /* The exported length must be set by psa_export_key() to a value between 0
843 * and export_size. On errors, the exported length must be 0. */
844 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
845 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
846 TEST_ASSERT( exported_length <= export_size );
847
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200848 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200849 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200851 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100852 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200854 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855
Gilles Peskineea38a922021-02-13 00:05:16 +0100856 /* Run sanity checks on the exported key. For non-canonical inputs,
857 * this validates the canonical representations. For canonical inputs,
858 * this doesn't directly validate the implementation, but it still helps
859 * by cross-validating the test data with the sanity check code. */
860 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200861 goto exit;
862
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200864 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865 else
866 {
Ronald Cron5425a212020-08-04 14:58:35 +0200867 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200868 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200869 &key2 ) );
870 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100871 reexported,
872 export_size,
873 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200874 ASSERT_COMPARE( exported, exported_length,
875 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200876 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100878 TEST_ASSERT( exported_length <=
879 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
880 psa_get_key_bits( &got_attributes ) ) );
881 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882
883destroy:
884 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key ) );
886 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887
888exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100889 /*
890 * Key attributes may have been returned by psa_get_key_attributes()
891 * thus reset them as required.
892 */
893 psa_reset_key_attributes( &got_attributes );
894
itayzafrir3e02b3b2018-06-12 17:06:52 +0300895 mbedtls_free( exported );
896 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200897 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898}
899/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100900
Moran Pekerf709f4a2018-06-06 17:26:04 +0300901/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300902void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200903 int type_arg,
904 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100905 int export_size_delta,
906 int expected_export_status_arg,
907 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300908{
Ronald Cron5425a212020-08-04 14:58:35 +0200909 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200911 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200912 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300913 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100916 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918
Gilles Peskine8817f612018-12-18 00:18:46 +0100919 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920
Gilles Peskine4747d192019-04-17 15:05:45 +0200921 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
922 psa_set_key_algorithm( &attributes, alg );
923 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300924
925 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200926 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300927
Gilles Peskine49c25912018-10-29 15:15:31 +0100928 /* Export the public key */
929 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200930 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200931 exported, export_size,
932 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100933 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100934 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100935 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200936 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100937 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200938 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200939 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100940 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100941 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100942 TEST_ASSERT( expected_public_key->len <=
943 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
944 TEST_ASSERT( expected_public_key->len <=
945 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100946 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
947 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100948 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300949
950exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100951 /*
952 * Key attributes may have been returned by psa_get_key_attributes()
953 * thus reset them as required.
954 */
955 psa_reset_key_attributes( &attributes );
956
itayzafrir3e02b3b2018-06-12 17:06:52 +0300957 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200958 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200959 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300960}
961/* END_CASE */
962
Gilles Peskine20035e32018-02-03 22:44:14 +0100963/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200964void import_and_exercise_key( data_t *data,
965 int type_arg,
966 int bits_arg,
967 int alg_arg )
968{
Ronald Cron5425a212020-08-04 14:58:35 +0200969 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200970 psa_key_type_t type = type_arg;
971 size_t bits = bits_arg;
972 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100973 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200975 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200976
Gilles Peskine8817f612018-12-18 00:18:46 +0100977 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200978
Gilles Peskine4747d192019-04-17 15:05:45 +0200979 psa_set_key_usage_flags( &attributes, usage );
980 psa_set_key_algorithm( &attributes, alg );
981 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200982
983 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200984 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200985
986 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200987 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
989 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200990
991 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100992 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200993 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200994
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_destroy_key( key ) );
996 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200997
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100999 /*
1000 * Key attributes may have been returned by psa_get_key_attributes()
1001 * thus reset them as required.
1002 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001003 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001004
1005 psa_reset_key_attributes( &attributes );
1006 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001007 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001008}
1009/* END_CASE */
1010
1011/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001012void effective_key_attributes( int type_arg, int expected_type_arg,
1013 int bits_arg, int expected_bits_arg,
1014 int usage_arg, int expected_usage_arg,
1015 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001016{
Ronald Cron5425a212020-08-04 14:58:35 +02001017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001018 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001019 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001020 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001021 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001022 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001023 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001024 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001025 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001027
Gilles Peskine8817f612018-12-18 00:18:46 +01001028 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001029
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001030 psa_set_key_usage_flags( &attributes, usage );
1031 psa_set_key_algorithm( &attributes, alg );
1032 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001033 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001034
Ronald Cron5425a212020-08-04 14:58:35 +02001035 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001036 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001037
Ronald Cron5425a212020-08-04 14:58:35 +02001038 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001039 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1040 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1041 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1042 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001043
1044exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001045 /*
1046 * Key attributes may have been returned by psa_get_key_attributes()
1047 * thus reset them as required.
1048 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001049 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001050
1051 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001052 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001053}
1054/* END_CASE */
1055
1056/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001057void check_key_policy( int type_arg, int bits_arg,
1058 int usage_arg, int alg_arg )
1059{
1060 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1061 usage_arg, usage_arg, alg_arg, alg_arg );
1062 goto exit;
1063}
1064/* END_CASE */
1065
1066/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001067void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001068{
1069 /* Test each valid way of initializing the object, except for `= {0}`, as
1070 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1071 * though it's OK by the C standard. We could test for this, but we'd need
1072 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001073 psa_key_attributes_t func = psa_key_attributes_init( );
1074 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1075 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001076
1077 memset( &zero, 0, sizeof( zero ) );
1078
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001079 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1080 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1081 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001082
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001083 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1084 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1085 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1086
1087 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1088 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1089 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1090
1091 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1092 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1093 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1094
1095 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1096 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1097 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001098}
1099/* END_CASE */
1100
1101/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102void mac_key_policy( int policy_usage,
1103 int policy_alg,
1104 int key_type,
1105 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001106 int exercise_alg,
1107 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001108{
Ronald Cron5425a212020-08-04 14:58:35 +02001109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001111 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001112 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001113 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001114 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001115
Gilles Peskine8817f612018-12-18 00:18:46 +01001116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001117
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001118 psa_set_key_usage_flags( &attributes, policy_usage );
1119 psa_set_key_algorithm( &attributes, policy_alg );
1120 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001121
Gilles Peskine049c7532019-05-15 20:22:09 +02001122 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001123 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001124
Ronald Cron5425a212020-08-04 14:58:35 +02001125 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001126 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001127 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001128 else
1129 TEST_EQUAL( status, expected_status );
1130
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001131 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001132
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001134 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001135 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001136 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001137 else
1138 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139
1140exit:
1141 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001142 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001143 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001144}
1145/* END_CASE */
1146
1147/* BEGIN_CASE */
1148void cipher_key_policy( int policy_usage,
1149 int policy_alg,
1150 int key_type,
1151 data_t *key_data,
1152 int exercise_alg )
1153{
Ronald Cron5425a212020-08-04 14:58:35 +02001154 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001155 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001156 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001157 psa_status_t status;
1158
Gilles Peskine8817f612018-12-18 00:18:46 +01001159 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001160
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001161 psa_set_key_usage_flags( &attributes, policy_usage );
1162 psa_set_key_algorithm( &attributes, policy_alg );
1163 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164
Gilles Peskine049c7532019-05-15 20:22:09 +02001165 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001166 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167
Ronald Cron5425a212020-08-04 14:58:35 +02001168 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 if( policy_alg == exercise_alg &&
1170 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001171 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001173 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001174 psa_cipher_abort( &operation );
1175
Ronald Cron5425a212020-08-04 14:58:35 +02001176 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177 if( policy_alg == exercise_alg &&
1178 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001180 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001181 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182
1183exit:
1184 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001186 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001187}
1188/* END_CASE */
1189
1190/* BEGIN_CASE */
1191void aead_key_policy( int policy_usage,
1192 int policy_alg,
1193 int key_type,
1194 data_t *key_data,
1195 int nonce_length_arg,
1196 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001197 int exercise_alg,
1198 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001199{
Ronald Cron5425a212020-08-04 14:58:35 +02001200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001202 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001203 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204 unsigned char nonce[16] = {0};
1205 size_t nonce_length = nonce_length_arg;
1206 unsigned char tag[16];
1207 size_t tag_length = tag_length_arg;
1208 size_t output_length;
1209
1210 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1211 TEST_ASSERT( tag_length <= sizeof( tag ) );
1212
Gilles Peskine8817f612018-12-18 00:18:46 +01001213 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001215 psa_set_key_usage_flags( &attributes, policy_usage );
1216 psa_set_key_algorithm( &attributes, policy_alg );
1217 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218
Gilles Peskine049c7532019-05-15 20:22:09 +02001219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001220 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001221
Ronald Cron5425a212020-08-04 14:58:35 +02001222 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001223 nonce, nonce_length,
1224 NULL, 0,
1225 NULL, 0,
1226 tag, tag_length,
1227 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001228 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1229 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001231 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001232
1233 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001234 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001235 nonce, nonce_length,
1236 NULL, 0,
1237 tag, tag_length,
1238 NULL, 0,
1239 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001240 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1241 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1242 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001243 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001244 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001245 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001246
1247exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001248 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001249 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001250}
1251/* END_CASE */
1252
1253/* BEGIN_CASE */
1254void asymmetric_encryption_key_policy( int policy_usage,
1255 int policy_alg,
1256 int key_type,
1257 data_t *key_data,
1258 int exercise_alg )
1259{
Ronald Cron5425a212020-08-04 14:58:35 +02001260 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262 psa_status_t status;
1263 size_t key_bits;
1264 size_t buffer_length;
1265 unsigned char *buffer = NULL;
1266 size_t output_length;
1267
Gilles Peskine8817f612018-12-18 00:18:46 +01001268 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001269
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001270 psa_set_key_usage_flags( &attributes, policy_usage );
1271 psa_set_key_algorithm( &attributes, policy_alg );
1272 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273
Gilles Peskine049c7532019-05-15 20:22:09 +02001274 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001275 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001276
Ronald Cron5425a212020-08-04 14:58:35 +02001277 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001278 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001279 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1280 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001281 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001282
Ronald Cron5425a212020-08-04 14:58:35 +02001283 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001284 NULL, 0,
1285 NULL, 0,
1286 buffer, buffer_length,
1287 &output_length );
1288 if( policy_alg == exercise_alg &&
1289 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001290 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001291 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001292 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001294 if( buffer_length != 0 )
1295 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001296 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001297 buffer, buffer_length,
1298 NULL, 0,
1299 buffer, buffer_length,
1300 &output_length );
1301 if( policy_alg == exercise_alg &&
1302 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001303 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001304 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001305 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306
1307exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001308 /*
1309 * Key attributes may have been returned by psa_get_key_attributes()
1310 * thus reset them as required.
1311 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001312 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001313
1314 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001315 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001316 mbedtls_free( buffer );
1317}
1318/* END_CASE */
1319
1320/* BEGIN_CASE */
1321void asymmetric_signature_key_policy( int policy_usage,
1322 int policy_alg,
1323 int key_type,
1324 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001325 int exercise_alg,
1326 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327{
Ronald Cron5425a212020-08-04 14:58:35 +02001328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001331 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1332 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1333 * compatible with the policy and `payload_length_arg` is supposed to be
1334 * a valid input length to sign. If `payload_length_arg <= 0`,
1335 * `exercise_alg` is supposed to be forbidden by the policy. */
1336 int compatible_alg = payload_length_arg > 0;
1337 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001338 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339 size_t signature_length;
1340
Gilles Peskine8817f612018-12-18 00:18:46 +01001341 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001342
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001343 psa_set_key_usage_flags( &attributes, policy_usage );
1344 psa_set_key_algorithm( &attributes, policy_alg );
1345 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346
Gilles Peskine049c7532019-05-15 20:22:09 +02001347 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001348 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001349
Ronald Cron5425a212020-08-04 14:58:35 +02001350 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001351 payload, payload_length,
1352 signature, sizeof( signature ),
1353 &signature_length );
1354 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001355 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001356 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001357 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001358
1359 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001360 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001361 payload, payload_length,
1362 signature, sizeof( signature ) );
1363 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001364 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001365 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001366 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001367
1368exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001369 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001370 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001371}
1372/* END_CASE */
1373
Janos Follathba3fab92019-06-11 14:50:16 +01001374/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001375void derive_key_policy( int policy_usage,
1376 int policy_alg,
1377 int key_type,
1378 data_t *key_data,
1379 int exercise_alg )
1380{
Ronald Cron5425a212020-08-04 14:58:35 +02001381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001383 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001384 psa_status_t status;
1385
Gilles Peskine8817f612018-12-18 00:18:46 +01001386 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001387
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001388 psa_set_key_usage_flags( &attributes, policy_usage );
1389 psa_set_key_algorithm( &attributes, policy_alg );
1390 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001391
Gilles Peskine049c7532019-05-15 20:22:09 +02001392 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001393 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001394
Janos Follathba3fab92019-06-11 14:50:16 +01001395 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1396
1397 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1398 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001399 {
Janos Follathba3fab92019-06-11 14:50:16 +01001400 PSA_ASSERT( psa_key_derivation_input_bytes(
1401 &operation,
1402 PSA_KEY_DERIVATION_INPUT_SEED,
1403 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001404 }
Janos Follathba3fab92019-06-11 14:50:16 +01001405
1406 status = psa_key_derivation_input_key( &operation,
1407 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001408 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001409
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410 if( policy_alg == exercise_alg &&
1411 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001412 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001413 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001414 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001415
1416exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001417 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001418 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001419 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001420}
1421/* END_CASE */
1422
1423/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001424void agreement_key_policy( int policy_usage,
1425 int policy_alg,
1426 int key_type_arg,
1427 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001428 int exercise_alg,
1429 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001430{
Ronald Cron5425a212020-08-04 14:58:35 +02001431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001433 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001434 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001435 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001436 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001437
Gilles Peskine8817f612018-12-18 00:18:46 +01001438 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001440 psa_set_key_usage_flags( &attributes, policy_usage );
1441 psa_set_key_algorithm( &attributes, policy_alg );
1442 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443
Gilles Peskine049c7532019-05-15 20:22:09 +02001444 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001445 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001447 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001448 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001449
Steven Cooremance48e852020-10-05 16:02:45 +02001450 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451
1452exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001453 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001454 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001455 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001456}
1457/* END_CASE */
1458
1459/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001460void key_policy_alg2( int key_type_arg, data_t *key_data,
1461 int usage_arg, int alg_arg, int alg2_arg )
1462{
Ronald Cron5425a212020-08-04 14:58:35 +02001463 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001464 psa_key_type_t key_type = key_type_arg;
1465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1466 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1467 psa_key_usage_t usage = usage_arg;
1468 psa_algorithm_t alg = alg_arg;
1469 psa_algorithm_t alg2 = alg2_arg;
1470
1471 PSA_ASSERT( psa_crypto_init( ) );
1472
1473 psa_set_key_usage_flags( &attributes, usage );
1474 psa_set_key_algorithm( &attributes, alg );
1475 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1476 psa_set_key_type( &attributes, key_type );
1477 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001478 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001479
Ronald Cron5425a212020-08-04 14:58:35 +02001480 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001481 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1482 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1483 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1484
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001485 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001486 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001487 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001488 goto exit;
1489
1490exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001491 /*
1492 * Key attributes may have been returned by psa_get_key_attributes()
1493 * thus reset them as required.
1494 */
1495 psa_reset_key_attributes( &got_attributes );
1496
Ronald Cron5425a212020-08-04 14:58:35 +02001497 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001498 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001499}
1500/* END_CASE */
1501
1502/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001503void raw_agreement_key_policy( int policy_usage,
1504 int policy_alg,
1505 int key_type_arg,
1506 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001507 int exercise_alg,
1508 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001509{
Ronald Cron5425a212020-08-04 14:58:35 +02001510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001512 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001513 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001514 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001515 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001516
1517 PSA_ASSERT( psa_crypto_init( ) );
1518
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001519 psa_set_key_usage_flags( &attributes, policy_usage );
1520 psa_set_key_algorithm( &attributes, policy_alg );
1521 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001522
Gilles Peskine049c7532019-05-15 20:22:09 +02001523 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001524 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001525
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001526 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001527
Steven Cooremance48e852020-10-05 16:02:45 +02001528 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001529
1530exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001531 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001532 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001533 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001534}
1535/* END_CASE */
1536
1537/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001538void copy_success( int source_usage_arg,
1539 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001540 int type_arg, data_t *material,
1541 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001542 int target_usage_arg,
1543 int target_alg_arg, int target_alg2_arg,
1544 int expected_usage_arg,
1545 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001546{
Gilles Peskineca25db92019-04-19 11:43:08 +02001547 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1548 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001549 psa_key_usage_t expected_usage = expected_usage_arg;
1550 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001551 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001552 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1553 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001554 uint8_t *export_buffer = NULL;
1555
Gilles Peskine57ab7212019-01-28 13:03:09 +01001556 PSA_ASSERT( psa_crypto_init( ) );
1557
Gilles Peskineca25db92019-04-19 11:43:08 +02001558 /* Prepare the source key. */
1559 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1560 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001561 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001562 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001563 PSA_ASSERT( psa_import_key( &source_attributes,
1564 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001565 &source_key ) );
1566 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001567
Gilles Peskineca25db92019-04-19 11:43:08 +02001568 /* Prepare the target attributes. */
1569 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001570 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001571 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001572 /* Set volatile lifetime to reset the key identifier to 0. */
1573 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1574 }
1575
Gilles Peskineca25db92019-04-19 11:43:08 +02001576 if( target_usage_arg != -1 )
1577 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1578 if( target_alg_arg != -1 )
1579 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001580 if( target_alg2_arg != -1 )
1581 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001582
1583 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001584 PSA_ASSERT( psa_copy_key( source_key,
1585 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001586
1587 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001588 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001589
1590 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001591 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001592 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1593 psa_get_key_type( &target_attributes ) );
1594 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1595 psa_get_key_bits( &target_attributes ) );
1596 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1597 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001598 TEST_EQUAL( expected_alg2,
1599 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001600 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1601 {
1602 size_t length;
1603 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001604 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001605 material->len, &length ) );
1606 ASSERT_COMPARE( material->x, material->len,
1607 export_buffer, length );
1608 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001609
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001610 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001611 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001612 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001613 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001614
Ronald Cron5425a212020-08-04 14:58:35 +02001615 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001616
1617exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001618 /*
1619 * Source and target key attributes may have been returned by
1620 * psa_get_key_attributes() thus reset them as required.
1621 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001622 psa_reset_key_attributes( &source_attributes );
1623 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001624
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001625 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001626 mbedtls_free( export_buffer );
1627}
1628/* END_CASE */
1629
1630/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001631void copy_fail( int source_usage_arg,
1632 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001633 int type_arg, data_t *material,
1634 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001635 int target_usage_arg,
1636 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001637 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001638 int expected_status_arg )
1639{
1640 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1641 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001642 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1643 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001644 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001645
1646 PSA_ASSERT( psa_crypto_init( ) );
1647
1648 /* Prepare the source key. */
1649 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1650 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001651 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001652 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001653 PSA_ASSERT( psa_import_key( &source_attributes,
1654 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001655 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001656
1657 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001658 psa_set_key_id( &target_attributes, key_id );
1659 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660 psa_set_key_type( &target_attributes, target_type_arg );
1661 psa_set_key_bits( &target_attributes, target_bits_arg );
1662 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1663 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001664 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001665
1666 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001667 TEST_EQUAL( psa_copy_key( source_key,
1668 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001669 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001670
Ronald Cron5425a212020-08-04 14:58:35 +02001671 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001672
Gilles Peskine4a644642019-05-03 17:14:08 +02001673exit:
1674 psa_reset_key_attributes( &source_attributes );
1675 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001676 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001677}
1678/* END_CASE */
1679
1680/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001681void hash_operation_init( )
1682{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001683 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001684 /* Test each valid way of initializing the object, except for `= {0}`, as
1685 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1686 * though it's OK by the C standard. We could test for this, but we'd need
1687 * to supress the Clang warning for the test. */
1688 psa_hash_operation_t func = psa_hash_operation_init( );
1689 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1690 psa_hash_operation_t zero;
1691
1692 memset( &zero, 0, sizeof( zero ) );
1693
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001694 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001695 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1696 PSA_ERROR_BAD_STATE );
1697 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1698 PSA_ERROR_BAD_STATE );
1699 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1700 PSA_ERROR_BAD_STATE );
1701
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001702 /* A default hash operation should be abortable without error. */
1703 PSA_ASSERT( psa_hash_abort( &func ) );
1704 PSA_ASSERT( psa_hash_abort( &init ) );
1705 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001706}
1707/* END_CASE */
1708
1709/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001710void hash_setup( int alg_arg,
1711 int expected_status_arg )
1712{
1713 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001714 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001716 psa_status_t status;
1717
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001719
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001720 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001721 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001722
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001723 /* Whether setup succeeded or failed, abort must succeed. */
1724 PSA_ASSERT( psa_hash_abort( &operation ) );
1725
1726 /* If setup failed, reproduce the failure, so as to
1727 * test the resulting state of the operation object. */
1728 if( status != PSA_SUCCESS )
1729 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1730
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001731 /* Now the operation object should be reusable. */
1732#if defined(KNOWN_SUPPORTED_HASH_ALG)
1733 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1734 PSA_ASSERT( psa_hash_abort( &operation ) );
1735#endif
1736
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001737exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001738 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001739}
1740/* END_CASE */
1741
1742/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001743void hash_compute_fail( int alg_arg, data_t *input,
1744 int output_size_arg, int expected_status_arg )
1745{
1746 psa_algorithm_t alg = alg_arg;
1747 uint8_t *output = NULL;
1748 size_t output_size = output_size_arg;
1749 size_t output_length = INVALID_EXPORT_LENGTH;
1750 psa_status_t expected_status = expected_status_arg;
1751 psa_status_t status;
1752
1753 ASSERT_ALLOC( output, output_size );
1754
1755 PSA_ASSERT( psa_crypto_init( ) );
1756
1757 status = psa_hash_compute( alg, input->x, input->len,
1758 output, output_size, &output_length );
1759 TEST_EQUAL( status, expected_status );
1760 TEST_ASSERT( output_length <= output_size );
1761
1762exit:
1763 mbedtls_free( output );
1764 PSA_DONE( );
1765}
1766/* END_CASE */
1767
1768/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001769void hash_compare_fail( int alg_arg, data_t *input,
1770 data_t *reference_hash,
1771 int expected_status_arg )
1772{
1773 psa_algorithm_t alg = alg_arg;
1774 psa_status_t expected_status = expected_status_arg;
1775 psa_status_t status;
1776
1777 PSA_ASSERT( psa_crypto_init( ) );
1778
1779 status = psa_hash_compare( alg, input->x, input->len,
1780 reference_hash->x, reference_hash->len );
1781 TEST_EQUAL( status, expected_status );
1782
1783exit:
1784 PSA_DONE( );
1785}
1786/* END_CASE */
1787
1788/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001789void hash_compute_compare( int alg_arg, data_t *input,
1790 data_t *expected_output )
1791{
1792 psa_algorithm_t alg = alg_arg;
1793 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1794 size_t output_length = INVALID_EXPORT_LENGTH;
1795 size_t i;
1796
1797 PSA_ASSERT( psa_crypto_init( ) );
1798
1799 /* Compute with tight buffer */
1800 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001801 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001802 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001803 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001804 ASSERT_COMPARE( output, output_length,
1805 expected_output->x, expected_output->len );
1806
1807 /* Compute with larger buffer */
1808 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1809 output, sizeof( output ),
1810 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001811 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001812 ASSERT_COMPARE( output, output_length,
1813 expected_output->x, expected_output->len );
1814
1815 /* Compare with correct hash */
1816 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1817 output, output_length ) );
1818
1819 /* Compare with trailing garbage */
1820 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1821 output, output_length + 1 ),
1822 PSA_ERROR_INVALID_SIGNATURE );
1823
1824 /* Compare with truncated hash */
1825 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1826 output, output_length - 1 ),
1827 PSA_ERROR_INVALID_SIGNATURE );
1828
1829 /* Compare with corrupted value */
1830 for( i = 0; i < output_length; i++ )
1831 {
Chris Jones9634bb12021-01-20 15:56:42 +00001832 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001833 output[i] ^= 1;
1834 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1835 output, output_length ),
1836 PSA_ERROR_INVALID_SIGNATURE );
1837 output[i] ^= 1;
1838 }
1839
1840exit:
1841 PSA_DONE( );
1842}
1843/* END_CASE */
1844
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001845/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001846void hash_bad_order( )
1847{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001848 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001849 unsigned char input[] = "";
1850 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001851 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001852 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1853 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1854 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001855 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001856 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001857 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001858
Gilles Peskine8817f612018-12-18 00:18:46 +01001859 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001860
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001861 /* Call setup twice in a row. */
1862 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1863 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1864 PSA_ERROR_BAD_STATE );
1865 PSA_ASSERT( psa_hash_abort( &operation ) );
1866
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001867 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001868 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001869 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001870 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001871
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001872 /* Call update after finish. */
1873 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1874 PSA_ASSERT( psa_hash_finish( &operation,
1875 hash, sizeof( hash ), &hash_len ) );
1876 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001877 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001878 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001879
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001880 /* Call verify without calling setup beforehand. */
1881 TEST_EQUAL( psa_hash_verify( &operation,
1882 valid_hash, sizeof( valid_hash ) ),
1883 PSA_ERROR_BAD_STATE );
1884 PSA_ASSERT( psa_hash_abort( &operation ) );
1885
1886 /* Call verify after finish. */
1887 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1888 PSA_ASSERT( psa_hash_finish( &operation,
1889 hash, sizeof( hash ), &hash_len ) );
1890 TEST_EQUAL( psa_hash_verify( &operation,
1891 valid_hash, sizeof( valid_hash ) ),
1892 PSA_ERROR_BAD_STATE );
1893 PSA_ASSERT( psa_hash_abort( &operation ) );
1894
1895 /* Call verify twice in a row. */
1896 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1897 PSA_ASSERT( psa_hash_verify( &operation,
1898 valid_hash, sizeof( valid_hash ) ) );
1899 TEST_EQUAL( psa_hash_verify( &operation,
1900 valid_hash, sizeof( valid_hash ) ),
1901 PSA_ERROR_BAD_STATE );
1902 PSA_ASSERT( psa_hash_abort( &operation ) );
1903
1904 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001905 TEST_EQUAL( psa_hash_finish( &operation,
1906 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001907 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001908 PSA_ASSERT( psa_hash_abort( &operation ) );
1909
1910 /* Call finish twice in a row. */
1911 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1912 PSA_ASSERT( psa_hash_finish( &operation,
1913 hash, sizeof( hash ), &hash_len ) );
1914 TEST_EQUAL( psa_hash_finish( &operation,
1915 hash, sizeof( hash ), &hash_len ),
1916 PSA_ERROR_BAD_STATE );
1917 PSA_ASSERT( psa_hash_abort( &operation ) );
1918
1919 /* Call finish after calling verify. */
1920 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1921 PSA_ASSERT( psa_hash_verify( &operation,
1922 valid_hash, sizeof( valid_hash ) ) );
1923 TEST_EQUAL( psa_hash_finish( &operation,
1924 hash, sizeof( hash ), &hash_len ),
1925 PSA_ERROR_BAD_STATE );
1926 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001927
1928exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001929 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001930}
1931/* END_CASE */
1932
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001933/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001934void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001935{
1936 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001937 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1938 * appended to it */
1939 unsigned char hash[] = {
1940 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1941 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1942 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001943 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001944 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001945
Gilles Peskine8817f612018-12-18 00:18:46 +01001946 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001947
itayzafrir27e69452018-11-01 14:26:34 +02001948 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001950 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001951 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001952
itayzafrir27e69452018-11-01 14:26:34 +02001953 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001955 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001957
itayzafrir27e69452018-11-01 14:26:34 +02001958 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001959 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001960 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001961 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001962
itayzafrirec93d302018-10-18 18:01:10 +03001963exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001964 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001965}
1966/* END_CASE */
1967
Ronald Cronee414c72021-03-18 18:50:08 +01001968/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001969void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001970{
1971 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001972 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001973 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001974 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001975 size_t hash_len;
1976
Gilles Peskine8817f612018-12-18 00:18:46 +01001977 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001978
itayzafrir58028322018-10-25 10:22:01 +03001979 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001980 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001981 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001982 hash, expected_size - 1, &hash_len ),
1983 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001984
1985exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001986 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001987}
1988/* END_CASE */
1989
Ronald Cronee414c72021-03-18 18:50:08 +01001990/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001991void hash_clone_source_state( )
1992{
1993 psa_algorithm_t alg = PSA_ALG_SHA_256;
1994 unsigned char hash[PSA_HASH_MAX_SIZE];
1995 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1996 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1997 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1998 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1999 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2000 size_t hash_len;
2001
2002 PSA_ASSERT( psa_crypto_init( ) );
2003 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2004
2005 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2006 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2007 PSA_ASSERT( psa_hash_finish( &op_finished,
2008 hash, sizeof( hash ), &hash_len ) );
2009 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2010 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2011
2012 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2013 PSA_ERROR_BAD_STATE );
2014
2015 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2016 PSA_ASSERT( psa_hash_finish( &op_init,
2017 hash, sizeof( hash ), &hash_len ) );
2018 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2019 PSA_ASSERT( psa_hash_finish( &op_finished,
2020 hash, sizeof( hash ), &hash_len ) );
2021 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2022 PSA_ASSERT( psa_hash_finish( &op_aborted,
2023 hash, sizeof( hash ), &hash_len ) );
2024
2025exit:
2026 psa_hash_abort( &op_source );
2027 psa_hash_abort( &op_init );
2028 psa_hash_abort( &op_setup );
2029 psa_hash_abort( &op_finished );
2030 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002031 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002032}
2033/* END_CASE */
2034
Ronald Cronee414c72021-03-18 18:50:08 +01002035/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002036void hash_clone_target_state( )
2037{
2038 psa_algorithm_t alg = PSA_ALG_SHA_256;
2039 unsigned char hash[PSA_HASH_MAX_SIZE];
2040 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2041 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2042 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2043 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2044 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2045 size_t hash_len;
2046
2047 PSA_ASSERT( psa_crypto_init( ) );
2048
2049 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2050 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2051 PSA_ASSERT( psa_hash_finish( &op_finished,
2052 hash, sizeof( hash ), &hash_len ) );
2053 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2054 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2055
2056 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2057 PSA_ASSERT( psa_hash_finish( &op_target,
2058 hash, sizeof( hash ), &hash_len ) );
2059
2060 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2061 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2062 PSA_ERROR_BAD_STATE );
2063 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2064 PSA_ERROR_BAD_STATE );
2065
2066exit:
2067 psa_hash_abort( &op_target );
2068 psa_hash_abort( &op_init );
2069 psa_hash_abort( &op_setup );
2070 psa_hash_abort( &op_finished );
2071 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002072 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002073}
2074/* END_CASE */
2075
itayzafrir58028322018-10-25 10:22:01 +03002076/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002077void mac_operation_init( )
2078{
Jaeden Amero252ef282019-02-15 14:05:35 +00002079 const uint8_t input[1] = { 0 };
2080
Jaeden Amero769ce272019-01-04 11:48:03 +00002081 /* Test each valid way of initializing the object, except for `= {0}`, as
2082 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2083 * though it's OK by the C standard. We could test for this, but we'd need
2084 * to supress the Clang warning for the test. */
2085 psa_mac_operation_t func = psa_mac_operation_init( );
2086 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2087 psa_mac_operation_t zero;
2088
2089 memset( &zero, 0, sizeof( zero ) );
2090
Jaeden Amero252ef282019-02-15 14:05:35 +00002091 /* A freshly-initialized MAC operation should not be usable. */
2092 TEST_EQUAL( psa_mac_update( &func,
2093 input, sizeof( input ) ),
2094 PSA_ERROR_BAD_STATE );
2095 TEST_EQUAL( psa_mac_update( &init,
2096 input, sizeof( input ) ),
2097 PSA_ERROR_BAD_STATE );
2098 TEST_EQUAL( psa_mac_update( &zero,
2099 input, sizeof( input ) ),
2100 PSA_ERROR_BAD_STATE );
2101
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002102 /* A default MAC operation should be abortable without error. */
2103 PSA_ASSERT( psa_mac_abort( &func ) );
2104 PSA_ASSERT( psa_mac_abort( &init ) );
2105 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002106}
2107/* END_CASE */
2108
2109/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110void mac_setup( int key_type_arg,
2111 data_t *key,
2112 int alg_arg,
2113 int expected_status_arg )
2114{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115 psa_key_type_t key_type = key_type_arg;
2116 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002117 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002118 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002119 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2120#if defined(KNOWN_SUPPORTED_MAC_ALG)
2121 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2122#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002123
Gilles Peskine8817f612018-12-18 00:18:46 +01002124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002125
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002126 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2127 &operation, &status ) )
2128 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002129 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002130
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002131 /* The operation object should be reusable. */
2132#if defined(KNOWN_SUPPORTED_MAC_ALG)
2133 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2134 smoke_test_key_data,
2135 sizeof( smoke_test_key_data ),
2136 KNOWN_SUPPORTED_MAC_ALG,
2137 &operation, &status ) )
2138 goto exit;
2139 TEST_EQUAL( status, PSA_SUCCESS );
2140#endif
2141
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002142exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002143 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002144}
2145/* END_CASE */
2146
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002147/* 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 +00002148void mac_bad_order( )
2149{
Ronald Cron5425a212020-08-04 14:58:35 +02002150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002151 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2152 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002153 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002154 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2155 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2156 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002158 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2159 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2160 size_t sign_mac_length = 0;
2161 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2162 const uint8_t verify_mac[] = {
2163 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2164 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2165 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2166
2167 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002168 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002169 psa_set_key_algorithm( &attributes, alg );
2170 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002171
Ronald Cron5425a212020-08-04 14:58:35 +02002172 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2173 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002174
Jaeden Amero252ef282019-02-15 14:05:35 +00002175 /* Call update without calling setup beforehand. */
2176 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2177 PSA_ERROR_BAD_STATE );
2178 PSA_ASSERT( psa_mac_abort( &operation ) );
2179
2180 /* Call sign finish without calling setup beforehand. */
2181 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2182 &sign_mac_length),
2183 PSA_ERROR_BAD_STATE );
2184 PSA_ASSERT( psa_mac_abort( &operation ) );
2185
2186 /* Call verify finish without calling setup beforehand. */
2187 TEST_EQUAL( psa_mac_verify_finish( &operation,
2188 verify_mac, sizeof( verify_mac ) ),
2189 PSA_ERROR_BAD_STATE );
2190 PSA_ASSERT( psa_mac_abort( &operation ) );
2191
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002192 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002193 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2194 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002195 PSA_ERROR_BAD_STATE );
2196 PSA_ASSERT( psa_mac_abort( &operation ) );
2197
Jaeden Amero252ef282019-02-15 14:05:35 +00002198 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002199 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002200 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2201 PSA_ASSERT( psa_mac_sign_finish( &operation,
2202 sign_mac, sizeof( sign_mac ),
2203 &sign_mac_length ) );
2204 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2205 PSA_ERROR_BAD_STATE );
2206 PSA_ASSERT( psa_mac_abort( &operation ) );
2207
2208 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002209 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002210 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2211 PSA_ASSERT( psa_mac_verify_finish( &operation,
2212 verify_mac, sizeof( verify_mac ) ) );
2213 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2214 PSA_ERROR_BAD_STATE );
2215 PSA_ASSERT( psa_mac_abort( &operation ) );
2216
2217 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002218 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002219 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2220 PSA_ASSERT( psa_mac_sign_finish( &operation,
2221 sign_mac, sizeof( sign_mac ),
2222 &sign_mac_length ) );
2223 TEST_EQUAL( psa_mac_sign_finish( &operation,
2224 sign_mac, sizeof( sign_mac ),
2225 &sign_mac_length ),
2226 PSA_ERROR_BAD_STATE );
2227 PSA_ASSERT( psa_mac_abort( &operation ) );
2228
2229 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002230 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002231 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2232 PSA_ASSERT( psa_mac_verify_finish( &operation,
2233 verify_mac, sizeof( verify_mac ) ) );
2234 TEST_EQUAL( psa_mac_verify_finish( &operation,
2235 verify_mac, sizeof( verify_mac ) ),
2236 PSA_ERROR_BAD_STATE );
2237 PSA_ASSERT( psa_mac_abort( &operation ) );
2238
2239 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002240 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002241 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2242 TEST_EQUAL( psa_mac_verify_finish( &operation,
2243 verify_mac, sizeof( verify_mac ) ),
2244 PSA_ERROR_BAD_STATE );
2245 PSA_ASSERT( psa_mac_abort( &operation ) );
2246
2247 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002248 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002249 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2250 TEST_EQUAL( psa_mac_sign_finish( &operation,
2251 sign_mac, sizeof( sign_mac ),
2252 &sign_mac_length ),
2253 PSA_ERROR_BAD_STATE );
2254 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002255
Ronald Cron5425a212020-08-04 14:58:35 +02002256 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002257
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
2263/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002264void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002265 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266 int alg_arg,
2267 data_t *input,
2268 data_t *expected_mac )
2269{
Ronald Cron5425a212020-08-04 14:58:35 +02002270 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002271 psa_key_type_t key_type = key_type_arg;
2272 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002273 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002275 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002276 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002277 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002278 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002279 const size_t output_sizes_to_test[] = {
2280 0,
2281 1,
2282 expected_mac->len - 1,
2283 expected_mac->len,
2284 expected_mac->len + 1,
2285 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002287 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002288 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002289 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002290
Gilles Peskine8817f612018-12-18 00:18:46 +01002291 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002292
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002293 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002294 psa_set_key_algorithm( &attributes, alg );
2295 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002296
Ronald Cron5425a212020-08-04 14:58:35 +02002297 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2298 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002299
Gilles Peskine8b356b52020-08-25 23:44:59 +02002300 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2301 {
2302 const size_t output_size = output_sizes_to_test[i];
2303 psa_status_t expected_status =
2304 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2305 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002306
Chris Jones9634bb12021-01-20 15:56:42 +00002307 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002308 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002309
Gilles Peskine8b356b52020-08-25 23:44:59 +02002310 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002311 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002312 PSA_ASSERT( psa_mac_update( &operation,
2313 input->x, input->len ) );
2314 TEST_EQUAL( psa_mac_sign_finish( &operation,
2315 actual_mac, output_size,
2316 &mac_length ),
2317 expected_status );
2318 PSA_ASSERT( psa_mac_abort( &operation ) );
2319
2320 if( expected_status == PSA_SUCCESS )
2321 {
2322 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2323 actual_mac, mac_length );
2324 }
2325 mbedtls_free( actual_mac );
2326 actual_mac = NULL;
2327 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002328
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002329exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002330 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002331 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002332 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002333 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002334}
2335/* END_CASE */
2336
2337/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002338void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002339 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002340 int alg_arg,
2341 data_t *input,
2342 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002343{
Ronald Cron5425a212020-08-04 14:58:35 +02002344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002345 psa_key_type_t key_type = key_type_arg;
2346 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002347 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002349 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002350
Gilles Peskine69c12672018-06-28 00:07:19 +02002351 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2352
Gilles Peskine8817f612018-12-18 00:18:46 +01002353 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002355 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002356 psa_set_key_algorithm( &attributes, alg );
2357 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002358
Ronald Cron5425a212020-08-04 14:58:35 +02002359 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2360 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002361
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002362 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002363 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002364 PSA_ASSERT( psa_mac_update( &operation,
2365 input->x, input->len ) );
2366 PSA_ASSERT( psa_mac_verify_finish( &operation,
2367 expected_mac->x,
2368 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002369
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002370 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002371 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002372 PSA_ASSERT( psa_mac_update( &operation,
2373 input->x, input->len ) );
2374 TEST_EQUAL( psa_mac_verify_finish( &operation,
2375 expected_mac->x,
2376 expected_mac->len - 1 ),
2377 PSA_ERROR_INVALID_SIGNATURE );
2378
2379 /* Test a MAC that's too long. */
2380 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2381 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002382 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002383 PSA_ASSERT( psa_mac_update( &operation,
2384 input->x, input->len ) );
2385 TEST_EQUAL( psa_mac_verify_finish( &operation,
2386 perturbed_mac,
2387 expected_mac->len + 1 ),
2388 PSA_ERROR_INVALID_SIGNATURE );
2389
2390 /* Test changing one byte. */
2391 for( size_t i = 0; i < expected_mac->len; i++ )
2392 {
Chris Jones9634bb12021-01-20 15:56:42 +00002393 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002394 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002395 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002396 PSA_ASSERT( psa_mac_update( &operation,
2397 input->x, input->len ) );
2398 TEST_EQUAL( psa_mac_verify_finish( &operation,
2399 perturbed_mac,
2400 expected_mac->len ),
2401 PSA_ERROR_INVALID_SIGNATURE );
2402 perturbed_mac[i] ^= 1;
2403 }
2404
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002406 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002407 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002408 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002409 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410}
2411/* END_CASE */
2412
2413/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002414void cipher_operation_init( )
2415{
Jaeden Ameroab439972019-02-15 14:12:05 +00002416 const uint8_t input[1] = { 0 };
2417 unsigned char output[1] = { 0 };
2418 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002419 /* Test each valid way of initializing the object, except for `= {0}`, as
2420 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2421 * though it's OK by the C standard. We could test for this, but we'd need
2422 * to supress the Clang warning for the test. */
2423 psa_cipher_operation_t func = psa_cipher_operation_init( );
2424 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2425 psa_cipher_operation_t zero;
2426
2427 memset( &zero, 0, sizeof( zero ) );
2428
Jaeden Ameroab439972019-02-15 14:12:05 +00002429 /* A freshly-initialized cipher operation should not be usable. */
2430 TEST_EQUAL( psa_cipher_update( &func,
2431 input, sizeof( input ),
2432 output, sizeof( output ),
2433 &output_length ),
2434 PSA_ERROR_BAD_STATE );
2435 TEST_EQUAL( psa_cipher_update( &init,
2436 input, sizeof( input ),
2437 output, sizeof( output ),
2438 &output_length ),
2439 PSA_ERROR_BAD_STATE );
2440 TEST_EQUAL( psa_cipher_update( &zero,
2441 input, sizeof( input ),
2442 output, sizeof( output ),
2443 &output_length ),
2444 PSA_ERROR_BAD_STATE );
2445
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002446 /* A default cipher operation should be abortable without error. */
2447 PSA_ASSERT( psa_cipher_abort( &func ) );
2448 PSA_ASSERT( psa_cipher_abort( &init ) );
2449 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002450}
2451/* END_CASE */
2452
2453/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454void cipher_setup( int key_type_arg,
2455 data_t *key,
2456 int alg_arg,
2457 int expected_status_arg )
2458{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459 psa_key_type_t key_type = key_type_arg;
2460 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002461 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002462 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002463 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002464#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002465 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2466#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467
Gilles Peskine8817f612018-12-18 00:18:46 +01002468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002469
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002470 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2471 &operation, &status ) )
2472 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002473 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002474
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002475 /* The operation object should be reusable. */
2476#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2477 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2478 smoke_test_key_data,
2479 sizeof( smoke_test_key_data ),
2480 KNOWN_SUPPORTED_CIPHER_ALG,
2481 &operation, &status ) )
2482 goto exit;
2483 TEST_EQUAL( status, PSA_SUCCESS );
2484#endif
2485
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002486exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002487 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002488 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002489}
2490/* END_CASE */
2491
Ronald Cronee414c72021-03-18 18:50:08 +01002492/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002493void cipher_bad_order( )
2494{
Ronald Cron5425a212020-08-04 14:58:35 +02002495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002496 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2497 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002499 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002500 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002501 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002502 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2503 0xaa, 0xaa, 0xaa, 0xaa };
2504 const uint8_t text[] = {
2505 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2506 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002507 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002508 size_t length = 0;
2509
2510 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002511 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2512 psa_set_key_algorithm( &attributes, alg );
2513 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2515 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002516
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002517 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002518 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2519 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002520 PSA_ERROR_BAD_STATE );
2521 PSA_ASSERT( psa_cipher_abort( &operation ) );
2522
2523 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002524 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2525 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002526 PSA_ERROR_BAD_STATE );
2527 PSA_ASSERT( psa_cipher_abort( &operation ) );
2528
Jaeden Ameroab439972019-02-15 14:12:05 +00002529 /* Generate an IV without calling setup beforehand. */
2530 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2531 buffer, sizeof( buffer ),
2532 &length ),
2533 PSA_ERROR_BAD_STATE );
2534 PSA_ASSERT( psa_cipher_abort( &operation ) );
2535
2536 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002537 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002538 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2539 buffer, sizeof( buffer ),
2540 &length ) );
2541 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2542 buffer, sizeof( buffer ),
2543 &length ),
2544 PSA_ERROR_BAD_STATE );
2545 PSA_ASSERT( psa_cipher_abort( &operation ) );
2546
2547 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002548 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002549 PSA_ASSERT( psa_cipher_set_iv( &operation,
2550 iv, sizeof( iv ) ) );
2551 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2552 buffer, sizeof( buffer ),
2553 &length ),
2554 PSA_ERROR_BAD_STATE );
2555 PSA_ASSERT( psa_cipher_abort( &operation ) );
2556
2557 /* Set an IV without calling setup beforehand. */
2558 TEST_EQUAL( psa_cipher_set_iv( &operation,
2559 iv, sizeof( iv ) ),
2560 PSA_ERROR_BAD_STATE );
2561 PSA_ASSERT( psa_cipher_abort( &operation ) );
2562
2563 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002564 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002565 PSA_ASSERT( psa_cipher_set_iv( &operation,
2566 iv, sizeof( iv ) ) );
2567 TEST_EQUAL( psa_cipher_set_iv( &operation,
2568 iv, sizeof( iv ) ),
2569 PSA_ERROR_BAD_STATE );
2570 PSA_ASSERT( psa_cipher_abort( &operation ) );
2571
2572 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002573 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002574 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2575 buffer, sizeof( buffer ),
2576 &length ) );
2577 TEST_EQUAL( psa_cipher_set_iv( &operation,
2578 iv, sizeof( iv ) ),
2579 PSA_ERROR_BAD_STATE );
2580 PSA_ASSERT( psa_cipher_abort( &operation ) );
2581
2582 /* Call update without calling setup beforehand. */
2583 TEST_EQUAL( psa_cipher_update( &operation,
2584 text, sizeof( text ),
2585 buffer, sizeof( buffer ),
2586 &length ),
2587 PSA_ERROR_BAD_STATE );
2588 PSA_ASSERT( psa_cipher_abort( &operation ) );
2589
2590 /* Call update without an IV where an IV is required. */
2591 TEST_EQUAL( psa_cipher_update( &operation,
2592 text, sizeof( text ),
2593 buffer, sizeof( buffer ),
2594 &length ),
2595 PSA_ERROR_BAD_STATE );
2596 PSA_ASSERT( psa_cipher_abort( &operation ) );
2597
2598 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002599 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002600 PSA_ASSERT( psa_cipher_set_iv( &operation,
2601 iv, sizeof( iv ) ) );
2602 PSA_ASSERT( psa_cipher_finish( &operation,
2603 buffer, sizeof( buffer ), &length ) );
2604 TEST_EQUAL( psa_cipher_update( &operation,
2605 text, sizeof( text ),
2606 buffer, sizeof( buffer ),
2607 &length ),
2608 PSA_ERROR_BAD_STATE );
2609 PSA_ASSERT( psa_cipher_abort( &operation ) );
2610
2611 /* Call finish without calling setup beforehand. */
2612 TEST_EQUAL( psa_cipher_finish( &operation,
2613 buffer, sizeof( buffer ), &length ),
2614 PSA_ERROR_BAD_STATE );
2615 PSA_ASSERT( psa_cipher_abort( &operation ) );
2616
2617 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002618 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002619 /* Not calling update means we are encrypting an empty buffer, which is OK
2620 * for cipher modes with padding. */
2621 TEST_EQUAL( psa_cipher_finish( &operation,
2622 buffer, sizeof( buffer ), &length ),
2623 PSA_ERROR_BAD_STATE );
2624 PSA_ASSERT( psa_cipher_abort( &operation ) );
2625
2626 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002628 PSA_ASSERT( psa_cipher_set_iv( &operation,
2629 iv, sizeof( iv ) ) );
2630 PSA_ASSERT( psa_cipher_finish( &operation,
2631 buffer, sizeof( buffer ), &length ) );
2632 TEST_EQUAL( psa_cipher_finish( &operation,
2633 buffer, sizeof( buffer ), &length ),
2634 PSA_ERROR_BAD_STATE );
2635 PSA_ASSERT( psa_cipher_abort( &operation ) );
2636
Ronald Cron5425a212020-08-04 14:58:35 +02002637 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002638
Jaeden Ameroab439972019-02-15 14:12:05 +00002639exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002640 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002641 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002642}
2643/* END_CASE */
2644
2645/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002646void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002647 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002648 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002649 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002650{
Ronald Cron5425a212020-08-04 14:58:35 +02002651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652 psa_status_t status;
2653 psa_key_type_t key_type = key_type_arg;
2654 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002655 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657 size_t output_buffer_size = 0;
2658 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002659 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002660 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002661 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002662
Gilles Peskine8817f612018-12-18 00:18:46 +01002663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002664
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002665 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2666 psa_set_key_algorithm( &attributes, alg );
2667 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002668
Ronald Cron5425a212020-08-04 14:58:35 +02002669 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2670 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002671
Ronald Cron5425a212020-08-04 14:58:35 +02002672 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002674 if( iv->len > 0 )
2675 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002676 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002677 }
2678
gabor-mezei-armceface22021-01-21 12:26:17 +01002679 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2680 TEST_ASSERT( output_buffer_size <=
2681 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002682 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002683
Gilles Peskine8817f612018-12-18 00:18:46 +01002684 PSA_ASSERT( psa_cipher_update( &operation,
2685 input->x, input->len,
2686 output, output_buffer_size,
2687 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002688 TEST_ASSERT( function_output_length <=
2689 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002692 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002693
Gilles Peskine50e586b2018-06-08 14:28:46 +02002694 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002695 ( output_buffer_size == 0 ? NULL :
2696 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002697 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002699 TEST_ASSERT( function_output_length <=
2700 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2701 TEST_ASSERT( function_output_length <=
2702 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002703 total_output_length += function_output_length;
2704
Gilles Peskinefe11b722018-12-18 00:24:04 +01002705 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706 if( expected_status == PSA_SUCCESS )
2707 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002708 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002709 ASSERT_COMPARE( expected_output->x, expected_output->len,
2710 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002711 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002712
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002714 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002715 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002716 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002717 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718}
2719/* END_CASE */
2720
2721/* BEGIN_CASE */
2722void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002723 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002724 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002725 int first_part_size_arg,
2726 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002727 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002728{
Ronald Cron5425a212020-08-04 14:58:35 +02002729 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002730 psa_key_type_t key_type = key_type_arg;
2731 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002732 size_t first_part_size = first_part_size_arg;
2733 size_t output1_length = output1_length_arg;
2734 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002735 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736 size_t output_buffer_size = 0;
2737 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002738 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002739 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002740 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002741
Gilles Peskine8817f612018-12-18 00:18:46 +01002742 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002743
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002744 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2745 psa_set_key_algorithm( &attributes, alg );
2746 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002747
Ronald Cron5425a212020-08-04 14:58:35 +02002748 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2749 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002750
Ronald Cron5425a212020-08-04 14:58:35 +02002751 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002753 if( iv->len > 0 )
2754 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002755 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002756 }
2757
gabor-mezei-armceface22021-01-21 12:26:17 +01002758 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2759 TEST_ASSERT( output_buffer_size <=
2760 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002761 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002762
Gilles Peskinee0866522019-02-19 19:44:00 +01002763 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002764 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2765 output, output_buffer_size,
2766 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002767 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002768 TEST_ASSERT( function_output_length <=
2769 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2770 TEST_ASSERT( function_output_length <=
2771 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002772 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002773
Gilles Peskine8817f612018-12-18 00:18:46 +01002774 PSA_ASSERT( psa_cipher_update( &operation,
2775 input->x + first_part_size,
2776 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002777 ( output_buffer_size == 0 ? NULL :
2778 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002779 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002781 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2784 alg,
2785 input->len - first_part_size ) );
2786 TEST_ASSERT( function_output_length <=
2787 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002788 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002789
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002791 ( output_buffer_size == 0 ? NULL :
2792 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002793 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002795 TEST_ASSERT( function_output_length <=
2796 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2797 TEST_ASSERT( function_output_length <=
2798 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002799 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002800 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002802 ASSERT_COMPARE( expected_output->x, expected_output->len,
2803 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002804
2805exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002806 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002807 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002808 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002809 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810}
2811/* END_CASE */
2812
2813/* BEGIN_CASE */
2814void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002815 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002816 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002817 int first_part_size_arg,
2818 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002819 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820{
Ronald Cron5425a212020-08-04 14:58:35 +02002821 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822 psa_key_type_t key_type = key_type_arg;
2823 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002824 size_t first_part_size = first_part_size_arg;
2825 size_t output1_length = output1_length_arg;
2826 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002827 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828 size_t output_buffer_size = 0;
2829 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002830 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002831 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002833
Gilles Peskine8817f612018-12-18 00:18:46 +01002834 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002835
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002836 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2837 psa_set_key_algorithm( &attributes, alg );
2838 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002839
Ronald Cron5425a212020-08-04 14:58:35 +02002840 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2841 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002842
Ronald Cron5425a212020-08-04 14:58:35 +02002843 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Steven Cooreman177deba2020-09-07 17:14:14 +02002845 if( iv->len > 0 )
2846 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002847 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002848 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849
gabor-mezei-armceface22021-01-21 12:26:17 +01002850 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2851 TEST_ASSERT( output_buffer_size <=
2852 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002853 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002854
Gilles Peskinee0866522019-02-19 19:44:00 +01002855 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002856 PSA_ASSERT( psa_cipher_update( &operation,
2857 input->x, first_part_size,
2858 output, output_buffer_size,
2859 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002860 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002861 TEST_ASSERT( function_output_length <=
2862 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2863 TEST_ASSERT( function_output_length <=
2864 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002865 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002866
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_cipher_update( &operation,
2868 input->x + first_part_size,
2869 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002870 ( output_buffer_size == 0 ? NULL :
2871 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002872 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002874 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002875 TEST_ASSERT( function_output_length <=
2876 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2877 alg,
2878 input->len - first_part_size ) );
2879 TEST_ASSERT( function_output_length <=
2880 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002881 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002882
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002884 ( output_buffer_size == 0 ? NULL :
2885 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002886 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002887 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002888 TEST_ASSERT( function_output_length <=
2889 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2890 TEST_ASSERT( function_output_length <=
2891 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002892 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002894
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002895 ASSERT_COMPARE( expected_output->x, expected_output->len,
2896 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897
2898exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002899 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002900 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002901 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002902 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903}
2904/* END_CASE */
2905
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906/* BEGIN_CASE */
2907void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002908 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002909 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002910 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911{
Ronald Cron5425a212020-08-04 14:58:35 +02002912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913 psa_status_t status;
2914 psa_key_type_t key_type = key_type_arg;
2915 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002916 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002917 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918 size_t output_buffer_size = 0;
2919 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002920 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002921 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002923
Gilles Peskine8817f612018-12-18 00:18:46 +01002924 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002925
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2927 psa_set_key_algorithm( &attributes, alg );
2928 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002929
Ronald Cron5425a212020-08-04 14:58:35 +02002930 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2931 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002932
Ronald Cron5425a212020-08-04 14:58:35 +02002933 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
Steven Cooreman177deba2020-09-07 17:14:14 +02002935 if( iv->len > 0 )
2936 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002937 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002938 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939
gabor-mezei-armceface22021-01-21 12:26:17 +01002940 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2941 TEST_ASSERT( output_buffer_size <=
2942 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002943 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944
Gilles Peskine8817f612018-12-18 00:18:46 +01002945 PSA_ASSERT( psa_cipher_update( &operation,
2946 input->x, input->len,
2947 output, output_buffer_size,
2948 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002949 TEST_ASSERT( function_output_length <=
2950 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2951 TEST_ASSERT( function_output_length <=
2952 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002953 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002954
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002956 ( output_buffer_size == 0 ? NULL :
2957 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002958 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002960 TEST_ASSERT( function_output_length <=
2961 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2962 TEST_ASSERT( function_output_length <=
2963 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002964 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002965 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966
2967 if( expected_status == PSA_SUCCESS )
2968 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002970 ASSERT_COMPARE( expected_output->x, expected_output->len,
2971 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972 }
2973
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002975 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002976 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002977 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002978 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002979}
2980/* END_CASE */
2981
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982/* BEGIN_CASE */
2983void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002984 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002985 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002986{
Ronald Cron5425a212020-08-04 14:58:35 +02002987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002988 psa_key_type_t key_type = key_type_arg;
2989 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002990 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002991 size_t iv_size = 16;
2992 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002993 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002994 size_t output1_size = 0;
2995 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002996 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002997 size_t output2_size = 0;
2998 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002999 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003000 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3001 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003003
Gilles Peskine8817f612018-12-18 00:18:46 +01003004 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3007 psa_set_key_algorithm( &attributes, alg );
3008 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003009
Ronald Cron5425a212020-08-04 14:58:35 +02003010 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3011 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003012
Ronald Cron5425a212020-08-04 14:58:35 +02003013 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3014 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003015
Steven Cooreman177deba2020-09-07 17:14:14 +02003016 if( alg != PSA_ALG_ECB_NO_PADDING )
3017 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003018 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3019 iv, iv_size,
3020 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003021 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003022 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3023 TEST_ASSERT( output1_size <=
3024 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003025 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003026
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3028 output1, output1_size,
3029 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003030 TEST_ASSERT( output1_length <=
3031 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3032 TEST_ASSERT( output1_length <=
3033 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3034
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003036 output1 + output1_length,
3037 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( function_output_length <=
3040 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3041 TEST_ASSERT( function_output_length <=
3042 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003043
Gilles Peskine048b7f02018-06-08 14:20:49 +02003044 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003045
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003047
3048 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003049 TEST_ASSERT( output2_size <=
3050 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3051 TEST_ASSERT( output2_size <=
3052 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003053 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003054
Steven Cooreman177deba2020-09-07 17:14:14 +02003055 if( iv_length > 0 )
3056 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003057 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3058 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003059 }
3060
Gilles Peskine8817f612018-12-18 00:18:46 +01003061 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3062 output2, output2_size,
3063 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003064 TEST_ASSERT( output2_length <=
3065 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3066 TEST_ASSERT( output2_length <=
3067 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3068
Gilles Peskine048b7f02018-06-08 14:20:49 +02003069 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_cipher_finish( &operation2,
3071 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003072 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003073 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003074 TEST_ASSERT( function_output_length <=
3075 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3076 TEST_ASSERT( function_output_length <=
3077 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003078
Gilles Peskine048b7f02018-06-08 14:20:49 +02003079 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003080
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003082
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003083 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003084
3085exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003086 psa_cipher_abort( &operation1 );
3087 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003088 mbedtls_free( output1 );
3089 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003090 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003091 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003092}
3093/* END_CASE */
3094
3095/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096void cipher_verify_output_multipart( int alg_arg,
3097 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003098 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003099 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003100 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003101{
Ronald Cron5425a212020-08-04 14:58:35 +02003102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003103 psa_key_type_t key_type = key_type_arg;
3104 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003105 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003106 unsigned char iv[16] = {0};
3107 size_t iv_size = 16;
3108 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003109 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003110 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003111 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003112 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003113 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003114 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003115 size_t function_output_length;
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;
Moran Pekerded84402018-06-06 16:36:50 +03003119
Gilles Peskine8817f612018-12-18 00:18:46 +01003120 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003121
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 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003128
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 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003131
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 }
3138
gabor-mezei-armceface22021-01-21 12:26:17 +01003139 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3140 TEST_ASSERT( output1_buffer_size <=
3141 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003142 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003143
Gilles Peskinee0866522019-02-19 19:44:00 +01003144 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003145
Gilles Peskine8817f612018-12-18 00:18:46 +01003146 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3147 output1, output1_buffer_size,
3148 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003149 TEST_ASSERT( function_output_length <=
3150 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3151 TEST_ASSERT( function_output_length <=
3152 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003153 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_cipher_update( &operation1,
3156 input->x + first_part_size,
3157 input->len - first_part_size,
3158 output1, output1_buffer_size,
3159 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003160 TEST_ASSERT( function_output_length <=
3161 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3162 alg,
3163 input->len - first_part_size ) );
3164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003166 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_finish( &operation1,
3169 output1 + output1_length,
3170 output1_buffer_size - output1_length,
3171 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003172 TEST_ASSERT( function_output_length <=
3173 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3174 TEST_ASSERT( function_output_length <=
3175 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003176 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003177
Gilles Peskine8817f612018-12-18 00:18:46 +01003178 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003179
Gilles Peskine048b7f02018-06-08 14:20:49 +02003180 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003181 TEST_ASSERT( output2_buffer_size <=
3182 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3183 TEST_ASSERT( output2_buffer_size <=
3184 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003185 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003186
Steven Cooreman177deba2020-09-07 17:14:14 +02003187 if( iv_length > 0 )
3188 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003189 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3190 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003191 }
Moran Pekerded84402018-06-06 16:36:50 +03003192
Gilles Peskine8817f612018-12-18 00:18:46 +01003193 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3194 output2, output2_buffer_size,
3195 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003196 TEST_ASSERT( function_output_length <=
3197 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3198 TEST_ASSERT( function_output_length <=
3199 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003200 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_cipher_update( &operation2,
3203 output1 + first_part_size,
3204 output1_length - first_part_size,
3205 output2, output2_buffer_size,
3206 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003207 TEST_ASSERT( function_output_length <=
3208 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3209 alg,
3210 output1_length - first_part_size ) );
3211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003213 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_finish( &operation2,
3216 output2 + output2_length,
3217 output2_buffer_size - output2_length,
3218 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003219 TEST_ASSERT( function_output_length <=
3220 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3221 TEST_ASSERT( function_output_length <=
3222 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003223 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003224
Gilles Peskine8817f612018-12-18 00:18:46 +01003225 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003226
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003227 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003228
3229exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003230 psa_cipher_abort( &operation1 );
3231 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003232 mbedtls_free( output1 );
3233 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003234 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003235 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003236}
3237/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003238
Gilles Peskine20035e32018-02-03 22:44:14 +01003239/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003240void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003241 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003242 data_t *nonce,
3243 data_t *additional_data,
3244 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003245 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246{
Ronald Cron5425a212020-08-04 14:58:35 +02003247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248 psa_key_type_t key_type = key_type_arg;
3249 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003250 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251 unsigned char *output_data = NULL;
3252 size_t output_size = 0;
3253 size_t output_length = 0;
3254 unsigned char *output_data2 = NULL;
3255 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003256 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003257 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259
Gilles Peskine8817f612018-12-18 00:18:46 +01003260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003261
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3263 psa_set_key_algorithm( &attributes, alg );
3264 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003265
Gilles Peskine049c7532019-05-15 20:22:09 +02003266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003267 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003268 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3269 key_bits = psa_get_key_bits( &attributes );
3270
3271 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3272 alg );
3273 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3274 * should be exact. */
3275 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3276 expected_result != PSA_ERROR_NOT_SUPPORTED )
3277 {
3278 TEST_EQUAL( output_size,
3279 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3280 TEST_ASSERT( output_size <=
3281 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3282 }
3283 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003284
Steven Cooremanf49478b2021-02-15 15:19:25 +01003285 status = psa_aead_encrypt( key, alg,
3286 nonce->x, nonce->len,
3287 additional_data->x,
3288 additional_data->len,
3289 input_data->x, input_data->len,
3290 output_data, output_size,
3291 &output_length );
3292
3293 /* If the operation is not supported, just skip and not fail in case the
3294 * encryption involves a common limitation of cryptography hardwares and
3295 * an alternative implementation. */
3296 if( status == PSA_ERROR_NOT_SUPPORTED )
3297 {
3298 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3299 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3300 }
3301
3302 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303
3304 if( PSA_SUCCESS == expected_result )
3305 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003306 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
Gilles Peskine003a4a92019-05-14 16:09:40 +02003308 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3309 * should be exact. */
3310 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003311 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003312
gabor-mezei-armceface22021-01-21 12:26:17 +01003313 TEST_ASSERT( input_data->len <=
3314 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3315
Ronald Cron5425a212020-08-04 14:58:35 +02003316 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003317 nonce->x, nonce->len,
3318 additional_data->x,
3319 additional_data->len,
3320 output_data, output_length,
3321 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003322 &output_length2 ),
3323 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003324
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003325 ASSERT_COMPARE( input_data->x, input_data->len,
3326 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003327 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003328
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003330 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331 mbedtls_free( output_data );
3332 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003333 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334}
3335/* END_CASE */
3336
3337/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003338void aead_encrypt( int key_type_arg, data_t *key_data,
3339 int alg_arg,
3340 data_t *nonce,
3341 data_t *additional_data,
3342 data_t *input_data,
3343 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344{
Ronald Cron5425a212020-08-04 14:58:35 +02003345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346 psa_key_type_t key_type = key_type_arg;
3347 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003348 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349 unsigned char *output_data = NULL;
3350 size_t output_size = 0;
3351 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354
Gilles Peskine8817f612018-12-18 00:18:46 +01003355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3358 psa_set_key_algorithm( &attributes, alg );
3359 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
Gilles Peskine049c7532019-05-15 20:22:09 +02003361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003362 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003363 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3364 key_bits = psa_get_key_bits( &attributes );
3365
3366 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3367 alg );
3368 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3369 * should be exact. */
3370 TEST_EQUAL( output_size,
3371 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3372 TEST_ASSERT( output_size <=
3373 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3374 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003375
Steven Cooremand588ea12021-01-11 19:36:04 +01003376 status = psa_aead_encrypt( key, alg,
3377 nonce->x, nonce->len,
3378 additional_data->x, additional_data->len,
3379 input_data->x, input_data->len,
3380 output_data, output_size,
3381 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003382
Ronald Cron28a45ed2021-02-09 20:35:42 +01003383 /* If the operation is not supported, just skip and not fail in case the
3384 * encryption involves a common limitation of cryptography hardwares and
3385 * an alternative implementation. */
3386 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003387 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003388 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3389 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003390 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003391
3392 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003393 ASSERT_COMPARE( expected_result->x, expected_result->len,
3394 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003395
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003397 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003399 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400}
3401/* END_CASE */
3402
3403/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003404void aead_decrypt( int key_type_arg, data_t *key_data,
3405 int alg_arg,
3406 data_t *nonce,
3407 data_t *additional_data,
3408 data_t *input_data,
3409 data_t *expected_data,
3410 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411{
Ronald Cron5425a212020-08-04 14:58:35 +02003412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003413 psa_key_type_t key_type = key_type_arg;
3414 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003415 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416 unsigned char *output_data = NULL;
3417 size_t output_size = 0;
3418 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003420 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003421 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003422
Gilles Peskine8817f612018-12-18 00:18:46 +01003423 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003424
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003425 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3426 psa_set_key_algorithm( &attributes, alg );
3427 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428
Gilles Peskine049c7532019-05-15 20:22:09 +02003429 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003430 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003431 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3432 key_bits = psa_get_key_bits( &attributes );
3433
3434 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3435 alg );
3436 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3437 expected_result != PSA_ERROR_NOT_SUPPORTED )
3438 {
3439 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3440 * should be exact. */
3441 TEST_EQUAL( output_size,
3442 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3443 TEST_ASSERT( output_size <=
3444 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3445 }
3446 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003447
Steven Cooremand588ea12021-01-11 19:36:04 +01003448 status = psa_aead_decrypt( key, alg,
3449 nonce->x, nonce->len,
3450 additional_data->x,
3451 additional_data->len,
3452 input_data->x, input_data->len,
3453 output_data, output_size,
3454 &output_length );
3455
Ronald Cron28a45ed2021-02-09 20:35:42 +01003456 /* If the operation is not supported, just skip and not fail in case the
3457 * decryption involves a common limitation of cryptography hardwares and
3458 * an alternative implementation. */
3459 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003460 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003461 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3462 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003463 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003464
3465 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003466
Gilles Peskine2d277862018-06-18 15:41:12 +02003467 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003468 ASSERT_COMPARE( expected_data->x, expected_data->len,
3469 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskinea1cac842018-06-11 19:33:02 +02003471exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003472 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003473 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003474 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475}
3476/* END_CASE */
3477
3478/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003479void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3480 int alg_arg,
3481 data_t *nonce,
3482 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003483 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003484 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003485 int do_test_data_chunked,
3486 int do_set_lengths,
3487 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003488{
Paul Elliottd3f82412021-06-16 16:52:21 +01003489 size_t ad_part_len = 0;
3490 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003491
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003492 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3493
3494 /* Temporary whilst we have algorithms that cannot support chunking */
3495 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003496 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003497 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3498 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003499 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003500 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003501
Paul Elliott329d5382021-07-22 17:10:45 +01003502 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003503 if( !aead_multipart_internal_func( key_type_arg, key_data,
3504 alg_arg, nonce,
3505 additional_data,
3506 ad_part_len,
3507 input_data, -1,
3508 do_set_lengths,
3509 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003510 1, 1, 0,
3511 ( ad_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003512 break;
3513
3514 /* length(0) part, length(ad_part_len) part, length(0) part... */
3515 mbedtls_test_set_step( 1000 + ad_part_len );
3516
3517 if( !aead_multipart_internal_func( key_type_arg, key_data,
3518 alg_arg, nonce,
3519 additional_data,
3520 ad_part_len,
3521 input_data, -1,
3522 do_set_lengths,
3523 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003524 1, 1, 1,
3525 ( ad_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003526 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003527 }
3528 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003529
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003530 /* Temporary whilst we have algorithms that cannot support chunking */
3531 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003532 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003533 for( data_part_len = 1; data_part_len <= input_data->len;
3534 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003535 {
Paul Elliott329d5382021-07-22 17:10:45 +01003536 /* Split data into length(data_part_len) parts. */
3537 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003538
3539 if( !aead_multipart_internal_func( key_type_arg, key_data,
3540 alg_arg, nonce,
3541 additional_data, -1,
3542 input_data, data_part_len,
3543 do_set_lengths,
3544 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003545 1, 1, 0,
3546 ( data_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003547 break;
3548
3549 /* length(0) part, length(data_part_len) part, length(0) part... */
3550 mbedtls_test_set_step( 3000 + data_part_len );
3551
3552 if( !aead_multipart_internal_func( key_type_arg, key_data,
3553 alg_arg, nonce,
3554 additional_data, -1,
3555 input_data, data_part_len,
3556 do_set_lengths,
3557 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003558 1, 1, 1,
3559 ( data_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003560 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003561 }
3562 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003563
Paul Elliott0023e0a2021-04-27 10:06:22 +01003564
Paul Elliott8fc45162021-06-23 16:06:01 +01003565 /* Goto is required to silence warnings about unused labels, as we
3566 * don't actually do any test assertions in this function. */
3567 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568}
3569/* END_CASE */
3570
3571/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003572void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3573 int alg_arg,
3574 data_t *nonce,
3575 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003576 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003577 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003578 int do_test_data_chunked,
3579 int do_set_lengths,
3580 data_t *expected_output,
3581 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582{
Paul Elliottd3f82412021-06-16 16:52:21 +01003583 size_t ad_part_len = 0;
3584 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003585
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003586 /* Temporary whilst we have algorithms that cannot support chunking */
3587 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003588 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003589 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3590 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003591 {
Paul Elliott329d5382021-07-22 17:10:45 +01003592 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003593 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003594
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003595 if( !aead_multipart_internal_func( key_type_arg, key_data,
3596 alg_arg, nonce,
3597 additional_data,
3598 ad_part_len,
3599 input_data, -1,
3600 do_set_lengths,
3601 expected_output,
3602 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003603 0, 0,
3604 ( ad_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003605 break;
3606
3607 /* length(0) part, length(ad_part_len) part, length(0) part... */
3608 mbedtls_test_set_step( 1000 + ad_part_len );
3609
3610 if( !aead_multipart_internal_func( key_type_arg, key_data,
3611 alg_arg, nonce,
3612 additional_data,
3613 ad_part_len,
3614 input_data, -1,
3615 do_set_lengths,
3616 expected_output,
3617 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003618 0, 1,
3619 ( ad_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003620 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003621 }
3622 }
3623
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003624 /* Temporary whilst we have algorithms that cannot support chunking */
3625 if( do_test_data_chunked == 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 Elliott329d5382021-07-22 17:10:45 +01003630 /* Split data into length(data_part_len) parts. */
3631 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003632
3633 if( !aead_multipart_internal_func( key_type_arg, key_data,
3634 alg_arg, nonce,
3635 additional_data, -1,
3636 input_data, data_part_len,
3637 do_set_lengths,
3638 expected_output,
3639 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003640 0, 0,
3641 ( data_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003642 break;
3643
3644 /* length(0) part, length(data_part_len) part, length(0) part... */
3645 mbedtls_test_set_step( 3000 + data_part_len );
3646
3647 if( !aead_multipart_internal_func( key_type_arg, key_data,
3648 alg_arg, nonce,
3649 additional_data, -1,
3650 input_data, data_part_len,
3651 do_set_lengths,
3652 expected_output,
3653 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003654 0, 1,
3655 ( data_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003656 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003657 }
3658 }
3659
Paul Elliott8fc45162021-06-23 16:06:01 +01003660 /* Goto is required to silence warnings about unused labels, as we
3661 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003662 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003663}
3664/* END_CASE */
3665
3666/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003667void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3668 int alg_arg,
3669 int nonce_len,
Paul Elliottd85f5472021-07-16 18:20:16 +01003670 int expected_generated_len_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003671 data_t *additional_data,
3672 data_t *input_data,
3673 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003674{
3675
3676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3677 psa_key_type_t key_type = key_type_arg;
3678 psa_algorithm_t alg = alg_arg;
3679 psa_aead_operation_t operation;
3680 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3682 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3683 size_t nonce_generated_len = 0;
Paul Elliottd85f5472021-07-16 18:20:16 +01003684 size_t expected_generated_len = expected_generated_len_arg;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003685 unsigned char *output_data = NULL;
3686 unsigned char *final_data = NULL;
3687 size_t output_size = 0;
3688 size_t finish_output_size = 0;
3689 size_t output_length = 0;
3690 size_t tag_length = 0;
3691 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003692
3693 PSA_ASSERT( psa_crypto_init( ) );
3694
3695 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3696 psa_set_key_algorithm( & attributes, alg );
3697 psa_set_key_type( & attributes, key_type );
3698
3699 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3700 &key ) );
3701
3702 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3703
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003704 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3705
3706 ASSERT_ALLOC( output_data, output_size );
3707
3708 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3709
3710 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3711
3712 ASSERT_ALLOC( final_data, finish_output_size );
3713
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003714 operation = psa_aead_operation_init( );
3715
3716 status = psa_aead_encrypt_setup( &operation, key, alg );
3717
3718 /* If the operation is not supported, just skip and not fail in case the
3719 * encryption involves a common limitation of cryptography hardwares and
3720 * an alternative implementation. */
3721 if( status == PSA_ERROR_NOT_SUPPORTED )
3722 {
3723 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3724 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len );
3725 }
3726
3727 PSA_ASSERT( status );
3728
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003729 status = psa_aead_generate_nonce( &operation, nonce_buffer,
3730 nonce_len,
3731 &nonce_generated_len );
3732
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003733 TEST_ASSERT( status == expected_status_arg );
3734
Paul Elliottd85f5472021-07-16 18:20:16 +01003735 TEST_EQUAL( nonce_generated_len, expected_generated_len );
3736
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003737 TEST_ASSERT( nonce_generated_len < PSA_AEAD_NONCE_MAX_SIZE );
3738
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003739 if( expected_status_arg == PSA_SUCCESS )
3740 {
3741
3742 /* Ensure we can still complete operation. */
3743
3744 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3745 additional_data->len ) );
3746
3747 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
3748 output_data, output_size, &output_length ) );
3749
3750 PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size,
3751 &output_length, tag_buffer,
3752 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3753 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003754
3755exit:
3756 psa_destroy_key( key );
Paul Elliott16906f92021-06-24 09:57:01 +01003757 mbedtls_free( output_data );
3758 mbedtls_free( final_data );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003759 psa_aead_abort( &operation );
3760 PSA_DONE( );
3761}
3762/* END_CASE */
3763
3764/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01003765void aead_multipart_state_test( int key_type_arg, data_t *key_data,
3766 int alg_arg,
3767 data_t *nonce,
3768 data_t *additional_data,
3769 data_t *input_data )
3770{
3771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3772 psa_key_type_t key_type = key_type_arg;
3773 psa_algorithm_t alg = alg_arg;
3774 psa_aead_operation_t operation;
3775 unsigned char *output_data = NULL;
3776 unsigned char *final_data = NULL;
3777 size_t output_size = 0;
3778 size_t finish_output_size = 0;
3779 size_t output_length = 0;
3780 size_t key_bits = 0;
3781 size_t tag_length = 0;
3782 size_t tag_size = 0;
3783 size_t nonce_length = 0;
3784 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3785 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3786 size_t output_part_length = 0;
3787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3788
3789 PSA_ASSERT( psa_crypto_init( ) );
3790
3791 psa_set_key_usage_flags( & attributes,
3792 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3793 psa_set_key_algorithm( & attributes, alg );
3794 psa_set_key_type( & attributes, key_type );
3795
3796 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3797 &key ) );
3798
3799 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3800 key_bits = psa_get_key_bits( &attributes );
3801
3802 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3803
3804 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
3805
3806 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3807
3808 ASSERT_ALLOC( output_data, output_size );
3809
3810 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3811
3812 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3813
3814 ASSERT_ALLOC( final_data, finish_output_size );
3815
3816 /* Test all operations error without calling setup first. */
3817
3818 operation = psa_aead_operation_init( );
3819
3820 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3821 PSA_ERROR_BAD_STATE );
3822
3823 psa_aead_abort( &operation );
3824
3825 operation = psa_aead_operation_init( );
3826
3827 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3828 PSA_AEAD_NONCE_MAX_SIZE,
3829 &nonce_length ),
3830 PSA_ERROR_BAD_STATE );
3831
3832 psa_aead_abort( &operation );
3833
Paul Elliott481be342021-07-16 17:38:47 +01003834 /* ------------------------------------------------------- */
3835
Paul Elliottc23a9a02021-06-21 18:32:46 +01003836 operation = psa_aead_operation_init( );
3837
3838 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3839 input_data->len ),
3840 PSA_ERROR_BAD_STATE );
3841
3842 psa_aead_abort( &operation );
3843
Paul Elliott481be342021-07-16 17:38:47 +01003844 /* ------------------------------------------------------- */
3845
Paul Elliottc23a9a02021-06-21 18:32:46 +01003846 operation = psa_aead_operation_init( );
3847
3848 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3849 additional_data->len ),
3850 PSA_ERROR_BAD_STATE );
3851
3852 psa_aead_abort( &operation );
3853
Paul Elliott481be342021-07-16 17:38:47 +01003854 /* ------------------------------------------------------- */
3855
Paul Elliottc23a9a02021-06-21 18:32:46 +01003856 operation = psa_aead_operation_init( );
3857
3858 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
3859 input_data->len, output_data,
3860 output_size, &output_length ),
3861 PSA_ERROR_BAD_STATE );
3862
3863 psa_aead_abort( &operation );
3864
Paul Elliott481be342021-07-16 17:38:47 +01003865 /* ------------------------------------------------------- */
3866
Paul Elliottc23a9a02021-06-21 18:32:46 +01003867 operation = psa_aead_operation_init( );
3868
3869 TEST_EQUAL( psa_aead_finish( &operation, final_data,
3870 finish_output_size,
3871 &output_part_length,
3872 tag_buffer, tag_length,
3873 &tag_size ),
3874 PSA_ERROR_BAD_STATE );
3875
3876 psa_aead_abort( &operation );
3877
Paul Elliott481be342021-07-16 17:38:47 +01003878 /* ------------------------------------------------------- */
3879
Paul Elliottc23a9a02021-06-21 18:32:46 +01003880 operation = psa_aead_operation_init( );
3881
3882 TEST_EQUAL( psa_aead_verify( &operation, final_data,
3883 finish_output_size,
3884 &output_part_length,
3885 tag_buffer,
3886 tag_length ),
3887 PSA_ERROR_BAD_STATE );
3888
3889 psa_aead_abort( &operation );
3890
3891 /* Test for double setups. */
3892
3893 operation = psa_aead_operation_init( );
3894
3895 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3896
3897 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3898 PSA_ERROR_BAD_STATE );
3899
3900 psa_aead_abort( &operation );
3901
Paul Elliott481be342021-07-16 17:38:47 +01003902 /* ------------------------------------------------------- */
3903
Paul Elliottc23a9a02021-06-21 18:32:46 +01003904 operation = psa_aead_operation_init( );
3905
3906 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3907
3908 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3909 PSA_ERROR_BAD_STATE );
3910
3911 psa_aead_abort( &operation );
3912
Paul Elliott374a2be2021-07-16 17:53:40 +01003913 /* ------------------------------------------------------- */
3914
3915 operation = psa_aead_operation_init( );
3916
3917 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3918
3919 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3920 PSA_ERROR_BAD_STATE );
3921
3922 psa_aead_abort( &operation );
3923
3924 /* ------------------------------------------------------- */
3925
3926 operation = psa_aead_operation_init( );
3927
3928 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3929
3930 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3931 PSA_ERROR_BAD_STATE );
3932
3933 psa_aead_abort( &operation );
3934
Paul Elliottc23a9a02021-06-21 18:32:46 +01003935 /* Test for not setting a nonce. */
3936
3937 operation = psa_aead_operation_init( );
3938
3939 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3940
3941 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3942 additional_data->len ),
3943 PSA_ERROR_BAD_STATE );
3944
3945 psa_aead_abort( &operation );
3946
3947 /* Test for double setting nonce. */
3948
3949 operation = psa_aead_operation_init( );
3950
3951 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3952
3953 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3954
3955 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3956 PSA_ERROR_BAD_STATE );
3957
3958 psa_aead_abort( &operation );
3959
Paul Elliott374a2be2021-07-16 17:53:40 +01003960 /* Test for double generating nonce. */
3961
3962 operation = psa_aead_operation_init( );
3963
3964 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3965
3966 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3967 PSA_AEAD_NONCE_MAX_SIZE,
3968 &nonce_length ) );
3969
3970 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3971 PSA_AEAD_NONCE_MAX_SIZE,
3972 &nonce_length ),
3973 PSA_ERROR_BAD_STATE );
3974
3975
3976 psa_aead_abort( &operation );
3977
3978 /* Test for generate nonce then set and vice versa */
3979
3980 operation = psa_aead_operation_init( );
3981
3982 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3983
3984 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3985 PSA_AEAD_NONCE_MAX_SIZE,
3986 &nonce_length ) );
3987
3988 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3989 PSA_ERROR_BAD_STATE );
3990
3991 psa_aead_abort( &operation );
3992
3993 /* ------------------------------------------------------- */
3994
3995 operation = psa_aead_operation_init( );
3996
3997 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3998
3999 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4000
4001 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4002 PSA_AEAD_NONCE_MAX_SIZE,
4003 &nonce_length ),
4004 PSA_ERROR_BAD_STATE );
4005
4006 psa_aead_abort( &operation );
4007
Paul Elliott7220cae2021-06-22 17:25:57 +01004008 /* Test for generating nonce in decrypt setup. */
4009
4010 operation = psa_aead_operation_init( );
4011
4012 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4013
4014 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4015 PSA_AEAD_NONCE_MAX_SIZE,
4016 &nonce_length ),
4017 PSA_ERROR_BAD_STATE );
4018
4019 psa_aead_abort( &operation );
4020
Paul Elliottc23a9a02021-06-21 18:32:46 +01004021 /* Test for setting lengths twice. */
4022
4023 operation = psa_aead_operation_init( );
4024
4025 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4026
4027 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4028
4029 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4030 input_data->len ) );
4031
4032 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4033 input_data->len ),
4034 PSA_ERROR_BAD_STATE );
4035
4036 psa_aead_abort( &operation );
4037
4038 /* Test for setting lengths after already starting data. */
4039
4040 operation = psa_aead_operation_init( );
4041
4042 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4043
4044 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4045
4046 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4047 input_data->len, output_data,
4048 output_size, &output_length ) );
4049
4050 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4051 input_data->len ),
4052 PSA_ERROR_BAD_STATE );
4053
4054 psa_aead_abort( &operation );
4055
Paul Elliott243080c2021-07-21 19:01:17 +01004056 /* Test for not sending any additional data or data after setting non zero
4057 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004058
4059 operation = psa_aead_operation_init( );
4060
4061 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4062
4063 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4064
4065 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4066 input_data->len ) );
4067
4068 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4069 finish_output_size,
4070 &output_part_length,
4071 tag_buffer, tag_length,
4072 &tag_size ),
4073 PSA_ERROR_INVALID_ARGUMENT );
4074
4075 psa_aead_abort( &operation );
4076
Paul Elliott243080c2021-07-21 19:01:17 +01004077 /* Test for not sending any additional data or data after setting non-zero
4078 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004079
4080 operation = psa_aead_operation_init( );
4081
4082 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4083
4084 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4085
4086 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4087 input_data->len ) );
4088
4089 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4090 finish_output_size,
4091 &output_part_length,
4092 tag_buffer,
4093 tag_length ),
4094 PSA_ERROR_INVALID_ARGUMENT );
4095
4096 psa_aead_abort( &operation );
4097
Paul Elliott243080c2021-07-21 19:01:17 +01004098 /* Test for not sending any additional data after setting a non-zero length
4099 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004100
4101 operation = psa_aead_operation_init( );
4102
4103 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4104
4105 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4106
4107 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4108 input_data->len ) );
4109
4110 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4111 input_data->len, output_data,
4112 output_size, &output_length ),
4113 PSA_ERROR_INVALID_ARGUMENT );
4114
4115 psa_aead_abort( &operation );
4116
4117 /* Test sending additional data after data. */
4118
4119 operation = psa_aead_operation_init( );
4120
4121 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4122
4123 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4124
4125 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4126 input_data->len, output_data,
4127 output_size, &output_length ) );
4128
4129 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4130 additional_data->len ),
4131 PSA_ERROR_BAD_STATE );
4132
4133 psa_aead_abort( &operation );
4134
Paul Elliott534d0b42021-06-22 19:15:20 +01004135 /* Test calling finish on decryption. */
4136
4137 operation = psa_aead_operation_init( );
4138
4139 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4140
4141 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->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_BAD_STATE );
4149
4150 psa_aead_abort( &operation );
4151
4152 /* Test calling verify on encryption. */
4153
4154 operation = psa_aead_operation_init( );
4155
4156 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4157
4158 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4159
4160 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4161 finish_output_size,
4162 &output_part_length,
4163 tag_buffer,
4164 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004165 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004166
4167 psa_aead_abort( &operation );
4168
4169
Paul Elliottc23a9a02021-06-21 18:32:46 +01004170exit:
4171 psa_destroy_key( key );
4172 psa_aead_abort( &operation );
4173 mbedtls_free( output_data );
4174 mbedtls_free( final_data );
4175 PSA_DONE( );
4176}
4177/* END_CASE */
4178
4179/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004180void signature_size( int type_arg,
4181 int bits,
4182 int alg_arg,
4183 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004184{
4185 psa_key_type_t type = type_arg;
4186 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004187 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004188
Gilles Peskinefe11b722018-12-18 00:24:04 +01004189 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004190
Gilles Peskinee59236f2018-01-27 23:32:46 +01004191exit:
4192 ;
4193}
4194/* END_CASE */
4195
4196/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004197void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4198 int alg_arg, data_t *input_data,
4199 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004200{
Ronald Cron5425a212020-08-04 14:58:35 +02004201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004202 psa_key_type_t key_type = key_type_arg;
4203 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004204 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004205 unsigned char *signature = NULL;
4206 size_t signature_size;
4207 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004209
Gilles Peskine8817f612018-12-18 00:18:46 +01004210 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004211
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004213 psa_set_key_algorithm( &attributes, alg );
4214 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004215
Gilles Peskine049c7532019-05-15 20:22:09 +02004216 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004217 &key ) );
4218 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004219 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004220
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004221 /* Allocate a buffer which has the size advertized by the
4222 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004223 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004224 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004225 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004226 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004227 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004228
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004229 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004230 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004231 input_data->x, input_data->len,
4232 signature, signature_size,
4233 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004234 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004235 ASSERT_COMPARE( output_data->x, output_data->len,
4236 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004237
4238exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004239 /*
4240 * Key attributes may have been returned by psa_get_key_attributes()
4241 * thus reset them as required.
4242 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004243 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004244
Ronald Cron5425a212020-08-04 14:58:35 +02004245 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004246 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004247 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004248}
4249/* END_CASE */
4250
4251/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004252void sign_hash_fail( int key_type_arg, data_t *key_data,
4253 int alg_arg, data_t *input_data,
4254 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004255{
Ronald Cron5425a212020-08-04 14:58:35 +02004256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004257 psa_key_type_t key_type = key_type_arg;
4258 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004259 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004260 psa_status_t actual_status;
4261 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004262 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004263 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004265
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004266 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004267
Gilles Peskine8817f612018-12-18 00:18:46 +01004268 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004269
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004271 psa_set_key_algorithm( &attributes, alg );
4272 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004273
Gilles Peskine049c7532019-05-15 20:22:09 +02004274 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004275 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004276
Ronald Cron5425a212020-08-04 14:58:35 +02004277 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004278 input_data->x, input_data->len,
4279 signature, signature_size,
4280 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004281 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004282 /* The value of *signature_length is unspecified on error, but
4283 * whatever it is, it should be less than signature_size, so that
4284 * if the caller tries to read *signature_length bytes without
4285 * checking the error code then they don't overflow a buffer. */
4286 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004287
4288exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004289 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004290 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004291 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004292 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004293}
4294/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004295
4296/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004297void sign_verify_hash( int key_type_arg, data_t *key_data,
4298 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004299{
Ronald Cron5425a212020-08-04 14:58:35 +02004300 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004301 psa_key_type_t key_type = key_type_arg;
4302 psa_algorithm_t alg = alg_arg;
4303 size_t key_bits;
4304 unsigned char *signature = NULL;
4305 size_t signature_size;
4306 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004308
Gilles Peskine8817f612018-12-18 00:18:46 +01004309 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004310
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004311 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004312 psa_set_key_algorithm( &attributes, alg );
4313 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004314
Gilles Peskine049c7532019-05-15 20:22:09 +02004315 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004316 &key ) );
4317 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004318 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004319
4320 /* Allocate a buffer which has the size advertized by the
4321 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004322 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004323 key_bits, alg );
4324 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004325 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004326 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004327
4328 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004329 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004330 input_data->x, input_data->len,
4331 signature, signature_size,
4332 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004333 /* Check that the signature length looks sensible. */
4334 TEST_ASSERT( signature_length <= signature_size );
4335 TEST_ASSERT( signature_length > 0 );
4336
4337 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004338 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004339 input_data->x, input_data->len,
4340 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004341
4342 if( input_data->len != 0 )
4343 {
4344 /* Flip a bit in the input and verify that the signature is now
4345 * detected as invalid. Flip a bit at the beginning, not at the end,
4346 * because ECDSA may ignore the last few bits of the input. */
4347 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004348 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004349 input_data->x, input_data->len,
4350 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004351 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004352 }
4353
4354exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004355 /*
4356 * Key attributes may have been returned by psa_get_key_attributes()
4357 * thus reset them as required.
4358 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004359 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004360
Ronald Cron5425a212020-08-04 14:58:35 +02004361 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004362 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004363 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004364}
4365/* END_CASE */
4366
4367/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004368void verify_hash( int key_type_arg, data_t *key_data,
4369 int alg_arg, data_t *hash_data,
4370 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004371{
Ronald Cron5425a212020-08-04 14:58:35 +02004372 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004373 psa_key_type_t key_type = key_type_arg;
4374 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004376
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004377 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004378
Gilles Peskine8817f612018-12-18 00:18:46 +01004379 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004380
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004381 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004382 psa_set_key_algorithm( &attributes, alg );
4383 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004384
Gilles Peskine049c7532019-05-15 20:22:09 +02004385 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004386 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004387
Ronald Cron5425a212020-08-04 14:58:35 +02004388 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004389 hash_data->x, hash_data->len,
4390 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004391
itayzafrir5c753392018-05-08 11:18:38 +03004392exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004393 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004394 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004395 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004396}
4397/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004398
4399/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004400void verify_hash_fail( int key_type_arg, data_t *key_data,
4401 int alg_arg, data_t *hash_data,
4402 data_t *signature_data,
4403 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004404{
Ronald Cron5425a212020-08-04 14:58:35 +02004405 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004406 psa_key_type_t key_type = key_type_arg;
4407 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004408 psa_status_t actual_status;
4409 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004411
Gilles Peskine8817f612018-12-18 00:18:46 +01004412 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004413
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004414 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004415 psa_set_key_algorithm( &attributes, alg );
4416 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004417
Gilles Peskine049c7532019-05-15 20:22:09 +02004418 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004419 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004420
Ronald Cron5425a212020-08-04 14:58:35 +02004421 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004422 hash_data->x, hash_data->len,
4423 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004424 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004425
4426exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004427 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004428 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004429 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004430}
4431/* END_CASE */
4432
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004433/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004434void sign_message_deterministic( int key_type_arg,
4435 data_t *key_data,
4436 int alg_arg,
4437 data_t *input_data,
4438 data_t *output_data )
4439{
4440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4441 psa_key_type_t key_type = key_type_arg;
4442 psa_algorithm_t alg = alg_arg;
4443 size_t key_bits;
4444 unsigned char *signature = NULL;
4445 size_t signature_size;
4446 size_t signature_length = 0xdeadbeef;
4447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4448
4449 PSA_ASSERT( psa_crypto_init( ) );
4450
4451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4452 psa_set_key_algorithm( &attributes, alg );
4453 psa_set_key_type( &attributes, key_type );
4454
4455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4456 &key ) );
4457 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4458 key_bits = psa_get_key_bits( &attributes );
4459
4460 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4461 TEST_ASSERT( signature_size != 0 );
4462 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4463 ASSERT_ALLOC( signature, signature_size );
4464
4465 PSA_ASSERT( psa_sign_message( key, alg,
4466 input_data->x, input_data->len,
4467 signature, signature_size,
4468 &signature_length ) );
4469
4470 ASSERT_COMPARE( output_data->x, output_data->len,
4471 signature, signature_length );
4472
4473exit:
4474 psa_reset_key_attributes( &attributes );
4475
4476 psa_destroy_key( key );
4477 mbedtls_free( signature );
4478 PSA_DONE( );
4479
4480}
4481/* END_CASE */
4482
4483/* BEGIN_CASE */
4484void sign_message_fail( int key_type_arg,
4485 data_t *key_data,
4486 int alg_arg,
4487 data_t *input_data,
4488 int signature_size_arg,
4489 int expected_status_arg )
4490{
4491 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4492 psa_key_type_t key_type = key_type_arg;
4493 psa_algorithm_t alg = alg_arg;
4494 size_t signature_size = signature_size_arg;
4495 psa_status_t actual_status;
4496 psa_status_t expected_status = expected_status_arg;
4497 unsigned char *signature = NULL;
4498 size_t signature_length = 0xdeadbeef;
4499 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4500
4501 ASSERT_ALLOC( signature, signature_size );
4502
4503 PSA_ASSERT( psa_crypto_init( ) );
4504
4505 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4506 psa_set_key_algorithm( &attributes, alg );
4507 psa_set_key_type( &attributes, key_type );
4508
4509 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4510 &key ) );
4511
4512 actual_status = psa_sign_message( key, alg,
4513 input_data->x, input_data->len,
4514 signature, signature_size,
4515 &signature_length );
4516 TEST_EQUAL( actual_status, expected_status );
4517 /* The value of *signature_length is unspecified on error, but
4518 * whatever it is, it should be less than signature_size, so that
4519 * if the caller tries to read *signature_length bytes without
4520 * checking the error code then they don't overflow a buffer. */
4521 TEST_ASSERT( signature_length <= signature_size );
4522
4523exit:
4524 psa_reset_key_attributes( &attributes );
4525 psa_destroy_key( key );
4526 mbedtls_free( signature );
4527 PSA_DONE( );
4528}
4529/* END_CASE */
4530
4531/* BEGIN_CASE */
4532void sign_verify_message( int key_type_arg,
4533 data_t *key_data,
4534 int alg_arg,
4535 data_t *input_data )
4536{
4537 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4538 psa_key_type_t key_type = key_type_arg;
4539 psa_algorithm_t alg = alg_arg;
4540 size_t key_bits;
4541 unsigned char *signature = NULL;
4542 size_t signature_size;
4543 size_t signature_length = 0xdeadbeef;
4544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4545
4546 PSA_ASSERT( psa_crypto_init( ) );
4547
4548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4549 PSA_KEY_USAGE_VERIFY_MESSAGE );
4550 psa_set_key_algorithm( &attributes, alg );
4551 psa_set_key_type( &attributes, key_type );
4552
4553 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4554 &key ) );
4555 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4556 key_bits = psa_get_key_bits( &attributes );
4557
4558 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4559 TEST_ASSERT( signature_size != 0 );
4560 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4561 ASSERT_ALLOC( signature, signature_size );
4562
4563 PSA_ASSERT( psa_sign_message( key, alg,
4564 input_data->x, input_data->len,
4565 signature, signature_size,
4566 &signature_length ) );
4567 TEST_ASSERT( signature_length <= signature_size );
4568 TEST_ASSERT( signature_length > 0 );
4569
4570 PSA_ASSERT( psa_verify_message( key, alg,
4571 input_data->x, input_data->len,
4572 signature, signature_length ) );
4573
4574 if( input_data->len != 0 )
4575 {
4576 /* Flip a bit in the input and verify that the signature is now
4577 * detected as invalid. Flip a bit at the beginning, not at the end,
4578 * because ECDSA may ignore the last few bits of the input. */
4579 input_data->x[0] ^= 1;
4580 TEST_EQUAL( psa_verify_message( key, alg,
4581 input_data->x, input_data->len,
4582 signature, signature_length ),
4583 PSA_ERROR_INVALID_SIGNATURE );
4584 }
4585
4586exit:
4587 psa_reset_key_attributes( &attributes );
4588
4589 psa_destroy_key( key );
4590 mbedtls_free( signature );
4591 PSA_DONE( );
4592}
4593/* END_CASE */
4594
4595/* BEGIN_CASE */
4596void verify_message( int key_type_arg,
4597 data_t *key_data,
4598 int alg_arg,
4599 data_t *input_data,
4600 data_t *signature_data )
4601{
4602 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4603 psa_key_type_t key_type = key_type_arg;
4604 psa_algorithm_t alg = alg_arg;
4605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4606
4607 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4608
4609 PSA_ASSERT( psa_crypto_init( ) );
4610
4611 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4612 psa_set_key_algorithm( &attributes, alg );
4613 psa_set_key_type( &attributes, key_type );
4614
4615 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4616 &key ) );
4617
4618 PSA_ASSERT( psa_verify_message( key, alg,
4619 input_data->x, input_data->len,
4620 signature_data->x, signature_data->len ) );
4621
4622exit:
4623 psa_reset_key_attributes( &attributes );
4624 psa_destroy_key( key );
4625 PSA_DONE( );
4626}
4627/* END_CASE */
4628
4629/* BEGIN_CASE */
4630void verify_message_fail( int key_type_arg,
4631 data_t *key_data,
4632 int alg_arg,
4633 data_t *hash_data,
4634 data_t *signature_data,
4635 int expected_status_arg )
4636{
4637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4638 psa_key_type_t key_type = key_type_arg;
4639 psa_algorithm_t alg = alg_arg;
4640 psa_status_t actual_status;
4641 psa_status_t expected_status = expected_status_arg;
4642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4643
4644 PSA_ASSERT( psa_crypto_init( ) );
4645
4646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4647 psa_set_key_algorithm( &attributes, alg );
4648 psa_set_key_type( &attributes, key_type );
4649
4650 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4651 &key ) );
4652
4653 actual_status = psa_verify_message( key, alg,
4654 hash_data->x, hash_data->len,
4655 signature_data->x,
4656 signature_data->len );
4657 TEST_EQUAL( actual_status, expected_status );
4658
4659exit:
4660 psa_reset_key_attributes( &attributes );
4661 psa_destroy_key( key );
4662 PSA_DONE( );
4663}
4664/* END_CASE */
4665
4666/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004667void asymmetric_encrypt( int key_type_arg,
4668 data_t *key_data,
4669 int alg_arg,
4670 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004671 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004672 int expected_output_length_arg,
4673 int expected_status_arg )
4674{
Ronald Cron5425a212020-08-04 14:58:35 +02004675 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004676 psa_key_type_t key_type = key_type_arg;
4677 psa_algorithm_t alg = alg_arg;
4678 size_t expected_output_length = expected_output_length_arg;
4679 size_t key_bits;
4680 unsigned char *output = NULL;
4681 size_t output_size;
4682 size_t output_length = ~0;
4683 psa_status_t actual_status;
4684 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004686
Gilles Peskine8817f612018-12-18 00:18:46 +01004687 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004688
Gilles Peskine656896e2018-06-29 19:12:28 +02004689 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004690 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4691 psa_set_key_algorithm( &attributes, alg );
4692 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004693 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004694 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004695
4696 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004697 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004698 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004699
Gilles Peskine656896e2018-06-29 19:12:28 +02004700 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004701 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004702 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004703
4704 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004705 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004706 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004707 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004708 output, output_size,
4709 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004710 TEST_EQUAL( actual_status, expected_status );
4711 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004712
Gilles Peskine68428122018-06-30 18:42:41 +02004713 /* If the label is empty, the test framework puts a non-null pointer
4714 * in label->x. Test that a null pointer works as well. */
4715 if( label->len == 0 )
4716 {
4717 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004718 if( output_size != 0 )
4719 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004720 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004721 input_data->x, input_data->len,
4722 NULL, label->len,
4723 output, output_size,
4724 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004725 TEST_EQUAL( actual_status, expected_status );
4726 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004727 }
4728
Gilles Peskine656896e2018-06-29 19:12:28 +02004729exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004730 /*
4731 * Key attributes may have been returned by psa_get_key_attributes()
4732 * thus reset them as required.
4733 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004734 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004735
Ronald Cron5425a212020-08-04 14:58:35 +02004736 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004737 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004738 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004739}
4740/* END_CASE */
4741
4742/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004743void asymmetric_encrypt_decrypt( int key_type_arg,
4744 data_t *key_data,
4745 int alg_arg,
4746 data_t *input_data,
4747 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004748{
Ronald Cron5425a212020-08-04 14:58:35 +02004749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004750 psa_key_type_t key_type = key_type_arg;
4751 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004752 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004753 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004754 size_t output_size;
4755 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004756 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004757 size_t output2_size;
4758 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004760
Gilles Peskine8817f612018-12-18 00:18:46 +01004761 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004762
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004763 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4764 psa_set_key_algorithm( &attributes, alg );
4765 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004766
Gilles Peskine049c7532019-05-15 20:22:09 +02004767 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004768 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004769
4770 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004771 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004772 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004773
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004774 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004775 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004776 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004777
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004778 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004779 TEST_ASSERT( output2_size <=
4780 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4781 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004782 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004783
Gilles Peskineeebd7382018-06-08 18:11:54 +02004784 /* We test encryption by checking that encrypt-then-decrypt gives back
4785 * the original plaintext because of the non-optional random
4786 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004787 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004788 input_data->x, input_data->len,
4789 label->x, label->len,
4790 output, output_size,
4791 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004792 /* We don't know what ciphertext length to expect, but check that
4793 * it looks sensible. */
4794 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004795
Ronald Cron5425a212020-08-04 14:58:35 +02004796 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004797 output, output_length,
4798 label->x, label->len,
4799 output2, output2_size,
4800 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004801 ASSERT_COMPARE( input_data->x, input_data->len,
4802 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004803
4804exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004805 /*
4806 * Key attributes may have been returned by psa_get_key_attributes()
4807 * thus reset them as required.
4808 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004809 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004810
Ronald Cron5425a212020-08-04 14:58:35 +02004811 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004812 mbedtls_free( output );
4813 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004814 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004815}
4816/* END_CASE */
4817
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004818/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004819void asymmetric_decrypt( int key_type_arg,
4820 data_t *key_data,
4821 int alg_arg,
4822 data_t *input_data,
4823 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004824 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004825{
Ronald Cron5425a212020-08-04 14:58:35 +02004826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004827 psa_key_type_t key_type = key_type_arg;
4828 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004829 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004830 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004831 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004832 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004833 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004834
Gilles Peskine8817f612018-12-18 00:18:46 +01004835 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004836
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004837 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4838 psa_set_key_algorithm( &attributes, alg );
4839 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004840
Gilles Peskine049c7532019-05-15 20:22:09 +02004841 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004842 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004843
gabor-mezei-armceface22021-01-21 12:26:17 +01004844 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4845 key_bits = psa_get_key_bits( &attributes );
4846
4847 /* Determine the maximum ciphertext length */
4848 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4849 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4850 ASSERT_ALLOC( output, output_size );
4851
Ronald Cron5425a212020-08-04 14:58:35 +02004852 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004853 input_data->x, input_data->len,
4854 label->x, label->len,
4855 output,
4856 output_size,
4857 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004858 ASSERT_COMPARE( expected_data->x, expected_data->len,
4859 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004860
Gilles Peskine68428122018-06-30 18:42:41 +02004861 /* If the label is empty, the test framework puts a non-null pointer
4862 * in label->x. Test that a null pointer works as well. */
4863 if( label->len == 0 )
4864 {
4865 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004866 if( output_size != 0 )
4867 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004868 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004869 input_data->x, input_data->len,
4870 NULL, label->len,
4871 output,
4872 output_size,
4873 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004874 ASSERT_COMPARE( expected_data->x, expected_data->len,
4875 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004876 }
4877
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004878exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004879 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004880 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004881 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004882 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004883}
4884/* END_CASE */
4885
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004886/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004887void asymmetric_decrypt_fail( int key_type_arg,
4888 data_t *key_data,
4889 int alg_arg,
4890 data_t *input_data,
4891 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004892 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004893 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004894{
Ronald Cron5425a212020-08-04 14:58:35 +02004895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004896 psa_key_type_t key_type = key_type_arg;
4897 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004898 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004899 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004900 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004901 psa_status_t actual_status;
4902 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004904
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004905 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004906
Gilles Peskine8817f612018-12-18 00:18:46 +01004907 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004908
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4910 psa_set_key_algorithm( &attributes, alg );
4911 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004912
Gilles Peskine049c7532019-05-15 20:22:09 +02004913 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004914 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004915
Ronald Cron5425a212020-08-04 14:58:35 +02004916 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004917 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004918 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004919 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004920 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004921 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004922 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004923
Gilles Peskine68428122018-06-30 18:42:41 +02004924 /* If the label is empty, the test framework puts a non-null pointer
4925 * in label->x. Test that a null pointer works as well. */
4926 if( label->len == 0 )
4927 {
4928 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004929 if( output_size != 0 )
4930 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004931 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004932 input_data->x, input_data->len,
4933 NULL, label->len,
4934 output, output_size,
4935 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004936 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004937 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004938 }
4939
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004940exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004941 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004942 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004943 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004944 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004945}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004946/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004947
4948/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004949void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004950{
4951 /* Test each valid way of initializing the object, except for `= {0}`, as
4952 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4953 * though it's OK by the C standard. We could test for this, but we'd need
4954 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004955 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004956 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4957 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4958 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004959
4960 memset( &zero, 0, sizeof( zero ) );
4961
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004963 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004964 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004965 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004966 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004967 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004968 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004971 PSA_ASSERT( psa_key_derivation_abort(&func) );
4972 PSA_ASSERT( psa_key_derivation_abort(&init) );
4973 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004974}
4975/* END_CASE */
4976
Janos Follath16de4a42019-06-13 16:32:24 +01004977/* BEGIN_CASE */
4978void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004979{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004980 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004981 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004983
Gilles Peskine8817f612018-12-18 00:18:46 +01004984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004985
Janos Follath16de4a42019-06-13 16:32:24 +01004986 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004987 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004988
4989exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004991 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004992}
4993/* END_CASE */
4994
Janos Follathaf3c2a02019-06-12 12:34:34 +01004995/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004996void derive_set_capacity( int alg_arg, int capacity_arg,
4997 int expected_status_arg )
4998{
4999 psa_algorithm_t alg = alg_arg;
5000 size_t capacity = capacity_arg;
5001 psa_status_t expected_status = expected_status_arg;
5002 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5003
5004 PSA_ASSERT( psa_crypto_init( ) );
5005
5006 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5007
5008 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5009 expected_status );
5010
5011exit:
5012 psa_key_derivation_abort( &operation );
5013 PSA_DONE( );
5014}
5015/* END_CASE */
5016
5017/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005018void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005019 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005020 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005021 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005022 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005023 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005024 int expected_status_arg3,
5025 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005026{
5027 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005028 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5029 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005030 psa_status_t expected_statuses[] = {expected_status_arg1,
5031 expected_status_arg2,
5032 expected_status_arg3};
5033 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005034 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5035 MBEDTLS_SVC_KEY_ID_INIT,
5036 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005037 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5039 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005040 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005041 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005042 psa_status_t expected_output_status = expected_output_status_arg;
5043 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005044
5045 PSA_ASSERT( psa_crypto_init( ) );
5046
5047 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5048 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005049
5050 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5051
5052 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5053 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005054 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005055 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005056 psa_set_key_type( &attributes, key_types[i] );
5057 PSA_ASSERT( psa_import_key( &attributes,
5058 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005059 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005060 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5061 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5062 {
5063 // When taking a private key as secret input, use key agreement
5064 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005065 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5066 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005067 expected_statuses[i] );
5068 }
5069 else
5070 {
5071 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005072 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005073 expected_statuses[i] );
5074 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005075 }
5076 else
5077 {
5078 TEST_EQUAL( psa_key_derivation_input_bytes(
5079 &operation, steps[i],
5080 inputs[i]->x, inputs[i]->len ),
5081 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005082 }
5083 }
5084
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005085 if( output_key_type != PSA_KEY_TYPE_NONE )
5086 {
5087 psa_reset_key_attributes( &attributes );
5088 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5089 psa_set_key_bits( &attributes, 8 );
5090 actual_output_status =
5091 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005092 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005093 }
5094 else
5095 {
5096 uint8_t buffer[1];
5097 actual_output_status =
5098 psa_key_derivation_output_bytes( &operation,
5099 buffer, sizeof( buffer ) );
5100 }
5101 TEST_EQUAL( actual_output_status, expected_output_status );
5102
Janos Follathaf3c2a02019-06-12 12:34:34 +01005103exit:
5104 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005105 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5106 psa_destroy_key( keys[i] );
5107 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005108 PSA_DONE( );
5109}
5110/* END_CASE */
5111
Janos Follathd958bb72019-07-03 15:02:16 +01005112/* BEGIN_CASE */
5113void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005114{
Janos Follathd958bb72019-07-03 15:02:16 +01005115 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005116 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005117 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005119 unsigned char input1[] = "Input 1";
5120 size_t input1_length = sizeof( input1 );
5121 unsigned char input2[] = "Input 2";
5122 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005123 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005124 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005125 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5126 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5127 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005129
Gilles Peskine8817f612018-12-18 00:18:46 +01005130 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005131
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005132 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5133 psa_set_key_algorithm( &attributes, alg );
5134 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005135
Gilles Peskine73676cb2019-05-15 20:15:10 +02005136 PSA_ASSERT( psa_import_key( &attributes,
5137 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005138 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005139
5140 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005141 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5142 input1, input1_length,
5143 input2, input2_length,
5144 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005145 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005146
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005148 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005149 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005153 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005154 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005155
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005156exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005158 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005159 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005160}
5161/* END_CASE */
5162
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005163/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005164void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005165{
5166 uint8_t output_buffer[16];
5167 size_t buffer_size = 16;
5168 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005170
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005171 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5172 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005173 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005174
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005176 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005177
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005179
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005180 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5181 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005182 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005183
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005185 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005186
5187exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005188 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005189}
5190/* END_CASE */
5191
5192/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005193void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005194 int step1_arg, data_t *input1,
5195 int step2_arg, data_t *input2,
5196 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005197 int requested_capacity_arg,
5198 data_t *expected_output1,
5199 data_t *expected_output2 )
5200{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005201 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005202 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5203 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005204 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5205 MBEDTLS_SVC_KEY_ID_INIT,
5206 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005207 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005209 uint8_t *expected_outputs[2] =
5210 {expected_output1->x, expected_output2->x};
5211 size_t output_sizes[2] =
5212 {expected_output1->len, expected_output2->len};
5213 size_t output_buffer_size = 0;
5214 uint8_t *output_buffer = NULL;
5215 size_t expected_capacity;
5216 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005218 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005219 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005220
5221 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5222 {
5223 if( output_sizes[i] > output_buffer_size )
5224 output_buffer_size = output_sizes[i];
5225 if( output_sizes[i] == 0 )
5226 expected_outputs[i] = NULL;
5227 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005228 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005230
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005231 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5232 psa_set_key_algorithm( &attributes, alg );
5233 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005234
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005235 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005236 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5237 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5238 requested_capacity ) );
5239 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005240 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005241 switch( steps[i] )
5242 {
5243 case 0:
5244 break;
5245 case PSA_KEY_DERIVATION_INPUT_SECRET:
5246 PSA_ASSERT( psa_import_key( &attributes,
5247 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005248 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005249
5250 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5251 {
5252 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5253 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5254 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5255 }
5256
Gilles Peskine1468da72019-05-29 17:35:49 +02005257 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005258 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005259 break;
5260 default:
5261 PSA_ASSERT( psa_key_derivation_input_bytes(
5262 &operation, steps[i],
5263 inputs[i]->x, inputs[i]->len ) );
5264 break;
5265 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005266 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005267
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005268 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005269 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005270 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005271 expected_capacity = requested_capacity;
5272
5273 /* Expansion phase. */
5274 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5275 {
5276 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005278 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005279 if( expected_capacity == 0 && output_sizes[i] == 0 )
5280 {
5281 /* Reading 0 bytes when 0 bytes are available can go either way. */
5282 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005283 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005284 continue;
5285 }
5286 else if( expected_capacity == 0 ||
5287 output_sizes[i] > expected_capacity )
5288 {
5289 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005290 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005291 expected_capacity = 0;
5292 continue;
5293 }
5294 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005295 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005296 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005297 ASSERT_COMPARE( output_buffer, output_sizes[i],
5298 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005300 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005302 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005303 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005304 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005305 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005306
5307exit:
5308 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005310 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5311 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005312 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005313}
5314/* END_CASE */
5315
5316/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005317void derive_full( int alg_arg,
5318 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005319 data_t *input1,
5320 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005321 int requested_capacity_arg )
5322{
Ronald Cron5425a212020-08-04 14:58:35 +02005323 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005324 psa_algorithm_t alg = alg_arg;
5325 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005326 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005327 unsigned char output_buffer[16];
5328 size_t expected_capacity = requested_capacity;
5329 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005330 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005331
Gilles Peskine8817f612018-12-18 00:18:46 +01005332 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005333
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005334 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5335 psa_set_key_algorithm( &attributes, alg );
5336 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005337
Gilles Peskine049c7532019-05-15 20:22:09 +02005338 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005339 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005340
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005341 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5342 input1->x, input1->len,
5343 input2->x, input2->len,
5344 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005345 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005346
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005348 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005349 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005350
5351 /* Expansion phase. */
5352 while( current_capacity > 0 )
5353 {
5354 size_t read_size = sizeof( output_buffer );
5355 if( read_size > current_capacity )
5356 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005358 output_buffer,
5359 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005360 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005361 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005362 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005363 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005364 }
5365
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366 /* Check that the operation refuses to go over capacity. */
5367 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005368 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005369
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005370 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005371
5372exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005373 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005374 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005375 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005376}
5377/* END_CASE */
5378
Janos Follathe60c9052019-07-03 13:51:30 +01005379/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005380void derive_key_exercise( int alg_arg,
5381 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005382 data_t *input1,
5383 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005384 int derived_type_arg,
5385 int derived_bits_arg,
5386 int derived_usage_arg,
5387 int derived_alg_arg )
5388{
Ronald Cron5425a212020-08-04 14:58:35 +02005389 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5390 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005391 psa_algorithm_t alg = alg_arg;
5392 psa_key_type_t derived_type = derived_type_arg;
5393 size_t derived_bits = derived_bits_arg;
5394 psa_key_usage_t derived_usage = derived_usage_arg;
5395 psa_algorithm_t derived_alg = derived_alg_arg;
5396 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005399 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005400
Gilles Peskine8817f612018-12-18 00:18:46 +01005401 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005402
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005403 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5404 psa_set_key_algorithm( &attributes, alg );
5405 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005406 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005407 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005408
5409 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005410 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5411 input1->x, input1->len,
5412 input2->x, input2->len,
5413 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005414 goto exit;
5415
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005416 psa_set_key_usage_flags( &attributes, derived_usage );
5417 psa_set_key_algorithm( &attributes, derived_alg );
5418 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005419 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005421 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005422
5423 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005424 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005425 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5426 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005427
5428 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005429 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005430 goto exit;
5431
5432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005433 /*
5434 * Key attributes may have been returned by psa_get_key_attributes()
5435 * thus reset them as required.
5436 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005437 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005438
5439 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005440 psa_destroy_key( base_key );
5441 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005442 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005443}
5444/* END_CASE */
5445
Janos Follath42fd8882019-07-03 14:17:09 +01005446/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005447void derive_key_export( int alg_arg,
5448 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005449 data_t *input1,
5450 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005451 int bytes1_arg,
5452 int bytes2_arg )
5453{
Ronald Cron5425a212020-08-04 14:58:35 +02005454 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5455 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005456 psa_algorithm_t alg = alg_arg;
5457 size_t bytes1 = bytes1_arg;
5458 size_t bytes2 = bytes2_arg;
5459 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005460 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005461 uint8_t *output_buffer = NULL;
5462 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005463 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5464 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005465 size_t length;
5466
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005467 ASSERT_ALLOC( output_buffer, capacity );
5468 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005470
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005471 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5472 psa_set_key_algorithm( &base_attributes, alg );
5473 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005474 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005475 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005476
5477 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005478 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5479 input1->x, input1->len,
5480 input2->x, input2->len,
5481 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005482 goto exit;
5483
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005485 output_buffer,
5486 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005487 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005488
5489 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005490 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5491 input1->x, input1->len,
5492 input2->x, input2->len,
5493 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005494 goto exit;
5495
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005496 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5497 psa_set_key_algorithm( &derived_attributes, 0 );
5498 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005499 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005501 &derived_key ) );
5502 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005503 export_buffer, bytes1,
5504 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005505 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005506 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005507 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005509 &derived_key ) );
5510 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005511 export_buffer + bytes1, bytes2,
5512 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005513 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005514
5515 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005516 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5517 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005518
5519exit:
5520 mbedtls_free( output_buffer );
5521 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005523 psa_destroy_key( base_key );
5524 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005525 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005526}
5527/* END_CASE */
5528
5529/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005530void derive_key( int alg_arg,
5531 data_t *key_data, data_t *input1, data_t *input2,
5532 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005533 int expected_status_arg,
5534 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005535{
Ronald Cron5425a212020-08-04 14:58:35 +02005536 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5537 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005538 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005539 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005540 size_t bits = bits_arg;
5541 psa_status_t expected_status = expected_status_arg;
5542 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5543 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5544 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5545
5546 PSA_ASSERT( psa_crypto_init( ) );
5547
5548 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5549 psa_set_key_algorithm( &base_attributes, alg );
5550 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5551 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005552 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005553
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005554 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5555 input1->x, input1->len,
5556 input2->x, input2->len,
5557 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005558 goto exit;
5559
5560 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5561 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005562 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005563 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005564
5565 psa_status_t status =
5566 psa_key_derivation_output_key( &derived_attributes,
5567 &operation,
5568 &derived_key );
5569 if( is_large_output > 0 )
5570 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5571 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005572
5573exit:
5574 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005575 psa_destroy_key( base_key );
5576 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005577 PSA_DONE( );
5578}
5579/* END_CASE */
5580
5581/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005582void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005583 int our_key_type_arg, int our_key_alg_arg,
5584 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005585 int expected_status_arg )
5586{
Ronald Cron5425a212020-08-04 14:58:35 +02005587 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005588 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005589 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005590 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005593 psa_status_t expected_status = expected_status_arg;
5594 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005595
Gilles Peskine8817f612018-12-18 00:18:46 +01005596 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005597
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005598 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005599 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005600 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005601 PSA_ASSERT( psa_import_key( &attributes,
5602 our_key_data->x, our_key_data->len,
5603 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005604
Gilles Peskine77f40d82019-04-11 21:27:06 +02005605 /* The tests currently include inputs that should fail at either step.
5606 * Test cases that fail at the setup step should be changed to call
5607 * key_derivation_setup instead, and this function should be renamed
5608 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005609 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005610 if( status == PSA_SUCCESS )
5611 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005612 TEST_EQUAL( psa_key_derivation_key_agreement(
5613 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5614 our_key,
5615 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005616 expected_status );
5617 }
5618 else
5619 {
5620 TEST_ASSERT( status == expected_status );
5621 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005622
5623exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005624 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005625 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005626 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005627}
5628/* END_CASE */
5629
5630/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005631void raw_key_agreement( int alg_arg,
5632 int our_key_type_arg, data_t *our_key_data,
5633 data_t *peer_key_data,
5634 data_t *expected_output )
5635{
Ronald Cron5425a212020-08-04 14:58:35 +02005636 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005637 psa_algorithm_t alg = alg_arg;
5638 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005640 unsigned char *output = NULL;
5641 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005642 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005643
5644 ASSERT_ALLOC( output, expected_output->len );
5645 PSA_ASSERT( psa_crypto_init( ) );
5646
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5648 psa_set_key_algorithm( &attributes, alg );
5649 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005650 PSA_ASSERT( psa_import_key( &attributes,
5651 our_key_data->x, our_key_data->len,
5652 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005653
gabor-mezei-armceface22021-01-21 12:26:17 +01005654 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5655 key_bits = psa_get_key_bits( &attributes );
5656
Gilles Peskinebe697d82019-05-16 18:00:41 +02005657 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5658 peer_key_data->x, peer_key_data->len,
5659 output, expected_output->len,
5660 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005661 ASSERT_COMPARE( output, output_length,
5662 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005663 TEST_ASSERT( output_length <=
5664 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5665 TEST_ASSERT( output_length <=
5666 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005667
5668exit:
5669 mbedtls_free( output );
5670 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005671 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005672}
5673/* END_CASE */
5674
5675/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005676void key_agreement_capacity( int alg_arg,
5677 int our_key_type_arg, data_t *our_key_data,
5678 data_t *peer_key_data,
5679 int expected_capacity_arg )
5680{
Ronald Cron5425a212020-08-04 14:58:35 +02005681 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005682 psa_algorithm_t alg = alg_arg;
5683 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005684 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005686 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005687 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005688
Gilles Peskine8817f612018-12-18 00:18:46 +01005689 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005690
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005691 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5692 psa_set_key_algorithm( &attributes, alg );
5693 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005694 PSA_ASSERT( psa_import_key( &attributes,
5695 our_key_data->x, our_key_data->len,
5696 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005697
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005698 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005699 PSA_ASSERT( psa_key_derivation_key_agreement(
5700 &operation,
5701 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5702 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005703 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5704 {
5705 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005706 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005707 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005708 NULL, 0 ) );
5709 }
Gilles Peskine59685592018-09-18 12:11:34 +02005710
Gilles Peskinebf491972018-10-25 22:36:12 +02005711 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005712 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005713 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005714 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005715
Gilles Peskinebf491972018-10-25 22:36:12 +02005716 /* Test the actual capacity by reading the output. */
5717 while( actual_capacity > sizeof( output ) )
5718 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005719 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005720 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005721 actual_capacity -= sizeof( output );
5722 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005724 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005725 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005726 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005727
Gilles Peskine59685592018-09-18 12:11:34 +02005728exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005729 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005730 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005731 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005732}
5733/* END_CASE */
5734
5735/* BEGIN_CASE */
5736void key_agreement_output( int alg_arg,
5737 int our_key_type_arg, data_t *our_key_data,
5738 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005739 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005740{
Ronald Cron5425a212020-08-04 14:58:35 +02005741 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005742 psa_algorithm_t alg = alg_arg;
5743 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005746 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005747
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005748 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5749 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005750
Gilles Peskine8817f612018-12-18 00:18:46 +01005751 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005752
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005753 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5754 psa_set_key_algorithm( &attributes, alg );
5755 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005756 PSA_ASSERT( psa_import_key( &attributes,
5757 our_key_data->x, our_key_data->len,
5758 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005759
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005761 PSA_ASSERT( psa_key_derivation_key_agreement(
5762 &operation,
5763 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5764 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005765 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5766 {
5767 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005769 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005770 NULL, 0 ) );
5771 }
Gilles Peskine59685592018-09-18 12:11:34 +02005772
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005773 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005774 actual_output,
5775 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005776 ASSERT_COMPARE( actual_output, expected_output1->len,
5777 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005778 if( expected_output2->len != 0 )
5779 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005780 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005781 actual_output,
5782 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005783 ASSERT_COMPARE( actual_output, expected_output2->len,
5784 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005785 }
Gilles Peskine59685592018-09-18 12:11:34 +02005786
5787exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005788 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005789 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005790 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005791 mbedtls_free( actual_output );
5792}
5793/* END_CASE */
5794
5795/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005796void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005797{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005798 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005799 unsigned char *output = NULL;
5800 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005801 size_t i;
5802 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005803
Simon Butcher49f8e312020-03-03 15:51:50 +00005804 TEST_ASSERT( bytes_arg >= 0 );
5805
Gilles Peskine91892022021-02-08 19:50:26 +01005806 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005807 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005808
Gilles Peskine8817f612018-12-18 00:18:46 +01005809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005810
Gilles Peskinea50d7392018-06-21 10:22:13 +02005811 /* Run several times, to ensure that every output byte will be
5812 * nonzero at least once with overwhelming probability
5813 * (2^(-8*number_of_runs)). */
5814 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005815 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005816 if( bytes != 0 )
5817 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005818 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005819
Gilles Peskinea50d7392018-06-21 10:22:13 +02005820 for( i = 0; i < bytes; i++ )
5821 {
5822 if( output[i] != 0 )
5823 ++changed[i];
5824 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005825 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005826
5827 /* Check that every byte was changed to nonzero at least once. This
5828 * validates that psa_generate_random is overwriting every byte of
5829 * the output buffer. */
5830 for( i = 0; i < bytes; i++ )
5831 {
5832 TEST_ASSERT( changed[i] != 0 );
5833 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005834
5835exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005836 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005837 mbedtls_free( output );
5838 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005839}
5840/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005841
5842/* BEGIN_CASE */
5843void generate_key( int type_arg,
5844 int bits_arg,
5845 int usage_arg,
5846 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005847 int expected_status_arg,
5848 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005849{
Ronald Cron5425a212020-08-04 14:58:35 +02005850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005851 psa_key_type_t type = type_arg;
5852 psa_key_usage_t usage = usage_arg;
5853 size_t bits = bits_arg;
5854 psa_algorithm_t alg = alg_arg;
5855 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005857 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005858
Gilles Peskine8817f612018-12-18 00:18:46 +01005859 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005860
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005861 psa_set_key_usage_flags( &attributes, usage );
5862 psa_set_key_algorithm( &attributes, alg );
5863 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005864 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005865
5866 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005867 psa_status_t status = psa_generate_key( &attributes, &key );
5868
5869 if( is_large_key > 0 )
5870 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5871 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005872 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005873 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005874
5875 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005876 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005877 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5878 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005879
Gilles Peskine818ca122018-06-20 18:16:48 +02005880 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005881 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005882 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005883
5884exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005885 /*
5886 * Key attributes may have been returned by psa_get_key_attributes()
5887 * thus reset them as required.
5888 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005889 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005890
Ronald Cron5425a212020-08-04 14:58:35 +02005891 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005892 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005893}
5894/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005895
Ronald Cronee414c72021-03-18 18:50:08 +01005896/* 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 +02005897void generate_key_rsa( int bits_arg,
5898 data_t *e_arg,
5899 int expected_status_arg )
5900{
Ronald Cron5425a212020-08-04 14:58:35 +02005901 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005902 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005903 size_t bits = bits_arg;
5904 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5905 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5906 psa_status_t expected_status = expected_status_arg;
5907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5908 uint8_t *exported = NULL;
5909 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005910 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005911 size_t exported_length = SIZE_MAX;
5912 uint8_t *e_read_buffer = NULL;
5913 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005914 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005915 size_t e_read_length = SIZE_MAX;
5916
5917 if( e_arg->len == 0 ||
5918 ( e_arg->len == 3 &&
5919 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5920 {
5921 is_default_public_exponent = 1;
5922 e_read_size = 0;
5923 }
5924 ASSERT_ALLOC( e_read_buffer, e_read_size );
5925 ASSERT_ALLOC( exported, exported_size );
5926
5927 PSA_ASSERT( psa_crypto_init( ) );
5928
5929 psa_set_key_usage_flags( &attributes, usage );
5930 psa_set_key_algorithm( &attributes, alg );
5931 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5932 e_arg->x, e_arg->len ) );
5933 psa_set_key_bits( &attributes, bits );
5934
5935 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005936 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005937 if( expected_status != PSA_SUCCESS )
5938 goto exit;
5939
5940 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005941 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005942 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5943 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5944 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5945 e_read_buffer, e_read_size,
5946 &e_read_length ) );
5947 if( is_default_public_exponent )
5948 TEST_EQUAL( e_read_length, 0 );
5949 else
5950 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5951
5952 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005953 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005954 goto exit;
5955
5956 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005957 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005958 exported, exported_size,
5959 &exported_length ) );
5960 {
5961 uint8_t *p = exported;
5962 uint8_t *end = exported + exported_length;
5963 size_t len;
5964 /* RSAPublicKey ::= SEQUENCE {
5965 * modulus INTEGER, -- n
5966 * publicExponent INTEGER } -- e
5967 */
5968 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005969 MBEDTLS_ASN1_SEQUENCE |
5970 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005971 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005972 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5973 MBEDTLS_ASN1_INTEGER ) );
5974 if( len >= 1 && p[0] == 0 )
5975 {
5976 ++p;
5977 --len;
5978 }
5979 if( e_arg->len == 0 )
5980 {
5981 TEST_EQUAL( len, 3 );
5982 TEST_EQUAL( p[0], 1 );
5983 TEST_EQUAL( p[1], 0 );
5984 TEST_EQUAL( p[2], 1 );
5985 }
5986 else
5987 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5988 }
5989
5990exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005991 /*
5992 * Key attributes may have been returned by psa_get_key_attributes() or
5993 * set by psa_set_key_domain_parameters() thus reset them as required.
5994 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005995 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005996
Ronald Cron5425a212020-08-04 14:58:35 +02005997 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005998 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005999 mbedtls_free( e_read_buffer );
6000 mbedtls_free( exported );
6001}
6002/* END_CASE */
6003
Darryl Greend49a4992018-06-18 17:27:26 +01006004/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006005void persistent_key_load_key_from_storage( data_t *data,
6006 int type_arg, int bits_arg,
6007 int usage_flags_arg, int alg_arg,
6008 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006009{
Ronald Cron71016a92020-08-28 19:01:50 +02006010 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6013 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006014 psa_key_type_t type = type_arg;
6015 size_t bits = bits_arg;
6016 psa_key_usage_t usage_flags = usage_flags_arg;
6017 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006018 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006019 unsigned char *first_export = NULL;
6020 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006021 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006022 size_t first_exported_length;
6023 size_t second_exported_length;
6024
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006025 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6026 {
6027 ASSERT_ALLOC( first_export, export_size );
6028 ASSERT_ALLOC( second_export, export_size );
6029 }
Darryl Greend49a4992018-06-18 17:27:26 +01006030
Gilles Peskine8817f612018-12-18 00:18:46 +01006031 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006032
Gilles Peskinec87af662019-05-15 16:12:22 +02006033 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006034 psa_set_key_usage_flags( &attributes, usage_flags );
6035 psa_set_key_algorithm( &attributes, alg );
6036 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006037 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006038
Darryl Green0c6575a2018-11-07 16:05:30 +00006039 switch( generation_method )
6040 {
6041 case IMPORT_KEY:
6042 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006043 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006044 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006045 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006046
Darryl Green0c6575a2018-11-07 16:05:30 +00006047 case GENERATE_KEY:
6048 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006049 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006050 break;
6051
6052 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006053#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006054 {
6055 /* Create base key */
6056 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6057 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6058 psa_set_key_usage_flags( &base_attributes,
6059 PSA_KEY_USAGE_DERIVE );
6060 psa_set_key_algorithm( &base_attributes, derive_alg );
6061 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006062 PSA_ASSERT( psa_import_key( &base_attributes,
6063 data->x, data->len,
6064 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006065 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006066 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006067 PSA_ASSERT( psa_key_derivation_input_key(
6068 &operation,
6069 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006070 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006071 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006072 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006073 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6074 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006075 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006076 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006077 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006078 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006079 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006080#else
6081 TEST_ASSUME( ! "KDF not supported in this configuration" );
6082#endif
6083 break;
6084
6085 default:
6086 TEST_ASSERT( ! "generation_method not implemented in test" );
6087 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006088 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006089 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006090
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006091 /* Export the key if permitted by the key policy. */
6092 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6093 {
Ronald Cron5425a212020-08-04 14:58:35 +02006094 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006095 first_export, export_size,
6096 &first_exported_length ) );
6097 if( generation_method == IMPORT_KEY )
6098 ASSERT_COMPARE( data->x, data->len,
6099 first_export, first_exported_length );
6100 }
Darryl Greend49a4992018-06-18 17:27:26 +01006101
6102 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006103 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006104 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006105 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006106
Darryl Greend49a4992018-06-18 17:27:26 +01006107 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006108 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006109 TEST_ASSERT( mbedtls_svc_key_id_equal(
6110 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006111 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6112 PSA_KEY_LIFETIME_PERSISTENT );
6113 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6114 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6115 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6116 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006117
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006118 /* Export the key again if permitted by the key policy. */
6119 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006120 {
Ronald Cron5425a212020-08-04 14:58:35 +02006121 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006122 second_export, export_size,
6123 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006124 ASSERT_COMPARE( first_export, first_exported_length,
6125 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006126 }
6127
6128 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006129 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006130 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006131
6132exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006133 /*
6134 * Key attributes may have been returned by psa_get_key_attributes()
6135 * thus reset them as required.
6136 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006137 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006138
Darryl Greend49a4992018-06-18 17:27:26 +01006139 mbedtls_free( first_export );
6140 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006141 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006142 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006143 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006144 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006145}
6146/* END_CASE */