blob: c5567406a7464f768b86de270607301ed3951b15 [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,
301 int is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100302{
303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
304 psa_key_type_t key_type = key_type_arg;
305 psa_algorithm_t alg = alg_arg;
306 psa_aead_operation_t operation;
307 unsigned char *output_data = NULL;
308 unsigned char *part_data = NULL;
309 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100310 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100311 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 size_t output_size = 0;
313 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100314 size_t output_length = 0;
315 size_t key_bits = 0;
316 size_t tag_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100317 uint32_t part_offset = 0;
318 size_t part_length = 0;
319 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100320 size_t tag_size = 0;
321 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
323 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
324
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 int test_ok = 0;
326
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 PSA_ASSERT( psa_crypto_init( ) );
328
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100329 if( is_encrypt )
330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
331 else
332 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
333
Paul Elliottd3f82412021-06-16 16:52:21 +0100334 psa_set_key_algorithm( &attributes, alg );
335 psa_set_key_type( &attributes, key_type );
336
337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
338 &key ) );
339
340 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
341 key_bits = psa_get_key_bits( &attributes );
342
343 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 {
347 /* Tag gets written at end of buffer. */
348 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
349 ( input_data->len +
350 tag_length ) );
351 data_true_size = input_data->len;
352 }
353 else
354 {
355 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
356 ( input_data->len -
357 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100358
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100359 /* Do not want to attempt to decrypt tag. */
360 data_true_size = input_data->len - tag_length;
361 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100362
363 ASSERT_ALLOC( output_data, output_size );
364
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100365 if( is_encrypt )
366 {
367 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
368 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
369 }
370 else
371 {
372 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
373 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
374 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100377
378 operation = psa_aead_operation_init( );
379
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380
381 if( is_encrypt )
382 status = psa_aead_encrypt_setup( &operation, key, alg );
383 else
384 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100385
386 /* If the operation is not supported, just skip and not fail in case the
387 * encryption involves a common limitation of cryptography hardwares and
388 * an alternative implementation. */
389 if( status == PSA_ERROR_NOT_SUPPORTED )
390 {
391 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
392 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
393 }
394
395 PSA_ASSERT( status );
396
397 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
398
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100399 if( do_set_lengths )
Paul Elliottd3f82412021-06-16 16:52:21 +0100400 {
401 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100402 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100403 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100404
405 if( ad_part_len != -1 )
406 {
407 /* Pass additional data in parts */
408 part_offset = 0;
409
410 while( part_offset < additional_data->len )
411 {
412 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
413 {
414 part_length = additional_data->len - part_offset;
415 }
416 else
417 {
418 part_length = ad_part_len;
419 }
420
421 PSA_ASSERT( psa_aead_update_ad( &operation,
422 additional_data->x + part_offset,
423 part_length ) );
424
425 part_offset += part_length;
426 }
427 }
428 else
429 {
430 /* Pass additional data in one go. */
431 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
432 additional_data->len ) );
433 }
434
435 if( data_part_len != -1 )
436 {
437 /* Pass data in parts */
438 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100439 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
441 ASSERT_ALLOC( part_data, part_data_size );
442
443 part_offset = 0;
444
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100445 while( part_offset < data_true_size )
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100447 if( ( data_true_size - part_offset ) < ( uint32_t ) data_part_len )
Paul Elliottd3f82412021-06-16 16:52:21 +0100448 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100449 part_length = ( data_true_size - part_offset );
Paul Elliottd3f82412021-06-16 16:52:21 +0100450 }
451 else
452 {
453 part_length = data_part_len;
454 }
455
456 PSA_ASSERT( psa_aead_update( &operation,
457 ( input_data->x + part_offset ),
458 part_length, part_data,
459 part_data_size,
460 &output_part_length ) );
461
462 if( output_data && output_part_length )
463 {
464 memcpy( ( output_data + part_offset ), part_data,
465 output_part_length );
466 }
467
468 part_offset += part_length;
469 output_length += output_part_length;
470 }
471 }
472 else
473 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100474 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100476 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 output_size, &output_length ) );
478 }
479
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100480 if( is_encrypt )
481 PSA_ASSERT( psa_aead_finish( &operation, final_data,
482 final_output_size,
483 &output_part_length,
484 tag_buffer, tag_length,
485 &tag_size ) );
486 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100487 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100488 status = psa_aead_verify( &operation, final_data,
489 final_output_size,
490 &output_part_length,
491 ( input_data->x + data_true_size ),
492 tag_length );
493
494 if( status != PSA_SUCCESS )
495 {
496 if( !expect_valid_signature )
497 {
498 /* Expected failure. */
499 test_ok = 1;
500 goto exit;
501 }
502 else
503 PSA_ASSERT( status );
504 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100505 }
506
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 if( output_data && output_part_length )
508 memcpy( ( output_data + output_length ), final_data,
509 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100510
511 output_length += output_part_length;
512
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513
514 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
515 * should be exact.*/
516 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100517 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100518 TEST_EQUAL( tag_length, tag_size );
519
520 if( output_data && tag_length )
521 memcpy( ( output_data + output_length ), tag_buffer,
522 tag_length );
523
524 output_length += tag_length;
525
526 TEST_EQUAL( output_length,
527 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
528 input_data->len ) );
529 TEST_ASSERT( output_length <=
530 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
531 }
532 else
533 {
534 TEST_EQUAL( output_length,
535 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
536 input_data->len ) );
537 TEST_ASSERT( output_length <=
538 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100539 }
540
Paul Elliottd3f82412021-06-16 16:52:21 +0100541
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100542 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100543 output_data, output_length );
544
Paul Elliottd3f82412021-06-16 16:52:21 +0100545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100547
548exit:
549 psa_destroy_key( key );
550 psa_aead_abort( &operation );
551 mbedtls_free( output_data );
552 mbedtls_free( part_data );
553 mbedtls_free( final_data );
554 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100555
556 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100557}
558
Gilles Peskinee59236f2018-01-27 23:32:46 +0100559/* END_HEADER */
560
561/* BEGIN_DEPENDENCIES
562 * depends_on:MBEDTLS_PSA_CRYPTO_C
563 * END_DEPENDENCIES
564 */
565
566/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200567void static_checks( )
568{
569 size_t max_truncated_mac_size =
570 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
571
572 /* Check that the length for a truncated MAC always fits in the algorithm
573 * encoding. The shifted mask is the maximum truncated value. The
574 * untruncated algorithm may be one byte larger. */
575 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
576}
577/* END_CASE */
578
579/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200580void import_with_policy( int type_arg,
581 int usage_arg, int alg_arg,
582 int expected_status_arg )
583{
584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
585 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200587 psa_key_type_t type = type_arg;
588 psa_key_usage_t usage = usage_arg;
589 psa_algorithm_t alg = alg_arg;
590 psa_status_t expected_status = expected_status_arg;
591 const uint8_t key_material[16] = {0};
592 psa_status_t status;
593
594 PSA_ASSERT( psa_crypto_init( ) );
595
596 psa_set_key_type( &attributes, type );
597 psa_set_key_usage_flags( &attributes, usage );
598 psa_set_key_algorithm( &attributes, alg );
599
600 status = psa_import_key( &attributes,
601 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200602 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200603 TEST_EQUAL( status, expected_status );
604 if( status != PSA_SUCCESS )
605 goto exit;
606
Ronald Cron5425a212020-08-04 14:58:35 +0200607 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200608 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
609 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
610 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200611 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200612
Ronald Cron5425a212020-08-04 14:58:35 +0200613 PSA_ASSERT( psa_destroy_key( key ) );
614 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200615
616exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100617 /*
618 * Key attributes may have been returned by psa_get_key_attributes()
619 * thus reset them as required.
620 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200621 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100622
623 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200624 PSA_DONE( );
625}
626/* END_CASE */
627
628/* BEGIN_CASE */
629void import_with_data( data_t *data, int type_arg,
630 int attr_bits_arg,
631 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200632{
633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
634 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200636 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200637 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200638 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640
Gilles Peskine8817f612018-12-18 00:18:46 +0100641 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642
Gilles Peskine4747d192019-04-17 15:05:45 +0200643 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200644 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200645
Ronald Cron5425a212020-08-04 14:58:35 +0200646 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100647 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200648 if( status != PSA_SUCCESS )
649 goto exit;
650
Ronald Cron5425a212020-08-04 14:58:35 +0200651 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200652 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200653 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200654 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200655 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200656
Ronald Cron5425a212020-08-04 14:58:35 +0200657 PSA_ASSERT( psa_destroy_key( key ) );
658 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659
660exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100661 /*
662 * Key attributes may have been returned by psa_get_key_attributes()
663 * thus reset them as required.
664 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200665 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100666
667 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200668 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669}
670/* END_CASE */
671
672/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200673void import_large_key( int type_arg, int byte_size_arg,
674 int expected_status_arg )
675{
676 psa_key_type_t type = type_arg;
677 size_t byte_size = byte_size_arg;
678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
679 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200681 psa_status_t status;
682 uint8_t *buffer = NULL;
683 size_t buffer_size = byte_size + 1;
684 size_t n;
685
Steven Cooreman69967ce2021-01-18 18:01:08 +0100686 /* Skip the test case if the target running the test cannot
687 * accomodate large keys due to heap size constraints */
688 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200689 memset( buffer, 'K', byte_size );
690
691 PSA_ASSERT( psa_crypto_init( ) );
692
693 /* Try importing the key */
694 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
695 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200696 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100697 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200698 TEST_EQUAL( status, expected_status );
699
700 if( status == PSA_SUCCESS )
701 {
Ronald Cron5425a212020-08-04 14:58:35 +0200702 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200703 TEST_EQUAL( psa_get_key_type( &attributes ), type );
704 TEST_EQUAL( psa_get_key_bits( &attributes ),
705 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200706 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200707 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200708 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200709 for( n = 0; n < byte_size; n++ )
710 TEST_EQUAL( buffer[n], 'K' );
711 for( n = byte_size; n < buffer_size; n++ )
712 TEST_EQUAL( buffer[n], 0 );
713 }
714
715exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100716 /*
717 * Key attributes may have been returned by psa_get_key_attributes()
718 * thus reset them as required.
719 */
720 psa_reset_key_attributes( &attributes );
721
Ronald Cron5425a212020-08-04 14:58:35 +0200722 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 PSA_DONE( );
724 mbedtls_free( buffer );
725}
726/* END_CASE */
727
728/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200729void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
730{
Ronald Cron5425a212020-08-04 14:58:35 +0200731 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200732 size_t bits = bits_arg;
733 psa_status_t expected_status = expected_status_arg;
734 psa_status_t status;
735 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200736 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200737 size_t buffer_size = /* Slight overapproximations */
738 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200739 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200740 unsigned char *p;
741 int ret;
742 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200744
Gilles Peskine8817f612018-12-18 00:18:46 +0100745 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200746 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200747
748 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
749 bits, keypair ) ) >= 0 );
750 length = ret;
751
752 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200753 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200754 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100755 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200756
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200757 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200758 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200759
760exit:
761 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200762 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763}
764/* END_CASE */
765
766/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300767void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300768 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200769 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 int expected_bits,
771 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200772 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100773 int canonical_input )
774{
Ronald Cron5425a212020-08-04 14:58:35 +0200775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200777 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200778 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100780 unsigned char *exported = NULL;
781 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100783 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100784 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200786 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787
Moran Pekercb088e72018-07-17 17:36:59 +0300788 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200789 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200791 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100792 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793
Gilles Peskine4747d192019-04-17 15:05:45 +0200794 psa_set_key_usage_flags( &attributes, usage_arg );
795 psa_set_key_algorithm( &attributes, alg );
796 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700797
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200799 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800
801 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200802 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200803 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
804 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200805 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806
807 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200808 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100809 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100810
811 /* The exported length must be set by psa_export_key() to a value between 0
812 * and export_size. On errors, the exported length must be 0. */
813 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
814 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
815 TEST_ASSERT( exported_length <= export_size );
816
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200817 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200818 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200820 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100821 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100822 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200823 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824
Gilles Peskineea38a922021-02-13 00:05:16 +0100825 /* Run sanity checks on the exported key. For non-canonical inputs,
826 * this validates the canonical representations. For canonical inputs,
827 * this doesn't directly validate the implementation, but it still helps
828 * by cross-validating the test data with the sanity check code. */
829 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200830 goto exit;
831
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200833 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100834 else
835 {
Ronald Cron5425a212020-08-04 14:58:35 +0200836 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200837 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200838 &key2 ) );
839 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100840 reexported,
841 export_size,
842 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200843 ASSERT_COMPARE( exported, exported_length,
844 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200845 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100846 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100847 TEST_ASSERT( exported_length <=
848 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
849 psa_get_key_bits( &got_attributes ) ) );
850 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100851
852destroy:
853 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200854 PSA_ASSERT( psa_destroy_key( key ) );
855 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856
857exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100858 /*
859 * Key attributes may have been returned by psa_get_key_attributes()
860 * thus reset them as required.
861 */
862 psa_reset_key_attributes( &got_attributes );
863
itayzafrir3e02b3b2018-06-12 17:06:52 +0300864 mbedtls_free( exported );
865 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200866 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867}
868/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100869
Moran Pekerf709f4a2018-06-06 17:26:04 +0300870/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300871void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200872 int type_arg,
873 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100874 int export_size_delta,
875 int expected_export_status_arg,
876 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300877{
Ronald Cron5425a212020-08-04 14:58:35 +0200878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300879 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200880 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200881 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300882 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300883 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100884 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100885 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300887
Gilles Peskine8817f612018-12-18 00:18:46 +0100888 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300889
Gilles Peskine4747d192019-04-17 15:05:45 +0200890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
891 psa_set_key_algorithm( &attributes, alg );
892 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300893
894 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200895 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300896
Gilles Peskine49c25912018-10-29 15:15:31 +0100897 /* Export the public key */
898 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200899 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200900 exported, export_size,
901 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100902 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100903 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100904 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200905 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100906 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200907 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200908 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100909 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100910 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100911 TEST_ASSERT( expected_public_key->len <=
912 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
913 TEST_ASSERT( expected_public_key->len <=
914 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
916 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100917 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918
919exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100920 /*
921 * Key attributes may have been returned by psa_get_key_attributes()
922 * thus reset them as required.
923 */
924 psa_reset_key_attributes( &attributes );
925
itayzafrir3e02b3b2018-06-12 17:06:52 +0300926 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200927 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200928 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929}
930/* END_CASE */
931
Gilles Peskine20035e32018-02-03 22:44:14 +0100932/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200933void import_and_exercise_key( data_t *data,
934 int type_arg,
935 int bits_arg,
936 int alg_arg )
937{
Ronald Cron5425a212020-08-04 14:58:35 +0200938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200939 psa_key_type_t type = type_arg;
940 size_t bits = bits_arg;
941 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100942 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200943 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200944 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200945
Gilles Peskine8817f612018-12-18 00:18:46 +0100946 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200947
Gilles Peskine4747d192019-04-17 15:05:45 +0200948 psa_set_key_usage_flags( &attributes, usage );
949 psa_set_key_algorithm( &attributes, alg );
950 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200951
952 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200953 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200954
955 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200956 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200957 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
958 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200959
960 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100961 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200962 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200963
Ronald Cron5425a212020-08-04 14:58:35 +0200964 PSA_ASSERT( psa_destroy_key( key ) );
965 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200966
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200967exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100968 /*
969 * Key attributes may have been returned by psa_get_key_attributes()
970 * thus reset them as required.
971 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200972 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100973
974 psa_reset_key_attributes( &attributes );
975 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200976 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200977}
978/* END_CASE */
979
980/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100981void effective_key_attributes( int type_arg, int expected_type_arg,
982 int bits_arg, int expected_bits_arg,
983 int usage_arg, int expected_usage_arg,
984 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200985{
Ronald Cron5425a212020-08-04 14:58:35 +0200986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100987 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100988 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100989 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100990 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200991 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100992 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200993 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100994 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200996
Gilles Peskine8817f612018-12-18 00:18:46 +0100997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200998
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 psa_set_key_usage_flags( &attributes, usage );
1000 psa_set_key_algorithm( &attributes, alg );
1001 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001002 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001003
Ronald Cron5425a212020-08-04 14:58:35 +02001004 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001005 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001006
Ronald Cron5425a212020-08-04 14:58:35 +02001007 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001008 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1009 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1010 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1011 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001012
1013exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001014 /*
1015 * Key attributes may have been returned by psa_get_key_attributes()
1016 * thus reset them as required.
1017 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001018 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001019
1020 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001021 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001022}
1023/* END_CASE */
1024
1025/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001026void check_key_policy( int type_arg, int bits_arg,
1027 int usage_arg, int alg_arg )
1028{
1029 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1030 usage_arg, usage_arg, alg_arg, alg_arg );
1031 goto exit;
1032}
1033/* END_CASE */
1034
1035/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001036void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001037{
1038 /* Test each valid way of initializing the object, except for `= {0}`, as
1039 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1040 * though it's OK by the C standard. We could test for this, but we'd need
1041 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001042 psa_key_attributes_t func = psa_key_attributes_init( );
1043 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1044 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001045
1046 memset( &zero, 0, sizeof( zero ) );
1047
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001048 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1049 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1050 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001051
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001052 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1053 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1054 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1055
1056 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1057 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1058 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1059
1060 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1061 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1062 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1063
1064 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1065 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1066 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001067}
1068/* END_CASE */
1069
1070/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071void mac_key_policy( int policy_usage,
1072 int policy_alg,
1073 int key_type,
1074 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001075 int exercise_alg,
1076 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001077{
Ronald Cron5425a212020-08-04 14:58:35 +02001078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001080 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001082 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001084
Gilles Peskine8817f612018-12-18 00:18:46 +01001085 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001087 psa_set_key_usage_flags( &attributes, policy_usage );
1088 psa_set_key_algorithm( &attributes, policy_alg );
1089 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001090
Gilles Peskine049c7532019-05-15 20:22:09 +02001091 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001092 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001093
Ronald Cron5425a212020-08-04 14:58:35 +02001094 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001095 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001096 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001097 else
1098 TEST_EQUAL( status, expected_status );
1099
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001101
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001103 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001104 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001105 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001106 else
1107 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001108
1109exit:
1110 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001112 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001113}
1114/* END_CASE */
1115
1116/* BEGIN_CASE */
1117void cipher_key_policy( int policy_usage,
1118 int policy_alg,
1119 int key_type,
1120 data_t *key_data,
1121 int exercise_alg )
1122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001125 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126 psa_status_t status;
1127
Gilles Peskine8817f612018-12-18 00:18:46 +01001128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001130 psa_set_key_usage_flags( &attributes, policy_usage );
1131 psa_set_key_algorithm( &attributes, policy_alg );
1132 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133
Gilles Peskine049c7532019-05-15 20:22:09 +02001134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001135 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001136
Ronald Cron5425a212020-08-04 14:58:35 +02001137 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001138 if( policy_alg == exercise_alg &&
1139 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001140 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001142 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001143 psa_cipher_abort( &operation );
1144
Ronald Cron5425a212020-08-04 14:58:35 +02001145 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001146 if( policy_alg == exercise_alg &&
1147 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001148 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001149 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001150 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001151
1152exit:
1153 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001154 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001155 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001156}
1157/* END_CASE */
1158
1159/* BEGIN_CASE */
1160void aead_key_policy( int policy_usage,
1161 int policy_alg,
1162 int key_type,
1163 data_t *key_data,
1164 int nonce_length_arg,
1165 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001166 int exercise_alg,
1167 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168{
Ronald Cron5425a212020-08-04 14:58:35 +02001169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001171 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001172 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001173 unsigned char nonce[16] = {0};
1174 size_t nonce_length = nonce_length_arg;
1175 unsigned char tag[16];
1176 size_t tag_length = tag_length_arg;
1177 size_t output_length;
1178
1179 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1180 TEST_ASSERT( tag_length <= sizeof( tag ) );
1181
Gilles Peskine8817f612018-12-18 00:18:46 +01001182 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001183
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001184 psa_set_key_usage_flags( &attributes, policy_usage );
1185 psa_set_key_algorithm( &attributes, policy_alg );
1186 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001187
Gilles Peskine049c7532019-05-15 20:22:09 +02001188 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001189 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001190
Ronald Cron5425a212020-08-04 14:58:35 +02001191 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001192 nonce, nonce_length,
1193 NULL, 0,
1194 NULL, 0,
1195 tag, tag_length,
1196 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001197 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1198 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001199 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001200 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201
1202 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001203 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204 nonce, nonce_length,
1205 NULL, 0,
1206 tag, tag_length,
1207 NULL, 0,
1208 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001209 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1210 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1211 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001212 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001214 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215
1216exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001218 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
1223void asymmetric_encryption_key_policy( int policy_usage,
1224 int policy_alg,
1225 int key_type,
1226 data_t *key_data,
1227 int exercise_alg )
1228{
Ronald Cron5425a212020-08-04 14:58:35 +02001229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 psa_status_t status;
1232 size_t key_bits;
1233 size_t buffer_length;
1234 unsigned char *buffer = NULL;
1235 size_t output_length;
1236
Gilles Peskine8817f612018-12-18 00:18:46 +01001237 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_set_key_usage_flags( &attributes, policy_usage );
1240 psa_set_key_algorithm( &attributes, policy_alg );
1241 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001242
Gilles Peskine049c7532019-05-15 20:22:09 +02001243 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001244 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001245
Ronald Cron5425a212020-08-04 14:58:35 +02001246 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001247 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1249 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001250 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001251
Ronald Cron5425a212020-08-04 14:58:35 +02001252 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001253 NULL, 0,
1254 NULL, 0,
1255 buffer, buffer_length,
1256 &output_length );
1257 if( policy_alg == exercise_alg &&
1258 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001259 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001261 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001263 if( buffer_length != 0 )
1264 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001265 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 buffer, buffer_length,
1267 NULL, 0,
1268 buffer, buffer_length,
1269 &output_length );
1270 if( policy_alg == exercise_alg &&
1271 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001272 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001274 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275
1276exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001277 /*
1278 * Key attributes may have been returned by psa_get_key_attributes()
1279 * thus reset them as required.
1280 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001281 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001282
1283 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001284 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001285 mbedtls_free( buffer );
1286}
1287/* END_CASE */
1288
1289/* BEGIN_CASE */
1290void asymmetric_signature_key_policy( int policy_usage,
1291 int policy_alg,
1292 int key_type,
1293 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001294 int exercise_alg,
1295 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296{
Ronald Cron5425a212020-08-04 14:58:35 +02001297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001300 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1301 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1302 * compatible with the policy and `payload_length_arg` is supposed to be
1303 * a valid input length to sign. If `payload_length_arg <= 0`,
1304 * `exercise_alg` is supposed to be forbidden by the policy. */
1305 int compatible_alg = payload_length_arg > 0;
1306 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001307 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 size_t signature_length;
1309
Gilles Peskine8817f612018-12-18 00:18:46 +01001310 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001311
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001312 psa_set_key_usage_flags( &attributes, policy_usage );
1313 psa_set_key_algorithm( &attributes, policy_alg );
1314 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001315
Gilles Peskine049c7532019-05-15 20:22:09 +02001316 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001317 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001318
Ronald Cron5425a212020-08-04 14:58:35 +02001319 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001320 payload, payload_length,
1321 signature, sizeof( signature ),
1322 &signature_length );
1323 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001324 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001326 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327
1328 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001329 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001330 payload, payload_length,
1331 signature, sizeof( signature ) );
1332 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001333 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001335 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001336
1337exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001338 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001339 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001340}
1341/* END_CASE */
1342
Janos Follathba3fab92019-06-11 14:50:16 +01001343/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001344void derive_key_policy( int policy_usage,
1345 int policy_alg,
1346 int key_type,
1347 data_t *key_data,
1348 int exercise_alg )
1349{
Ronald Cron5425a212020-08-04 14:58:35 +02001350 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001352 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001353 psa_status_t status;
1354
Gilles Peskine8817f612018-12-18 00:18:46 +01001355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001356
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001357 psa_set_key_usage_flags( &attributes, policy_usage );
1358 psa_set_key_algorithm( &attributes, policy_alg );
1359 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001360
Gilles Peskine049c7532019-05-15 20:22:09 +02001361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001362 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001363
Janos Follathba3fab92019-06-11 14:50:16 +01001364 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1365
1366 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1367 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001368 {
Janos Follathba3fab92019-06-11 14:50:16 +01001369 PSA_ASSERT( psa_key_derivation_input_bytes(
1370 &operation,
1371 PSA_KEY_DERIVATION_INPUT_SEED,
1372 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001373 }
Janos Follathba3fab92019-06-11 14:50:16 +01001374
1375 status = psa_key_derivation_input_key( &operation,
1376 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001377 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001378
Gilles Peskineea0fb492018-07-12 17:17:20 +02001379 if( policy_alg == exercise_alg &&
1380 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001381 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001382 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001383 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001384
1385exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001386 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001387 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001388 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001389}
1390/* END_CASE */
1391
1392/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001393void agreement_key_policy( int policy_usage,
1394 int policy_alg,
1395 int key_type_arg,
1396 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001397 int exercise_alg,
1398 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001399{
Ronald Cron5425a212020-08-04 14:58:35 +02001400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001402 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001403 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001404 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001405 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001406
Gilles Peskine8817f612018-12-18 00:18:46 +01001407 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001408
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001409 psa_set_key_usage_flags( &attributes, policy_usage );
1410 psa_set_key_algorithm( &attributes, policy_alg );
1411 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001412
Gilles Peskine049c7532019-05-15 20:22:09 +02001413 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001414 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001415
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001416 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001417 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001418
Steven Cooremance48e852020-10-05 16:02:45 +02001419 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001420
1421exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001422 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001423 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001424 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425}
1426/* END_CASE */
1427
1428/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001429void key_policy_alg2( int key_type_arg, data_t *key_data,
1430 int usage_arg, int alg_arg, int alg2_arg )
1431{
Ronald Cron5425a212020-08-04 14:58:35 +02001432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001433 psa_key_type_t key_type = key_type_arg;
1434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1435 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1436 psa_key_usage_t usage = usage_arg;
1437 psa_algorithm_t alg = alg_arg;
1438 psa_algorithm_t alg2 = alg2_arg;
1439
1440 PSA_ASSERT( psa_crypto_init( ) );
1441
1442 psa_set_key_usage_flags( &attributes, usage );
1443 psa_set_key_algorithm( &attributes, alg );
1444 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1445 psa_set_key_type( &attributes, key_type );
1446 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001447 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001448
Ronald Cron5425a212020-08-04 14:58:35 +02001449 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001450 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1451 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1452 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1453
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001454 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001455 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001456 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001457 goto exit;
1458
1459exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001460 /*
1461 * Key attributes may have been returned by psa_get_key_attributes()
1462 * thus reset them as required.
1463 */
1464 psa_reset_key_attributes( &got_attributes );
1465
Ronald Cron5425a212020-08-04 14:58:35 +02001466 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001467 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468}
1469/* END_CASE */
1470
1471/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001472void raw_agreement_key_policy( int policy_usage,
1473 int policy_alg,
1474 int key_type_arg,
1475 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001476 int exercise_alg,
1477 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001478{
Ronald Cron5425a212020-08-04 14:58:35 +02001479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001481 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001482 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001483 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001484 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001485
1486 PSA_ASSERT( psa_crypto_init( ) );
1487
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001488 psa_set_key_usage_flags( &attributes, policy_usage );
1489 psa_set_key_algorithm( &attributes, policy_alg );
1490 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001491
Gilles Peskine049c7532019-05-15 20:22:09 +02001492 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001493 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001494
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001495 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001496
Steven Cooremance48e852020-10-05 16:02:45 +02001497 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001498
1499exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001500 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001501 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001502 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001503}
1504/* END_CASE */
1505
1506/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001507void copy_success( int source_usage_arg,
1508 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001509 int type_arg, data_t *material,
1510 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001511 int target_usage_arg,
1512 int target_alg_arg, int target_alg2_arg,
1513 int expected_usage_arg,
1514 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001515{
Gilles Peskineca25db92019-04-19 11:43:08 +02001516 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1517 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001518 psa_key_usage_t expected_usage = expected_usage_arg;
1519 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001520 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001521 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1522 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001523 uint8_t *export_buffer = NULL;
1524
Gilles Peskine57ab7212019-01-28 13:03:09 +01001525 PSA_ASSERT( psa_crypto_init( ) );
1526
Gilles Peskineca25db92019-04-19 11:43:08 +02001527 /* Prepare the source key. */
1528 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1529 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001530 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001531 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001532 PSA_ASSERT( psa_import_key( &source_attributes,
1533 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001534 &source_key ) );
1535 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001536
Gilles Peskineca25db92019-04-19 11:43:08 +02001537 /* Prepare the target attributes. */
1538 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001539 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001540 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001541 /* Set volatile lifetime to reset the key identifier to 0. */
1542 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1543 }
1544
Gilles Peskineca25db92019-04-19 11:43:08 +02001545 if( target_usage_arg != -1 )
1546 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1547 if( target_alg_arg != -1 )
1548 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001549 if( target_alg2_arg != -1 )
1550 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001551
1552 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001553 PSA_ASSERT( psa_copy_key( source_key,
1554 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001555
1556 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001557 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001558
1559 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001560 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001561 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1562 psa_get_key_type( &target_attributes ) );
1563 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1564 psa_get_key_bits( &target_attributes ) );
1565 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1566 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001567 TEST_EQUAL( expected_alg2,
1568 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001569 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1570 {
1571 size_t length;
1572 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001573 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001574 material->len, &length ) );
1575 ASSERT_COMPARE( material->x, material->len,
1576 export_buffer, length );
1577 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001578
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001579 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001580 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001581 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001582 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001583
Ronald Cron5425a212020-08-04 14:58:35 +02001584 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001585
1586exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001587 /*
1588 * Source and target key attributes may have been returned by
1589 * psa_get_key_attributes() thus reset them as required.
1590 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001591 psa_reset_key_attributes( &source_attributes );
1592 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001593
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001594 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 mbedtls_free( export_buffer );
1596}
1597/* END_CASE */
1598
1599/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001600void copy_fail( int source_usage_arg,
1601 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001602 int type_arg, data_t *material,
1603 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001604 int target_usage_arg,
1605 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001606 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001607 int expected_status_arg )
1608{
1609 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1610 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001611 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1612 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001613 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001614
1615 PSA_ASSERT( psa_crypto_init( ) );
1616
1617 /* Prepare the source key. */
1618 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1619 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001620 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001621 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001622 PSA_ASSERT( psa_import_key( &source_attributes,
1623 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001624 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001625
1626 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001627 psa_set_key_id( &target_attributes, key_id );
1628 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001629 psa_set_key_type( &target_attributes, target_type_arg );
1630 psa_set_key_bits( &target_attributes, target_bits_arg );
1631 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1632 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001633 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001634
1635 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001636 TEST_EQUAL( psa_copy_key( source_key,
1637 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001638 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001639
Ronald Cron5425a212020-08-04 14:58:35 +02001640 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001641
Gilles Peskine4a644642019-05-03 17:14:08 +02001642exit:
1643 psa_reset_key_attributes( &source_attributes );
1644 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001645 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001646}
1647/* END_CASE */
1648
1649/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001650void hash_operation_init( )
1651{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001652 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001653 /* Test each valid way of initializing the object, except for `= {0}`, as
1654 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1655 * though it's OK by the C standard. We could test for this, but we'd need
1656 * to supress the Clang warning for the test. */
1657 psa_hash_operation_t func = psa_hash_operation_init( );
1658 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1659 psa_hash_operation_t zero;
1660
1661 memset( &zero, 0, sizeof( zero ) );
1662
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001663 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001664 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1665 PSA_ERROR_BAD_STATE );
1666 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1667 PSA_ERROR_BAD_STATE );
1668 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1669 PSA_ERROR_BAD_STATE );
1670
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001671 /* A default hash operation should be abortable without error. */
1672 PSA_ASSERT( psa_hash_abort( &func ) );
1673 PSA_ASSERT( psa_hash_abort( &init ) );
1674 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001675}
1676/* END_CASE */
1677
1678/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001679void hash_setup( int alg_arg,
1680 int expected_status_arg )
1681{
1682 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001683 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001684 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001685 psa_status_t status;
1686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001688
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001689 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001690 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001691
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001692 /* Whether setup succeeded or failed, abort must succeed. */
1693 PSA_ASSERT( psa_hash_abort( &operation ) );
1694
1695 /* If setup failed, reproduce the failure, so as to
1696 * test the resulting state of the operation object. */
1697 if( status != PSA_SUCCESS )
1698 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1699
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001700 /* Now the operation object should be reusable. */
1701#if defined(KNOWN_SUPPORTED_HASH_ALG)
1702 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1703 PSA_ASSERT( psa_hash_abort( &operation ) );
1704#endif
1705
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001707 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001708}
1709/* END_CASE */
1710
1711/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001712void hash_compute_fail( int alg_arg, data_t *input,
1713 int output_size_arg, int expected_status_arg )
1714{
1715 psa_algorithm_t alg = alg_arg;
1716 uint8_t *output = NULL;
1717 size_t output_size = output_size_arg;
1718 size_t output_length = INVALID_EXPORT_LENGTH;
1719 psa_status_t expected_status = expected_status_arg;
1720 psa_status_t status;
1721
1722 ASSERT_ALLOC( output, output_size );
1723
1724 PSA_ASSERT( psa_crypto_init( ) );
1725
1726 status = psa_hash_compute( alg, input->x, input->len,
1727 output, output_size, &output_length );
1728 TEST_EQUAL( status, expected_status );
1729 TEST_ASSERT( output_length <= output_size );
1730
1731exit:
1732 mbedtls_free( output );
1733 PSA_DONE( );
1734}
1735/* END_CASE */
1736
1737/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001738void hash_compare_fail( int alg_arg, data_t *input,
1739 data_t *reference_hash,
1740 int expected_status_arg )
1741{
1742 psa_algorithm_t alg = alg_arg;
1743 psa_status_t expected_status = expected_status_arg;
1744 psa_status_t status;
1745
1746 PSA_ASSERT( psa_crypto_init( ) );
1747
1748 status = psa_hash_compare( alg, input->x, input->len,
1749 reference_hash->x, reference_hash->len );
1750 TEST_EQUAL( status, expected_status );
1751
1752exit:
1753 PSA_DONE( );
1754}
1755/* END_CASE */
1756
1757/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001758void hash_compute_compare( int alg_arg, data_t *input,
1759 data_t *expected_output )
1760{
1761 psa_algorithm_t alg = alg_arg;
1762 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1763 size_t output_length = INVALID_EXPORT_LENGTH;
1764 size_t i;
1765
1766 PSA_ASSERT( psa_crypto_init( ) );
1767
1768 /* Compute with tight buffer */
1769 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001770 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001771 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001772 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001773 ASSERT_COMPARE( output, output_length,
1774 expected_output->x, expected_output->len );
1775
1776 /* Compute with larger buffer */
1777 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1778 output, sizeof( output ),
1779 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001780 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001781 ASSERT_COMPARE( output, output_length,
1782 expected_output->x, expected_output->len );
1783
1784 /* Compare with correct hash */
1785 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1786 output, output_length ) );
1787
1788 /* Compare with trailing garbage */
1789 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1790 output, output_length + 1 ),
1791 PSA_ERROR_INVALID_SIGNATURE );
1792
1793 /* Compare with truncated hash */
1794 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1795 output, output_length - 1 ),
1796 PSA_ERROR_INVALID_SIGNATURE );
1797
1798 /* Compare with corrupted value */
1799 for( i = 0; i < output_length; i++ )
1800 {
Chris Jones9634bb12021-01-20 15:56:42 +00001801 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001802 output[i] ^= 1;
1803 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1804 output, output_length ),
1805 PSA_ERROR_INVALID_SIGNATURE );
1806 output[i] ^= 1;
1807 }
1808
1809exit:
1810 PSA_DONE( );
1811}
1812/* END_CASE */
1813
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001814/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001815void hash_bad_order( )
1816{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001817 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001818 unsigned char input[] = "";
1819 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001820 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001821 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1822 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1823 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001824 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001825 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001826 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001827
Gilles Peskine8817f612018-12-18 00:18:46 +01001828 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001829
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001830 /* Call setup twice in a row. */
1831 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1832 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1833 PSA_ERROR_BAD_STATE );
1834 PSA_ASSERT( psa_hash_abort( &operation ) );
1835
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001836 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001837 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001838 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001839 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001840
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001841 /* Call update after finish. */
1842 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1843 PSA_ASSERT( psa_hash_finish( &operation,
1844 hash, sizeof( hash ), &hash_len ) );
1845 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001846 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001847 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001848
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001849 /* Call verify without calling setup beforehand. */
1850 TEST_EQUAL( psa_hash_verify( &operation,
1851 valid_hash, sizeof( valid_hash ) ),
1852 PSA_ERROR_BAD_STATE );
1853 PSA_ASSERT( psa_hash_abort( &operation ) );
1854
1855 /* Call verify after finish. */
1856 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1857 PSA_ASSERT( psa_hash_finish( &operation,
1858 hash, sizeof( hash ), &hash_len ) );
1859 TEST_EQUAL( psa_hash_verify( &operation,
1860 valid_hash, sizeof( valid_hash ) ),
1861 PSA_ERROR_BAD_STATE );
1862 PSA_ASSERT( psa_hash_abort( &operation ) );
1863
1864 /* Call verify twice in a row. */
1865 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1866 PSA_ASSERT( psa_hash_verify( &operation,
1867 valid_hash, sizeof( valid_hash ) ) );
1868 TEST_EQUAL( psa_hash_verify( &operation,
1869 valid_hash, sizeof( valid_hash ) ),
1870 PSA_ERROR_BAD_STATE );
1871 PSA_ASSERT( psa_hash_abort( &operation ) );
1872
1873 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001874 TEST_EQUAL( psa_hash_finish( &operation,
1875 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001876 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001877 PSA_ASSERT( psa_hash_abort( &operation ) );
1878
1879 /* Call finish twice in a row. */
1880 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1881 PSA_ASSERT( psa_hash_finish( &operation,
1882 hash, sizeof( hash ), &hash_len ) );
1883 TEST_EQUAL( psa_hash_finish( &operation,
1884 hash, sizeof( hash ), &hash_len ),
1885 PSA_ERROR_BAD_STATE );
1886 PSA_ASSERT( psa_hash_abort( &operation ) );
1887
1888 /* Call finish after calling verify. */
1889 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1890 PSA_ASSERT( psa_hash_verify( &operation,
1891 valid_hash, sizeof( valid_hash ) ) );
1892 TEST_EQUAL( psa_hash_finish( &operation,
1893 hash, sizeof( hash ), &hash_len ),
1894 PSA_ERROR_BAD_STATE );
1895 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001896
1897exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001898 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001899}
1900/* END_CASE */
1901
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001902/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001903void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001904{
1905 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001906 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1907 * appended to it */
1908 unsigned char hash[] = {
1909 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1910 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1911 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001912 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001913 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001914
Gilles Peskine8817f612018-12-18 00:18:46 +01001915 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001916
itayzafrir27e69452018-11-01 14:26:34 +02001917 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001918 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001919 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001920 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001921
itayzafrir27e69452018-11-01 14:26:34 +02001922 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001923 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001924 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001925 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001926
itayzafrir27e69452018-11-01 14:26:34 +02001927 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001928 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001929 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001930 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001931
itayzafrirec93d302018-10-18 18:01:10 +03001932exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001933 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001934}
1935/* END_CASE */
1936
Ronald Cronee414c72021-03-18 18:50:08 +01001937/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001938void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001939{
1940 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001941 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001942 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001943 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001944 size_t hash_len;
1945
Gilles Peskine8817f612018-12-18 00:18:46 +01001946 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001947
itayzafrir58028322018-10-25 10:22:01 +03001948 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001950 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001951 hash, expected_size - 1, &hash_len ),
1952 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001953
1954exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001955 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001956}
1957/* END_CASE */
1958
Ronald Cronee414c72021-03-18 18:50:08 +01001959/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001960void hash_clone_source_state( )
1961{
1962 psa_algorithm_t alg = PSA_ALG_SHA_256;
1963 unsigned char hash[PSA_HASH_MAX_SIZE];
1964 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1965 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1966 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1967 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1968 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1969 size_t hash_len;
1970
1971 PSA_ASSERT( psa_crypto_init( ) );
1972 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1973
1974 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1975 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1976 PSA_ASSERT( psa_hash_finish( &op_finished,
1977 hash, sizeof( hash ), &hash_len ) );
1978 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1979 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1980
1981 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1982 PSA_ERROR_BAD_STATE );
1983
1984 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1985 PSA_ASSERT( psa_hash_finish( &op_init,
1986 hash, sizeof( hash ), &hash_len ) );
1987 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1988 PSA_ASSERT( psa_hash_finish( &op_finished,
1989 hash, sizeof( hash ), &hash_len ) );
1990 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1991 PSA_ASSERT( psa_hash_finish( &op_aborted,
1992 hash, sizeof( hash ), &hash_len ) );
1993
1994exit:
1995 psa_hash_abort( &op_source );
1996 psa_hash_abort( &op_init );
1997 psa_hash_abort( &op_setup );
1998 psa_hash_abort( &op_finished );
1999 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002000 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002001}
2002/* END_CASE */
2003
Ronald Cronee414c72021-03-18 18:50:08 +01002004/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002005void hash_clone_target_state( )
2006{
2007 psa_algorithm_t alg = PSA_ALG_SHA_256;
2008 unsigned char hash[PSA_HASH_MAX_SIZE];
2009 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2010 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2011 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2012 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2013 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2014 size_t hash_len;
2015
2016 PSA_ASSERT( psa_crypto_init( ) );
2017
2018 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2019 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2020 PSA_ASSERT( psa_hash_finish( &op_finished,
2021 hash, sizeof( hash ), &hash_len ) );
2022 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2023 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2024
2025 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2026 PSA_ASSERT( psa_hash_finish( &op_target,
2027 hash, sizeof( hash ), &hash_len ) );
2028
2029 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2030 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2031 PSA_ERROR_BAD_STATE );
2032 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2033 PSA_ERROR_BAD_STATE );
2034
2035exit:
2036 psa_hash_abort( &op_target );
2037 psa_hash_abort( &op_init );
2038 psa_hash_abort( &op_setup );
2039 psa_hash_abort( &op_finished );
2040 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002041 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002042}
2043/* END_CASE */
2044
itayzafrir58028322018-10-25 10:22:01 +03002045/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002046void mac_operation_init( )
2047{
Jaeden Amero252ef282019-02-15 14:05:35 +00002048 const uint8_t input[1] = { 0 };
2049
Jaeden Amero769ce272019-01-04 11:48:03 +00002050 /* Test each valid way of initializing the object, except for `= {0}`, as
2051 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2052 * though it's OK by the C standard. We could test for this, but we'd need
2053 * to supress the Clang warning for the test. */
2054 psa_mac_operation_t func = psa_mac_operation_init( );
2055 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2056 psa_mac_operation_t zero;
2057
2058 memset( &zero, 0, sizeof( zero ) );
2059
Jaeden Amero252ef282019-02-15 14:05:35 +00002060 /* A freshly-initialized MAC operation should not be usable. */
2061 TEST_EQUAL( psa_mac_update( &func,
2062 input, sizeof( input ) ),
2063 PSA_ERROR_BAD_STATE );
2064 TEST_EQUAL( psa_mac_update( &init,
2065 input, sizeof( input ) ),
2066 PSA_ERROR_BAD_STATE );
2067 TEST_EQUAL( psa_mac_update( &zero,
2068 input, sizeof( input ) ),
2069 PSA_ERROR_BAD_STATE );
2070
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002071 /* A default MAC operation should be abortable without error. */
2072 PSA_ASSERT( psa_mac_abort( &func ) );
2073 PSA_ASSERT( psa_mac_abort( &init ) );
2074 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002075}
2076/* END_CASE */
2077
2078/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002079void mac_setup( int key_type_arg,
2080 data_t *key,
2081 int alg_arg,
2082 int expected_status_arg )
2083{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002084 psa_key_type_t key_type = key_type_arg;
2085 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002086 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002087 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002088 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2089#if defined(KNOWN_SUPPORTED_MAC_ALG)
2090 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2091#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002092
Gilles Peskine8817f612018-12-18 00:18:46 +01002093 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002094
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002095 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2096 &operation, &status ) )
2097 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002098 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002099
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002100 /* The operation object should be reusable. */
2101#if defined(KNOWN_SUPPORTED_MAC_ALG)
2102 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2103 smoke_test_key_data,
2104 sizeof( smoke_test_key_data ),
2105 KNOWN_SUPPORTED_MAC_ALG,
2106 &operation, &status ) )
2107 goto exit;
2108 TEST_EQUAL( status, PSA_SUCCESS );
2109#endif
2110
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002111exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002112 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002113}
2114/* END_CASE */
2115
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002116/* 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 +00002117void mac_bad_order( )
2118{
Ronald Cron5425a212020-08-04 14:58:35 +02002119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002120 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2121 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002122 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002123 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2124 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2125 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2128 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2129 size_t sign_mac_length = 0;
2130 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2131 const uint8_t verify_mac[] = {
2132 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2133 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2134 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2135
2136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002138 psa_set_key_algorithm( &attributes, alg );
2139 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002140
Ronald Cron5425a212020-08-04 14:58:35 +02002141 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2142 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002143
Jaeden Amero252ef282019-02-15 14:05:35 +00002144 /* Call update without calling setup beforehand. */
2145 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2146 PSA_ERROR_BAD_STATE );
2147 PSA_ASSERT( psa_mac_abort( &operation ) );
2148
2149 /* Call sign finish without calling setup beforehand. */
2150 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2151 &sign_mac_length),
2152 PSA_ERROR_BAD_STATE );
2153 PSA_ASSERT( psa_mac_abort( &operation ) );
2154
2155 /* Call verify finish without calling setup beforehand. */
2156 TEST_EQUAL( psa_mac_verify_finish( &operation,
2157 verify_mac, sizeof( verify_mac ) ),
2158 PSA_ERROR_BAD_STATE );
2159 PSA_ASSERT( psa_mac_abort( &operation ) );
2160
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002161 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002162 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2163 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002164 PSA_ERROR_BAD_STATE );
2165 PSA_ASSERT( psa_mac_abort( &operation ) );
2166
Jaeden Amero252ef282019-02-15 14:05:35 +00002167 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002168 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002169 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2170 PSA_ASSERT( psa_mac_sign_finish( &operation,
2171 sign_mac, sizeof( sign_mac ),
2172 &sign_mac_length ) );
2173 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2174 PSA_ERROR_BAD_STATE );
2175 PSA_ASSERT( psa_mac_abort( &operation ) );
2176
2177 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002178 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002179 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2180 PSA_ASSERT( psa_mac_verify_finish( &operation,
2181 verify_mac, sizeof( verify_mac ) ) );
2182 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2183 PSA_ERROR_BAD_STATE );
2184 PSA_ASSERT( psa_mac_abort( &operation ) );
2185
2186 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002187 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002188 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2189 PSA_ASSERT( psa_mac_sign_finish( &operation,
2190 sign_mac, sizeof( sign_mac ),
2191 &sign_mac_length ) );
2192 TEST_EQUAL( psa_mac_sign_finish( &operation,
2193 sign_mac, sizeof( sign_mac ),
2194 &sign_mac_length ),
2195 PSA_ERROR_BAD_STATE );
2196 PSA_ASSERT( psa_mac_abort( &operation ) );
2197
2198 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002199 PSA_ASSERT( psa_mac_verify_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_verify_finish( &operation,
2202 verify_mac, sizeof( verify_mac ) ) );
2203 TEST_EQUAL( psa_mac_verify_finish( &operation,
2204 verify_mac, sizeof( verify_mac ) ),
2205 PSA_ERROR_BAD_STATE );
2206 PSA_ASSERT( psa_mac_abort( &operation ) );
2207
2208 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002209 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002210 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2211 TEST_EQUAL( psa_mac_verify_finish( &operation,
2212 verify_mac, sizeof( verify_mac ) ),
2213 PSA_ERROR_BAD_STATE );
2214 PSA_ASSERT( psa_mac_abort( &operation ) );
2215
2216 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002218 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2219 TEST_EQUAL( psa_mac_sign_finish( &operation,
2220 sign_mac, sizeof( sign_mac ),
2221 &sign_mac_length ),
2222 PSA_ERROR_BAD_STATE );
2223 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002224
Ronald Cron5425a212020-08-04 14:58:35 +02002225 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002226
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002227exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002228 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002229}
2230/* END_CASE */
2231
2232/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002233void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002234 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002235 int alg_arg,
2236 data_t *input,
2237 data_t *expected_mac )
2238{
Ronald Cron5425a212020-08-04 14:58:35 +02002239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002240 psa_key_type_t key_type = key_type_arg;
2241 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002242 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002244 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002245 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002246 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002247 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002248 const size_t output_sizes_to_test[] = {
2249 0,
2250 1,
2251 expected_mac->len - 1,
2252 expected_mac->len,
2253 expected_mac->len + 1,
2254 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002255
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002256 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002257 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002258 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002259
Gilles Peskine8817f612018-12-18 00:18:46 +01002260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002263 psa_set_key_algorithm( &attributes, alg );
2264 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002265
Ronald Cron5425a212020-08-04 14:58:35 +02002266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2267 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268
Gilles Peskine8b356b52020-08-25 23:44:59 +02002269 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2270 {
2271 const size_t output_size = output_sizes_to_test[i];
2272 psa_status_t expected_status =
2273 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2274 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002275
Chris Jones9634bb12021-01-20 15:56:42 +00002276 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002277 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002278
Gilles Peskine8b356b52020-08-25 23:44:59 +02002279 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002280 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002281 PSA_ASSERT( psa_mac_update( &operation,
2282 input->x, input->len ) );
2283 TEST_EQUAL( psa_mac_sign_finish( &operation,
2284 actual_mac, output_size,
2285 &mac_length ),
2286 expected_status );
2287 PSA_ASSERT( psa_mac_abort( &operation ) );
2288
2289 if( expected_status == PSA_SUCCESS )
2290 {
2291 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2292 actual_mac, mac_length );
2293 }
2294 mbedtls_free( actual_mac );
2295 actual_mac = NULL;
2296 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002297
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002298exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002299 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002300 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002301 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002302 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002303}
2304/* END_CASE */
2305
2306/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002307void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002308 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002309 int alg_arg,
2310 data_t *input,
2311 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002312{
Ronald Cron5425a212020-08-04 14:58:35 +02002313 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002314 psa_key_type_t key_type = key_type_arg;
2315 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002316 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002318 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319
Gilles Peskine69c12672018-06-28 00:07:19 +02002320 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2321
Gilles Peskine8817f612018-12-18 00:18:46 +01002322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002323
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002324 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002325 psa_set_key_algorithm( &attributes, alg );
2326 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002327
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2329 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002330
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002331 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002332 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002333 PSA_ASSERT( psa_mac_update( &operation,
2334 input->x, input->len ) );
2335 PSA_ASSERT( psa_mac_verify_finish( &operation,
2336 expected_mac->x,
2337 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002339 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002340 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002341 PSA_ASSERT( psa_mac_update( &operation,
2342 input->x, input->len ) );
2343 TEST_EQUAL( psa_mac_verify_finish( &operation,
2344 expected_mac->x,
2345 expected_mac->len - 1 ),
2346 PSA_ERROR_INVALID_SIGNATURE );
2347
2348 /* Test a MAC that's too long. */
2349 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2350 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002351 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002352 PSA_ASSERT( psa_mac_update( &operation,
2353 input->x, input->len ) );
2354 TEST_EQUAL( psa_mac_verify_finish( &operation,
2355 perturbed_mac,
2356 expected_mac->len + 1 ),
2357 PSA_ERROR_INVALID_SIGNATURE );
2358
2359 /* Test changing one byte. */
2360 for( size_t i = 0; i < expected_mac->len; i++ )
2361 {
Chris Jones9634bb12021-01-20 15:56:42 +00002362 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002363 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002364 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002365 PSA_ASSERT( psa_mac_update( &operation,
2366 input->x, input->len ) );
2367 TEST_EQUAL( psa_mac_verify_finish( &operation,
2368 perturbed_mac,
2369 expected_mac->len ),
2370 PSA_ERROR_INVALID_SIGNATURE );
2371 perturbed_mac[i] ^= 1;
2372 }
2373
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002375 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002376 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002377 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002378 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002379}
2380/* END_CASE */
2381
2382/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002383void cipher_operation_init( )
2384{
Jaeden Ameroab439972019-02-15 14:12:05 +00002385 const uint8_t input[1] = { 0 };
2386 unsigned char output[1] = { 0 };
2387 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002388 /* Test each valid way of initializing the object, except for `= {0}`, as
2389 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2390 * though it's OK by the C standard. We could test for this, but we'd need
2391 * to supress the Clang warning for the test. */
2392 psa_cipher_operation_t func = psa_cipher_operation_init( );
2393 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2394 psa_cipher_operation_t zero;
2395
2396 memset( &zero, 0, sizeof( zero ) );
2397
Jaeden Ameroab439972019-02-15 14:12:05 +00002398 /* A freshly-initialized cipher operation should not be usable. */
2399 TEST_EQUAL( psa_cipher_update( &func,
2400 input, sizeof( input ),
2401 output, sizeof( output ),
2402 &output_length ),
2403 PSA_ERROR_BAD_STATE );
2404 TEST_EQUAL( psa_cipher_update( &init,
2405 input, sizeof( input ),
2406 output, sizeof( output ),
2407 &output_length ),
2408 PSA_ERROR_BAD_STATE );
2409 TEST_EQUAL( psa_cipher_update( &zero,
2410 input, sizeof( input ),
2411 output, sizeof( output ),
2412 &output_length ),
2413 PSA_ERROR_BAD_STATE );
2414
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002415 /* A default cipher operation should be abortable without error. */
2416 PSA_ASSERT( psa_cipher_abort( &func ) );
2417 PSA_ASSERT( psa_cipher_abort( &init ) );
2418 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002419}
2420/* END_CASE */
2421
2422/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002423void cipher_setup( int key_type_arg,
2424 data_t *key,
2425 int alg_arg,
2426 int expected_status_arg )
2427{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002428 psa_key_type_t key_type = key_type_arg;
2429 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002430 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002431 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002432 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002433#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002434 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2435#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002436
Gilles Peskine8817f612018-12-18 00:18:46 +01002437 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002438
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002439 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2440 &operation, &status ) )
2441 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002442 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002443
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002444 /* The operation object should be reusable. */
2445#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2446 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2447 smoke_test_key_data,
2448 sizeof( smoke_test_key_data ),
2449 KNOWN_SUPPORTED_CIPHER_ALG,
2450 &operation, &status ) )
2451 goto exit;
2452 TEST_EQUAL( status, PSA_SUCCESS );
2453#endif
2454
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002456 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002457 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458}
2459/* END_CASE */
2460
Ronald Cronee414c72021-03-18 18:50:08 +01002461/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002462void cipher_bad_order( )
2463{
Ronald Cron5425a212020-08-04 14:58:35 +02002464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002465 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2466 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002468 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002469 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002470 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002471 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2472 0xaa, 0xaa, 0xaa, 0xaa };
2473 const uint8_t text[] = {
2474 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2475 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002476 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002477 size_t length = 0;
2478
2479 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2481 psa_set_key_algorithm( &attributes, alg );
2482 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002483 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2484 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002485
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002486 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002487 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2488 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002489 PSA_ERROR_BAD_STATE );
2490 PSA_ASSERT( psa_cipher_abort( &operation ) );
2491
2492 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002493 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2494 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002495 PSA_ERROR_BAD_STATE );
2496 PSA_ASSERT( psa_cipher_abort( &operation ) );
2497
Jaeden Ameroab439972019-02-15 14:12:05 +00002498 /* Generate an IV without calling setup beforehand. */
2499 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2500 buffer, sizeof( buffer ),
2501 &length ),
2502 PSA_ERROR_BAD_STATE );
2503 PSA_ASSERT( psa_cipher_abort( &operation ) );
2504
2505 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002506 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002507 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2508 buffer, sizeof( buffer ),
2509 &length ) );
2510 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2511 buffer, sizeof( buffer ),
2512 &length ),
2513 PSA_ERROR_BAD_STATE );
2514 PSA_ASSERT( psa_cipher_abort( &operation ) );
2515
2516 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002517 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002518 PSA_ASSERT( psa_cipher_set_iv( &operation,
2519 iv, sizeof( iv ) ) );
2520 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2521 buffer, sizeof( buffer ),
2522 &length ),
2523 PSA_ERROR_BAD_STATE );
2524 PSA_ASSERT( psa_cipher_abort( &operation ) );
2525
2526 /* Set an IV without calling setup beforehand. */
2527 TEST_EQUAL( psa_cipher_set_iv( &operation,
2528 iv, sizeof( iv ) ),
2529 PSA_ERROR_BAD_STATE );
2530 PSA_ASSERT( psa_cipher_abort( &operation ) );
2531
2532 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002534 PSA_ASSERT( psa_cipher_set_iv( &operation,
2535 iv, sizeof( iv ) ) );
2536 TEST_EQUAL( psa_cipher_set_iv( &operation,
2537 iv, sizeof( iv ) ),
2538 PSA_ERROR_BAD_STATE );
2539 PSA_ASSERT( psa_cipher_abort( &operation ) );
2540
2541 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002542 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002543 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2544 buffer, sizeof( buffer ),
2545 &length ) );
2546 TEST_EQUAL( psa_cipher_set_iv( &operation,
2547 iv, sizeof( iv ) ),
2548 PSA_ERROR_BAD_STATE );
2549 PSA_ASSERT( psa_cipher_abort( &operation ) );
2550
2551 /* Call update without calling setup beforehand. */
2552 TEST_EQUAL( psa_cipher_update( &operation,
2553 text, sizeof( text ),
2554 buffer, sizeof( buffer ),
2555 &length ),
2556 PSA_ERROR_BAD_STATE );
2557 PSA_ASSERT( psa_cipher_abort( &operation ) );
2558
2559 /* Call update without an IV where an IV is required. */
2560 TEST_EQUAL( psa_cipher_update( &operation,
2561 text, sizeof( text ),
2562 buffer, sizeof( buffer ),
2563 &length ),
2564 PSA_ERROR_BAD_STATE );
2565 PSA_ASSERT( psa_cipher_abort( &operation ) );
2566
2567 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002568 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002569 PSA_ASSERT( psa_cipher_set_iv( &operation,
2570 iv, sizeof( iv ) ) );
2571 PSA_ASSERT( psa_cipher_finish( &operation,
2572 buffer, sizeof( buffer ), &length ) );
2573 TEST_EQUAL( psa_cipher_update( &operation,
2574 text, sizeof( text ),
2575 buffer, sizeof( buffer ),
2576 &length ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Call finish without calling setup beforehand. */
2581 TEST_EQUAL( psa_cipher_finish( &operation,
2582 buffer, sizeof( buffer ), &length ),
2583 PSA_ERROR_BAD_STATE );
2584 PSA_ASSERT( psa_cipher_abort( &operation ) );
2585
2586 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002587 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002588 /* Not calling update means we are encrypting an empty buffer, which is OK
2589 * for cipher modes with padding. */
2590 TEST_EQUAL( psa_cipher_finish( &operation,
2591 buffer, sizeof( buffer ), &length ),
2592 PSA_ERROR_BAD_STATE );
2593 PSA_ASSERT( psa_cipher_abort( &operation ) );
2594
2595 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002596 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002597 PSA_ASSERT( psa_cipher_set_iv( &operation,
2598 iv, sizeof( iv ) ) );
2599 PSA_ASSERT( psa_cipher_finish( &operation,
2600 buffer, sizeof( buffer ), &length ) );
2601 TEST_EQUAL( psa_cipher_finish( &operation,
2602 buffer, sizeof( buffer ), &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
Ronald Cron5425a212020-08-04 14:58:35 +02002606 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002607
Jaeden Ameroab439972019-02-15 14:12:05 +00002608exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002609 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002610 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002611}
2612/* END_CASE */
2613
2614/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002615void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002616 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002617 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002618 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002619{
Ronald Cron5425a212020-08-04 14:58:35 +02002620 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002621 psa_status_t status;
2622 psa_key_type_t key_type = key_type_arg;
2623 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002624 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002625 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002626 size_t output_buffer_size = 0;
2627 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002628 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002629 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002630 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002631
Gilles Peskine8817f612018-12-18 00:18:46 +01002632 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002634 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2635 psa_set_key_algorithm( &attributes, alg );
2636 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002637
Ronald Cron5425a212020-08-04 14:58:35 +02002638 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2639 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640
Ronald Cron5425a212020-08-04 14:58:35 +02002641 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002643 if( iv->len > 0 )
2644 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002645 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002646 }
2647
gabor-mezei-armceface22021-01-21 12:26:17 +01002648 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2649 TEST_ASSERT( output_buffer_size <=
2650 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002651 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_cipher_update( &operation,
2654 input->x, input->len,
2655 output, output_buffer_size,
2656 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002657 TEST_ASSERT( function_output_length <=
2658 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2659 TEST_ASSERT( function_output_length <=
2660 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002661 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002662
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002664 ( output_buffer_size == 0 ? NULL :
2665 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002666 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002667 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002668 TEST_ASSERT( function_output_length <=
2669 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2670 TEST_ASSERT( function_output_length <=
2671 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002672 total_output_length += function_output_length;
2673
Gilles Peskinefe11b722018-12-18 00:24:04 +01002674 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675 if( expected_status == PSA_SUCCESS )
2676 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002677 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002678 ASSERT_COMPARE( expected_output->x, expected_output->len,
2679 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002681
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002683 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002684 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002685 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002686 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687}
2688/* END_CASE */
2689
2690/* BEGIN_CASE */
2691void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002692 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002693 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002694 int first_part_size_arg,
2695 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002696 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002697{
Ronald Cron5425a212020-08-04 14:58:35 +02002698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002699 psa_key_type_t key_type = key_type_arg;
2700 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002701 size_t first_part_size = first_part_size_arg;
2702 size_t output1_length = output1_length_arg;
2703 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002704 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705 size_t output_buffer_size = 0;
2706 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002707 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002708 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002712
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2714 psa_set_key_algorithm( &attributes, alg );
2715 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002716
Ronald Cron5425a212020-08-04 14:58:35 +02002717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2718 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002719
Ronald Cron5425a212020-08-04 14:58:35 +02002720 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002722 if( iv->len > 0 )
2723 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002724 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002725 }
2726
gabor-mezei-armceface22021-01-21 12:26:17 +01002727 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2728 TEST_ASSERT( output_buffer_size <=
2729 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002730 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731
Gilles Peskinee0866522019-02-19 19:44:00 +01002732 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002733 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2734 output, output_buffer_size,
2735 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002736 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002737 TEST_ASSERT( function_output_length <=
2738 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2739 TEST_ASSERT( function_output_length <=
2740 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002741 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002742
Gilles Peskine8817f612018-12-18 00:18:46 +01002743 PSA_ASSERT( psa_cipher_update( &operation,
2744 input->x + first_part_size,
2745 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002746 ( output_buffer_size == 0 ? NULL :
2747 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002748 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002749 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002750 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002751 TEST_ASSERT( function_output_length <=
2752 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2753 alg,
2754 input->len - first_part_size ) );
2755 TEST_ASSERT( function_output_length <=
2756 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002757 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002758
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002760 ( output_buffer_size == 0 ? NULL :
2761 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002762 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002763 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002764 TEST_ASSERT( function_output_length <=
2765 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2766 TEST_ASSERT( function_output_length <=
2767 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002768 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002771 ASSERT_COMPARE( expected_output->x, expected_output->len,
2772 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002773
2774exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002775 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002776 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002777 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002778 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002779}
2780/* END_CASE */
2781
2782/* BEGIN_CASE */
2783void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002784 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002785 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002786 int first_part_size_arg,
2787 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002788 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789{
Ronald Cron5425a212020-08-04 14:58:35 +02002790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791 psa_key_type_t key_type = key_type_arg;
2792 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002793 size_t first_part_size = first_part_size_arg;
2794 size_t output1_length = output1_length_arg;
2795 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002796 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797 size_t output_buffer_size = 0;
2798 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002799 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002800 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002801 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002802
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002804
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002805 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2806 psa_set_key_algorithm( &attributes, alg );
2807 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002808
Ronald Cron5425a212020-08-04 14:58:35 +02002809 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2810 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002811
Ronald Cron5425a212020-08-04 14:58:35 +02002812 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813
Steven Cooreman177deba2020-09-07 17:14:14 +02002814 if( iv->len > 0 )
2815 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002816 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002817 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818
gabor-mezei-armceface22021-01-21 12:26:17 +01002819 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2820 TEST_ASSERT( output_buffer_size <=
2821 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002822 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
Gilles Peskinee0866522019-02-19 19:44:00 +01002824 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002825 PSA_ASSERT( psa_cipher_update( &operation,
2826 input->x, first_part_size,
2827 output, output_buffer_size,
2828 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002829 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002830 TEST_ASSERT( function_output_length <=
2831 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2832 TEST_ASSERT( function_output_length <=
2833 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002834 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_cipher_update( &operation,
2837 input->x + first_part_size,
2838 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002839 ( output_buffer_size == 0 ? NULL :
2840 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002841 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002843 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002844 TEST_ASSERT( function_output_length <=
2845 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2846 alg,
2847 input->len - first_part_size ) );
2848 TEST_ASSERT( function_output_length <=
2849 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002850 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002851
Gilles Peskine8817f612018-12-18 00:18:46 +01002852 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002853 ( output_buffer_size == 0 ? NULL :
2854 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002855 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002856 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002857 TEST_ASSERT( function_output_length <=
2858 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2859 TEST_ASSERT( function_output_length <=
2860 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002861 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002862 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002863
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002864 ASSERT_COMPARE( expected_output->x, expected_output->len,
2865 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866
2867exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002868 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002869 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002870 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002871 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002872}
2873/* END_CASE */
2874
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875/* BEGIN_CASE */
2876void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002877 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002878 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002879 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880{
Ronald Cron5425a212020-08-04 14:58:35 +02002881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002882 psa_status_t status;
2883 psa_key_type_t key_type = key_type_arg;
2884 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002885 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002886 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887 size_t output_buffer_size = 0;
2888 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002889 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002890 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2896 psa_set_key_algorithm( &attributes, alg );
2897 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002898
Ronald Cron5425a212020-08-04 14:58:35 +02002899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2900 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901
Ronald Cron5425a212020-08-04 14:58:35 +02002902 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903
Steven Cooreman177deba2020-09-07 17:14:14 +02002904 if( iv->len > 0 )
2905 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002906 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002907 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908
gabor-mezei-armceface22021-01-21 12:26:17 +01002909 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2910 TEST_ASSERT( output_buffer_size <=
2911 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002912 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_cipher_update( &operation,
2915 input->x, input->len,
2916 output, output_buffer_size,
2917 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002918 TEST_ASSERT( function_output_length <=
2919 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2920 TEST_ASSERT( function_output_length <=
2921 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002922 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002923
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002925 ( output_buffer_size == 0 ? NULL :
2926 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002927 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002929 TEST_ASSERT( function_output_length <=
2930 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2931 TEST_ASSERT( function_output_length <=
2932 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002933 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002934 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002935
2936 if( expected_status == PSA_SUCCESS )
2937 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002939 ASSERT_COMPARE( expected_output->x, expected_output->len,
2940 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941 }
2942
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002944 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002945 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002946 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002947 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948}
2949/* END_CASE */
2950
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951/* BEGIN_CASE */
2952void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002953 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002954 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002955{
Ronald Cron5425a212020-08-04 14:58:35 +02002956 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002957 psa_key_type_t key_type = key_type_arg;
2958 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002959 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002960 size_t iv_size = 16;
2961 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002962 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002963 size_t output1_size = 0;
2964 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002965 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002966 size_t output2_size = 0;
2967 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002968 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002969 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2970 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002971 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002972
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002974
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002975 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2976 psa_set_key_algorithm( &attributes, alg );
2977 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002978
Ronald Cron5425a212020-08-04 14:58:35 +02002979 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2980 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981
Ronald Cron5425a212020-08-04 14:58:35 +02002982 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2983 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002984
Steven Cooreman177deba2020-09-07 17:14:14 +02002985 if( alg != PSA_ALG_ECB_NO_PADDING )
2986 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002987 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2988 iv, iv_size,
2989 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002990 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002991 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2992 TEST_ASSERT( output1_size <=
2993 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002994 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2997 output1, output1_size,
2998 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002999 TEST_ASSERT( output1_length <=
3000 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3001 TEST_ASSERT( output1_length <=
3002 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3003
Gilles Peskine8817f612018-12-18 00:18:46 +01003004 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003005 output1 + output1_length,
3006 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003007 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003008 TEST_ASSERT( function_output_length <=
3009 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3010 TEST_ASSERT( function_output_length <=
3011 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003012
Gilles Peskine048b7f02018-06-08 14:20:49 +02003013 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003014
Gilles Peskine8817f612018-12-18 00:18:46 +01003015 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003016
3017 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003018 TEST_ASSERT( output2_size <=
3019 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3020 TEST_ASSERT( output2_size <=
3021 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003022 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003023
Steven Cooreman177deba2020-09-07 17:14:14 +02003024 if( iv_length > 0 )
3025 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003026 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3027 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003028 }
3029
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3031 output2, output2_size,
3032 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003033 TEST_ASSERT( output2_length <=
3034 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3035 TEST_ASSERT( output2_length <=
3036 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3037
Gilles Peskine048b7f02018-06-08 14:20:49 +02003038 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_cipher_finish( &operation2,
3040 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003041 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003042 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003043 TEST_ASSERT( function_output_length <=
3044 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3045 TEST_ASSERT( function_output_length <=
3046 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003047
Gilles Peskine048b7f02018-06-08 14:20:49 +02003048 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003049
Gilles Peskine8817f612018-12-18 00:18:46 +01003050 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003051
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003052 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003053
3054exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003055 psa_cipher_abort( &operation1 );
3056 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003057 mbedtls_free( output1 );
3058 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003059 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003060 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003061}
3062/* END_CASE */
3063
3064/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003065void cipher_verify_output_multipart( int alg_arg,
3066 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003067 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003068 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003069 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003070{
Ronald Cron5425a212020-08-04 14:58:35 +02003071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003072 psa_key_type_t key_type = key_type_arg;
3073 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003074 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003075 unsigned char iv[16] = {0};
3076 size_t iv_size = 16;
3077 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003079 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003080 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003081 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003082 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003083 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003084 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003085 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3086 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003091 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3092 psa_set_key_algorithm( &attributes, alg );
3093 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003094
Ronald Cron5425a212020-08-04 14:58:35 +02003095 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3096 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003097
Ronald Cron5425a212020-08-04 14:58:35 +02003098 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3099 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003100
Steven Cooreman177deba2020-09-07 17:14:14 +02003101 if( alg != PSA_ALG_ECB_NO_PADDING )
3102 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003103 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3104 iv, iv_size,
3105 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003106 }
3107
gabor-mezei-armceface22021-01-21 12:26:17 +01003108 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3109 TEST_ASSERT( output1_buffer_size <=
3110 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003111 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003112
Gilles Peskinee0866522019-02-19 19:44:00 +01003113 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003114
Gilles Peskine8817f612018-12-18 00:18:46 +01003115 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3116 output1, output1_buffer_size,
3117 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003118 TEST_ASSERT( function_output_length <=
3119 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3120 TEST_ASSERT( function_output_length <=
3121 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003122 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003123
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_cipher_update( &operation1,
3125 input->x + first_part_size,
3126 input->len - first_part_size,
3127 output1, output1_buffer_size,
3128 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003129 TEST_ASSERT( function_output_length <=
3130 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3131 alg,
3132 input->len - first_part_size ) );
3133 TEST_ASSERT( function_output_length <=
3134 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003135 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003136
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_cipher_finish( &operation1,
3138 output1 + output1_length,
3139 output1_buffer_size - output1_length,
3140 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003141 TEST_ASSERT( function_output_length <=
3142 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3143 TEST_ASSERT( function_output_length <=
3144 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003145 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003146
Gilles Peskine8817f612018-12-18 00:18:46 +01003147 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003148
Gilles Peskine048b7f02018-06-08 14:20:49 +02003149 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003150 TEST_ASSERT( output2_buffer_size <=
3151 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3152 TEST_ASSERT( output2_buffer_size <=
3153 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003154 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003155
Steven Cooreman177deba2020-09-07 17:14:14 +02003156 if( iv_length > 0 )
3157 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003158 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3159 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003160 }
Moran Pekerded84402018-06-06 16:36:50 +03003161
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3163 output2, output2_buffer_size,
3164 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003165 TEST_ASSERT( function_output_length <=
3166 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3167 TEST_ASSERT( function_output_length <=
3168 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003169 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003170
Gilles Peskine8817f612018-12-18 00:18:46 +01003171 PSA_ASSERT( psa_cipher_update( &operation2,
3172 output1 + first_part_size,
3173 output1_length - first_part_size,
3174 output2, output2_buffer_size,
3175 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( function_output_length <=
3177 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3178 alg,
3179 output1_length - first_part_size ) );
3180 TEST_ASSERT( function_output_length <=
3181 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003182 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003183
Gilles Peskine8817f612018-12-18 00:18:46 +01003184 PSA_ASSERT( psa_cipher_finish( &operation2,
3185 output2 + output2_length,
3186 output2_buffer_size - output2_length,
3187 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003188 TEST_ASSERT( function_output_length <=
3189 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3190 TEST_ASSERT( function_output_length <=
3191 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003192 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003193
Gilles Peskine8817f612018-12-18 00:18:46 +01003194 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003195
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003196 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003197
3198exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003199 psa_cipher_abort( &operation1 );
3200 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003201 mbedtls_free( output1 );
3202 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003203 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003204 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003205}
3206/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003207
Gilles Peskine20035e32018-02-03 22:44:14 +01003208/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003209void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003210 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003211 data_t *nonce,
3212 data_t *additional_data,
3213 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003214 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003215{
Ronald Cron5425a212020-08-04 14:58:35 +02003216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003217 psa_key_type_t key_type = key_type_arg;
3218 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003219 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003220 unsigned char *output_data = NULL;
3221 size_t output_size = 0;
3222 size_t output_length = 0;
3223 unsigned char *output_data2 = NULL;
3224 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003225 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003226 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003230
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003231 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3232 psa_set_key_algorithm( &attributes, alg );
3233 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003234
Gilles Peskine049c7532019-05-15 20:22:09 +02003235 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003236 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003237 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3238 key_bits = psa_get_key_bits( &attributes );
3239
3240 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3241 alg );
3242 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3243 * should be exact. */
3244 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3245 expected_result != PSA_ERROR_NOT_SUPPORTED )
3246 {
3247 TEST_EQUAL( output_size,
3248 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3249 TEST_ASSERT( output_size <=
3250 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3251 }
3252 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253
Steven Cooremanf49478b2021-02-15 15:19:25 +01003254 status = psa_aead_encrypt( key, alg,
3255 nonce->x, nonce->len,
3256 additional_data->x,
3257 additional_data->len,
3258 input_data->x, input_data->len,
3259 output_data, output_size,
3260 &output_length );
3261
3262 /* If the operation is not supported, just skip and not fail in case the
3263 * encryption involves a common limitation of cryptography hardwares and
3264 * an alternative implementation. */
3265 if( status == PSA_ERROR_NOT_SUPPORTED )
3266 {
3267 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3268 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3269 }
3270
3271 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003272
3273 if( PSA_SUCCESS == expected_result )
3274 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003275 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003276
Gilles Peskine003a4a92019-05-14 16:09:40 +02003277 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3278 * should be exact. */
3279 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003280 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003281
gabor-mezei-armceface22021-01-21 12:26:17 +01003282 TEST_ASSERT( input_data->len <=
3283 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3284
Ronald Cron5425a212020-08-04 14:58:35 +02003285 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003286 nonce->x, nonce->len,
3287 additional_data->x,
3288 additional_data->len,
3289 output_data, output_length,
3290 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003291 &output_length2 ),
3292 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003293
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003294 ASSERT_COMPARE( input_data->x, input_data->len,
3295 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003296 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003297
Gilles Peskinea1cac842018-06-11 19:33:02 +02003298exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003299 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003300 mbedtls_free( output_data );
3301 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003302 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003307void aead_encrypt( int key_type_arg, data_t *key_data,
3308 int alg_arg,
3309 data_t *nonce,
3310 data_t *additional_data,
3311 data_t *input_data,
3312 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003313{
Ronald Cron5425a212020-08-04 14:58:35 +02003314 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315 psa_key_type_t key_type = key_type_arg;
3316 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003317 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003318 unsigned char *output_data = NULL;
3319 size_t output_size = 0;
3320 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003321 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003322 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003323
Gilles Peskine8817f612018-12-18 00:18:46 +01003324 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003325
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003326 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3327 psa_set_key_algorithm( &attributes, alg );
3328 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329
Gilles Peskine049c7532019-05-15 20:22:09 +02003330 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003331 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003332 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3333 key_bits = psa_get_key_bits( &attributes );
3334
3335 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3336 alg );
3337 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3338 * should be exact. */
3339 TEST_EQUAL( output_size,
3340 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3341 TEST_ASSERT( output_size <=
3342 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3343 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Steven Cooremand588ea12021-01-11 19:36:04 +01003345 status = psa_aead_encrypt( key, alg,
3346 nonce->x, nonce->len,
3347 additional_data->x, additional_data->len,
3348 input_data->x, input_data->len,
3349 output_data, output_size,
3350 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Ronald Cron28a45ed2021-02-09 20:35:42 +01003352 /* If the operation is not supported, just skip and not fail in case the
3353 * encryption involves a common limitation of cryptography hardwares and
3354 * an alternative implementation. */
3355 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003356 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003357 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3358 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003359 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003360
3361 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003362 ASSERT_COMPARE( expected_result->x, expected_result->len,
3363 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003364
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003366 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003368 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003369}
3370/* END_CASE */
3371
3372/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003373void aead_decrypt( int key_type_arg, data_t *key_data,
3374 int alg_arg,
3375 data_t *nonce,
3376 data_t *additional_data,
3377 data_t *input_data,
3378 data_t *expected_data,
3379 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380{
Ronald Cron5425a212020-08-04 14:58:35 +02003381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003382 psa_key_type_t key_type = key_type_arg;
3383 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003384 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385 unsigned char *output_data = NULL;
3386 size_t output_size = 0;
3387 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003389 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003390 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391
Gilles Peskine8817f612018-12-18 00:18:46 +01003392 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003394 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3395 psa_set_key_algorithm( &attributes, alg );
3396 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003397
Gilles Peskine049c7532019-05-15 20:22:09 +02003398 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003399 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003400 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3401 key_bits = psa_get_key_bits( &attributes );
3402
3403 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3404 alg );
3405 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3406 expected_result != PSA_ERROR_NOT_SUPPORTED )
3407 {
3408 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3409 * should be exact. */
3410 TEST_EQUAL( output_size,
3411 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3412 TEST_ASSERT( output_size <=
3413 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3414 }
3415 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416
Steven Cooremand588ea12021-01-11 19:36:04 +01003417 status = psa_aead_decrypt( key, alg,
3418 nonce->x, nonce->len,
3419 additional_data->x,
3420 additional_data->len,
3421 input_data->x, input_data->len,
3422 output_data, output_size,
3423 &output_length );
3424
Ronald Cron28a45ed2021-02-09 20:35:42 +01003425 /* If the operation is not supported, just skip and not fail in case the
3426 * decryption involves a common limitation of cryptography hardwares and
3427 * an alternative implementation. */
3428 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003429 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003430 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3431 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003432 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003433
3434 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003435
Gilles Peskine2d277862018-06-18 15:41:12 +02003436 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003437 ASSERT_COMPARE( expected_data->x, expected_data->len,
3438 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003439
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003441 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003443 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444}
3445/* END_CASE */
3446
3447/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003448void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3449 int alg_arg,
3450 data_t *nonce,
3451 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003452 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003453 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003454 int do_test_data_chunked,
3455 int do_set_lengths,
3456 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003457{
Paul Elliottd3f82412021-06-16 16:52:21 +01003458 size_t ad_part_len = 0;
3459 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003460
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003461 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3462
3463 /* Temporary whilst we have algorithms that cannot support chunking */
3464 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003465 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003466 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3467 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003468 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003469 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003470
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003471 if( !aead_multipart_internal_func( key_type_arg, key_data,
3472 alg_arg, nonce,
3473 additional_data,
3474 ad_part_len,
3475 input_data, -1,
3476 do_set_lengths,
3477 expected_output,
3478 1, 1 ) )
3479 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003480 }
3481 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003482
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003483 /* Temporary whilst we have algorithms that cannot support chunking */
3484 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003485 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003486 for( data_part_len = 1; data_part_len <= input_data->len;
3487 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003488 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003489 mbedtls_test_set_step( 1000 + data_part_len );
3490
3491 if( !aead_multipart_internal_func( key_type_arg, key_data,
3492 alg_arg, nonce,
3493 additional_data, -1,
3494 input_data, data_part_len,
3495 do_set_lengths,
3496 expected_output,
3497 1, 1 ) )
3498 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003499 }
3500 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003501
Paul Elliott0023e0a2021-04-27 10:06:22 +01003502
Paul Elliott8fc45162021-06-23 16:06:01 +01003503 /* Goto is required to silence warnings about unused labels, as we
3504 * don't actually do any test assertions in this function. */
3505 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003506}
3507/* END_CASE */
3508
3509/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003510void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3511 int alg_arg,
3512 data_t *nonce,
3513 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003514 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003515 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003516 int do_test_data_chunked,
3517 int do_set_lengths,
3518 data_t *expected_output,
3519 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003520{
Paul Elliottd3f82412021-06-16 16:52:21 +01003521 size_t ad_part_len = 0;
3522 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003523
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003524 /* Temporary whilst we have algorithms that cannot support chunking */
3525 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003526 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003527 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3528 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003529 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003530 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003532 if( !aead_multipart_internal_func( key_type_arg, key_data,
3533 alg_arg, nonce,
3534 additional_data,
3535 ad_part_len,
3536 input_data, -1,
3537 do_set_lengths,
3538 expected_output,
3539 expect_valid_signature,
3540 0 ) )
3541 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003542 }
3543 }
3544
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003545 /* Temporary whilst we have algorithms that cannot support chunking */
3546 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003547 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003548 for( data_part_len = 1; data_part_len <= input_data->len;
3549 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 mbedtls_test_set_step( 1000 + data_part_len );
3552
3553 if( !aead_multipart_internal_func( key_type_arg, key_data,
3554 alg_arg, nonce,
3555 additional_data, -1,
3556 input_data, data_part_len,
3557 do_set_lengths,
3558 expected_output,
3559 expect_valid_signature,
3560 0 ) )
3561 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003562 }
3563 }
3564
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. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003567 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568}
3569/* END_CASE */
3570
3571/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003572void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3573 int alg_arg,
3574 int nonce_len,
Paul Elliottd85f5472021-07-16 18:20:16 +01003575 int expected_generated_len_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003576 data_t *additional_data,
3577 data_t *input_data,
3578 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003579{
3580
3581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3582 psa_key_type_t key_type = key_type_arg;
3583 psa_algorithm_t alg = alg_arg;
3584 psa_aead_operation_t operation;
3585 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3587 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3588 size_t nonce_generated_len = 0;
Paul Elliottd85f5472021-07-16 18:20:16 +01003589 size_t expected_generated_len = expected_generated_len_arg;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003590 unsigned char *output_data = NULL;
3591 unsigned char *final_data = NULL;
3592 size_t output_size = 0;
3593 size_t finish_output_size = 0;
3594 size_t output_length = 0;
3595 size_t tag_length = 0;
3596 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003597
3598 PSA_ASSERT( psa_crypto_init( ) );
3599
3600 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3601 psa_set_key_algorithm( & attributes, alg );
3602 psa_set_key_type( & attributes, key_type );
3603
3604 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3605 &key ) );
3606
3607 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3608
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003609 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3610
3611 ASSERT_ALLOC( output_data, output_size );
3612
3613 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3614
3615 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3616
3617 ASSERT_ALLOC( final_data, finish_output_size );
3618
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003619 operation = psa_aead_operation_init( );
3620
3621 status = psa_aead_encrypt_setup( &operation, key, alg );
3622
3623 /* If the operation is not supported, just skip and not fail in case the
3624 * encryption involves a common limitation of cryptography hardwares and
3625 * an alternative implementation. */
3626 if( status == PSA_ERROR_NOT_SUPPORTED )
3627 {
3628 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3629 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len );
3630 }
3631
3632 PSA_ASSERT( status );
3633
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003634 status = psa_aead_generate_nonce( &operation, nonce_buffer,
3635 nonce_len,
3636 &nonce_generated_len );
3637
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003638 TEST_ASSERT( status == expected_status_arg );
3639
Paul Elliottd85f5472021-07-16 18:20:16 +01003640 TEST_EQUAL( nonce_generated_len, expected_generated_len );
3641
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003642 TEST_ASSERT( nonce_generated_len < PSA_AEAD_NONCE_MAX_SIZE );
3643
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003644 if( expected_status_arg == PSA_SUCCESS )
3645 {
3646
3647 /* Ensure we can still complete operation. */
3648
3649 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3650 additional_data->len ) );
3651
3652 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
3653 output_data, output_size, &output_length ) );
3654
3655 PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size,
3656 &output_length, tag_buffer,
3657 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3658 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003659
3660exit:
3661 psa_destroy_key( key );
Paul Elliott16906f92021-06-24 09:57:01 +01003662 mbedtls_free( output_data );
3663 mbedtls_free( final_data );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003664 psa_aead_abort( &operation );
3665 PSA_DONE( );
3666}
3667/* END_CASE */
3668
3669/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01003670void aead_multipart_state_test( int key_type_arg, data_t *key_data,
3671 int alg_arg,
3672 data_t *nonce,
3673 data_t *additional_data,
3674 data_t *input_data )
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 unsigned char *output_data = NULL;
3681 unsigned char *final_data = NULL;
3682 size_t output_size = 0;
3683 size_t finish_output_size = 0;
3684 size_t output_length = 0;
3685 size_t key_bits = 0;
3686 size_t tag_length = 0;
3687 size_t tag_size = 0;
3688 size_t nonce_length = 0;
3689 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3690 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3691 size_t output_part_length = 0;
3692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3693
3694 PSA_ASSERT( psa_crypto_init( ) );
3695
3696 psa_set_key_usage_flags( & attributes,
3697 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3698 psa_set_key_algorithm( & attributes, alg );
3699 psa_set_key_type( & attributes, key_type );
3700
3701 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3702 &key ) );
3703
3704 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3705 key_bits = psa_get_key_bits( &attributes );
3706
3707 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3708
3709 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
3710
3711 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3712
3713 ASSERT_ALLOC( output_data, output_size );
3714
3715 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3716
3717 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3718
3719 ASSERT_ALLOC( final_data, finish_output_size );
3720
3721 /* Test all operations error without calling setup first. */
3722
3723 operation = psa_aead_operation_init( );
3724
3725 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3726 PSA_ERROR_BAD_STATE );
3727
3728 psa_aead_abort( &operation );
3729
3730 operation = psa_aead_operation_init( );
3731
3732 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3733 PSA_AEAD_NONCE_MAX_SIZE,
3734 &nonce_length ),
3735 PSA_ERROR_BAD_STATE );
3736
3737 psa_aead_abort( &operation );
3738
Paul Elliott481be342021-07-16 17:38:47 +01003739 /* ------------------------------------------------------- */
3740
Paul Elliottc23a9a02021-06-21 18:32:46 +01003741 operation = psa_aead_operation_init( );
3742
3743 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3744 input_data->len ),
3745 PSA_ERROR_BAD_STATE );
3746
3747 psa_aead_abort( &operation );
3748
Paul Elliott481be342021-07-16 17:38:47 +01003749 /* ------------------------------------------------------- */
3750
Paul Elliottc23a9a02021-06-21 18:32:46 +01003751 operation = psa_aead_operation_init( );
3752
3753 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3754 additional_data->len ),
3755 PSA_ERROR_BAD_STATE );
3756
3757 psa_aead_abort( &operation );
3758
Paul Elliott481be342021-07-16 17:38:47 +01003759 /* ------------------------------------------------------- */
3760
Paul Elliottc23a9a02021-06-21 18:32:46 +01003761 operation = psa_aead_operation_init( );
3762
3763 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
3764 input_data->len, output_data,
3765 output_size, &output_length ),
3766 PSA_ERROR_BAD_STATE );
3767
3768 psa_aead_abort( &operation );
3769
Paul Elliott481be342021-07-16 17:38:47 +01003770 /* ------------------------------------------------------- */
3771
Paul Elliottc23a9a02021-06-21 18:32:46 +01003772 operation = psa_aead_operation_init( );
3773
3774 TEST_EQUAL( psa_aead_finish( &operation, final_data,
3775 finish_output_size,
3776 &output_part_length,
3777 tag_buffer, tag_length,
3778 &tag_size ),
3779 PSA_ERROR_BAD_STATE );
3780
3781 psa_aead_abort( &operation );
3782
Paul Elliott481be342021-07-16 17:38:47 +01003783 /* ------------------------------------------------------- */
3784
Paul Elliottc23a9a02021-06-21 18:32:46 +01003785 operation = psa_aead_operation_init( );
3786
3787 TEST_EQUAL( psa_aead_verify( &operation, final_data,
3788 finish_output_size,
3789 &output_part_length,
3790 tag_buffer,
3791 tag_length ),
3792 PSA_ERROR_BAD_STATE );
3793
3794 psa_aead_abort( &operation );
3795
3796 /* Test for double setups. */
3797
3798 operation = psa_aead_operation_init( );
3799
3800 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3801
3802 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3803 PSA_ERROR_BAD_STATE );
3804
3805 psa_aead_abort( &operation );
3806
Paul Elliott481be342021-07-16 17:38:47 +01003807 /* ------------------------------------------------------- */
3808
Paul Elliottc23a9a02021-06-21 18:32:46 +01003809 operation = psa_aead_operation_init( );
3810
3811 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3812
3813 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3814 PSA_ERROR_BAD_STATE );
3815
3816 psa_aead_abort( &operation );
3817
Paul Elliott374a2be2021-07-16 17:53:40 +01003818 /* ------------------------------------------------------- */
3819
3820 operation = psa_aead_operation_init( );
3821
3822 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3823
3824 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3825 PSA_ERROR_BAD_STATE );
3826
3827 psa_aead_abort( &operation );
3828
3829 /* ------------------------------------------------------- */
3830
3831 operation = psa_aead_operation_init( );
3832
3833 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3834
3835 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3836 PSA_ERROR_BAD_STATE );
3837
3838 psa_aead_abort( &operation );
3839
Paul Elliottc23a9a02021-06-21 18:32:46 +01003840 /* Test for not setting a nonce. */
3841
3842 operation = psa_aead_operation_init( );
3843
3844 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3845
3846 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3847 additional_data->len ),
3848 PSA_ERROR_BAD_STATE );
3849
3850 psa_aead_abort( &operation );
3851
3852 /* Test for double setting nonce. */
3853
3854 operation = psa_aead_operation_init( );
3855
3856 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3857
3858 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3859
3860 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3861 PSA_ERROR_BAD_STATE );
3862
3863 psa_aead_abort( &operation );
3864
Paul Elliott374a2be2021-07-16 17:53:40 +01003865 /* Test for double generating nonce. */
3866
3867 operation = psa_aead_operation_init( );
3868
3869 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3870
3871 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3872 PSA_AEAD_NONCE_MAX_SIZE,
3873 &nonce_length ) );
3874
3875 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3876 PSA_AEAD_NONCE_MAX_SIZE,
3877 &nonce_length ),
3878 PSA_ERROR_BAD_STATE );
3879
3880
3881 psa_aead_abort( &operation );
3882
3883 /* Test for generate nonce then set and vice versa */
3884
3885 operation = psa_aead_operation_init( );
3886
3887 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3888
3889 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3890 PSA_AEAD_NONCE_MAX_SIZE,
3891 &nonce_length ) );
3892
3893 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3894 PSA_ERROR_BAD_STATE );
3895
3896 psa_aead_abort( &operation );
3897
3898 /* ------------------------------------------------------- */
3899
3900 operation = psa_aead_operation_init( );
3901
3902 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3903
3904 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3905
3906 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3907 PSA_AEAD_NONCE_MAX_SIZE,
3908 &nonce_length ),
3909 PSA_ERROR_BAD_STATE );
3910
3911 psa_aead_abort( &operation );
3912
Paul Elliott7220cae2021-06-22 17:25:57 +01003913 /* Test for generating nonce in decrypt setup. */
3914
3915 operation = psa_aead_operation_init( );
3916
3917 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3918
3919 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3920 PSA_AEAD_NONCE_MAX_SIZE,
3921 &nonce_length ),
3922 PSA_ERROR_BAD_STATE );
3923
3924 psa_aead_abort( &operation );
3925
Paul Elliottc23a9a02021-06-21 18:32:46 +01003926 /* Test for setting lengths twice. */
3927
3928 operation = psa_aead_operation_init( );
3929
3930 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3931
3932 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3933
3934 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3935 input_data->len ) );
3936
3937 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3938 input_data->len ),
3939 PSA_ERROR_BAD_STATE );
3940
3941 psa_aead_abort( &operation );
3942
Paul Elliott01876512021-06-23 18:13:04 +01003943 /* Test that generate/set nonce and set lengths are interchangeable (we
3944 * already tested set nonce followed by set lengths above). */
3945
3946 operation = psa_aead_operation_init( );
3947
3948 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3949
3950 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3951 input_data->len ) );
3952
3953 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3954
3955 psa_aead_abort( &operation );
3956
Paul Elliott481be342021-07-16 17:38:47 +01003957 /* ------------------------------------------------------- */
3958
Paul Elliott01876512021-06-23 18:13:04 +01003959 operation = psa_aead_operation_init( );
3960
3961 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3962
3963 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3964 input_data->len ) );
3965
3966 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3967 PSA_AEAD_NONCE_MAX_SIZE,
3968 &nonce_length ) );
3969
3970 psa_aead_abort( &operation );
3971
Paul Elliott481be342021-07-16 17:38:47 +01003972 /* ------------------------------------------------------- */
3973
Paul Elliott01876512021-06-23 18:13:04 +01003974 operation = psa_aead_operation_init( );
3975
3976 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3977
3978 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3979 PSA_AEAD_NONCE_MAX_SIZE,
3980 &nonce_length ) );
3981
3982 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3983 input_data->len ) );
3984
3985 psa_aead_abort( &operation );
3986
Paul Elliottc23a9a02021-06-21 18:32:46 +01003987 /* Test for setting lengths after already starting data. */
3988
3989 operation = psa_aead_operation_init( );
3990
3991 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3992
3993 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3994
3995 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
3996 input_data->len, output_data,
3997 output_size, &output_length ) );
3998
3999 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4000 input_data->len ),
4001 PSA_ERROR_BAD_STATE );
4002
4003 psa_aead_abort( &operation );
4004
Paul Elliott243080c2021-07-21 19:01:17 +01004005 /* Test for not sending any additional data or data after setting non zero
4006 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004007
4008 operation = psa_aead_operation_init( );
4009
4010 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4011
4012 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4013
4014 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4015 input_data->len ) );
4016
4017 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4018 finish_output_size,
4019 &output_part_length,
4020 tag_buffer, tag_length,
4021 &tag_size ),
4022 PSA_ERROR_INVALID_ARGUMENT );
4023
4024 psa_aead_abort( &operation );
4025
Paul Elliott243080c2021-07-21 19:01:17 +01004026 /* Test for not sending any additional data or data after setting non-zero
4027 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004028
4029 operation = psa_aead_operation_init( );
4030
4031 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4032
4033 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4034
4035 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4036 input_data->len ) );
4037
4038 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4039 finish_output_size,
4040 &output_part_length,
4041 tag_buffer,
4042 tag_length ),
4043 PSA_ERROR_INVALID_ARGUMENT );
4044
4045 psa_aead_abort( &operation );
4046
Paul Elliott243080c2021-07-21 19:01:17 +01004047 /* Test for not sending any additional data after setting a non-zero length
4048 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004049
4050 operation = psa_aead_operation_init( );
4051
4052 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4053
4054 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4055
4056 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4057 input_data->len ) );
4058
4059 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4060 input_data->len, output_data,
4061 output_size, &output_length ),
4062 PSA_ERROR_INVALID_ARGUMENT );
4063
4064 psa_aead_abort( &operation );
4065
4066 /* Test sending additional data after data. */
4067
4068 operation = psa_aead_operation_init( );
4069
4070 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4071
4072 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4073
4074 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4075 input_data->len, output_data,
4076 output_size, &output_length ) );
4077
4078 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4079 additional_data->len ),
4080 PSA_ERROR_BAD_STATE );
4081
4082 psa_aead_abort( &operation );
4083
Paul Elliott534d0b42021-06-22 19:15:20 +01004084 /* Test calling finish on decryption. */
4085
4086 operation = psa_aead_operation_init( );
4087
4088 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4089
4090 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4091
4092 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4093 finish_output_size,
4094 &output_part_length,
4095 tag_buffer, tag_length,
4096 &tag_size ),
4097 PSA_ERROR_BAD_STATE );
4098
4099 psa_aead_abort( &operation );
4100
4101 /* Test calling verify on encryption. */
4102
4103 operation = psa_aead_operation_init( );
4104
4105 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4106
4107 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4108
4109 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4110 finish_output_size,
4111 &output_part_length,
4112 tag_buffer,
4113 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004114 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004115
4116 psa_aead_abort( &operation );
4117
4118
Paul Elliottc23a9a02021-06-21 18:32:46 +01004119exit:
4120 psa_destroy_key( key );
4121 psa_aead_abort( &operation );
4122 mbedtls_free( output_data );
4123 mbedtls_free( final_data );
4124 PSA_DONE( );
4125}
4126/* END_CASE */
4127
4128/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004129void signature_size( int type_arg,
4130 int bits,
4131 int alg_arg,
4132 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004133{
4134 psa_key_type_t type = type_arg;
4135 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004136 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004137
Gilles Peskinefe11b722018-12-18 00:24:04 +01004138 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004139
Gilles Peskinee59236f2018-01-27 23:32:46 +01004140exit:
4141 ;
4142}
4143/* END_CASE */
4144
4145/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004146void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4147 int alg_arg, data_t *input_data,
4148 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004149{
Ronald Cron5425a212020-08-04 14:58:35 +02004150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004151 psa_key_type_t key_type = key_type_arg;
4152 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004153 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004154 unsigned char *signature = NULL;
4155 size_t signature_size;
4156 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004158
Gilles Peskine8817f612018-12-18 00:18:46 +01004159 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004160
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004161 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004162 psa_set_key_algorithm( &attributes, alg );
4163 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004164
Gilles Peskine049c7532019-05-15 20:22:09 +02004165 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004166 &key ) );
4167 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004168 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004169
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004170 /* Allocate a buffer which has the size advertized by the
4171 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004172 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004173 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004174 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004175 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004176 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004177
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004178 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004179 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004180 input_data->x, input_data->len,
4181 signature, signature_size,
4182 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004183 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004184 ASSERT_COMPARE( output_data->x, output_data->len,
4185 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004186
4187exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004188 /*
4189 * Key attributes may have been returned by psa_get_key_attributes()
4190 * thus reset them as required.
4191 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004192 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004193
Ronald Cron5425a212020-08-04 14:58:35 +02004194 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004195 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004196 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004197}
4198/* END_CASE */
4199
4200/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004201void sign_hash_fail( int key_type_arg, data_t *key_data,
4202 int alg_arg, data_t *input_data,
4203 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004204{
Ronald Cron5425a212020-08-04 14:58:35 +02004205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004206 psa_key_type_t key_type = key_type_arg;
4207 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004208 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004209 psa_status_t actual_status;
4210 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004211 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004212 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004214
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004215 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004216
Gilles Peskine8817f612018-12-18 00:18:46 +01004217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004218
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004220 psa_set_key_algorithm( &attributes, alg );
4221 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004222
Gilles Peskine049c7532019-05-15 20:22:09 +02004223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004224 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004225
Ronald Cron5425a212020-08-04 14:58:35 +02004226 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004227 input_data->x, input_data->len,
4228 signature, signature_size,
4229 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004230 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004231 /* The value of *signature_length is unspecified on error, but
4232 * whatever it is, it should be less than signature_size, so that
4233 * if the caller tries to read *signature_length bytes without
4234 * checking the error code then they don't overflow a buffer. */
4235 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004236
4237exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004238 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004239 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004240 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004241 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004242}
4243/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004244
4245/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004246void sign_verify_hash( int key_type_arg, data_t *key_data,
4247 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004248{
Ronald Cron5425a212020-08-04 14:58:35 +02004249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004250 psa_key_type_t key_type = key_type_arg;
4251 psa_algorithm_t alg = alg_arg;
4252 size_t key_bits;
4253 unsigned char *signature = NULL;
4254 size_t signature_size;
4255 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004257
Gilles Peskine8817f612018-12-18 00:18:46 +01004258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004259
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004261 psa_set_key_algorithm( &attributes, alg );
4262 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004263
Gilles Peskine049c7532019-05-15 20:22:09 +02004264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004265 &key ) );
4266 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004267 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004268
4269 /* Allocate a buffer which has the size advertized by the
4270 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004271 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004272 key_bits, alg );
4273 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004274 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004275 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004276
4277 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004278 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004279 input_data->x, input_data->len,
4280 signature, signature_size,
4281 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004282 /* Check that the signature length looks sensible. */
4283 TEST_ASSERT( signature_length <= signature_size );
4284 TEST_ASSERT( signature_length > 0 );
4285
4286 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004287 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004288 input_data->x, input_data->len,
4289 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004290
4291 if( input_data->len != 0 )
4292 {
4293 /* Flip a bit in the input and verify that the signature is now
4294 * detected as invalid. Flip a bit at the beginning, not at the end,
4295 * because ECDSA may ignore the last few bits of the input. */
4296 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004297 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004298 input_data->x, input_data->len,
4299 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004300 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004301 }
4302
4303exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004304 /*
4305 * Key attributes may have been returned by psa_get_key_attributes()
4306 * thus reset them as required.
4307 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004308 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004309
Ronald Cron5425a212020-08-04 14:58:35 +02004310 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004311 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004312 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004313}
4314/* END_CASE */
4315
4316/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004317void verify_hash( int key_type_arg, data_t *key_data,
4318 int alg_arg, data_t *hash_data,
4319 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004320{
Ronald Cron5425a212020-08-04 14:58:35 +02004321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004322 psa_key_type_t key_type = key_type_arg;
4323 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004325
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004326 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004327
Gilles Peskine8817f612018-12-18 00:18:46 +01004328 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004329
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004331 psa_set_key_algorithm( &attributes, alg );
4332 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004333
Gilles Peskine049c7532019-05-15 20:22:09 +02004334 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004335 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004336
Ronald Cron5425a212020-08-04 14:58:35 +02004337 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004338 hash_data->x, hash_data->len,
4339 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004340
itayzafrir5c753392018-05-08 11:18:38 +03004341exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004342 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004343 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004344 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004345}
4346/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004347
4348/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004349void verify_hash_fail( int key_type_arg, data_t *key_data,
4350 int alg_arg, data_t *hash_data,
4351 data_t *signature_data,
4352 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004353{
Ronald Cron5425a212020-08-04 14:58:35 +02004354 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004355 psa_key_type_t key_type = key_type_arg;
4356 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004357 psa_status_t actual_status;
4358 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004360
Gilles Peskine8817f612018-12-18 00:18:46 +01004361 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004362
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004364 psa_set_key_algorithm( &attributes, alg );
4365 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004366
Gilles Peskine049c7532019-05-15 20:22:09 +02004367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004368 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004369
Ronald Cron5425a212020-08-04 14:58:35 +02004370 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004371 hash_data->x, hash_data->len,
4372 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004373 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004374
4375exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004376 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004377 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004378 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004379}
4380/* END_CASE */
4381
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004382/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004383void sign_message_deterministic( int key_type_arg,
4384 data_t *key_data,
4385 int alg_arg,
4386 data_t *input_data,
4387 data_t *output_data )
4388{
4389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4390 psa_key_type_t key_type = key_type_arg;
4391 psa_algorithm_t alg = alg_arg;
4392 size_t key_bits;
4393 unsigned char *signature = NULL;
4394 size_t signature_size;
4395 size_t signature_length = 0xdeadbeef;
4396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4397
4398 PSA_ASSERT( psa_crypto_init( ) );
4399
4400 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4401 psa_set_key_algorithm( &attributes, alg );
4402 psa_set_key_type( &attributes, key_type );
4403
4404 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4405 &key ) );
4406 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4407 key_bits = psa_get_key_bits( &attributes );
4408
4409 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4410 TEST_ASSERT( signature_size != 0 );
4411 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4412 ASSERT_ALLOC( signature, signature_size );
4413
4414 PSA_ASSERT( psa_sign_message( key, alg,
4415 input_data->x, input_data->len,
4416 signature, signature_size,
4417 &signature_length ) );
4418
4419 ASSERT_COMPARE( output_data->x, output_data->len,
4420 signature, signature_length );
4421
4422exit:
4423 psa_reset_key_attributes( &attributes );
4424
4425 psa_destroy_key( key );
4426 mbedtls_free( signature );
4427 PSA_DONE( );
4428
4429}
4430/* END_CASE */
4431
4432/* BEGIN_CASE */
4433void sign_message_fail( int key_type_arg,
4434 data_t *key_data,
4435 int alg_arg,
4436 data_t *input_data,
4437 int signature_size_arg,
4438 int expected_status_arg )
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 signature_size = signature_size_arg;
4444 psa_status_t actual_status;
4445 psa_status_t expected_status = expected_status_arg;
4446 unsigned char *signature = NULL;
4447 size_t signature_length = 0xdeadbeef;
4448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4449
4450 ASSERT_ALLOC( signature, signature_size );
4451
4452 PSA_ASSERT( psa_crypto_init( ) );
4453
4454 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4455 psa_set_key_algorithm( &attributes, alg );
4456 psa_set_key_type( &attributes, key_type );
4457
4458 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4459 &key ) );
4460
4461 actual_status = psa_sign_message( key, alg,
4462 input_data->x, input_data->len,
4463 signature, signature_size,
4464 &signature_length );
4465 TEST_EQUAL( actual_status, expected_status );
4466 /* The value of *signature_length is unspecified on error, but
4467 * whatever it is, it should be less than signature_size, so that
4468 * if the caller tries to read *signature_length bytes without
4469 * checking the error code then they don't overflow a buffer. */
4470 TEST_ASSERT( signature_length <= signature_size );
4471
4472exit:
4473 psa_reset_key_attributes( &attributes );
4474 psa_destroy_key( key );
4475 mbedtls_free( signature );
4476 PSA_DONE( );
4477}
4478/* END_CASE */
4479
4480/* BEGIN_CASE */
4481void sign_verify_message( int key_type_arg,
4482 data_t *key_data,
4483 int alg_arg,
4484 data_t *input_data )
4485{
4486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4487 psa_key_type_t key_type = key_type_arg;
4488 psa_algorithm_t alg = alg_arg;
4489 size_t key_bits;
4490 unsigned char *signature = NULL;
4491 size_t signature_size;
4492 size_t signature_length = 0xdeadbeef;
4493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4494
4495 PSA_ASSERT( psa_crypto_init( ) );
4496
4497 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4498 PSA_KEY_USAGE_VERIFY_MESSAGE );
4499 psa_set_key_algorithm( &attributes, alg );
4500 psa_set_key_type( &attributes, key_type );
4501
4502 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4503 &key ) );
4504 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4505 key_bits = psa_get_key_bits( &attributes );
4506
4507 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4508 TEST_ASSERT( signature_size != 0 );
4509 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4510 ASSERT_ALLOC( signature, signature_size );
4511
4512 PSA_ASSERT( psa_sign_message( key, alg,
4513 input_data->x, input_data->len,
4514 signature, signature_size,
4515 &signature_length ) );
4516 TEST_ASSERT( signature_length <= signature_size );
4517 TEST_ASSERT( signature_length > 0 );
4518
4519 PSA_ASSERT( psa_verify_message( key, alg,
4520 input_data->x, input_data->len,
4521 signature, signature_length ) );
4522
4523 if( input_data->len != 0 )
4524 {
4525 /* Flip a bit in the input and verify that the signature is now
4526 * detected as invalid. Flip a bit at the beginning, not at the end,
4527 * because ECDSA may ignore the last few bits of the input. */
4528 input_data->x[0] ^= 1;
4529 TEST_EQUAL( psa_verify_message( key, alg,
4530 input_data->x, input_data->len,
4531 signature, signature_length ),
4532 PSA_ERROR_INVALID_SIGNATURE );
4533 }
4534
4535exit:
4536 psa_reset_key_attributes( &attributes );
4537
4538 psa_destroy_key( key );
4539 mbedtls_free( signature );
4540 PSA_DONE( );
4541}
4542/* END_CASE */
4543
4544/* BEGIN_CASE */
4545void verify_message( int key_type_arg,
4546 data_t *key_data,
4547 int alg_arg,
4548 data_t *input_data,
4549 data_t *signature_data )
4550{
4551 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4552 psa_key_type_t key_type = key_type_arg;
4553 psa_algorithm_t alg = alg_arg;
4554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4555
4556 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4557
4558 PSA_ASSERT( psa_crypto_init( ) );
4559
4560 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4561 psa_set_key_algorithm( &attributes, alg );
4562 psa_set_key_type( &attributes, key_type );
4563
4564 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4565 &key ) );
4566
4567 PSA_ASSERT( psa_verify_message( key, alg,
4568 input_data->x, input_data->len,
4569 signature_data->x, signature_data->len ) );
4570
4571exit:
4572 psa_reset_key_attributes( &attributes );
4573 psa_destroy_key( key );
4574 PSA_DONE( );
4575}
4576/* END_CASE */
4577
4578/* BEGIN_CASE */
4579void verify_message_fail( int key_type_arg,
4580 data_t *key_data,
4581 int alg_arg,
4582 data_t *hash_data,
4583 data_t *signature_data,
4584 int expected_status_arg )
4585{
4586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4587 psa_key_type_t key_type = key_type_arg;
4588 psa_algorithm_t alg = alg_arg;
4589 psa_status_t actual_status;
4590 psa_status_t expected_status = expected_status_arg;
4591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4592
4593 PSA_ASSERT( psa_crypto_init( ) );
4594
4595 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4596 psa_set_key_algorithm( &attributes, alg );
4597 psa_set_key_type( &attributes, key_type );
4598
4599 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4600 &key ) );
4601
4602 actual_status = psa_verify_message( key, alg,
4603 hash_data->x, hash_data->len,
4604 signature_data->x,
4605 signature_data->len );
4606 TEST_EQUAL( actual_status, expected_status );
4607
4608exit:
4609 psa_reset_key_attributes( &attributes );
4610 psa_destroy_key( key );
4611 PSA_DONE( );
4612}
4613/* END_CASE */
4614
4615/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004616void asymmetric_encrypt( int key_type_arg,
4617 data_t *key_data,
4618 int alg_arg,
4619 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004620 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004621 int expected_output_length_arg,
4622 int expected_status_arg )
4623{
Ronald Cron5425a212020-08-04 14:58:35 +02004624 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004625 psa_key_type_t key_type = key_type_arg;
4626 psa_algorithm_t alg = alg_arg;
4627 size_t expected_output_length = expected_output_length_arg;
4628 size_t key_bits;
4629 unsigned char *output = NULL;
4630 size_t output_size;
4631 size_t output_length = ~0;
4632 psa_status_t actual_status;
4633 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004635
Gilles Peskine8817f612018-12-18 00:18:46 +01004636 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004637
Gilles Peskine656896e2018-06-29 19:12:28 +02004638 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004639 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4640 psa_set_key_algorithm( &attributes, alg );
4641 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004642 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004643 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004644
4645 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004646 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004647 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004648
Gilles Peskine656896e2018-06-29 19:12:28 +02004649 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004650 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004651 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004652
4653 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004654 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004655 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004656 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004657 output, output_size,
4658 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004659 TEST_EQUAL( actual_status, expected_status );
4660 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004661
Gilles Peskine68428122018-06-30 18:42:41 +02004662 /* If the label is empty, the test framework puts a non-null pointer
4663 * in label->x. Test that a null pointer works as well. */
4664 if( label->len == 0 )
4665 {
4666 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004667 if( output_size != 0 )
4668 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004669 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004670 input_data->x, input_data->len,
4671 NULL, label->len,
4672 output, output_size,
4673 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004674 TEST_EQUAL( actual_status, expected_status );
4675 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004676 }
4677
Gilles Peskine656896e2018-06-29 19:12:28 +02004678exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004679 /*
4680 * Key attributes may have been returned by psa_get_key_attributes()
4681 * thus reset them as required.
4682 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004683 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004684
Ronald Cron5425a212020-08-04 14:58:35 +02004685 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004686 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004687 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004688}
4689/* END_CASE */
4690
4691/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004692void asymmetric_encrypt_decrypt( int key_type_arg,
4693 data_t *key_data,
4694 int alg_arg,
4695 data_t *input_data,
4696 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004697{
Ronald Cron5425a212020-08-04 14:58:35 +02004698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004699 psa_key_type_t key_type = key_type_arg;
4700 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004701 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004702 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004703 size_t output_size;
4704 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004705 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004706 size_t output2_size;
4707 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004708 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004709
Gilles Peskine8817f612018-12-18 00:18:46 +01004710 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004711
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004712 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4713 psa_set_key_algorithm( &attributes, alg );
4714 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004715
Gilles Peskine049c7532019-05-15 20:22:09 +02004716 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004717 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004718
4719 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004720 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004721 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004722
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004723 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004724 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004725 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004726
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004727 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004728 TEST_ASSERT( output2_size <=
4729 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4730 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004731 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004732
Gilles Peskineeebd7382018-06-08 18:11:54 +02004733 /* We test encryption by checking that encrypt-then-decrypt gives back
4734 * the original plaintext because of the non-optional random
4735 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004736 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004737 input_data->x, input_data->len,
4738 label->x, label->len,
4739 output, output_size,
4740 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004741 /* We don't know what ciphertext length to expect, but check that
4742 * it looks sensible. */
4743 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004744
Ronald Cron5425a212020-08-04 14:58:35 +02004745 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004746 output, output_length,
4747 label->x, label->len,
4748 output2, output2_size,
4749 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004750 ASSERT_COMPARE( input_data->x, input_data->len,
4751 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004752
4753exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004754 /*
4755 * Key attributes may have been returned by psa_get_key_attributes()
4756 * thus reset them as required.
4757 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004758 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004759
Ronald Cron5425a212020-08-04 14:58:35 +02004760 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004761 mbedtls_free( output );
4762 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004763 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004764}
4765/* END_CASE */
4766
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004767/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004768void asymmetric_decrypt( int key_type_arg,
4769 data_t *key_data,
4770 int alg_arg,
4771 data_t *input_data,
4772 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004773 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004774{
Ronald Cron5425a212020-08-04 14:58:35 +02004775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004776 psa_key_type_t key_type = key_type_arg;
4777 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004778 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004779 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004780 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004781 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004783
Gilles Peskine8817f612018-12-18 00:18:46 +01004784 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004785
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004786 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4787 psa_set_key_algorithm( &attributes, alg );
4788 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004789
Gilles Peskine049c7532019-05-15 20:22:09 +02004790 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004791 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004792
gabor-mezei-armceface22021-01-21 12:26:17 +01004793 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4794 key_bits = psa_get_key_bits( &attributes );
4795
4796 /* Determine the maximum ciphertext length */
4797 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4798 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4799 ASSERT_ALLOC( output, output_size );
4800
Ronald Cron5425a212020-08-04 14:58:35 +02004801 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004802 input_data->x, input_data->len,
4803 label->x, label->len,
4804 output,
4805 output_size,
4806 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004807 ASSERT_COMPARE( expected_data->x, expected_data->len,
4808 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004809
Gilles Peskine68428122018-06-30 18:42:41 +02004810 /* If the label is empty, the test framework puts a non-null pointer
4811 * in label->x. Test that a null pointer works as well. */
4812 if( label->len == 0 )
4813 {
4814 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004815 if( output_size != 0 )
4816 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004817 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004818 input_data->x, input_data->len,
4819 NULL, label->len,
4820 output,
4821 output_size,
4822 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004823 ASSERT_COMPARE( expected_data->x, expected_data->len,
4824 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004825 }
4826
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004827exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004828 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004829 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004830 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004831 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004832}
4833/* END_CASE */
4834
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004835/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004836void asymmetric_decrypt_fail( int key_type_arg,
4837 data_t *key_data,
4838 int alg_arg,
4839 data_t *input_data,
4840 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004841 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004842 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004843{
Ronald Cron5425a212020-08-04 14:58:35 +02004844 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004845 psa_key_type_t key_type = key_type_arg;
4846 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004847 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004848 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004849 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004850 psa_status_t actual_status;
4851 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004853
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004854 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004855
Gilles Peskine8817f612018-12-18 00:18:46 +01004856 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004857
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004858 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4859 psa_set_key_algorithm( &attributes, alg );
4860 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004861
Gilles Peskine049c7532019-05-15 20:22:09 +02004862 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004863 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004864
Ronald Cron5425a212020-08-04 14:58:35 +02004865 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004866 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004867 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004868 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004869 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004870 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004871 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004872
Gilles Peskine68428122018-06-30 18:42:41 +02004873 /* If the label is empty, the test framework puts a non-null pointer
4874 * in label->x. Test that a null pointer works as well. */
4875 if( label->len == 0 )
4876 {
4877 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004878 if( output_size != 0 )
4879 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004880 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004881 input_data->x, input_data->len,
4882 NULL, label->len,
4883 output, output_size,
4884 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004885 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004886 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004887 }
4888
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004889exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004890 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004891 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004892 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004893 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004894}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004895/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004896
4897/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004898void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004899{
4900 /* Test each valid way of initializing the object, except for `= {0}`, as
4901 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4902 * though it's OK by the C standard. We could test for this, but we'd need
4903 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004904 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004905 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4906 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4907 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004908
4909 memset( &zero, 0, sizeof( zero ) );
4910
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004912 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004913 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004914 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004915 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004916 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004917 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004920 PSA_ASSERT( psa_key_derivation_abort(&func) );
4921 PSA_ASSERT( psa_key_derivation_abort(&init) );
4922 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004923}
4924/* END_CASE */
4925
Janos Follath16de4a42019-06-13 16:32:24 +01004926/* BEGIN_CASE */
4927void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004928{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004929 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004930 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004932
Gilles Peskine8817f612018-12-18 00:18:46 +01004933 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004934
Janos Follath16de4a42019-06-13 16:32:24 +01004935 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004936 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004937
4938exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004940 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004941}
4942/* END_CASE */
4943
Janos Follathaf3c2a02019-06-12 12:34:34 +01004944/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004945void derive_set_capacity( int alg_arg, int capacity_arg,
4946 int expected_status_arg )
4947{
4948 psa_algorithm_t alg = alg_arg;
4949 size_t capacity = capacity_arg;
4950 psa_status_t expected_status = expected_status_arg;
4951 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4952
4953 PSA_ASSERT( psa_crypto_init( ) );
4954
4955 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4956
4957 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4958 expected_status );
4959
4960exit:
4961 psa_key_derivation_abort( &operation );
4962 PSA_DONE( );
4963}
4964/* END_CASE */
4965
4966/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004967void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004968 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004969 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004970 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004971 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004972 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004973 int expected_status_arg3,
4974 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004975{
4976 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004977 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4978 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004979 psa_status_t expected_statuses[] = {expected_status_arg1,
4980 expected_status_arg2,
4981 expected_status_arg3};
4982 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004983 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4984 MBEDTLS_SVC_KEY_ID_INIT,
4985 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004986 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4987 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4988 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004989 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004990 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004991 psa_status_t expected_output_status = expected_output_status_arg;
4992 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004993
4994 PSA_ASSERT( psa_crypto_init( ) );
4995
4996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4997 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004998
4999 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5000
5001 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5002 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005003 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005004 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005005 psa_set_key_type( &attributes, key_types[i] );
5006 PSA_ASSERT( psa_import_key( &attributes,
5007 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005008 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005009 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5010 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5011 {
5012 // When taking a private key as secret input, use key agreement
5013 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005014 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5015 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005016 expected_statuses[i] );
5017 }
5018 else
5019 {
5020 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005021 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005022 expected_statuses[i] );
5023 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005024 }
5025 else
5026 {
5027 TEST_EQUAL( psa_key_derivation_input_bytes(
5028 &operation, steps[i],
5029 inputs[i]->x, inputs[i]->len ),
5030 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005031 }
5032 }
5033
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005034 if( output_key_type != PSA_KEY_TYPE_NONE )
5035 {
5036 psa_reset_key_attributes( &attributes );
5037 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5038 psa_set_key_bits( &attributes, 8 );
5039 actual_output_status =
5040 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005041 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005042 }
5043 else
5044 {
5045 uint8_t buffer[1];
5046 actual_output_status =
5047 psa_key_derivation_output_bytes( &operation,
5048 buffer, sizeof( buffer ) );
5049 }
5050 TEST_EQUAL( actual_output_status, expected_output_status );
5051
Janos Follathaf3c2a02019-06-12 12:34:34 +01005052exit:
5053 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005054 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5055 psa_destroy_key( keys[i] );
5056 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005057 PSA_DONE( );
5058}
5059/* END_CASE */
5060
Janos Follathd958bb72019-07-03 15:02:16 +01005061/* BEGIN_CASE */
5062void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005063{
Janos Follathd958bb72019-07-03 15:02:16 +01005064 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005066 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005068 unsigned char input1[] = "Input 1";
5069 size_t input1_length = sizeof( input1 );
5070 unsigned char input2[] = "Input 2";
5071 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005072 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005073 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005074 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5075 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5076 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005077 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005078
Gilles Peskine8817f612018-12-18 00:18:46 +01005079 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005080
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005081 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5082 psa_set_key_algorithm( &attributes, alg );
5083 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005084
Gilles Peskine73676cb2019-05-15 20:15:10 +02005085 PSA_ASSERT( psa_import_key( &attributes,
5086 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005087 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005088
5089 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005090 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5091 input1, input1_length,
5092 input2, input2_length,
5093 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005094 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005095
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005096 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005097 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005098 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005099
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005101
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005102 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005103 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005104
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005105exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005107 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005108 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005109}
5110/* END_CASE */
5111
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005112/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005113void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005114{
5115 uint8_t output_buffer[16];
5116 size_t buffer_size = 16;
5117 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005119
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005120 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5121 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005122 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005123
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005125 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005126
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005128
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005129 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5130 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005131 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005132
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005133 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005134 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005135
5136exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005138}
5139/* END_CASE */
5140
5141/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005142void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005143 int step1_arg, data_t *input1,
5144 int step2_arg, data_t *input2,
5145 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005146 int requested_capacity_arg,
5147 data_t *expected_output1,
5148 data_t *expected_output2 )
5149{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005150 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005151 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5152 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005153 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5154 MBEDTLS_SVC_KEY_ID_INIT,
5155 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005156 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005158 uint8_t *expected_outputs[2] =
5159 {expected_output1->x, expected_output2->x};
5160 size_t output_sizes[2] =
5161 {expected_output1->len, expected_output2->len};
5162 size_t output_buffer_size = 0;
5163 uint8_t *output_buffer = NULL;
5164 size_t expected_capacity;
5165 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005167 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005168 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005169
5170 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5171 {
5172 if( output_sizes[i] > output_buffer_size )
5173 output_buffer_size = output_sizes[i];
5174 if( output_sizes[i] == 0 )
5175 expected_outputs[i] = NULL;
5176 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005177 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005178 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005179
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5181 psa_set_key_algorithm( &attributes, alg );
5182 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005183
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005184 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005185 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5186 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5187 requested_capacity ) );
5188 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005190 switch( steps[i] )
5191 {
5192 case 0:
5193 break;
5194 case PSA_KEY_DERIVATION_INPUT_SECRET:
5195 PSA_ASSERT( psa_import_key( &attributes,
5196 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005197 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005198
5199 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5200 {
5201 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5202 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5203 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5204 }
5205
Gilles Peskine1468da72019-05-29 17:35:49 +02005206 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005207 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005208 break;
5209 default:
5210 PSA_ASSERT( psa_key_derivation_input_bytes(
5211 &operation, steps[i],
5212 inputs[i]->x, inputs[i]->len ) );
5213 break;
5214 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005216
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005218 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005219 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005220 expected_capacity = requested_capacity;
5221
5222 /* Expansion phase. */
5223 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5224 {
5225 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005227 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005228 if( expected_capacity == 0 && output_sizes[i] == 0 )
5229 {
5230 /* Reading 0 bytes when 0 bytes are available can go either way. */
5231 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005232 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005233 continue;
5234 }
5235 else if( expected_capacity == 0 ||
5236 output_sizes[i] > expected_capacity )
5237 {
5238 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005239 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005240 expected_capacity = 0;
5241 continue;
5242 }
5243 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005244 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005245 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005246 ASSERT_COMPARE( output_buffer, output_sizes[i],
5247 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005249 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005251 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005252 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005253 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005254 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005255
5256exit:
5257 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005258 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005259 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5260 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005261 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005262}
5263/* END_CASE */
5264
5265/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005266void derive_full( int alg_arg,
5267 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005268 data_t *input1,
5269 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005270 int requested_capacity_arg )
5271{
Ronald Cron5425a212020-08-04 14:58:35 +02005272 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005273 psa_algorithm_t alg = alg_arg;
5274 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005276 unsigned char output_buffer[16];
5277 size_t expected_capacity = requested_capacity;
5278 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005280
Gilles Peskine8817f612018-12-18 00:18:46 +01005281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005282
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5284 psa_set_key_algorithm( &attributes, alg );
5285 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005286
Gilles Peskine049c7532019-05-15 20:22:09 +02005287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005288 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005289
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005290 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5291 input1->x, input1->len,
5292 input2->x, input2->len,
5293 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005294 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005295
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005297 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005298 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005299
5300 /* Expansion phase. */
5301 while( current_capacity > 0 )
5302 {
5303 size_t read_size = sizeof( output_buffer );
5304 if( read_size > current_capacity )
5305 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005307 output_buffer,
5308 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005309 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005310 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005311 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005312 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005313 }
5314
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 /* Check that the operation refuses to go over capacity. */
5316 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005317 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005318
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005319 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005320
5321exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005323 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005324 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005325}
5326/* END_CASE */
5327
Janos Follathe60c9052019-07-03 13:51:30 +01005328/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005329void derive_key_exercise( int alg_arg,
5330 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005331 data_t *input1,
5332 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005333 int derived_type_arg,
5334 int derived_bits_arg,
5335 int derived_usage_arg,
5336 int derived_alg_arg )
5337{
Ronald Cron5425a212020-08-04 14:58:35 +02005338 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5339 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005340 psa_algorithm_t alg = alg_arg;
5341 psa_key_type_t derived_type = derived_type_arg;
5342 size_t derived_bits = derived_bits_arg;
5343 psa_key_usage_t derived_usage = derived_usage_arg;
5344 psa_algorithm_t derived_alg = derived_alg_arg;
5345 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005348 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005349
Gilles Peskine8817f612018-12-18 00:18:46 +01005350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005351
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5353 psa_set_key_algorithm( &attributes, alg );
5354 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005355 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005356 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005357
5358 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005359 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5360 input1->x, input1->len,
5361 input2->x, input2->len,
5362 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005363 goto exit;
5364
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005365 psa_set_key_usage_flags( &attributes, derived_usage );
5366 psa_set_key_algorithm( &attributes, derived_alg );
5367 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005368 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005370 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005371
5372 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005373 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005374 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5375 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005376
5377 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005378 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005379 goto exit;
5380
5381exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005382 /*
5383 * Key attributes may have been returned by psa_get_key_attributes()
5384 * thus reset them as required.
5385 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005386 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005387
5388 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005389 psa_destroy_key( base_key );
5390 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005391 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005392}
5393/* END_CASE */
5394
Janos Follath42fd8882019-07-03 14:17:09 +01005395/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005396void derive_key_export( int alg_arg,
5397 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005398 data_t *input1,
5399 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005400 int bytes1_arg,
5401 int bytes2_arg )
5402{
Ronald Cron5425a212020-08-04 14:58:35 +02005403 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5404 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005405 psa_algorithm_t alg = alg_arg;
5406 size_t bytes1 = bytes1_arg;
5407 size_t bytes2 = bytes2_arg;
5408 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005410 uint8_t *output_buffer = NULL;
5411 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005412 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5413 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005414 size_t length;
5415
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005416 ASSERT_ALLOC( output_buffer, capacity );
5417 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005418 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005419
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005420 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5421 psa_set_key_algorithm( &base_attributes, alg );
5422 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005423 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005424 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005425
5426 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005427 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5428 input1->x, input1->len,
5429 input2->x, input2->len,
5430 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005431 goto exit;
5432
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005434 output_buffer,
5435 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005437
5438 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005439 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5440 input1->x, input1->len,
5441 input2->x, input2->len,
5442 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005443 goto exit;
5444
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005445 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5446 psa_set_key_algorithm( &derived_attributes, 0 );
5447 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005448 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005449 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005450 &derived_key ) );
5451 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005452 export_buffer, bytes1,
5453 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005454 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005455 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005456 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005458 &derived_key ) );
5459 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005460 export_buffer + bytes1, bytes2,
5461 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005462 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005463
5464 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005465 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5466 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005467
5468exit:
5469 mbedtls_free( output_buffer );
5470 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005471 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005472 psa_destroy_key( base_key );
5473 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005474 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005475}
5476/* END_CASE */
5477
5478/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005479void derive_key( int alg_arg,
5480 data_t *key_data, data_t *input1, data_t *input2,
5481 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005482 int expected_status_arg,
5483 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005484{
Ronald Cron5425a212020-08-04 14:58:35 +02005485 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5486 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005487 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005488 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005489 size_t bits = bits_arg;
5490 psa_status_t expected_status = expected_status_arg;
5491 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5492 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5493 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5494
5495 PSA_ASSERT( psa_crypto_init( ) );
5496
5497 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5498 psa_set_key_algorithm( &base_attributes, alg );
5499 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5500 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005501 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005502
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005503 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5504 input1->x, input1->len,
5505 input2->x, input2->len,
5506 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005507 goto exit;
5508
5509 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5510 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005511 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005512 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005513
5514 psa_status_t status =
5515 psa_key_derivation_output_key( &derived_attributes,
5516 &operation,
5517 &derived_key );
5518 if( is_large_output > 0 )
5519 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5520 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005521
5522exit:
5523 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005524 psa_destroy_key( base_key );
5525 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005526 PSA_DONE( );
5527}
5528/* END_CASE */
5529
5530/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005531void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005532 int our_key_type_arg, int our_key_alg_arg,
5533 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005534 int expected_status_arg )
5535{
Ronald Cron5425a212020-08-04 14:58:35 +02005536 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005537 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005538 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005539 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005540 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005541 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005542 psa_status_t expected_status = expected_status_arg;
5543 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005544
Gilles Peskine8817f612018-12-18 00:18:46 +01005545 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005546
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005547 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005548 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005549 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005550 PSA_ASSERT( psa_import_key( &attributes,
5551 our_key_data->x, our_key_data->len,
5552 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005553
Gilles Peskine77f40d82019-04-11 21:27:06 +02005554 /* The tests currently include inputs that should fail at either step.
5555 * Test cases that fail at the setup step should be changed to call
5556 * key_derivation_setup instead, and this function should be renamed
5557 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005559 if( status == PSA_SUCCESS )
5560 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005561 TEST_EQUAL( psa_key_derivation_key_agreement(
5562 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5563 our_key,
5564 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005565 expected_status );
5566 }
5567 else
5568 {
5569 TEST_ASSERT( status == expected_status );
5570 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005571
5572exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005573 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005574 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005575 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005576}
5577/* END_CASE */
5578
5579/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005580void raw_key_agreement( int alg_arg,
5581 int our_key_type_arg, data_t *our_key_data,
5582 data_t *peer_key_data,
5583 data_t *expected_output )
5584{
Ronald Cron5425a212020-08-04 14:58:35 +02005585 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005586 psa_algorithm_t alg = alg_arg;
5587 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005589 unsigned char *output = NULL;
5590 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005591 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005592
5593 ASSERT_ALLOC( output, expected_output->len );
5594 PSA_ASSERT( psa_crypto_init( ) );
5595
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005596 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5597 psa_set_key_algorithm( &attributes, alg );
5598 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005599 PSA_ASSERT( psa_import_key( &attributes,
5600 our_key_data->x, our_key_data->len,
5601 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005602
gabor-mezei-armceface22021-01-21 12:26:17 +01005603 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5604 key_bits = psa_get_key_bits( &attributes );
5605
Gilles Peskinebe697d82019-05-16 18:00:41 +02005606 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5607 peer_key_data->x, peer_key_data->len,
5608 output, expected_output->len,
5609 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005610 ASSERT_COMPARE( output, output_length,
5611 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005612 TEST_ASSERT( output_length <=
5613 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5614 TEST_ASSERT( output_length <=
5615 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005616
5617exit:
5618 mbedtls_free( output );
5619 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005620 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005621}
5622/* END_CASE */
5623
5624/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005625void key_agreement_capacity( int alg_arg,
5626 int our_key_type_arg, data_t *our_key_data,
5627 data_t *peer_key_data,
5628 int expected_capacity_arg )
5629{
Ronald Cron5425a212020-08-04 14:58:35 +02005630 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005631 psa_algorithm_t alg = alg_arg;
5632 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005635 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005636 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005637
Gilles Peskine8817f612018-12-18 00:18:46 +01005638 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005639
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005640 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5641 psa_set_key_algorithm( &attributes, alg );
5642 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005643 PSA_ASSERT( psa_import_key( &attributes,
5644 our_key_data->x, our_key_data->len,
5645 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005646
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005648 PSA_ASSERT( psa_key_derivation_key_agreement(
5649 &operation,
5650 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5651 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005652 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5653 {
5654 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005655 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005656 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005657 NULL, 0 ) );
5658 }
Gilles Peskine59685592018-09-18 12:11:34 +02005659
Gilles Peskinebf491972018-10-25 22:36:12 +02005660 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005661 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005662 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005663 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005664
Gilles Peskinebf491972018-10-25 22:36:12 +02005665 /* Test the actual capacity by reading the output. */
5666 while( actual_capacity > sizeof( output ) )
5667 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005668 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005669 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005670 actual_capacity -= sizeof( output );
5671 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005672 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005673 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005674 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005675 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005676
Gilles Peskine59685592018-09-18 12:11:34 +02005677exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005678 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005679 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005680 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005681}
5682/* END_CASE */
5683
5684/* BEGIN_CASE */
5685void key_agreement_output( int alg_arg,
5686 int our_key_type_arg, data_t *our_key_data,
5687 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005688 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005689{
Ronald Cron5425a212020-08-04 14:58:35 +02005690 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005691 psa_algorithm_t alg = alg_arg;
5692 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005693 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005694 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005695 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005696
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005697 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5698 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005699
Gilles Peskine8817f612018-12-18 00:18:46 +01005700 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005701
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005702 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5703 psa_set_key_algorithm( &attributes, alg );
5704 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005705 PSA_ASSERT( psa_import_key( &attributes,
5706 our_key_data->x, our_key_data->len,
5707 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005708
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005709 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005710 PSA_ASSERT( psa_key_derivation_key_agreement(
5711 &operation,
5712 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5713 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005714 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5715 {
5716 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005717 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005718 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005719 NULL, 0 ) );
5720 }
Gilles Peskine59685592018-09-18 12:11:34 +02005721
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005722 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005723 actual_output,
5724 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005725 ASSERT_COMPARE( actual_output, expected_output1->len,
5726 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005727 if( expected_output2->len != 0 )
5728 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005729 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005730 actual_output,
5731 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005732 ASSERT_COMPARE( actual_output, expected_output2->len,
5733 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005734 }
Gilles Peskine59685592018-09-18 12:11:34 +02005735
5736exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005737 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005738 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005739 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005740 mbedtls_free( actual_output );
5741}
5742/* END_CASE */
5743
5744/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005745void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005746{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005747 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005748 unsigned char *output = NULL;
5749 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005750 size_t i;
5751 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005752
Simon Butcher49f8e312020-03-03 15:51:50 +00005753 TEST_ASSERT( bytes_arg >= 0 );
5754
Gilles Peskine91892022021-02-08 19:50:26 +01005755 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005756 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005757
Gilles Peskine8817f612018-12-18 00:18:46 +01005758 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005759
Gilles Peskinea50d7392018-06-21 10:22:13 +02005760 /* Run several times, to ensure that every output byte will be
5761 * nonzero at least once with overwhelming probability
5762 * (2^(-8*number_of_runs)). */
5763 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005764 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005765 if( bytes != 0 )
5766 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005767 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005768
Gilles Peskinea50d7392018-06-21 10:22:13 +02005769 for( i = 0; i < bytes; i++ )
5770 {
5771 if( output[i] != 0 )
5772 ++changed[i];
5773 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005774 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005775
5776 /* Check that every byte was changed to nonzero at least once. This
5777 * validates that psa_generate_random is overwriting every byte of
5778 * the output buffer. */
5779 for( i = 0; i < bytes; i++ )
5780 {
5781 TEST_ASSERT( changed[i] != 0 );
5782 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005783
5784exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005785 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005786 mbedtls_free( output );
5787 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005788}
5789/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005790
5791/* BEGIN_CASE */
5792void generate_key( int type_arg,
5793 int bits_arg,
5794 int usage_arg,
5795 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005796 int expected_status_arg,
5797 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005798{
Ronald Cron5425a212020-08-04 14:58:35 +02005799 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005800 psa_key_type_t type = type_arg;
5801 psa_key_usage_t usage = usage_arg;
5802 size_t bits = bits_arg;
5803 psa_algorithm_t alg = alg_arg;
5804 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005806 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005807
Gilles Peskine8817f612018-12-18 00:18:46 +01005808 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005809
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005810 psa_set_key_usage_flags( &attributes, usage );
5811 psa_set_key_algorithm( &attributes, alg );
5812 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005813 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005814
5815 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005816 psa_status_t status = psa_generate_key( &attributes, &key );
5817
5818 if( is_large_key > 0 )
5819 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5820 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005821 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005822 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005823
5824 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005825 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005826 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5827 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005828
Gilles Peskine818ca122018-06-20 18:16:48 +02005829 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005830 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005831 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005832
5833exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005834 /*
5835 * Key attributes may have been returned by psa_get_key_attributes()
5836 * thus reset them as required.
5837 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005838 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005839
Ronald Cron5425a212020-08-04 14:58:35 +02005840 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005841 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005842}
5843/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005844
Ronald Cronee414c72021-03-18 18:50:08 +01005845/* 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 +02005846void generate_key_rsa( int bits_arg,
5847 data_t *e_arg,
5848 int expected_status_arg )
5849{
Ronald Cron5425a212020-08-04 14:58:35 +02005850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005851 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005852 size_t bits = bits_arg;
5853 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5854 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5855 psa_status_t expected_status = expected_status_arg;
5856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5857 uint8_t *exported = NULL;
5858 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005859 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005860 size_t exported_length = SIZE_MAX;
5861 uint8_t *e_read_buffer = NULL;
5862 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005863 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005864 size_t e_read_length = SIZE_MAX;
5865
5866 if( e_arg->len == 0 ||
5867 ( e_arg->len == 3 &&
5868 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5869 {
5870 is_default_public_exponent = 1;
5871 e_read_size = 0;
5872 }
5873 ASSERT_ALLOC( e_read_buffer, e_read_size );
5874 ASSERT_ALLOC( exported, exported_size );
5875
5876 PSA_ASSERT( psa_crypto_init( ) );
5877
5878 psa_set_key_usage_flags( &attributes, usage );
5879 psa_set_key_algorithm( &attributes, alg );
5880 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5881 e_arg->x, e_arg->len ) );
5882 psa_set_key_bits( &attributes, bits );
5883
5884 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005885 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005886 if( expected_status != PSA_SUCCESS )
5887 goto exit;
5888
5889 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005890 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005891 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5892 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5893 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5894 e_read_buffer, e_read_size,
5895 &e_read_length ) );
5896 if( is_default_public_exponent )
5897 TEST_EQUAL( e_read_length, 0 );
5898 else
5899 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5900
5901 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005902 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005903 goto exit;
5904
5905 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005906 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005907 exported, exported_size,
5908 &exported_length ) );
5909 {
5910 uint8_t *p = exported;
5911 uint8_t *end = exported + exported_length;
5912 size_t len;
5913 /* RSAPublicKey ::= SEQUENCE {
5914 * modulus INTEGER, -- n
5915 * publicExponent INTEGER } -- e
5916 */
5917 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005918 MBEDTLS_ASN1_SEQUENCE |
5919 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005920 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005921 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5922 MBEDTLS_ASN1_INTEGER ) );
5923 if( len >= 1 && p[0] == 0 )
5924 {
5925 ++p;
5926 --len;
5927 }
5928 if( e_arg->len == 0 )
5929 {
5930 TEST_EQUAL( len, 3 );
5931 TEST_EQUAL( p[0], 1 );
5932 TEST_EQUAL( p[1], 0 );
5933 TEST_EQUAL( p[2], 1 );
5934 }
5935 else
5936 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5937 }
5938
5939exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005940 /*
5941 * Key attributes may have been returned by psa_get_key_attributes() or
5942 * set by psa_set_key_domain_parameters() thus reset them as required.
5943 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005944 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005945
Ronald Cron5425a212020-08-04 14:58:35 +02005946 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005947 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005948 mbedtls_free( e_read_buffer );
5949 mbedtls_free( exported );
5950}
5951/* END_CASE */
5952
Darryl Greend49a4992018-06-18 17:27:26 +01005953/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005954void persistent_key_load_key_from_storage( data_t *data,
5955 int type_arg, int bits_arg,
5956 int usage_flags_arg, int alg_arg,
5957 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005958{
Ronald Cron71016a92020-08-28 19:01:50 +02005959 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5962 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005963 psa_key_type_t type = type_arg;
5964 size_t bits = bits_arg;
5965 psa_key_usage_t usage_flags = usage_flags_arg;
5966 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005967 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005968 unsigned char *first_export = NULL;
5969 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005970 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005971 size_t first_exported_length;
5972 size_t second_exported_length;
5973
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005974 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5975 {
5976 ASSERT_ALLOC( first_export, export_size );
5977 ASSERT_ALLOC( second_export, export_size );
5978 }
Darryl Greend49a4992018-06-18 17:27:26 +01005979
Gilles Peskine8817f612018-12-18 00:18:46 +01005980 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005981
Gilles Peskinec87af662019-05-15 16:12:22 +02005982 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005983 psa_set_key_usage_flags( &attributes, usage_flags );
5984 psa_set_key_algorithm( &attributes, alg );
5985 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005986 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005987
Darryl Green0c6575a2018-11-07 16:05:30 +00005988 switch( generation_method )
5989 {
5990 case IMPORT_KEY:
5991 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005992 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005993 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005994 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005995
Darryl Green0c6575a2018-11-07 16:05:30 +00005996 case GENERATE_KEY:
5997 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005998 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005999 break;
6000
6001 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006002#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006003 {
6004 /* Create base key */
6005 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6006 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6007 psa_set_key_usage_flags( &base_attributes,
6008 PSA_KEY_USAGE_DERIVE );
6009 psa_set_key_algorithm( &base_attributes, derive_alg );
6010 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006011 PSA_ASSERT( psa_import_key( &base_attributes,
6012 data->x, data->len,
6013 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006014 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006015 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006016 PSA_ASSERT( psa_key_derivation_input_key(
6017 &operation,
6018 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006019 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006020 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006021 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006022 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6023 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006024 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006025 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006026 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006027 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006028 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006029#else
6030 TEST_ASSUME( ! "KDF not supported in this configuration" );
6031#endif
6032 break;
6033
6034 default:
6035 TEST_ASSERT( ! "generation_method not implemented in test" );
6036 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006037 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006038 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006039
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006040 /* Export the key if permitted by the key policy. */
6041 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6042 {
Ronald Cron5425a212020-08-04 14:58:35 +02006043 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006044 first_export, export_size,
6045 &first_exported_length ) );
6046 if( generation_method == IMPORT_KEY )
6047 ASSERT_COMPARE( data->x, data->len,
6048 first_export, first_exported_length );
6049 }
Darryl Greend49a4992018-06-18 17:27:26 +01006050
6051 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006052 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006053 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006054 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006055
Darryl Greend49a4992018-06-18 17:27:26 +01006056 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006057 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006058 TEST_ASSERT( mbedtls_svc_key_id_equal(
6059 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006060 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6061 PSA_KEY_LIFETIME_PERSISTENT );
6062 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6063 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6064 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6065 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006066
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006067 /* Export the key again if permitted by the key policy. */
6068 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006069 {
Ronald Cron5425a212020-08-04 14:58:35 +02006070 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006071 second_export, export_size,
6072 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006073 ASSERT_COMPARE( first_export, first_exported_length,
6074 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006075 }
6076
6077 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006078 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006079 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006080
6081exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006082 /*
6083 * Key attributes may have been returned by psa_get_key_attributes()
6084 * thus reset them as required.
6085 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006086 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006087
Darryl Greend49a4992018-06-18 17:27:26 +01006088 mbedtls_free( first_export );
6089 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006090 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006091 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006092 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006093 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006094}
6095/* END_CASE */