blob: 779f594dcacc4a819d9f5b6b97d07b93fb15ea5d [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"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020024#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010025
Gilles Peskine4023c012021-05-27 13:21:20 +020026/* If this comes up, it's a bug in the test code or in the test data. */
27#define UNUSED 0xdeadbeef
28
Dave Rodgman647791d2021-06-23 12:49:59 +010029/* Assert that an operation is (not) active.
30 * This serves as a proxy for checking if the operation is aborted. */
31#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
32#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010033
34/** An invalid export length that will never be set by psa_export_key(). */
35static const size_t INVALID_EXPORT_LENGTH = ~0U;
36
Gilles Peskinea7aa4422018-08-14 15:17:54 +020037/** Test if a buffer contains a constant byte value.
38 *
39 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 *
41 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020042 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 * \param size Size of the buffer in bytes.
44 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020045 * \return 1 if the buffer is all-bits-zero.
46 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020047 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020048static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020049{
50 size_t i;
51 for( i = 0; i < size; i++ )
52 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020053 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020054 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020055 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020056 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020057}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010058#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020059/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
60static int asn1_write_10x( unsigned char **p,
61 unsigned char *start,
62 size_t bits,
63 unsigned char x )
64{
65 int ret;
66 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020067 if( bits == 0 )
68 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
69 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020070 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030071 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020072 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
73 *p -= len;
74 ( *p )[len-1] = x;
75 if( bits % 8 == 0 )
76 ( *p )[1] |= 1;
77 else
78 ( *p )[0] |= 1 << ( bits % 8 );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
80 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
81 MBEDTLS_ASN1_INTEGER ) );
82 return( len );
83}
84
85static int construct_fake_rsa_key( unsigned char *buffer,
86 size_t buffer_size,
87 unsigned char **p,
88 size_t bits,
89 int keypair )
90{
91 size_t half_bits = ( bits + 1 ) / 2;
92 int ret;
93 int len = 0;
94 /* Construct something that looks like a DER encoding of
95 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
96 * RSAPrivateKey ::= SEQUENCE {
97 * version Version,
98 * modulus INTEGER, -- n
99 * publicExponent INTEGER, -- e
100 * privateExponent INTEGER, -- d
101 * prime1 INTEGER, -- p
102 * prime2 INTEGER, -- q
103 * exponent1 INTEGER, -- d mod (p-1)
104 * exponent2 INTEGER, -- d mod (q-1)
105 * coefficient INTEGER, -- (inverse of q) mod p
106 * otherPrimeInfos OtherPrimeInfos OPTIONAL
107 * }
108 * Or, for a public key, the same structure with only
109 * version, modulus and publicExponent.
110 */
111 *p = buffer + buffer_size;
112 if( keypair )
113 {
114 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
115 asn1_write_10x( p, buffer, half_bits, 1 ) );
116 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
117 asn1_write_10x( p, buffer, half_bits, 1 ) );
118 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
119 asn1_write_10x( p, buffer, half_bits, 1 ) );
120 MBEDTLS_ASN1_CHK_ADD( len, /* q */
121 asn1_write_10x( p, buffer, half_bits, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
123 asn1_write_10x( p, buffer, half_bits, 3 ) );
124 MBEDTLS_ASN1_CHK_ADD( len, /* d */
125 asn1_write_10x( p, buffer, bits, 1 ) );
126 }
127 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
128 asn1_write_10x( p, buffer, 17, 1 ) );
129 MBEDTLS_ASN1_CHK_ADD( len, /* n */
130 asn1_write_10x( p, buffer, bits, 1 ) );
131 if( keypair )
132 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
133 mbedtls_asn1_write_int( p, buffer, 0 ) );
134 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
135 {
136 const unsigned char tag =
137 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
138 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
139 }
140 return( len );
141}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100142#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200143
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100144int exercise_mac_setup( psa_key_type_t key_type,
145 const unsigned char *key_bytes,
146 size_t key_length,
147 psa_algorithm_t alg,
148 psa_mac_operation_t *operation,
149 psa_status_t *status )
150{
Ronald Cron5425a212020-08-04 14:58:35 +0200151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100153
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100154 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200155 psa_set_key_algorithm( &attributes, alg );
156 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200157 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158
Ronald Cron5425a212020-08-04 14:58:35 +0200159 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100160 /* Whether setup succeeded or failed, abort must succeed. */
161 PSA_ASSERT( psa_mac_abort( operation ) );
162 /* If setup failed, reproduce the failure, so that the caller can
163 * test the resulting state of the operation object. */
164 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100165 {
Ronald Cron5425a212020-08-04 14:58:35 +0200166 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100167 }
168
Ronald Cron5425a212020-08-04 14:58:35 +0200169 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170 return( 1 );
171
172exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200173 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100174 return( 0 );
175}
176
177int exercise_cipher_setup( psa_key_type_t key_type,
178 const unsigned char *key_bytes,
179 size_t key_length,
180 psa_algorithm_t alg,
181 psa_cipher_operation_t *operation,
182 psa_status_t *status )
183{
Ronald Cron5425a212020-08-04 14:58:35 +0200184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100186
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
188 psa_set_key_algorithm( &attributes, alg );
189 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200190 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191
Ronald Cron5425a212020-08-04 14:58:35 +0200192 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100193 /* Whether setup succeeded or failed, abort must succeed. */
194 PSA_ASSERT( psa_cipher_abort( operation ) );
195 /* If setup failed, reproduce the failure, so that the caller can
196 * test the resulting state of the operation object. */
197 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100198 {
Ronald Cron5425a212020-08-04 14:58:35 +0200199 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100200 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201 }
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100204 return( 1 );
205
206exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200207 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100208 return( 0 );
209}
210
Ronald Cron5425a212020-08-04 14:58:35 +0200211static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212{
213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200214 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200215 uint8_t buffer[1];
216 size_t length;
217 int ok = 0;
218
Ronald Cronecfb2372020-07-23 17:13:42 +0200219 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
221 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
222 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200223 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000224 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200225 TEST_EQUAL(
226 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
227 TEST_EQUAL(
228 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200229 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
233 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
234
Ronald Cron5425a212020-08-04 14:58:35 +0200235 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000236 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200237 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200238 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000239 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241 ok = 1;
242
243exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100244 /*
245 * Key attributes may have been returned by psa_get_key_attributes()
246 * thus reset them as required.
247 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200248 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100249
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200250 return( ok );
251}
252
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200253/* Assert that a key isn't reported as having a slot number. */
254#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
255#define ASSERT_NO_SLOT_NUMBER( attributes ) \
256 do \
257 { \
258 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
259 TEST_EQUAL( psa_get_key_slot_number( \
260 attributes, \
261 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
262 PSA_ERROR_INVALID_ARGUMENT ); \
263 } \
264 while( 0 )
265#else /* MBEDTLS_PSA_CRYPTO_SE_C */
266#define ASSERT_NO_SLOT_NUMBER( attributes ) \
267 ( (void) 0 )
268#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
269
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100270/* An overapproximation of the amount of storage needed for a key of the
271 * given type and with the given content. The API doesn't make it easy
272 * to find a good value for the size. The current implementation doesn't
273 * care about the value anyway. */
274#define KEY_BITS_FROM_DATA( type, data ) \
275 ( data )->len
276
Darryl Green0c6575a2018-11-07 16:05:30 +0000277typedef enum {
278 IMPORT_KEY = 0,
279 GENERATE_KEY = 1,
280 DERIVE_KEY = 2
281} generate_method;
282
Paul Elliott33746aa2021-09-15 16:40:40 +0100283typedef enum
284{
285 DO_NOT_SET_LENGTHS = 0,
286 SET_LENGTHS_BEFORE_NONCE = 1,
287 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100288} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100289
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100290typedef enum
291{
292 USE_NULL_TAG = 0,
293 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100294} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100295
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100296/*!
297 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100298 * \param key_type_arg Type of key passed in
299 * \param key_data The encryption / decryption key data
300 * \param alg_arg The type of algorithm used
301 * \param nonce Nonce data
302 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100303 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100304 * feed additional data in to be encrypted /
305 * decrypted. If -1, no chunking.
306 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100307 * \param data_part_len_arg If not -1, the length of chunks to feed
308 * the data in to be encrypted / decrypted. If
309 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100310 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100311 * expected here, this controls whether or not
312 * to set lengths, and in what order with
313 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100315 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100316 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100317 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319 */
320static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
321 int alg_arg,
322 data_t *nonce,
323 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100324 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100326 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100327 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100329 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100330 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100331{
332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
333 psa_key_type_t key_type = key_type_arg;
334 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100335 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100336 unsigned char *output_data = NULL;
337 unsigned char *part_data = NULL;
338 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100340 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 size_t output_size = 0;
342 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 size_t output_length = 0;
344 size_t key_bits = 0;
345 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100346 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100347 size_t part_length = 0;
348 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100349 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100350 size_t ad_part_len = 0;
351 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100352 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
354 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
355
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100356 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100357 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100358
Paul Elliottd3f82412021-06-16 16:52:21 +0100359 PSA_ASSERT( psa_crypto_init( ) );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
363 else
364 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
365
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 psa_set_key_algorithm( &attributes, alg );
367 psa_set_key_type( &attributes, key_type );
368
369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
370 &key ) );
371
372 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
373 key_bits = psa_get_key_bits( &attributes );
374
375 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
376
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100377 if( is_encrypt )
378 {
379 /* Tag gets written at end of buffer. */
380 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
381 ( input_data->len +
382 tag_length ) );
383 data_true_size = input_data->len;
384 }
385 else
386 {
387 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
388 ( input_data->len -
389 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100390
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100391 /* Do not want to attempt to decrypt tag. */
392 data_true_size = input_data->len - tag_length;
393 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100394
395 ASSERT_ALLOC( output_data, output_size );
396
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100397 if( is_encrypt )
398 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100399 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200400 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100401 }
402 else
403 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100404 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200405 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100406 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100407
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100408 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100409
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100410 if( is_encrypt )
411 status = psa_aead_encrypt_setup( &operation, key, alg );
412 else
413 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100414
415 /* If the operation is not supported, just skip and not fail in case the
416 * encryption involves a common limitation of cryptography hardwares and
417 * an alternative implementation. */
418 if( status == PSA_ERROR_NOT_SUPPORTED )
419 {
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
421 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
422 }
423
424 PSA_ASSERT( status );
425
Paul Elliott33746aa2021-09-15 16:40:40 +0100426 if( set_lengths_method == DO_NOT_SET_LENGTHS )
427 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
428 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100429 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100430 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
431 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100432 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
433 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100434 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100435 {
436 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
437
Paul Elliott33746aa2021-09-15 16:40:40 +0100438 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
439 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100440 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100441
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100442 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100443 {
444 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100445 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100446
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100447 for( part_offset = 0, part_count = 0;
448 part_offset < additional_data->len;
449 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100450 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100451 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100452 {
Paul Elliott329d5382021-07-22 17:10:45 +0100453 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100454 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100455 else if( additional_data->len - part_offset < ad_part_len )
456 {
457 part_length = additional_data->len - part_offset;
458 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100459 else
460 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100461 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 }
463
464 PSA_ASSERT( psa_aead_update_ad( &operation,
465 additional_data->x + part_offset,
466 part_length ) );
467
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 }
469 }
470 else
471 {
472 /* Pass additional data in one go. */
473 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
474 additional_data->len ) );
475 }
476
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100477 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
479 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100480 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100482 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100483
484 ASSERT_ALLOC( part_data, part_data_size );
485
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100486 for( part_offset = 0, part_count = 0;
487 part_offset < data_true_size;
488 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100489 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100490 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 {
Paul Elliott329d5382021-07-22 17:10:45 +0100492 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100493 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100494 else if( ( data_true_size - part_offset ) < data_part_len )
495 {
496 part_length = ( data_true_size - part_offset );
497 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100498 else
499 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100500 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100501 }
502
503 PSA_ASSERT( psa_aead_update( &operation,
504 ( input_data->x + part_offset ),
505 part_length, part_data,
506 part_data_size,
507 &output_part_length ) );
508
509 if( output_data && output_part_length )
510 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100511 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100512 output_part_length );
513 }
514
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 output_length += output_part_length;
516 }
517 }
518 else
519 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100520 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100521 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100522 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100523 output_size, &output_length ) );
524 }
525
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100526 if( is_encrypt )
527 PSA_ASSERT( psa_aead_finish( &operation, final_data,
528 final_output_size,
529 &output_part_length,
530 tag_buffer, tag_length,
531 &tag_size ) );
532 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100533 {
Paul Elliott9961a662021-09-17 19:19:02 +0100534 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100535 final_output_size,
536 &output_part_length,
537 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100538 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100539 }
540
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100541 if( output_data && output_part_length )
542 memcpy( ( output_data + output_length ), final_data,
543 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100544
545 output_length += output_part_length;
546
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100547
548 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
549 * should be exact.*/
550 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100551 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100552 TEST_EQUAL( tag_length, tag_size );
553
554 if( output_data && tag_length )
555 memcpy( ( output_data + output_length ), tag_buffer,
556 tag_length );
557
558 output_length += tag_length;
559
560 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200561 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
562 input_data->len ) );
563 TEST_LE_U( output_length,
564 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100565 }
566 else
567 {
568 TEST_EQUAL( output_length,
569 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
570 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200571 TEST_LE_U( output_length,
572 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100573 }
574
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100576 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100577 output_data, output_length );
578
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100580 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100581
582exit:
583 psa_destroy_key( key );
584 psa_aead_abort( &operation );
585 mbedtls_free( output_data );
586 mbedtls_free( part_data );
587 mbedtls_free( final_data );
588 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100589
590 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100591}
592
Neil Armstrong4766f992022-02-28 16:23:59 +0100593/*!
594 * \brief Internal Function for MAC multipart tests.
595 * \param key_type_arg Type of key passed in
596 * \param key_data The encryption / decryption key data
597 * \param alg_arg The type of algorithm used
598 * \param input_data Data to encrypt / decrypt
599 * \param data_part_len_arg If not -1, the length of chunks to feed
600 * the data in to be encrypted / decrypted. If
601 * -1, no chunking
602 * \param expected_output Expected output
603 * \param is_verify If non-zero this is an verify operation.
604 * \param do_zero_parts If non-zero, interleave zero length chunks
605 * with normal length chunks.
606 * \return int Zero on failure, non-zero on success.
607 */
608static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
609 int alg_arg,
610 data_t *input_data,
611 int data_part_len_arg,
612 data_t *expected_output,
613 int is_verify,
614 int do_zero_parts )
615{
616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
617 psa_key_type_t key_type = key_type_arg;
618 psa_algorithm_t alg = alg_arg;
619 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
620 unsigned char mac[PSA_MAC_MAX_SIZE];
621 size_t part_offset = 0;
622 size_t part_length = 0;
623 size_t data_part_len = 0;
624 size_t mac_len = 0;
625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
626 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
627
628 int test_ok = 0;
629 size_t part_count = 0;
630
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100631 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100632
633 if( is_verify )
634 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
635 else
636 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
637
638 psa_set_key_algorithm( &attributes, alg );
639 psa_set_key_type( &attributes, key_type );
640
641 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
642 &key ) );
643
644 if( is_verify )
645 status = psa_mac_verify_setup( &operation, key, alg );
646 else
647 status = psa_mac_sign_setup( &operation, key, alg );
648
649 PSA_ASSERT( status );
650
651 if( data_part_len_arg != -1 )
652 {
653 /* Pass data in parts */
654 data_part_len = ( size_t ) data_part_len_arg;
655
656 for( part_offset = 0, part_count = 0;
657 part_offset < input_data->len;
658 part_offset += part_length, part_count++ )
659 {
660 if( do_zero_parts && ( part_count & 0x01 ) )
661 {
662 part_length = 0;
663 }
664 else if( ( input_data->len - part_offset ) < data_part_len )
665 {
666 part_length = ( input_data->len - part_offset );
667 }
668 else
669 {
670 part_length = data_part_len;
671 }
672
673 PSA_ASSERT( psa_mac_update( &operation,
674 ( input_data->x + part_offset ),
675 part_length ) );
676 }
677 }
678 else
679 {
680 /* Pass all data in one go. */
681 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
682 input_data->len ) );
683 }
684
685 if( is_verify )
686 {
687 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
688 expected_output->len ) );
689 }
690 else
691 {
692 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
693 PSA_MAC_MAX_SIZE, &mac_len ) );
694
695 ASSERT_COMPARE( expected_output->x, expected_output->len,
696 mac, mac_len );
697 }
698
699 test_ok = 1;
700
701exit:
702 psa_destroy_key( key );
703 psa_mac_abort( &operation );
704 PSA_DONE( );
705
706 return( test_ok );
707}
708
Neil Armstrong75673ab2022-06-15 17:39:01 +0200709#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200710static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
711 psa_pake_operation_t *server,
712 psa_pake_operation_t *client,
713 int client_input_first,
714 int round, int inject_error )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200715{
716 unsigned char *buffer0 = NULL, *buffer1 = NULL;
717 size_t buffer_length = (
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
719 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
720 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200721 /* The output should be exactly this size according to the spec */
722 const size_t expected_size_key_share =
723 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
724 /* The output should be exactly this size according to the spec */
725 const size_t expected_size_zk_public =
726 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
727 /* The output can be smaller: the spec allows stripping leading zeroes */
728 const size_t max_expected_size_zk_proof =
729 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200730 size_t buffer0_off = 0;
731 size_t buffer1_off = 0;
732 size_t s_g1_len, s_g2_len, s_a_len;
733 size_t s_g1_off, s_g2_off, s_a_off;
734 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
735 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
736 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
737 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
738 size_t c_g1_len, c_g2_len, c_a_len;
739 size_t c_g1_off, c_g2_off, c_a_off;
740 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
741 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
742 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
743 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
744 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200745 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200746
747 ASSERT_ALLOC( buffer0, buffer_length );
748 ASSERT_ALLOC( buffer1, buffer_length );
749
750 switch( round )
751 {
752 case 1:
753 /* Server first round Output */
754 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
755 buffer0 + buffer0_off,
756 512 - buffer0_off, &s_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200757 TEST_EQUAL( s_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200758 s_g1_off = buffer0_off;
759 buffer0_off += s_g1_len;
760 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
761 buffer0 + buffer0_off,
762 512 - buffer0_off, &s_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200763 TEST_EQUAL( s_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200764 s_x1_pk_off = buffer0_off;
765 buffer0_off += s_x1_pk_len;
766 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
767 buffer0 + buffer0_off,
768 512 - buffer0_off, &s_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200769 TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200770 s_x1_pr_off = buffer0_off;
771 buffer0_off += s_x1_pr_len;
772 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
773 buffer0 + buffer0_off,
774 512 - buffer0_off, &s_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200775 TEST_EQUAL( s_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200776 s_g2_off = buffer0_off;
777 buffer0_off += s_g2_len;
778 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
779 buffer0 + buffer0_off,
780 512 - buffer0_off, &s_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200781 TEST_EQUAL( s_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200782 s_x2_pk_off = buffer0_off;
783 buffer0_off += s_x2_pk_len;
784 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
785 buffer0 + buffer0_off,
786 512 - buffer0_off, &s_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200787 TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200788 s_x2_pr_off = buffer0_off;
789 buffer0_off += s_x2_pr_len;
790
791 if( inject_error == 1 )
792 {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500793 buffer0[s_x1_pr_off + 8] ^= 1;
794 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200795 expected_status = PSA_ERROR_DATA_INVALID;
796 }
797
Neil Armstrong51009d72022-09-05 17:59:54 +0200798 /*
799 * When injecting errors in inputs, the implementation is
800 * free to detect it right away of with a delay.
801 * This permits delaying the error until the end of the input
802 * sequence, if no error appears then, this will be treated
803 * as an error.
804 */
805
Neil Armstrongf983caf2022-06-15 15:27:48 +0200806 if( client_input_first == 1 )
807 {
808 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200809 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
810 buffer0 + s_g1_off, s_g1_len );
811 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200812 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200813 TEST_EQUAL( status, expected_status );
814 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200815 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200816 else
817 {
818 TEST_EQUAL( status, PSA_SUCCESS );
819 }
820
821 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
822 buffer0 + s_x1_pk_off,
823 s_x1_pk_len );
824 if( inject_error == 1 && status != PSA_SUCCESS )
825 {
826 TEST_EQUAL( status, expected_status );
827 break;
828 }
829 else
830 {
831 TEST_EQUAL( status, PSA_SUCCESS );
832 }
833
834 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
835 buffer0 + s_x1_pr_off,
836 s_x1_pr_len );
837 if( inject_error == 1 && status != PSA_SUCCESS )
838 {
839 TEST_EQUAL( status, expected_status );
840 break;
841 }
842 else
843 {
844 TEST_EQUAL( status, PSA_SUCCESS );
845 }
846
847 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
848 buffer0 + s_g2_off,
849 s_g2_len );
850 if( inject_error == 1 && status != PSA_SUCCESS )
851 {
852 TEST_EQUAL( status, expected_status );
853 break;
854 }
855 else
856 {
857 TEST_EQUAL( status, PSA_SUCCESS );
858 }
859
860 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
861 buffer0 + s_x2_pk_off,
862 s_x2_pk_len );
863 if( inject_error == 1 && status != PSA_SUCCESS )
864 {
865 TEST_EQUAL( status, expected_status );
866 break;
867 }
868 else
869 {
870 TEST_EQUAL( status, PSA_SUCCESS );
871 }
872
873 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
874 buffer0 + s_x2_pr_off,
875 s_x2_pr_len );
876 if( inject_error == 1 && status != PSA_SUCCESS )
877 {
878 TEST_EQUAL( status, expected_status );
879 break;
880 }
881 else
882 {
883 TEST_EQUAL( status, PSA_SUCCESS );
884 }
885
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200886 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200887 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200888 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200889 }
890
891 /* Client first round Output */
892 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
893 buffer1 + buffer1_off,
894 512 - buffer1_off, &c_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200895 TEST_EQUAL( c_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200896 c_g1_off = buffer1_off;
897 buffer1_off += c_g1_len;
898 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
899 buffer1 + buffer1_off,
900 512 - buffer1_off, &c_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200901 TEST_EQUAL( c_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 c_x1_pk_off = buffer1_off;
903 buffer1_off += c_x1_pk_len;
904 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
905 buffer1 + buffer1_off,
906 512 - buffer1_off, &c_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200907 TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200908 c_x1_pr_off = buffer1_off;
909 buffer1_off += c_x1_pr_len;
910 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
911 buffer1 + buffer1_off,
912 512 - buffer1_off, &c_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200913 TEST_EQUAL( c_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200914 c_g2_off = buffer1_off;
915 buffer1_off += c_g2_len;
916 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
917 buffer1 + buffer1_off,
918 512 - buffer1_off, &c_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200919 TEST_EQUAL( c_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200920 c_x2_pk_off = buffer1_off;
921 buffer1_off += c_x2_pk_len;
922 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
923 buffer1 + buffer1_off,
924 512 - buffer1_off, &c_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200925 TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200926 c_x2_pr_off = buffer1_off;
927 buffer1_off += c_x2_pr_len;
928
929 if( client_input_first == 0 )
930 {
931 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200932 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
933 buffer0 + s_g1_off, s_g1_len );
934 if( inject_error == 1 && status != PSA_SUCCESS )
935 {
936 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200937 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200938 }
939 else
940 {
941 TEST_EQUAL( status, PSA_SUCCESS );
942 }
943
944 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
945 buffer0 + s_x1_pk_off,
946 s_x1_pk_len );
947 if( inject_error == 1 && status != PSA_SUCCESS )
948 {
949 TEST_EQUAL( status, expected_status );
950 break;
951 }
952 else
953 {
954 TEST_EQUAL( status, PSA_SUCCESS );
955 }
956
957 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
958 buffer0 + s_x1_pr_off,
959 s_x1_pr_len );
960 if( inject_error == 1 && status != PSA_SUCCESS )
961 {
962 TEST_EQUAL( status, expected_status );
963 break;
964 }
965 else
966 {
967 TEST_EQUAL( status, PSA_SUCCESS );
968 }
969
970 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
971 buffer0 + s_g2_off,
972 s_g2_len );
973 if( inject_error == 1 && status != PSA_SUCCESS )
974 {
975 TEST_EQUAL( status, expected_status );
976 break;
977 }
978 else
979 {
980 TEST_EQUAL( status, PSA_SUCCESS );
981 }
982
983 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
984 buffer0 + s_x2_pk_off,
985 s_x2_pk_len );
986 if( inject_error == 1 && status != PSA_SUCCESS )
987 {
988 TEST_EQUAL( status, expected_status );
989 break;
990 }
991 else
992 {
993 TEST_EQUAL( status, PSA_SUCCESS );
994 }
995
996 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
997 buffer0 + s_x2_pr_off,
998 s_x2_pr_len );
999 if( inject_error == 1 && status != PSA_SUCCESS )
1000 {
1001 TEST_EQUAL( status, expected_status );
1002 break;
1003 }
1004 else
1005 {
1006 TEST_EQUAL( status, PSA_SUCCESS );
1007 }
1008
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001009 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001010 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001011 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001012 }
1013
1014 if( inject_error == 2 )
1015 {
Andrzej Kurekc0182042022-11-08 08:12:56 -05001016 buffer1[c_x1_pr_off + 12] ^= 1;
1017 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001018 expected_status = PSA_ERROR_DATA_INVALID;
1019 }
1020
1021 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001022 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1023 buffer1 + c_g1_off, c_g1_len );
1024 if( inject_error == 2 && status != PSA_SUCCESS )
1025 {
1026 TEST_EQUAL( status, expected_status );
1027 break;
1028 }
1029 else
1030 {
1031 TEST_EQUAL( status, PSA_SUCCESS );
1032 }
1033
1034 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1035 buffer1 + c_x1_pk_off, c_x1_pk_len );
1036 if( inject_error == 2 && status != PSA_SUCCESS )
1037 {
1038 TEST_EQUAL( status, expected_status );
1039 break;
1040 }
1041 else
1042 {
1043 TEST_EQUAL( status, PSA_SUCCESS );
1044 }
1045
1046 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1047 buffer1 + c_x1_pr_off, c_x1_pr_len );
1048 if( inject_error == 2 && status != PSA_SUCCESS )
1049 {
1050 TEST_EQUAL( status, expected_status );
1051 break;
1052 }
1053 else
1054 {
1055 TEST_EQUAL( status, PSA_SUCCESS );
1056 }
1057
1058 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1059 buffer1 + c_g2_off, c_g2_len );
1060 if( inject_error == 2 && status != PSA_SUCCESS )
1061 {
1062 TEST_EQUAL( status, expected_status );
1063 break;
1064 }
1065 else
1066 {
1067 TEST_EQUAL( status, PSA_SUCCESS );
1068 }
1069
1070 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1071 buffer1 + c_x2_pk_off, c_x2_pk_len );
1072 if( inject_error == 2 && status != PSA_SUCCESS )
1073 {
1074 TEST_EQUAL( status, expected_status );
1075 break;
1076 }
1077 else
1078 {
1079 TEST_EQUAL( status, PSA_SUCCESS );
1080 }
1081
1082 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1083 buffer1 + c_x2_pr_off, c_x2_pr_len );
1084 if( inject_error == 2 && status != PSA_SUCCESS )
1085 {
1086 TEST_EQUAL( status, expected_status );
1087 break;
1088 }
1089 else
1090 {
1091 TEST_EQUAL( status, PSA_SUCCESS );
1092 }
1093
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001094 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001095 if( inject_error == 2 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001096 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001097
1098 break;
1099
1100 case 2:
1101 /* Server second round Output */
1102 buffer0_off = 0;
1103
1104 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1105 buffer0 + buffer0_off,
1106 512 - buffer0_off, &s_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001107 TEST_EQUAL( s_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001108 s_a_off = buffer0_off;
1109 buffer0_off += s_a_len;
1110 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1111 buffer0 + buffer0_off,
1112 512 - buffer0_off, &s_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001113 TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001114 s_x2s_pk_off = buffer0_off;
1115 buffer0_off += s_x2s_pk_len;
1116 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1117 buffer0 + buffer0_off,
1118 512 - buffer0_off, &s_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001119 TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 s_x2s_pr_off = buffer0_off;
1121 buffer0_off += s_x2s_pr_len;
1122
1123 if( inject_error == 3 )
1124 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001125 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001126 expected_status = PSA_ERROR_DATA_INVALID;
1127 }
1128
1129 if( client_input_first == 1 )
1130 {
1131 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001132 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1133 buffer0 + s_a_off, s_a_len );
1134 if( inject_error == 3 && status != PSA_SUCCESS )
1135 {
1136 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001137 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001138 }
1139 else
1140 {
1141 TEST_EQUAL( status, PSA_SUCCESS );
1142 }
1143
1144 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1145 buffer0 + s_x2s_pk_off,
1146 s_x2s_pk_len );
1147 if( inject_error == 3 && status != PSA_SUCCESS )
1148 {
1149 TEST_EQUAL( status, expected_status );
1150 break;
1151 }
1152 else
1153 {
1154 TEST_EQUAL( status, PSA_SUCCESS );
1155 }
1156
1157 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1158 buffer0 + s_x2s_pr_off,
1159 s_x2s_pr_len );
1160 if( inject_error == 3 && status != PSA_SUCCESS )
1161 {
1162 TEST_EQUAL( status, expected_status );
1163 break;
1164 }
1165 else
1166 {
1167 TEST_EQUAL( status, PSA_SUCCESS );
1168 }
1169
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001170 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001171 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001172 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001173 }
1174
1175 /* Client second round Output */
1176 buffer1_off = 0;
1177
1178 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1179 buffer1 + buffer1_off,
1180 512 - buffer1_off, &c_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001181 TEST_EQUAL( c_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001182 c_a_off = buffer1_off;
1183 buffer1_off += c_a_len;
1184 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1185 buffer1 + buffer1_off,
1186 512 - buffer1_off, &c_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001187 TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001188 c_x2s_pk_off = buffer1_off;
1189 buffer1_off += c_x2s_pk_len;
1190 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1191 buffer1 + buffer1_off,
1192 512 - buffer1_off, &c_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001193 TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001194 c_x2s_pr_off = buffer1_off;
1195 buffer1_off += c_x2s_pr_len;
1196
1197 if( client_input_first == 0 )
1198 {
1199 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001200 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1201 buffer0 + s_a_off, s_a_len );
1202 if( inject_error == 3 && status != PSA_SUCCESS )
1203 {
1204 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001206 }
1207 else
1208 {
1209 TEST_EQUAL( status, PSA_SUCCESS );
1210 }
1211
1212 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1213 buffer0 + s_x2s_pk_off,
1214 s_x2s_pk_len );
1215 if( inject_error == 3 && status != PSA_SUCCESS )
1216 {
1217 TEST_EQUAL( status, expected_status );
1218 break;
1219 }
1220 else
1221 {
1222 TEST_EQUAL( status, PSA_SUCCESS );
1223 }
1224
1225 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1226 buffer0 + s_x2s_pr_off,
1227 s_x2s_pr_len );
1228 if( inject_error == 3 && status != PSA_SUCCESS )
1229 {
1230 TEST_EQUAL( status, expected_status );
1231 break;
1232 }
1233 else
1234 {
1235 TEST_EQUAL( status, PSA_SUCCESS );
1236 }
1237
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001238 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001239 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001240 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001241 }
1242
1243 if( inject_error == 4 )
1244 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001245 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001246 expected_status = PSA_ERROR_DATA_INVALID;
1247 }
1248
1249 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001250 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1251 buffer1 + c_a_off, c_a_len );
1252 if( inject_error == 4 && status != PSA_SUCCESS )
1253 {
1254 TEST_EQUAL( status, expected_status );
1255 break;
1256 }
1257 else
1258 {
1259 TEST_EQUAL( status, PSA_SUCCESS );
1260 }
1261
1262 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1263 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1264 if( inject_error == 4 && status != PSA_SUCCESS )
1265 {
1266 TEST_EQUAL( status, expected_status );
1267 break;
1268 }
1269 else
1270 {
1271 TEST_EQUAL( status, PSA_SUCCESS );
1272 }
1273
1274 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1275 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1276 if( inject_error == 4 && status != PSA_SUCCESS )
1277 {
1278 TEST_EQUAL( status, expected_status );
1279 break;
1280 }
1281 else
1282 {
1283 TEST_EQUAL( status, PSA_SUCCESS );
1284 }
1285
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001286 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001287 if( inject_error == 4 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001288 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001289
1290 break;
1291
1292 }
1293
Neil Armstrongf983caf2022-06-15 15:27:48 +02001294exit:
1295 mbedtls_free( buffer0 );
1296 mbedtls_free( buffer1 );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001297}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001298#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001299
Gilles Peskinee59236f2018-01-27 23:32:46 +01001300/* END_HEADER */
1301
1302/* BEGIN_DEPENDENCIES
1303 * depends_on:MBEDTLS_PSA_CRYPTO_C
1304 * END_DEPENDENCIES
1305 */
1306
1307/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001308void static_checks( )
1309{
1310 size_t max_truncated_mac_size =
1311 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1312
1313 /* Check that the length for a truncated MAC always fits in the algorithm
1314 * encoding. The shifted mask is the maximum truncated value. The
1315 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001316 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001317}
1318/* END_CASE */
1319
1320/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001321void import_with_policy( int type_arg,
1322 int usage_arg, int alg_arg,
1323 int expected_status_arg )
1324{
1325 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1326 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001327 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001328 psa_key_type_t type = type_arg;
1329 psa_key_usage_t usage = usage_arg;
1330 psa_algorithm_t alg = alg_arg;
1331 psa_status_t expected_status = expected_status_arg;
1332 const uint8_t key_material[16] = {0};
1333 psa_status_t status;
1334
1335 PSA_ASSERT( psa_crypto_init( ) );
1336
1337 psa_set_key_type( &attributes, type );
1338 psa_set_key_usage_flags( &attributes, usage );
1339 psa_set_key_algorithm( &attributes, alg );
1340
1341 status = psa_import_key( &attributes,
1342 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001343 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001344 TEST_EQUAL( status, expected_status );
1345 if( status != PSA_SUCCESS )
1346 goto exit;
1347
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001349 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001350 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001351 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001352 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001353 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001354
Ronald Cron5425a212020-08-04 14:58:35 +02001355 PSA_ASSERT( psa_destroy_key( key ) );
1356 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001357
1358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001359 /*
1360 * Key attributes may have been returned by psa_get_key_attributes()
1361 * thus reset them as required.
1362 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001363 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001364
1365 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001366 PSA_DONE( );
1367}
1368/* END_CASE */
1369
1370/* BEGIN_CASE */
1371void import_with_data( data_t *data, int type_arg,
1372 int attr_bits_arg,
1373 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374{
1375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1376 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001379 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001380 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001381 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382
Gilles Peskine8817f612018-12-18 00:18:46 +01001383 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384
Gilles Peskine4747d192019-04-17 15:05:45 +02001385 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001386 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001387
Ronald Cron5425a212020-08-04 14:58:35 +02001388 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001389 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001390 if( status != PSA_SUCCESS )
1391 goto exit;
1392
Ronald Cron5425a212020-08-04 14:58:35 +02001393 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001394 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001395 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001396 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001397 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001398
Ronald Cron5425a212020-08-04 14:58:35 +02001399 PSA_ASSERT( psa_destroy_key( key ) );
1400 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001401
1402exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001403 /*
1404 * Key attributes may have been returned by psa_get_key_attributes()
1405 * thus reset them as required.
1406 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001407 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001408
1409 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001410 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001415void import_large_key( int type_arg, int byte_size_arg,
1416 int expected_status_arg )
1417{
1418 psa_key_type_t type = type_arg;
1419 size_t byte_size = byte_size_arg;
1420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1421 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001423 psa_status_t status;
1424 uint8_t *buffer = NULL;
1425 size_t buffer_size = byte_size + 1;
1426 size_t n;
1427
Steven Cooreman69967ce2021-01-18 18:01:08 +01001428 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001429 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001430 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001431 memset( buffer, 'K', byte_size );
1432
1433 PSA_ASSERT( psa_crypto_init( ) );
1434
1435 /* Try importing the key */
1436 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1437 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001438 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001439 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001440 TEST_EQUAL( status, expected_status );
1441
1442 if( status == PSA_SUCCESS )
1443 {
Ronald Cron5425a212020-08-04 14:58:35 +02001444 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001445 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1446 TEST_EQUAL( psa_get_key_bits( &attributes ),
1447 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001448 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001449 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001450 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001451 for( n = 0; n < byte_size; n++ )
1452 TEST_EQUAL( buffer[n], 'K' );
1453 for( n = byte_size; n < buffer_size; n++ )
1454 TEST_EQUAL( buffer[n], 0 );
1455 }
1456
1457exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001458 /*
1459 * Key attributes may have been returned by psa_get_key_attributes()
1460 * thus reset them as required.
1461 */
1462 psa_reset_key_attributes( &attributes );
1463
Ronald Cron5425a212020-08-04 14:58:35 +02001464 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001465 PSA_DONE( );
1466 mbedtls_free( buffer );
1467}
1468/* END_CASE */
1469
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001470/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001471void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1472{
Ronald Cron5425a212020-08-04 14:58:35 +02001473 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001474 size_t bits = bits_arg;
1475 psa_status_t expected_status = expected_status_arg;
1476 psa_status_t status;
1477 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001478 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001479 size_t buffer_size = /* Slight overapproximations */
1480 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001481 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001482 unsigned char *p;
1483 int ret;
1484 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001486
Gilles Peskine8817f612018-12-18 00:18:46 +01001487 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001488 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001489
1490 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1491 bits, keypair ) ) >= 0 );
1492 length = ret;
1493
1494 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001495 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001496 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001497 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001498
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001499 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001500 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001501
1502exit:
1503 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001504 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001505}
1506/* END_CASE */
1507
1508/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001509void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001510 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001511 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301512 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513 int expected_bits,
1514 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001515 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001516 int canonical_input )
1517{
Ronald Cron5425a212020-08-04 14:58:35 +02001518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001520 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001521 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301523 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524 unsigned char *exported = NULL;
1525 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001527 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001528 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001530 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531
Moran Pekercb088e72018-07-17 17:36:59 +03001532 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001533 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001534 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001535 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001536 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001537
Archana4d7ae1d2021-07-07 02:50:22 +05301538 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001539 psa_set_key_usage_flags( &attributes, usage_arg );
1540 psa_set_key_algorithm( &attributes, alg );
1541 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001542
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001543 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001544 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545
1546 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001547 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001548 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1549 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001550 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001551
1552 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001553 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001554 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001555
1556 /* The exported length must be set by psa_export_key() to a value between 0
1557 * and export_size. On errors, the exported length must be 0. */
1558 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1559 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001560 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001561
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001562 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001563 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001565 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001566 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001568 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001569
Gilles Peskineea38a922021-02-13 00:05:16 +01001570 /* Run sanity checks on the exported key. For non-canonical inputs,
1571 * this validates the canonical representations. For canonical inputs,
1572 * this doesn't directly validate the implementation, but it still helps
1573 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301574 if( !psa_key_lifetime_is_external( lifetime ) )
1575 {
1576 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1577 goto exit;
1578 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001579
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001580 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001581 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001582 else
1583 {
Ronald Cron5425a212020-08-04 14:58:35 +02001584 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001585 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001586 &key2 ) );
1587 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001588 reexported,
1589 export_size,
1590 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001591 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301592 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001593 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001595 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301596 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301597 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001598 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001599
1600destroy:
1601 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001602 PSA_ASSERT( psa_destroy_key( key ) );
1603 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001604
1605exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001606 /*
1607 * Key attributes may have been returned by psa_get_key_attributes()
1608 * thus reset them as required.
1609 */
1610 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301611 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001612 mbedtls_free( exported );
1613 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001614 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615}
1616/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001617
Moran Pekerf709f4a2018-06-06 17:26:04 +03001618/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001619void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001620 int type_arg,
1621 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301622 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001623 int export_size_delta,
1624 int expected_export_status_arg,
1625 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001626{
Ronald Cron5425a212020-08-04 14:58:35 +02001627 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001628 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001629 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001630 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001631 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301632 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001633 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001634 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001635 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001636 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001637
Gilles Peskine8817f612018-12-18 00:18:46 +01001638 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001639
Archana4d7ae1d2021-07-07 02:50:22 +05301640 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001641 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1642 psa_set_key_algorithm( &attributes, alg );
1643 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001644
1645 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001646 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001647
Gilles Peskine49c25912018-10-29 15:15:31 +01001648 /* Export the public key */
1649 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001650 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001651 exported, export_size,
1652 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001653 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001654 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001655 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001656 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001657 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001658 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001659 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001660 TEST_LE_U( expected_public_key->len,
1661 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1662 TEST_LE_U( expected_public_key->len,
1663 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1664 TEST_LE_U( expected_public_key->len,
1665 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001666 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1667 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001668 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001669exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001670 /*
1671 * Key attributes may have been returned by psa_get_key_attributes()
1672 * thus reset them as required.
1673 */
1674 psa_reset_key_attributes( &attributes );
1675
itayzafrir3e02b3b2018-06-12 17:06:52 +03001676 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001677 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001678 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001679}
1680/* END_CASE */
1681
Gilles Peskine20035e32018-02-03 22:44:14 +01001682/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001683void import_and_exercise_key( data_t *data,
1684 int type_arg,
1685 int bits_arg,
1686 int alg_arg )
1687{
Ronald Cron5425a212020-08-04 14:58:35 +02001688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689 psa_key_type_t type = type_arg;
1690 size_t bits = bits_arg;
1691 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001692 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001694 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001695
Gilles Peskine8817f612018-12-18 00:18:46 +01001696 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001697
Gilles Peskine4747d192019-04-17 15:05:45 +02001698 psa_set_key_usage_flags( &attributes, usage );
1699 psa_set_key_algorithm( &attributes, alg );
1700 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001701
1702 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001703 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001704
1705 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001706 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001707 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1708 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709
1710 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001711 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001712 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001713
Ronald Cron5425a212020-08-04 14:58:35 +02001714 PSA_ASSERT( psa_destroy_key( key ) );
1715 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001716
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001717exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001718 /*
1719 * Key attributes may have been returned by psa_get_key_attributes()
1720 * thus reset them as required.
1721 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001722 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001723
1724 psa_reset_key_attributes( &attributes );
1725 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001726 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001727}
1728/* END_CASE */
1729
1730/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001731void effective_key_attributes( int type_arg, int expected_type_arg,
1732 int bits_arg, int expected_bits_arg,
1733 int usage_arg, int expected_usage_arg,
1734 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001735{
Ronald Cron5425a212020-08-04 14:58:35 +02001736 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001737 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001738 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001739 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001740 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001741 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001742 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001743 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001744 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001746
Gilles Peskine8817f612018-12-18 00:18:46 +01001747 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001748
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001749 psa_set_key_usage_flags( &attributes, usage );
1750 psa_set_key_algorithm( &attributes, alg );
1751 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001752 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001753
Ronald Cron5425a212020-08-04 14:58:35 +02001754 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001755 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001756
Ronald Cron5425a212020-08-04 14:58:35 +02001757 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001758 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1759 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1760 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1761 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001762
1763exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001764 /*
1765 * Key attributes may have been returned by psa_get_key_attributes()
1766 * thus reset them as required.
1767 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001768 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001769
1770 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001771 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001772}
1773/* END_CASE */
1774
1775/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001776void check_key_policy( int type_arg, int bits_arg,
1777 int usage_arg, int alg_arg )
1778{
1779 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001780 usage_arg,
1781 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001782 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001783 goto exit;
1784}
1785/* END_CASE */
1786
1787/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001788void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001789{
1790 /* Test each valid way of initializing the object, except for `= {0}`, as
1791 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1792 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001793 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001794 psa_key_attributes_t func = psa_key_attributes_init( );
1795 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1796 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001797
1798 memset( &zero, 0, sizeof( zero ) );
1799
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001800 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1801 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1802 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001803
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001804 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1805 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1806 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1807
1808 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1809 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1810 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1811
1812 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1813 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1814 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1815
1816 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1817 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1818 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001819}
1820/* END_CASE */
1821
1822/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001823void mac_key_policy( int policy_usage_arg,
1824 int policy_alg_arg,
1825 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001826 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001827 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001828 int expected_status_sign_arg,
1829 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001830{
Ronald Cron5425a212020-08-04 14:58:35 +02001831 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001833 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001834 psa_key_type_t key_type = key_type_arg;
1835 psa_algorithm_t policy_alg = policy_alg_arg;
1836 psa_algorithm_t exercise_alg = exercise_alg_arg;
1837 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001838 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001839 psa_status_t expected_status_sign = expected_status_sign_arg;
1840 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001841 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001842
Gilles Peskine8817f612018-12-18 00:18:46 +01001843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001844
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001845 psa_set_key_usage_flags( &attributes, policy_usage );
1846 psa_set_key_algorithm( &attributes, policy_alg );
1847 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001848
Gilles Peskine049c7532019-05-15 20:22:09 +02001849 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001850 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001851
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001852 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1853 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001854
Ronald Cron5425a212020-08-04 14:58:35 +02001855 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001856 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001857
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001858 /* Calculate the MAC, one-shot case. */
1859 uint8_t input[128] = {0};
1860 size_t mac_len;
1861 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1862 input, 128,
1863 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1864 expected_status_sign );
1865
Neil Armstrong3af9b972022-02-07 12:20:21 +01001866 /* Calculate the MAC, multi-part case. */
1867 PSA_ASSERT( psa_mac_abort( &operation ) );
1868 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1869 if( status == PSA_SUCCESS )
1870 {
1871 status = psa_mac_update( &operation, input, 128 );
1872 if( status == PSA_SUCCESS )
1873 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1874 &mac_len ),
1875 expected_status_sign );
1876 else
1877 TEST_EQUAL( status, expected_status_sign );
1878 }
1879 else
1880 {
1881 TEST_EQUAL( status, expected_status_sign );
1882 }
1883 PSA_ASSERT( psa_mac_abort( &operation ) );
1884
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001885 /* Verify correct MAC, one-shot case. */
1886 status = psa_mac_verify( key, exercise_alg, input, 128,
1887 mac, mac_len );
1888
1889 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1890 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001892 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893
Neil Armstrong3af9b972022-02-07 12:20:21 +01001894 /* Verify correct MAC, multi-part case. */
1895 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1896 if( status == PSA_SUCCESS )
1897 {
1898 status = psa_mac_update( &operation, input, 128 );
1899 if( status == PSA_SUCCESS )
1900 {
1901 status = psa_mac_verify_finish( &operation, mac, mac_len );
1902 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1903 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1904 else
1905 TEST_EQUAL( status, expected_status_verify );
1906 }
1907 else
1908 {
1909 TEST_EQUAL( status, expected_status_verify );
1910 }
1911 }
1912 else
1913 {
1914 TEST_EQUAL( status, expected_status_verify );
1915 }
1916
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001917 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001918
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001919 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001920 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001921 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922
1923exit:
1924 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001925 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001926 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001927}
1928/* END_CASE */
1929
1930/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001931void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932 int policy_alg,
1933 int key_type,
1934 data_t *key_data,
1935 int exercise_alg )
1936{
Ronald Cron5425a212020-08-04 14:58:35 +02001937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001938 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001939 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001940 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001941 size_t output_buffer_size = 0;
1942 size_t input_buffer_size = 0;
1943 size_t output_length = 0;
1944 uint8_t *output = NULL;
1945 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946 psa_status_t status;
1947
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001948 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1949 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1950 input_buffer_size );
1951
1952 ASSERT_ALLOC( input, input_buffer_size );
1953 ASSERT_ALLOC( output, output_buffer_size );
1954
Gilles Peskine8817f612018-12-18 00:18:46 +01001955 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001957 psa_set_key_usage_flags( &attributes, policy_usage );
1958 psa_set_key_algorithm( &attributes, policy_alg );
1959 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001960
Gilles Peskine049c7532019-05-15 20:22:09 +02001961 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001962 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001964 /* Check if no key usage flag implication is done */
1965 TEST_EQUAL( policy_usage,
1966 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001967
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001968 /* Encrypt check, one-shot */
1969 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1970 output, output_buffer_size,
1971 &output_length);
1972 if( policy_alg == exercise_alg &&
1973 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1974 PSA_ASSERT( status );
1975 else
1976 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1977
1978 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001979 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980 if( policy_alg == exercise_alg &&
1981 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001982 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001984 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 psa_cipher_abort( &operation );
1986
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001987 /* Decrypt check, one-shot */
1988 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1989 input, input_buffer_size,
1990 &output_length);
1991 if( policy_alg == exercise_alg &&
1992 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1993 PSA_ASSERT( status );
1994 else
1995 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1996
1997 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001998 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001999 if( policy_alg == exercise_alg &&
2000 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002001 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002003 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004
2005exit:
2006 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002007 mbedtls_free( input );
2008 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002009 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002010 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011}
2012/* END_CASE */
2013
2014/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002015void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016 int policy_alg,
2017 int key_type,
2018 data_t *key_data,
2019 int nonce_length_arg,
2020 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002021 int exercise_alg,
2022 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023{
Ronald Cron5425a212020-08-04 14:58:35 +02002024 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002026 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002027 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002029 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 unsigned char nonce[16] = {0};
2031 size_t nonce_length = nonce_length_arg;
2032 unsigned char tag[16];
2033 size_t tag_length = tag_length_arg;
2034 size_t output_length;
2035
Gilles Peskine7be11a72022-04-14 00:12:57 +02002036 TEST_LE_U( nonce_length, sizeof( nonce ) );
2037 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038
Gilles Peskine8817f612018-12-18 00:18:46 +01002039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002041 psa_set_key_usage_flags( &attributes, policy_usage );
2042 psa_set_key_algorithm( &attributes, policy_alg );
2043 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044
Gilles Peskine049c7532019-05-15 20:22:09 +02002045 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002046 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002048 /* Check if no key usage implication is done */
2049 TEST_EQUAL( policy_usage,
2050 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002051
Neil Armstrong752d8112022-02-07 14:51:11 +01002052 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002053 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002054 nonce, nonce_length,
2055 NULL, 0,
2056 NULL, 0,
2057 tag, tag_length,
2058 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002059 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2060 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002062 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063
Neil Armstrong752d8112022-02-07 14:51:11 +01002064 /* Encrypt check, multi-part */
2065 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2066 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2067 TEST_EQUAL( status, expected_status );
2068 else
2069 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2070
2071 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002073 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002074 nonce, nonce_length,
2075 NULL, 0,
2076 tag, tag_length,
2077 NULL, 0,
2078 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002079 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2080 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2081 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002082 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002084 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085
Neil Armstrong752d8112022-02-07 14:51:11 +01002086 /* Decrypt check, multi-part */
2087 PSA_ASSERT( psa_aead_abort( &operation ) );
2088 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2089 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2090 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2091 else
2092 TEST_EQUAL( status, expected_status );
2093
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002095 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002096 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002097 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098}
2099/* END_CASE */
2100
2101/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002102void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103 int policy_alg,
2104 int key_type,
2105 data_t *key_data,
2106 int exercise_alg )
2107{
Ronald Cron5425a212020-08-04 14:58:35 +02002108 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002110 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111 psa_status_t status;
2112 size_t key_bits;
2113 size_t buffer_length;
2114 unsigned char *buffer = NULL;
2115 size_t output_length;
2116
Gilles Peskine8817f612018-12-18 00:18:46 +01002117 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002119 psa_set_key_usage_flags( &attributes, policy_usage );
2120 psa_set_key_algorithm( &attributes, policy_alg );
2121 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122
Gilles Peskine049c7532019-05-15 20:22:09 +02002123 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002124 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002126 /* Check if no key usage implication is done */
2127 TEST_EQUAL( policy_usage,
2128 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002129
Ronald Cron5425a212020-08-04 14:58:35 +02002130 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2133 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002134 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135
Ronald Cron5425a212020-08-04 14:58:35 +02002136 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137 NULL, 0,
2138 NULL, 0,
2139 buffer, buffer_length,
2140 &output_length );
2141 if( policy_alg == exercise_alg &&
2142 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002143 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002144 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002145 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002146
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002147 if( buffer_length != 0 )
2148 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002149 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150 buffer, buffer_length,
2151 NULL, 0,
2152 buffer, buffer_length,
2153 &output_length );
2154 if( policy_alg == exercise_alg &&
2155 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002156 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002157 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002158 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159
2160exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002161 /*
2162 * Key attributes may have been returned by psa_get_key_attributes()
2163 * thus reset them as required.
2164 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002165 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002166
2167 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002168 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002169 mbedtls_free( buffer );
2170}
2171/* END_CASE */
2172
2173/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002174void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175 int policy_alg,
2176 int key_type,
2177 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002178 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002179 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002180 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002181{
Ronald Cron5425a212020-08-04 14:58:35 +02002182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002184 psa_key_usage_t policy_usage = policy_usage_arg;
2185 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002186 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002187 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2188 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2189 * compatible with the policy and `payload_length_arg` is supposed to be
2190 * a valid input length to sign. If `payload_length_arg <= 0`,
2191 * `exercise_alg` is supposed to be forbidden by the policy. */
2192 int compatible_alg = payload_length_arg > 0;
2193 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002194 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195 size_t signature_length;
2196
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002197 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002198 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002199 TEST_EQUAL( expected_usage,
2200 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002201
Gilles Peskine8817f612018-12-18 00:18:46 +01002202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002204 psa_set_key_usage_flags( &attributes, policy_usage );
2205 psa_set_key_algorithm( &attributes, policy_alg );
2206 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
Gilles Peskine049c7532019-05-15 20:22:09 +02002208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002209 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002210
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002211 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2212
Ronald Cron5425a212020-08-04 14:58:35 +02002213 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002214 payload, payload_length,
2215 signature, sizeof( signature ),
2216 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002217 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002218 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002219 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002220 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002221
2222 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002223 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002224 payload, payload_length,
2225 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002226 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002227 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002228 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002229 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002230
Gilles Peskinef7b41372021-09-22 16:15:05 +02002231 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002232 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002233 {
2234 status = psa_sign_message( key, exercise_alg,
2235 payload, payload_length,
2236 signature, sizeof( signature ),
2237 &signature_length );
2238 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2239 PSA_ASSERT( status );
2240 else
2241 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2242
2243 memset( signature, 0, sizeof( signature ) );
2244 status = psa_verify_message( key, exercise_alg,
2245 payload, payload_length,
2246 signature, sizeof( signature ) );
2247 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2248 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2249 else
2250 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2251 }
2252
Gilles Peskined5b33222018-06-18 22:20:03 +02002253exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002254 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002255 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002256}
2257/* END_CASE */
2258
Janos Follathba3fab92019-06-11 14:50:16 +01002259/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002260void derive_key_policy( int policy_usage,
2261 int policy_alg,
2262 int key_type,
2263 data_t *key_data,
2264 int exercise_alg )
2265{
Ronald Cron5425a212020-08-04 14:58:35 +02002266 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002268 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002269 psa_status_t status;
2270
Gilles Peskine8817f612018-12-18 00:18:46 +01002271 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002273 psa_set_key_usage_flags( &attributes, policy_usage );
2274 psa_set_key_algorithm( &attributes, policy_alg );
2275 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002276
Gilles Peskine049c7532019-05-15 20:22:09 +02002277 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002278 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002279
Janos Follathba3fab92019-06-11 14:50:16 +01002280 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2281
2282 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2283 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002284 {
Janos Follathba3fab92019-06-11 14:50:16 +01002285 PSA_ASSERT( psa_key_derivation_input_bytes(
2286 &operation,
2287 PSA_KEY_DERIVATION_INPUT_SEED,
2288 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002289 }
Janos Follathba3fab92019-06-11 14:50:16 +01002290
2291 status = psa_key_derivation_input_key( &operation,
2292 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002293 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002294
Gilles Peskineea0fb492018-07-12 17:17:20 +02002295 if( policy_alg == exercise_alg &&
2296 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002297 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002298 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002299 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002300
2301exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002302 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002303 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002304 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002305}
2306/* END_CASE */
2307
2308/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002309void agreement_key_policy( int policy_usage,
2310 int policy_alg,
2311 int key_type_arg,
2312 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002313 int exercise_alg,
2314 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002315{
Ronald Cron5425a212020-08-04 14:58:35 +02002316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002318 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002319 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002320 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002321 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002322
Gilles Peskine8817f612018-12-18 00:18:46 +01002323 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002324
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002325 psa_set_key_usage_flags( &attributes, policy_usage );
2326 psa_set_key_algorithm( &attributes, policy_alg );
2327 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002328
Gilles Peskine049c7532019-05-15 20:22:09 +02002329 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002330 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002332 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002333 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002334
Steven Cooremance48e852020-10-05 16:02:45 +02002335 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002336
2337exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002338 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002340 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341}
2342/* END_CASE */
2343
2344/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002345void key_policy_alg2( int key_type_arg, data_t *key_data,
2346 int usage_arg, int alg_arg, int alg2_arg )
2347{
Ronald Cron5425a212020-08-04 14:58:35 +02002348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002349 psa_key_type_t key_type = key_type_arg;
2350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2352 psa_key_usage_t usage = usage_arg;
2353 psa_algorithm_t alg = alg_arg;
2354 psa_algorithm_t alg2 = alg2_arg;
2355
2356 PSA_ASSERT( psa_crypto_init( ) );
2357
2358 psa_set_key_usage_flags( &attributes, usage );
2359 psa_set_key_algorithm( &attributes, alg );
2360 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2361 psa_set_key_type( &attributes, key_type );
2362 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002363 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002364
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002365 /* Update the usage flags to obtain implicit usage flags */
2366 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002368 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2369 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2370 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2371
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002372 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002373 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002374 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002375 goto exit;
2376
2377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002378 /*
2379 * Key attributes may have been returned by psa_get_key_attributes()
2380 * thus reset them as required.
2381 */
2382 psa_reset_key_attributes( &got_attributes );
2383
Ronald Cron5425a212020-08-04 14:58:35 +02002384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002385 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002386}
2387/* END_CASE */
2388
2389/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002390void raw_agreement_key_policy( int policy_usage,
2391 int policy_alg,
2392 int key_type_arg,
2393 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002394 int exercise_alg,
2395 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002396{
Ronald Cron5425a212020-08-04 14:58:35 +02002397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002399 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002400 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002401 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002402 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002403
2404 PSA_ASSERT( psa_crypto_init( ) );
2405
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002406 psa_set_key_usage_flags( &attributes, policy_usage );
2407 psa_set_key_algorithm( &attributes, policy_alg );
2408 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002409
Gilles Peskine049c7532019-05-15 20:22:09 +02002410 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002411 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002412
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002413 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002414
Steven Cooremance48e852020-10-05 16:02:45 +02002415 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002416
2417exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002418 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002419 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002420 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002421}
2422/* END_CASE */
2423
2424/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002425void copy_success( int source_usage_arg,
2426 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302427 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002428 int type_arg, data_t *material,
2429 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002430 int target_usage_arg,
2431 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302432 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002433 int expected_usage_arg,
2434 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002435{
Gilles Peskineca25db92019-04-19 11:43:08 +02002436 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2437 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002438 psa_key_usage_t expected_usage = expected_usage_arg;
2439 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002440 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302441 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2442 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002443 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2444 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002445 uint8_t *export_buffer = NULL;
2446
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447 PSA_ASSERT( psa_crypto_init( ) );
2448
Gilles Peskineca25db92019-04-19 11:43:08 +02002449 /* Prepare the source key. */
2450 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2451 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002452 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002453 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302454 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002455 PSA_ASSERT( psa_import_key( &source_attributes,
2456 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002457 &source_key ) );
2458 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459
Gilles Peskineca25db92019-04-19 11:43:08 +02002460 /* Prepare the target attributes. */
2461 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002462 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002463 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002464 }
Archana8a180362021-07-05 02:18:48 +05302465 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002466
Gilles Peskineca25db92019-04-19 11:43:08 +02002467 if( target_usage_arg != -1 )
2468 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2469 if( target_alg_arg != -1 )
2470 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002471 if( target_alg2_arg != -1 )
2472 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002473
Archana8a180362021-07-05 02:18:48 +05302474
Gilles Peskine57ab7212019-01-28 13:03:09 +01002475 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002476 PSA_ASSERT( psa_copy_key( source_key,
2477 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478
2479 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002480 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002481
2482 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002483 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002484 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2485 psa_get_key_type( &target_attributes ) );
2486 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2487 psa_get_key_bits( &target_attributes ) );
2488 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2489 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002490 TEST_EQUAL( expected_alg2,
2491 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002492 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2493 {
2494 size_t length;
2495 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002496 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002497 material->len, &length ) );
2498 ASSERT_COMPARE( material->x, material->len,
2499 export_buffer, length );
2500 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002501
Archana8a180362021-07-05 02:18:48 +05302502 if( !psa_key_lifetime_is_external( target_lifetime ) )
2503 {
2504 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2505 goto exit;
2506 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2507 goto exit;
2508 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002509
Ronald Cron5425a212020-08-04 14:58:35 +02002510 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002511
2512exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002513 /*
2514 * Source and target key attributes may have been returned by
2515 * psa_get_key_attributes() thus reset them as required.
2516 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002517 psa_reset_key_attributes( &source_attributes );
2518 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002519
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002520 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002521 mbedtls_free( export_buffer );
2522}
2523/* END_CASE */
2524
2525/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002526void copy_fail( int source_usage_arg,
2527 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302528 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002529 int type_arg, data_t *material,
2530 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002531 int target_usage_arg,
2532 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002533 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002534 int expected_status_arg )
2535{
2536 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2537 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002538 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2539 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002540 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002541
2542 PSA_ASSERT( psa_crypto_init( ) );
2543
2544 /* Prepare the source key. */
2545 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2546 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002547 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002548 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302549 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002550 PSA_ASSERT( psa_import_key( &source_attributes,
2551 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002552 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002553
2554 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002555 psa_set_key_id( &target_attributes, key_id );
2556 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002557 psa_set_key_type( &target_attributes, target_type_arg );
2558 psa_set_key_bits( &target_attributes, target_bits_arg );
2559 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2560 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002561 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002562
2563 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002564 TEST_EQUAL( psa_copy_key( source_key,
2565 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002566 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002567
Ronald Cron5425a212020-08-04 14:58:35 +02002568 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002569
Gilles Peskine4a644642019-05-03 17:14:08 +02002570exit:
2571 psa_reset_key_attributes( &source_attributes );
2572 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002573 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002574}
2575/* END_CASE */
2576
2577/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002578void hash_operation_init( )
2579{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002580 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002581 /* Test each valid way of initializing the object, except for `= {0}`, as
2582 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2583 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002584 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002585 psa_hash_operation_t func = psa_hash_operation_init( );
2586 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2587 psa_hash_operation_t zero;
2588
2589 memset( &zero, 0, sizeof( zero ) );
2590
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002591 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002592 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2593 PSA_ERROR_BAD_STATE );
2594 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2595 PSA_ERROR_BAD_STATE );
2596 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2597 PSA_ERROR_BAD_STATE );
2598
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002599 /* A default hash operation should be abortable without error. */
2600 PSA_ASSERT( psa_hash_abort( &func ) );
2601 PSA_ASSERT( psa_hash_abort( &init ) );
2602 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002603}
2604/* END_CASE */
2605
2606/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607void hash_setup( int alg_arg,
2608 int expected_status_arg )
2609{
2610 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002611 uint8_t *output = NULL;
2612 size_t output_size = 0;
2613 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002614 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002615 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002616 psa_status_t status;
2617
Gilles Peskine8817f612018-12-18 00:18:46 +01002618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619
Neil Armstrongedb20862022-02-07 15:47:44 +01002620 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002621 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002622 ASSERT_ALLOC( output, output_size );
2623
2624 status = psa_hash_compute( alg, NULL, 0,
2625 output, output_size, &output_length );
2626 TEST_EQUAL( status, expected_status );
2627
2628 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002629 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002630 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002632 /* Whether setup succeeded or failed, abort must succeed. */
2633 PSA_ASSERT( psa_hash_abort( &operation ) );
2634
2635 /* If setup failed, reproduce the failure, so as to
2636 * test the resulting state of the operation object. */
2637 if( status != PSA_SUCCESS )
2638 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2639
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002640 /* Now the operation object should be reusable. */
2641#if defined(KNOWN_SUPPORTED_HASH_ALG)
2642 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2643 PSA_ASSERT( psa_hash_abort( &operation ) );
2644#endif
2645
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002646exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002647 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002648 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002649}
2650/* END_CASE */
2651
2652/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002653void hash_compute_fail( int alg_arg, data_t *input,
2654 int output_size_arg, int expected_status_arg )
2655{
2656 psa_algorithm_t alg = alg_arg;
2657 uint8_t *output = NULL;
2658 size_t output_size = output_size_arg;
2659 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002660 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002661 psa_status_t expected_status = expected_status_arg;
2662 psa_status_t status;
2663
2664 ASSERT_ALLOC( output, output_size );
2665
2666 PSA_ASSERT( psa_crypto_init( ) );
2667
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002668 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002669 status = psa_hash_compute( alg, input->x, input->len,
2670 output, output_size, &output_length );
2671 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002672 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002673
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002674 /* Hash Compute, multi-part */
2675 status = psa_hash_setup( &operation, alg );
2676 if( status == PSA_SUCCESS )
2677 {
2678 status = psa_hash_update( &operation, input->x, input->len );
2679 if( status == PSA_SUCCESS )
2680 {
2681 status = psa_hash_finish( &operation, output, output_size,
2682 &output_length );
2683 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002684 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002685 else
2686 TEST_EQUAL( status, expected_status );
2687 }
2688 else
2689 {
2690 TEST_EQUAL( status, expected_status );
2691 }
2692 }
2693 else
2694 {
2695 TEST_EQUAL( status, expected_status );
2696 }
2697
Gilles Peskine0a749c82019-11-28 19:33:58 +01002698exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002699 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002700 mbedtls_free( output );
2701 PSA_DONE( );
2702}
2703/* END_CASE */
2704
2705/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002706void hash_compare_fail( int alg_arg, data_t *input,
2707 data_t *reference_hash,
2708 int expected_status_arg )
2709{
2710 psa_algorithm_t alg = alg_arg;
2711 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002712 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002713 psa_status_t status;
2714
2715 PSA_ASSERT( psa_crypto_init( ) );
2716
Neil Armstrong55a1be12022-02-07 11:23:20 +01002717 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002718 status = psa_hash_compare( alg, input->x, input->len,
2719 reference_hash->x, reference_hash->len );
2720 TEST_EQUAL( status, expected_status );
2721
Neil Armstrong55a1be12022-02-07 11:23:20 +01002722 /* Hash Compare, multi-part */
2723 status = psa_hash_setup( &operation, alg );
2724 if( status == PSA_SUCCESS )
2725 {
2726 status = psa_hash_update( &operation, input->x, input->len );
2727 if( status == PSA_SUCCESS )
2728 {
2729 status = psa_hash_verify( &operation, reference_hash->x,
2730 reference_hash->len );
2731 TEST_EQUAL( status, expected_status );
2732 }
2733 else
2734 {
2735 TEST_EQUAL( status, expected_status );
2736 }
2737 }
2738 else
2739 {
2740 TEST_EQUAL( status, expected_status );
2741 }
2742
Gilles Peskine88e08462020-01-28 20:43:00 +01002743exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002744 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002745 PSA_DONE( );
2746}
2747/* END_CASE */
2748
2749/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002750void hash_compute_compare( int alg_arg, data_t *input,
2751 data_t *expected_output )
2752{
2753 psa_algorithm_t alg = alg_arg;
2754 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2755 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002756 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002757 size_t i;
2758
2759 PSA_ASSERT( psa_crypto_init( ) );
2760
Neil Armstrongca30a002022-02-07 11:40:23 +01002761 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002762 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002763 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002764 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002765 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002766 ASSERT_COMPARE( output, output_length,
2767 expected_output->x, expected_output->len );
2768
Neil Armstrongca30a002022-02-07 11:40:23 +01002769 /* Compute with tight buffer, multi-part */
2770 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2771 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2772 PSA_ASSERT( psa_hash_finish( &operation, output,
2773 PSA_HASH_LENGTH( alg ),
2774 &output_length ) );
2775 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2776 ASSERT_COMPARE( output, output_length,
2777 expected_output->x, expected_output->len );
2778
2779 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002780 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2781 output, sizeof( output ),
2782 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002783 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002784 ASSERT_COMPARE( output, output_length,
2785 expected_output->x, expected_output->len );
2786
Neil Armstrongca30a002022-02-07 11:40:23 +01002787 /* Compute with larger buffer, multi-part */
2788 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2789 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2790 PSA_ASSERT( psa_hash_finish( &operation, output,
2791 sizeof( output ), &output_length ) );
2792 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2793 ASSERT_COMPARE( output, output_length,
2794 expected_output->x, expected_output->len );
2795
2796 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002797 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2798 output, output_length ) );
2799
Neil Armstrongca30a002022-02-07 11:40:23 +01002800 /* Compare with correct hash, multi-part */
2801 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2802 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2803 PSA_ASSERT( psa_hash_verify( &operation, output,
2804 output_length ) );
2805
2806 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002807 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2808 output, output_length + 1 ),
2809 PSA_ERROR_INVALID_SIGNATURE );
2810
Neil Armstrongca30a002022-02-07 11:40:23 +01002811 /* Compare with trailing garbage, multi-part */
2812 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2813 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2814 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2815 PSA_ERROR_INVALID_SIGNATURE );
2816
2817 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002818 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2819 output, output_length - 1 ),
2820 PSA_ERROR_INVALID_SIGNATURE );
2821
Neil Armstrongca30a002022-02-07 11:40:23 +01002822 /* Compare with truncated hash, multi-part */
2823 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2824 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2825 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2826 PSA_ERROR_INVALID_SIGNATURE );
2827
Gilles Peskine0a749c82019-11-28 19:33:58 +01002828 /* Compare with corrupted value */
2829 for( i = 0; i < output_length; i++ )
2830 {
Chris Jones9634bb12021-01-20 15:56:42 +00002831 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002832 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002833
2834 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002835 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2836 output, output_length ),
2837 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002838
2839 /* Multi-Part */
2840 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2841 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2842 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2843 PSA_ERROR_INVALID_SIGNATURE );
2844
Gilles Peskine0a749c82019-11-28 19:33:58 +01002845 output[i] ^= 1;
2846 }
2847
2848exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002849 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002850 PSA_DONE( );
2851}
2852/* END_CASE */
2853
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002854/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002855void hash_bad_order( )
2856{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002857 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002858 unsigned char input[] = "";
2859 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002860 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002861 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2862 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2863 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002864 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002865 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002866 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002867
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002869
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002870 /* Call setup twice in a row. */
2871 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002872 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002873 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2874 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002875 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002876 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002877 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002878
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002879 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002880 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002881 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002882 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002883
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002884 /* Check that update calls abort on error. */
2885 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002886 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002887 ASSERT_OPERATION_IS_ACTIVE( operation );
2888 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2889 PSA_ERROR_BAD_STATE );
2890 ASSERT_OPERATION_IS_INACTIVE( operation );
2891 PSA_ASSERT( psa_hash_abort( &operation ) );
2892 ASSERT_OPERATION_IS_INACTIVE( operation );
2893
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002894 /* Call update after finish. */
2895 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2896 PSA_ASSERT( psa_hash_finish( &operation,
2897 hash, sizeof( hash ), &hash_len ) );
2898 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002899 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002900 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002901
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002902 /* Call verify without calling setup beforehand. */
2903 TEST_EQUAL( psa_hash_verify( &operation,
2904 valid_hash, sizeof( valid_hash ) ),
2905 PSA_ERROR_BAD_STATE );
2906 PSA_ASSERT( psa_hash_abort( &operation ) );
2907
2908 /* Call verify after finish. */
2909 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2910 PSA_ASSERT( psa_hash_finish( &operation,
2911 hash, sizeof( hash ), &hash_len ) );
2912 TEST_EQUAL( psa_hash_verify( &operation,
2913 valid_hash, sizeof( valid_hash ) ),
2914 PSA_ERROR_BAD_STATE );
2915 PSA_ASSERT( psa_hash_abort( &operation ) );
2916
2917 /* Call verify twice in a row. */
2918 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002919 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002920 PSA_ASSERT( psa_hash_verify( &operation,
2921 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002922 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002923 TEST_EQUAL( psa_hash_verify( &operation,
2924 valid_hash, sizeof( valid_hash ) ),
2925 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002926 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002927 PSA_ASSERT( psa_hash_abort( &operation ) );
2928
2929 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002930 TEST_EQUAL( psa_hash_finish( &operation,
2931 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002932 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002933 PSA_ASSERT( psa_hash_abort( &operation ) );
2934
2935 /* Call finish twice in a row. */
2936 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2937 PSA_ASSERT( psa_hash_finish( &operation,
2938 hash, sizeof( hash ), &hash_len ) );
2939 TEST_EQUAL( psa_hash_finish( &operation,
2940 hash, sizeof( hash ), &hash_len ),
2941 PSA_ERROR_BAD_STATE );
2942 PSA_ASSERT( psa_hash_abort( &operation ) );
2943
2944 /* Call finish after calling verify. */
2945 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2946 PSA_ASSERT( psa_hash_verify( &operation,
2947 valid_hash, sizeof( valid_hash ) ) );
2948 TEST_EQUAL( psa_hash_finish( &operation,
2949 hash, sizeof( hash ), &hash_len ),
2950 PSA_ERROR_BAD_STATE );
2951 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002952
2953exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002954 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002955}
2956/* END_CASE */
2957
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002958/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002959void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002960{
2961 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002962 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2963 * appended to it */
2964 unsigned char hash[] = {
2965 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2966 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2967 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002968 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002969 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002970
Gilles Peskine8817f612018-12-18 00:18:46 +01002971 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002972
itayzafrir27e69452018-11-01 14:26:34 +02002973 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002974 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002975 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002976 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002977 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002978 ASSERT_OPERATION_IS_INACTIVE( operation );
2979 PSA_ASSERT( psa_hash_abort( &operation ) );
2980 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002981
itayzafrir27e69452018-11-01 14:26:34 +02002982 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002983 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002984 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002985 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002986
itayzafrir27e69452018-11-01 14:26:34 +02002987 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002988 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002989 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002990 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002991
itayzafrirec93d302018-10-18 18:01:10 +03002992exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002993 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002994}
2995/* END_CASE */
2996
Ronald Cronee414c72021-03-18 18:50:08 +01002997/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002998void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002999{
3000 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003001 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003002 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003003 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003004 size_t hash_len;
3005
Gilles Peskine8817f612018-12-18 00:18:46 +01003006 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003007
itayzafrir58028322018-10-25 10:22:01 +03003008 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003010 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003011 hash, expected_size - 1, &hash_len ),
3012 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003013
3014exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003015 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003016}
3017/* END_CASE */
3018
Ronald Cronee414c72021-03-18 18:50:08 +01003019/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003020void hash_clone_source_state( )
3021{
3022 psa_algorithm_t alg = PSA_ALG_SHA_256;
3023 unsigned char hash[PSA_HASH_MAX_SIZE];
3024 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3025 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3026 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3027 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3028 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3029 size_t hash_len;
3030
3031 PSA_ASSERT( psa_crypto_init( ) );
3032 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3033
3034 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3035 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3036 PSA_ASSERT( psa_hash_finish( &op_finished,
3037 hash, sizeof( hash ), &hash_len ) );
3038 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3039 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3040
3041 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3042 PSA_ERROR_BAD_STATE );
3043
3044 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3045 PSA_ASSERT( psa_hash_finish( &op_init,
3046 hash, sizeof( hash ), &hash_len ) );
3047 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3048 PSA_ASSERT( psa_hash_finish( &op_finished,
3049 hash, sizeof( hash ), &hash_len ) );
3050 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3051 PSA_ASSERT( psa_hash_finish( &op_aborted,
3052 hash, sizeof( hash ), &hash_len ) );
3053
3054exit:
3055 psa_hash_abort( &op_source );
3056 psa_hash_abort( &op_init );
3057 psa_hash_abort( &op_setup );
3058 psa_hash_abort( &op_finished );
3059 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003060 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003061}
3062/* END_CASE */
3063
Ronald Cronee414c72021-03-18 18:50:08 +01003064/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003065void hash_clone_target_state( )
3066{
3067 psa_algorithm_t alg = PSA_ALG_SHA_256;
3068 unsigned char hash[PSA_HASH_MAX_SIZE];
3069 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3070 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3071 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3072 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3073 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3074 size_t hash_len;
3075
3076 PSA_ASSERT( psa_crypto_init( ) );
3077
3078 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3079 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3080 PSA_ASSERT( psa_hash_finish( &op_finished,
3081 hash, sizeof( hash ), &hash_len ) );
3082 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3083 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3084
3085 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3086 PSA_ASSERT( psa_hash_finish( &op_target,
3087 hash, sizeof( hash ), &hash_len ) );
3088
3089 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3090 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3091 PSA_ERROR_BAD_STATE );
3092 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3093 PSA_ERROR_BAD_STATE );
3094
3095exit:
3096 psa_hash_abort( &op_target );
3097 psa_hash_abort( &op_init );
3098 psa_hash_abort( &op_setup );
3099 psa_hash_abort( &op_finished );
3100 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003101 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003102}
3103/* END_CASE */
3104
itayzafrir58028322018-10-25 10:22:01 +03003105/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003106void mac_operation_init( )
3107{
Jaeden Amero252ef282019-02-15 14:05:35 +00003108 const uint8_t input[1] = { 0 };
3109
Jaeden Amero769ce272019-01-04 11:48:03 +00003110 /* Test each valid way of initializing the object, except for `= {0}`, as
3111 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3112 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003113 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003114 psa_mac_operation_t func = psa_mac_operation_init( );
3115 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3116 psa_mac_operation_t zero;
3117
3118 memset( &zero, 0, sizeof( zero ) );
3119
Jaeden Amero252ef282019-02-15 14:05:35 +00003120 /* A freshly-initialized MAC operation should not be usable. */
3121 TEST_EQUAL( psa_mac_update( &func,
3122 input, sizeof( input ) ),
3123 PSA_ERROR_BAD_STATE );
3124 TEST_EQUAL( psa_mac_update( &init,
3125 input, sizeof( input ) ),
3126 PSA_ERROR_BAD_STATE );
3127 TEST_EQUAL( psa_mac_update( &zero,
3128 input, sizeof( input ) ),
3129 PSA_ERROR_BAD_STATE );
3130
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003131 /* A default MAC operation should be abortable without error. */
3132 PSA_ASSERT( psa_mac_abort( &func ) );
3133 PSA_ASSERT( psa_mac_abort( &init ) );
3134 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003135}
3136/* END_CASE */
3137
3138/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003139void mac_setup( int key_type_arg,
3140 data_t *key,
3141 int alg_arg,
3142 int expected_status_arg )
3143{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003144 psa_key_type_t key_type = key_type_arg;
3145 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003146 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003147 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003148 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3149#if defined(KNOWN_SUPPORTED_MAC_ALG)
3150 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3151#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003152
Gilles Peskine8817f612018-12-18 00:18:46 +01003153 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003154
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003155 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3156 &operation, &status ) )
3157 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003158 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003160 /* The operation object should be reusable. */
3161#if defined(KNOWN_SUPPORTED_MAC_ALG)
3162 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3163 smoke_test_key_data,
3164 sizeof( smoke_test_key_data ),
3165 KNOWN_SUPPORTED_MAC_ALG,
3166 &operation, &status ) )
3167 goto exit;
3168 TEST_EQUAL( status, PSA_SUCCESS );
3169#endif
3170
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003171exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003172 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173}
3174/* END_CASE */
3175
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003176/* 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 +00003177void mac_bad_order( )
3178{
Ronald Cron5425a212020-08-04 14:58:35 +02003179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003180 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3181 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003182 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003183 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3184 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3185 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003187 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3188 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3189 size_t sign_mac_length = 0;
3190 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3191 const uint8_t verify_mac[] = {
3192 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3193 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3194 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3195
3196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003197 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003198 psa_set_key_algorithm( &attributes, alg );
3199 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003200
Ronald Cron5425a212020-08-04 14:58:35 +02003201 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3202 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003203
Jaeden Amero252ef282019-02-15 14:05:35 +00003204 /* Call update without calling setup beforehand. */
3205 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3206 PSA_ERROR_BAD_STATE );
3207 PSA_ASSERT( psa_mac_abort( &operation ) );
3208
3209 /* Call sign finish without calling setup beforehand. */
3210 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3211 &sign_mac_length),
3212 PSA_ERROR_BAD_STATE );
3213 PSA_ASSERT( psa_mac_abort( &operation ) );
3214
3215 /* Call verify finish without calling setup beforehand. */
3216 TEST_EQUAL( psa_mac_verify_finish( &operation,
3217 verify_mac, sizeof( verify_mac ) ),
3218 PSA_ERROR_BAD_STATE );
3219 PSA_ASSERT( psa_mac_abort( &operation ) );
3220
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003221 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003222 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003223 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003224 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003225 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003226 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003227 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003228 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003229
Jaeden Amero252ef282019-02-15 14:05:35 +00003230 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003231 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003232 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3233 PSA_ASSERT( psa_mac_sign_finish( &operation,
3234 sign_mac, sizeof( sign_mac ),
3235 &sign_mac_length ) );
3236 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3237 PSA_ERROR_BAD_STATE );
3238 PSA_ASSERT( psa_mac_abort( &operation ) );
3239
3240 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003241 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003242 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3243 PSA_ASSERT( psa_mac_verify_finish( &operation,
3244 verify_mac, sizeof( verify_mac ) ) );
3245 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3246 PSA_ERROR_BAD_STATE );
3247 PSA_ASSERT( psa_mac_abort( &operation ) );
3248
3249 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003250 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003251 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3252 PSA_ASSERT( psa_mac_sign_finish( &operation,
3253 sign_mac, sizeof( sign_mac ),
3254 &sign_mac_length ) );
3255 TEST_EQUAL( psa_mac_sign_finish( &operation,
3256 sign_mac, sizeof( sign_mac ),
3257 &sign_mac_length ),
3258 PSA_ERROR_BAD_STATE );
3259 PSA_ASSERT( psa_mac_abort( &operation ) );
3260
3261 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003262 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003263 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3264 PSA_ASSERT( psa_mac_verify_finish( &operation,
3265 verify_mac, sizeof( verify_mac ) ) );
3266 TEST_EQUAL( psa_mac_verify_finish( &operation,
3267 verify_mac, sizeof( verify_mac ) ),
3268 PSA_ERROR_BAD_STATE );
3269 PSA_ASSERT( psa_mac_abort( &operation ) );
3270
3271 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003272 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003273 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003274 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003275 TEST_EQUAL( psa_mac_verify_finish( &operation,
3276 verify_mac, sizeof( verify_mac ) ),
3277 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003278 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003279 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003280 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003281
3282 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003283 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003284 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003285 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003286 TEST_EQUAL( psa_mac_sign_finish( &operation,
3287 sign_mac, sizeof( sign_mac ),
3288 &sign_mac_length ),
3289 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003290 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003291 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003292 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003293
Ronald Cron5425a212020-08-04 14:58:35 +02003294 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003295
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003296exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003297 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003298}
3299/* END_CASE */
3300
3301/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003302void mac_sign_verify_multi( int key_type_arg,
3303 data_t *key_data,
3304 int alg_arg,
3305 data_t *input,
3306 int is_verify,
3307 data_t *expected_mac )
3308{
3309 size_t data_part_len = 0;
3310
3311 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3312 {
3313 /* Split data into length(data_part_len) parts. */
3314 mbedtls_test_set_step( 2000 + data_part_len );
3315
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003316 if( mac_multipart_internal_func( key_type_arg, key_data,
3317 alg_arg,
3318 input, data_part_len,
3319 expected_mac,
3320 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003321 break;
3322
3323 /* length(0) part, length(data_part_len) part, length(0) part... */
3324 mbedtls_test_set_step( 3000 + data_part_len );
3325
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003326 if( mac_multipart_internal_func( key_type_arg, key_data,
3327 alg_arg,
3328 input, data_part_len,
3329 expected_mac,
3330 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003331 break;
3332 }
3333
3334 /* Goto is required to silence warnings about unused labels, as we
3335 * don't actually do any test assertions in this function. */
3336 goto exit;
3337}
3338/* END_CASE */
3339
3340/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003341void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003342 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003343 int alg_arg,
3344 data_t *input,
3345 data_t *expected_mac )
3346{
Ronald Cron5425a212020-08-04 14:58:35 +02003347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003348 psa_key_type_t key_type = key_type_arg;
3349 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003350 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003352 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003353 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003354 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003355 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003356 const size_t output_sizes_to_test[] = {
3357 0,
3358 1,
3359 expected_mac->len - 1,
3360 expected_mac->len,
3361 expected_mac->len + 1,
3362 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363
Gilles Peskine7be11a72022-04-14 00:12:57 +02003364 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003365 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003366 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003367
Gilles Peskine8817f612018-12-18 00:18:46 +01003368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003369
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003370 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003371 psa_set_key_algorithm( &attributes, alg );
3372 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003373
Ronald Cron5425a212020-08-04 14:58:35 +02003374 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3375 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376
Gilles Peskine8b356b52020-08-25 23:44:59 +02003377 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3378 {
3379 const size_t output_size = output_sizes_to_test[i];
3380 psa_status_t expected_status =
3381 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3382 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003383
Chris Jones9634bb12021-01-20 15:56:42 +00003384 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003385 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003386
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003387 /* Calculate the MAC, one-shot case. */
3388 TEST_EQUAL( psa_mac_compute( key, alg,
3389 input->x, input->len,
3390 actual_mac, output_size, &mac_length ),
3391 expected_status );
3392 if( expected_status == PSA_SUCCESS )
3393 {
3394 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3395 actual_mac, mac_length );
3396 }
3397
3398 if( output_size > 0 )
3399 memset( actual_mac, 0, output_size );
3400
3401 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003402 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003403 PSA_ASSERT( psa_mac_update( &operation,
3404 input->x, input->len ) );
3405 TEST_EQUAL( psa_mac_sign_finish( &operation,
3406 actual_mac, output_size,
3407 &mac_length ),
3408 expected_status );
3409 PSA_ASSERT( psa_mac_abort( &operation ) );
3410
3411 if( expected_status == PSA_SUCCESS )
3412 {
3413 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3414 actual_mac, mac_length );
3415 }
3416 mbedtls_free( actual_mac );
3417 actual_mac = NULL;
3418 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003419
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003420exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003421 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003422 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003423 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003424 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003425}
3426/* END_CASE */
3427
3428/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003429void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003430 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003431 int alg_arg,
3432 data_t *input,
3433 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003434{
Ronald Cron5425a212020-08-04 14:58:35 +02003435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003436 psa_key_type_t key_type = key_type_arg;
3437 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003438 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003440 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003441
Gilles Peskine7be11a72022-04-14 00:12:57 +02003442 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003443
Gilles Peskine8817f612018-12-18 00:18:46 +01003444 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003445
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003446 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_set_key_algorithm( &attributes, alg );
3448 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003449
Ronald Cron5425a212020-08-04 14:58:35 +02003450 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3451 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003452
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003453 /* Verify correct MAC, one-shot case. */
3454 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3455 expected_mac->x, expected_mac->len ) );
3456
3457 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003458 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003459 PSA_ASSERT( psa_mac_update( &operation,
3460 input->x, input->len ) );
3461 PSA_ASSERT( psa_mac_verify_finish( &operation,
3462 expected_mac->x,
3463 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003464
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003465 /* Test a MAC that's too short, one-shot case. */
3466 TEST_EQUAL( psa_mac_verify( key, alg,
3467 input->x, input->len,
3468 expected_mac->x,
3469 expected_mac->len - 1 ),
3470 PSA_ERROR_INVALID_SIGNATURE );
3471
3472 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003473 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003474 PSA_ASSERT( psa_mac_update( &operation,
3475 input->x, input->len ) );
3476 TEST_EQUAL( psa_mac_verify_finish( &operation,
3477 expected_mac->x,
3478 expected_mac->len - 1 ),
3479 PSA_ERROR_INVALID_SIGNATURE );
3480
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003481 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003482 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3483 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003484 TEST_EQUAL( psa_mac_verify( key, alg,
3485 input->x, input->len,
3486 perturbed_mac, expected_mac->len + 1 ),
3487 PSA_ERROR_INVALID_SIGNATURE );
3488
3489 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003490 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003491 PSA_ASSERT( psa_mac_update( &operation,
3492 input->x, input->len ) );
3493 TEST_EQUAL( psa_mac_verify_finish( &operation,
3494 perturbed_mac,
3495 expected_mac->len + 1 ),
3496 PSA_ERROR_INVALID_SIGNATURE );
3497
3498 /* Test changing one byte. */
3499 for( size_t i = 0; i < expected_mac->len; i++ )
3500 {
Chris Jones9634bb12021-01-20 15:56:42 +00003501 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003502 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003503
3504 TEST_EQUAL( psa_mac_verify( key, alg,
3505 input->x, input->len,
3506 perturbed_mac, expected_mac->len ),
3507 PSA_ERROR_INVALID_SIGNATURE );
3508
Ronald Cron5425a212020-08-04 14:58:35 +02003509 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003510 PSA_ASSERT( psa_mac_update( &operation,
3511 input->x, input->len ) );
3512 TEST_EQUAL( psa_mac_verify_finish( &operation,
3513 perturbed_mac,
3514 expected_mac->len ),
3515 PSA_ERROR_INVALID_SIGNATURE );
3516 perturbed_mac[i] ^= 1;
3517 }
3518
Gilles Peskine8c9def32018-02-08 10:02:12 +01003519exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003520 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003521 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003522 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003523 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003524}
3525/* END_CASE */
3526
3527/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003528void cipher_operation_init( )
3529{
Jaeden Ameroab439972019-02-15 14:12:05 +00003530 const uint8_t input[1] = { 0 };
3531 unsigned char output[1] = { 0 };
3532 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003533 /* Test each valid way of initializing the object, except for `= {0}`, as
3534 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3535 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003536 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003537 psa_cipher_operation_t func = psa_cipher_operation_init( );
3538 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3539 psa_cipher_operation_t zero;
3540
3541 memset( &zero, 0, sizeof( zero ) );
3542
Jaeden Ameroab439972019-02-15 14:12:05 +00003543 /* A freshly-initialized cipher operation should not be usable. */
3544 TEST_EQUAL( psa_cipher_update( &func,
3545 input, sizeof( input ),
3546 output, sizeof( output ),
3547 &output_length ),
3548 PSA_ERROR_BAD_STATE );
3549 TEST_EQUAL( psa_cipher_update( &init,
3550 input, sizeof( input ),
3551 output, sizeof( output ),
3552 &output_length ),
3553 PSA_ERROR_BAD_STATE );
3554 TEST_EQUAL( psa_cipher_update( &zero,
3555 input, sizeof( input ),
3556 output, sizeof( output ),
3557 &output_length ),
3558 PSA_ERROR_BAD_STATE );
3559
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003560 /* A default cipher operation should be abortable without error. */
3561 PSA_ASSERT( psa_cipher_abort( &func ) );
3562 PSA_ASSERT( psa_cipher_abort( &init ) );
3563 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003564}
3565/* END_CASE */
3566
3567/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003568void cipher_setup( int key_type_arg,
3569 data_t *key,
3570 int alg_arg,
3571 int expected_status_arg )
3572{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003573 psa_key_type_t key_type = key_type_arg;
3574 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003575 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003576 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003577 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003578#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003579 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3580#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003581
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003583
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003584 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3585 &operation, &status ) )
3586 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003587 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003588
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003589 /* The operation object should be reusable. */
3590#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3591 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3592 smoke_test_key_data,
3593 sizeof( smoke_test_key_data ),
3594 KNOWN_SUPPORTED_CIPHER_ALG,
3595 &operation, &status ) )
3596 goto exit;
3597 TEST_EQUAL( status, PSA_SUCCESS );
3598#endif
3599
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003600exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003601 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003602 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003603}
3604/* END_CASE */
3605
Ronald Cronee414c72021-03-18 18:50:08 +01003606/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003607void cipher_bad_order( )
3608{
Ronald Cron5425a212020-08-04 14:58:35 +02003609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003610 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3611 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003613 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003614 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003615 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003616 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3617 0xaa, 0xaa, 0xaa, 0xaa };
3618 const uint8_t text[] = {
3619 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3620 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003621 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003622 size_t length = 0;
3623
3624 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003625 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3626 psa_set_key_algorithm( &attributes, alg );
3627 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003628 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3629 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003630
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003631 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003632 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003633 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003634 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003635 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003636 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003637 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003638 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003639
3640 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003641 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003642 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003643 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003644 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003645 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003646 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003647 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003648
Jaeden Ameroab439972019-02-15 14:12:05 +00003649 /* Generate an IV without calling setup beforehand. */
3650 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3651 buffer, sizeof( buffer ),
3652 &length ),
3653 PSA_ERROR_BAD_STATE );
3654 PSA_ASSERT( psa_cipher_abort( &operation ) );
3655
3656 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003657 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003658 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3659 buffer, sizeof( buffer ),
3660 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003661 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003662 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3663 buffer, sizeof( buffer ),
3664 &length ),
3665 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003666 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003667 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003668 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003669
3670 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003671 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003672 PSA_ASSERT( psa_cipher_set_iv( &operation,
3673 iv, sizeof( iv ) ) );
3674 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3675 buffer, sizeof( buffer ),
3676 &length ),
3677 PSA_ERROR_BAD_STATE );
3678 PSA_ASSERT( psa_cipher_abort( &operation ) );
3679
3680 /* Set an IV without calling setup beforehand. */
3681 TEST_EQUAL( psa_cipher_set_iv( &operation,
3682 iv, sizeof( iv ) ),
3683 PSA_ERROR_BAD_STATE );
3684 PSA_ASSERT( psa_cipher_abort( &operation ) );
3685
3686 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003687 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003688 PSA_ASSERT( psa_cipher_set_iv( &operation,
3689 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003690 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003691 TEST_EQUAL( psa_cipher_set_iv( &operation,
3692 iv, sizeof( iv ) ),
3693 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003694 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003695 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003696 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003697
3698 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003699 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003700 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3701 buffer, sizeof( buffer ),
3702 &length ) );
3703 TEST_EQUAL( psa_cipher_set_iv( &operation,
3704 iv, sizeof( iv ) ),
3705 PSA_ERROR_BAD_STATE );
3706 PSA_ASSERT( psa_cipher_abort( &operation ) );
3707
3708 /* Call update without calling setup beforehand. */
3709 TEST_EQUAL( psa_cipher_update( &operation,
3710 text, sizeof( text ),
3711 buffer, sizeof( buffer ),
3712 &length ),
3713 PSA_ERROR_BAD_STATE );
3714 PSA_ASSERT( psa_cipher_abort( &operation ) );
3715
3716 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003717 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003718 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003719 TEST_EQUAL( psa_cipher_update( &operation,
3720 text, sizeof( text ),
3721 buffer, sizeof( buffer ),
3722 &length ),
3723 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003724 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003725 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003726 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003727
3728 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003729 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003730 PSA_ASSERT( psa_cipher_set_iv( &operation,
3731 iv, sizeof( iv ) ) );
3732 PSA_ASSERT( psa_cipher_finish( &operation,
3733 buffer, sizeof( buffer ), &length ) );
3734 TEST_EQUAL( psa_cipher_update( &operation,
3735 text, sizeof( text ),
3736 buffer, sizeof( buffer ),
3737 &length ),
3738 PSA_ERROR_BAD_STATE );
3739 PSA_ASSERT( psa_cipher_abort( &operation ) );
3740
3741 /* Call finish without calling setup beforehand. */
3742 TEST_EQUAL( psa_cipher_finish( &operation,
3743 buffer, sizeof( buffer ), &length ),
3744 PSA_ERROR_BAD_STATE );
3745 PSA_ASSERT( psa_cipher_abort( &operation ) );
3746
3747 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003748 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003749 /* Not calling update means we are encrypting an empty buffer, which is OK
3750 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003751 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003752 TEST_EQUAL( psa_cipher_finish( &operation,
3753 buffer, sizeof( buffer ), &length ),
3754 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003755 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003756 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003757 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003758
3759 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003760 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003761 PSA_ASSERT( psa_cipher_set_iv( &operation,
3762 iv, sizeof( iv ) ) );
3763 PSA_ASSERT( psa_cipher_finish( &operation,
3764 buffer, sizeof( buffer ), &length ) );
3765 TEST_EQUAL( psa_cipher_finish( &operation,
3766 buffer, sizeof( buffer ), &length ),
3767 PSA_ERROR_BAD_STATE );
3768 PSA_ASSERT( psa_cipher_abort( &operation ) );
3769
Ronald Cron5425a212020-08-04 14:58:35 +02003770 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003771
Jaeden Ameroab439972019-02-15 14:12:05 +00003772exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003773 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003774 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003775}
3776/* END_CASE */
3777
3778/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003779void cipher_encrypt_fail( int alg_arg,
3780 int key_type_arg,
3781 data_t *key_data,
3782 data_t *input,
3783 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003784{
Ronald Cron5425a212020-08-04 14:58:35 +02003785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003786 psa_status_t status;
3787 psa_key_type_t key_type = key_type_arg;
3788 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003789 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003790 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3791 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3792 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003793 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003794 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003795 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003796 size_t function_output_length;
3797 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3799
3800 if ( PSA_ERROR_BAD_STATE != expected_status )
3801 {
3802 PSA_ASSERT( psa_crypto_init( ) );
3803
3804 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3805 psa_set_key_algorithm( &attributes, alg );
3806 psa_set_key_type( &attributes, key_type );
3807
3808 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3809 input->len );
3810 ASSERT_ALLOC( output, output_buffer_size );
3811
3812 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3813 &key ) );
3814 }
3815
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003816 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003817 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3818 output_buffer_size, &output_length );
3819
3820 TEST_EQUAL( status, expected_status );
3821
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003822 /* Encrypt, multi-part */
3823 status = psa_cipher_encrypt_setup( &operation, key, alg );
3824 if( status == PSA_SUCCESS )
3825 {
3826 if( alg != PSA_ALG_ECB_NO_PADDING )
3827 {
3828 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3829 iv, iv_size,
3830 &iv_length ) );
3831 }
3832
3833 status = psa_cipher_update( &operation, input->x, input->len,
3834 output, output_buffer_size,
3835 &function_output_length );
3836 if( status == PSA_SUCCESS )
3837 {
3838 output_length += function_output_length;
3839
3840 status = psa_cipher_finish( &operation, output + output_length,
3841 output_buffer_size - output_length,
3842 &function_output_length );
3843
3844 TEST_EQUAL( status, expected_status );
3845 }
3846 else
3847 {
3848 TEST_EQUAL( status, expected_status );
3849 }
3850 }
3851 else
3852 {
3853 TEST_EQUAL( status, expected_status );
3854 }
3855
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003856exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003857 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003858 mbedtls_free( output );
3859 psa_destroy_key( key );
3860 PSA_DONE( );
3861}
3862/* END_CASE */
3863
3864/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003865void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3866 data_t *input, int iv_length,
3867 int expected_result )
3868{
3869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3870 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3872 size_t output_buffer_size = 0;
3873 unsigned char *output = NULL;
3874
3875 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3876 ASSERT_ALLOC( output, output_buffer_size );
3877
3878 PSA_ASSERT( psa_crypto_init( ) );
3879
3880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3881 psa_set_key_algorithm( &attributes, alg );
3882 psa_set_key_type( &attributes, key_type );
3883
3884 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3885 &key ) );
3886 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3887 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3888 iv_length ) );
3889
3890exit:
3891 psa_cipher_abort( &operation );
3892 mbedtls_free( output );
3893 psa_destroy_key( key );
3894 PSA_DONE( );
3895}
3896/* END_CASE */
3897
3898/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003899void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3900 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003901{
3902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3903 psa_key_type_t key_type = key_type_arg;
3904 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003905 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3906 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003907 unsigned char *output = NULL;
3908 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003909 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3911
3912 PSA_ASSERT( psa_crypto_init( ) );
3913
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003914 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003915 TEST_LE_U( ciphertext->len,
3916 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3917 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003918 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003919 TEST_LE_U( plaintext->len,
3920 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3921 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3922 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003923
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003924
3925 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003926 psa_set_key_usage_flags( &attributes,
3927 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003928 psa_set_key_algorithm( &attributes, alg );
3929 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003930 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3931 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003932 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3933 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003934 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003935
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003936 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003937 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3938 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3939 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003940 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3941 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003942 PSA_ERROR_BAD_STATE );
3943
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003944 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003945 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3946 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003947 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003948 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003949 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3950 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003951 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003952 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003953
Gilles Peskine286c3142022-04-20 17:09:38 +02003954 /* Multipart encryption */
3955 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3956 output_length = 0;
3957 length = ~0;
3958 PSA_ASSERT( psa_cipher_update( &operation,
3959 plaintext->x, plaintext->len,
3960 output, output_buffer_size,
3961 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003962 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003963 output_length += length;
3964 PSA_ASSERT( psa_cipher_finish( &operation,
3965 output + output_length,
3966 output_buffer_size - output_length,
3967 &length ) );
3968 output_length += length;
3969 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003970 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003971
Gilles Peskine286c3142022-04-20 17:09:38 +02003972 /* Multipart encryption */
3973 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3974 output_length = 0;
3975 length = ~0;
3976 PSA_ASSERT( psa_cipher_update( &operation,
3977 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003978 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003979 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003980 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003981 output_length += length;
3982 PSA_ASSERT( psa_cipher_finish( &operation,
3983 output + output_length,
3984 output_buffer_size - output_length,
3985 &length ) );
3986 output_length += length;
3987 ASSERT_COMPARE( plaintext->x, plaintext->len,
3988 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003989
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003990 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003991 output_length = ~0;
3992 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3993 output, output_buffer_size,
3994 &output_length ) );
3995 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3996 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003997
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003998 /* One-shot decryption */
3999 output_length = ~0;
4000 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4001 output, output_buffer_size,
4002 &output_length ) );
4003 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004004 output, output_length );
4005
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004006exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004007 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004008 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004009 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004010 psa_destroy_key( key );
4011 PSA_DONE( );
4012}
4013/* END_CASE */
4014
4015/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004016void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4017{
4018 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4019 psa_algorithm_t alg = alg_arg;
4020 psa_key_type_t key_type = key_type_arg;
4021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4022 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4023 psa_status_t status;
4024
4025 PSA_ASSERT( psa_crypto_init( ) );
4026
4027 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4028 psa_set_key_algorithm( &attributes, alg );
4029 psa_set_key_type( &attributes, key_type );
4030
4031 /* Usage of either of these two size macros would cause divide by zero
4032 * with incorrect key types previously. Input length should be irrelevant
4033 * here. */
4034 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4035 0 );
4036 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4037
4038
4039 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4040 &key ) );
4041
4042 /* Should fail due to invalid alg type (to support invalid key type).
4043 * Encrypt or decrypt will end up in the same place. */
4044 status = psa_cipher_encrypt_setup( &operation, key, alg );
4045
4046 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4047
4048exit:
4049 psa_cipher_abort( &operation );
4050 psa_destroy_key( key );
4051 PSA_DONE( );
4052}
4053/* END_CASE */
4054
4055/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004056void cipher_encrypt_validation( int alg_arg,
4057 int key_type_arg,
4058 data_t *key_data,
4059 data_t *input )
4060{
4061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4062 psa_key_type_t key_type = key_type_arg;
4063 psa_algorithm_t alg = alg_arg;
4064 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4065 unsigned char *output1 = NULL;
4066 size_t output1_buffer_size = 0;
4067 size_t output1_length = 0;
4068 unsigned char *output2 = NULL;
4069 size_t output2_buffer_size = 0;
4070 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004071 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004072 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004074
Gilles Peskine8817f612018-12-18 00:18:46 +01004075 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004077 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4078 psa_set_key_algorithm( &attributes, alg );
4079 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004080
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004081 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4082 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4083 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4084 ASSERT_ALLOC( output1, output1_buffer_size );
4085 ASSERT_ALLOC( output2, output2_buffer_size );
4086
Ronald Cron5425a212020-08-04 14:58:35 +02004087 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4088 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004089
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004090 /* The one-shot cipher encryption uses generated iv so validating
4091 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004092 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4093 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004094 TEST_LE_U( output1_length,
4095 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4096 TEST_LE_U( output1_length,
4097 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004098
4099 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4100 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004101
Gilles Peskine8817f612018-12-18 00:18:46 +01004102 PSA_ASSERT( psa_cipher_update( &operation,
4103 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004104 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004105 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004106 TEST_LE_U( function_output_length,
4107 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4108 TEST_LE_U( function_output_length,
4109 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004110 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004111
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004112 PSA_ASSERT( psa_cipher_finish( &operation,
4113 output2 + output2_length,
4114 output2_buffer_size - output2_length,
4115 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004116 TEST_LE_U( function_output_length,
4117 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4118 TEST_LE_U( function_output_length,
4119 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004120 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004121
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004122 PSA_ASSERT( psa_cipher_abort( &operation ) );
4123 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4124 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004125
Gilles Peskine50e586b2018-06-08 14:28:46 +02004126exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004127 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004128 mbedtls_free( output1 );
4129 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004130 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004131 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004132}
4133/* END_CASE */
4134
4135/* BEGIN_CASE */
4136void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004137 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004138 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004139 int first_part_size_arg,
4140 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004141 data_t *expected_output,
4142 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004143{
Ronald Cron5425a212020-08-04 14:58:35 +02004144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004145 psa_key_type_t key_type = key_type_arg;
4146 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004147 psa_status_t status;
4148 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004149 size_t first_part_size = first_part_size_arg;
4150 size_t output1_length = output1_length_arg;
4151 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004152 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004153 size_t output_buffer_size = 0;
4154 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004155 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004156 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158
Gilles Peskine8817f612018-12-18 00:18:46 +01004159 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004160
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004161 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4162 psa_set_key_algorithm( &attributes, alg );
4163 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004164
Ronald Cron5425a212020-08-04 14:58:35 +02004165 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4166 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004167
Ronald Cron5425a212020-08-04 14:58:35 +02004168 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004169
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004170 if( iv->len > 0 )
4171 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004172 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004173 }
4174
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004175 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4176 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004177 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004178
Gilles Peskine7be11a72022-04-14 00:12:57 +02004179 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004180 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4181 output, output_buffer_size,
4182 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004183 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004184 TEST_LE_U( function_output_length,
4185 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4186 TEST_LE_U( function_output_length,
4187 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004188 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004189
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004190 if( first_part_size < input->len )
4191 {
4192 PSA_ASSERT( psa_cipher_update( &operation,
4193 input->x + first_part_size,
4194 input->len - first_part_size,
4195 ( output_buffer_size == 0 ? NULL :
4196 output + total_output_length ),
4197 output_buffer_size - total_output_length,
4198 &function_output_length ) );
4199 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004200 TEST_LE_U( function_output_length,
4201 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4202 alg,
4203 input->len - first_part_size ) );
4204 TEST_LE_U( function_output_length,
4205 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004206 total_output_length += function_output_length;
4207 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004208
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004209 status = psa_cipher_finish( &operation,
4210 ( output_buffer_size == 0 ? NULL :
4211 output + total_output_length ),
4212 output_buffer_size - total_output_length,
4213 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004214 TEST_LE_U( function_output_length,
4215 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4216 TEST_LE_U( function_output_length,
4217 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004218 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004219 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004220
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004221 if( expected_status == PSA_SUCCESS )
4222 {
4223 PSA_ASSERT( psa_cipher_abort( &operation ) );
4224
4225 ASSERT_COMPARE( expected_output->x, expected_output->len,
4226 output, total_output_length );
4227 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004228
4229exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004230 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004231 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004232 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004233 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004234}
4235/* END_CASE */
4236
4237/* BEGIN_CASE */
4238void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004239 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004240 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004241 int first_part_size_arg,
4242 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004243 data_t *expected_output,
4244 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004245{
Ronald Cron5425a212020-08-04 14:58:35 +02004246 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004247 psa_key_type_t key_type = key_type_arg;
4248 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004249 psa_status_t status;
4250 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004251 size_t first_part_size = first_part_size_arg;
4252 size_t output1_length = output1_length_arg;
4253 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004254 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004255 size_t output_buffer_size = 0;
4256 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004257 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004258 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004260
Gilles Peskine8817f612018-12-18 00:18:46 +01004261 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004263 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4264 psa_set_key_algorithm( &attributes, alg );
4265 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004266
Ronald Cron5425a212020-08-04 14:58:35 +02004267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4268 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004269
Ronald Cron5425a212020-08-04 14:58:35 +02004270 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004271
Steven Cooreman177deba2020-09-07 17:14:14 +02004272 if( iv->len > 0 )
4273 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004274 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004275 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004276
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004277 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4278 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004279 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004280
Gilles Peskine7be11a72022-04-14 00:12:57 +02004281 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004282 PSA_ASSERT( psa_cipher_update( &operation,
4283 input->x, first_part_size,
4284 output, output_buffer_size,
4285 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004286 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004287 TEST_LE_U( function_output_length,
4288 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4289 TEST_LE_U( function_output_length,
4290 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004291 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004292
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004293 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004294 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004295 PSA_ASSERT( psa_cipher_update( &operation,
4296 input->x + first_part_size,
4297 input->len - first_part_size,
4298 ( output_buffer_size == 0 ? NULL :
4299 output + total_output_length ),
4300 output_buffer_size - total_output_length,
4301 &function_output_length ) );
4302 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004303 TEST_LE_U( function_output_length,
4304 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4305 alg,
4306 input->len - first_part_size ) );
4307 TEST_LE_U( function_output_length,
4308 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004309 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004310 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004311
Gilles Peskine50e586b2018-06-08 14:28:46 +02004312 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004313 ( output_buffer_size == 0 ? NULL :
4314 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004315 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004317 TEST_LE_U( function_output_length,
4318 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4319 TEST_LE_U( function_output_length,
4320 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004321 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004322 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004323
4324 if( expected_status == PSA_SUCCESS )
4325 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004326 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004327
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004328 ASSERT_COMPARE( expected_output->x, expected_output->len,
4329 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004330 }
4331
Gilles Peskine50e586b2018-06-08 14:28:46 +02004332exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004333 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004334 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004335 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004336 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004337}
4338/* END_CASE */
4339
Gilles Peskine50e586b2018-06-08 14:28:46 +02004340/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004341void cipher_decrypt_fail( int alg_arg,
4342 int key_type_arg,
4343 data_t *key_data,
4344 data_t *iv,
4345 data_t *input_arg,
4346 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004347{
4348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4349 psa_status_t status;
4350 psa_key_type_t key_type = key_type_arg;
4351 psa_algorithm_t alg = alg_arg;
4352 psa_status_t expected_status = expected_status_arg;
4353 unsigned char *input = NULL;
4354 size_t input_buffer_size = 0;
4355 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004356 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004357 size_t output_buffer_size = 0;
4358 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004359 size_t function_output_length;
4360 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4362
4363 if ( PSA_ERROR_BAD_STATE != expected_status )
4364 {
4365 PSA_ASSERT( psa_crypto_init( ) );
4366
4367 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4368 psa_set_key_algorithm( &attributes, alg );
4369 psa_set_key_type( &attributes, key_type );
4370
4371 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4372 &key ) );
4373 }
4374
4375 /* Allocate input buffer and copy the iv and the plaintext */
4376 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4377 if ( input_buffer_size > 0 )
4378 {
4379 ASSERT_ALLOC( input, input_buffer_size );
4380 memcpy( input, iv->x, iv->len );
4381 memcpy( input + iv->len, input_arg->x, input_arg->len );
4382 }
4383
4384 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4385 ASSERT_ALLOC( output, output_buffer_size );
4386
Neil Armstrong66a479f2022-02-07 15:41:19 +01004387 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004388 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4389 output_buffer_size, &output_length );
4390 TEST_EQUAL( status, expected_status );
4391
Neil Armstrong66a479f2022-02-07 15:41:19 +01004392 /* Decrypt, multi-part */
4393 status = psa_cipher_decrypt_setup( &operation, key, alg );
4394 if( status == PSA_SUCCESS )
4395 {
4396 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4397 input_arg->len ) +
4398 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4399 ASSERT_ALLOC( output_multi, output_buffer_size );
4400
4401 if( iv->len > 0 )
4402 {
4403 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4404
4405 if( status != PSA_SUCCESS )
4406 TEST_EQUAL( status, expected_status );
4407 }
4408
4409 if( status == PSA_SUCCESS )
4410 {
4411 status = psa_cipher_update( &operation,
4412 input_arg->x, input_arg->len,
4413 output_multi, output_buffer_size,
4414 &function_output_length );
4415 if( status == PSA_SUCCESS )
4416 {
4417 output_length = function_output_length;
4418
4419 status = psa_cipher_finish( &operation,
4420 output_multi + output_length,
4421 output_buffer_size - output_length,
4422 &function_output_length );
4423
4424 TEST_EQUAL( status, expected_status );
4425 }
4426 else
4427 {
4428 TEST_EQUAL( status, expected_status );
4429 }
4430 }
4431 else
4432 {
4433 TEST_EQUAL( status, expected_status );
4434 }
4435 }
4436 else
4437 {
4438 TEST_EQUAL( status, expected_status );
4439 }
4440
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004441exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004442 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004443 mbedtls_free( input );
4444 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004445 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446 psa_destroy_key( key );
4447 PSA_DONE( );
4448}
4449/* END_CASE */
4450
4451/* BEGIN_CASE */
4452void cipher_decrypt( int alg_arg,
4453 int key_type_arg,
4454 data_t *key_data,
4455 data_t *iv,
4456 data_t *input_arg,
4457 data_t *expected_output )
4458{
4459 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4460 psa_key_type_t key_type = key_type_arg;
4461 psa_algorithm_t alg = alg_arg;
4462 unsigned char *input = NULL;
4463 size_t input_buffer_size = 0;
4464 unsigned char *output = NULL;
4465 size_t output_buffer_size = 0;
4466 size_t output_length = 0;
4467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4468
4469 PSA_ASSERT( psa_crypto_init( ) );
4470
4471 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4472 psa_set_key_algorithm( &attributes, alg );
4473 psa_set_key_type( &attributes, key_type );
4474
4475 /* Allocate input buffer and copy the iv and the plaintext */
4476 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4477 if ( input_buffer_size > 0 )
4478 {
4479 ASSERT_ALLOC( input, input_buffer_size );
4480 memcpy( input, iv->x, iv->len );
4481 memcpy( input + iv->len, input_arg->x, input_arg->len );
4482 }
4483
4484 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4485 ASSERT_ALLOC( output, output_buffer_size );
4486
4487 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4488 &key ) );
4489
4490 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4491 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004492 TEST_LE_U( output_length,
4493 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4494 TEST_LE_U( output_length,
4495 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004496
4497 ASSERT_COMPARE( expected_output->x, expected_output->len,
4498 output, output_length );
4499exit:
4500 mbedtls_free( input );
4501 mbedtls_free( output );
4502 psa_destroy_key( key );
4503 PSA_DONE( );
4504}
4505/* END_CASE */
4506
4507/* BEGIN_CASE */
4508void cipher_verify_output( int alg_arg,
4509 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004510 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004511 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004512{
Ronald Cron5425a212020-08-04 14:58:35 +02004513 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004514 psa_key_type_t key_type = key_type_arg;
4515 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004516 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004517 size_t output1_size = 0;
4518 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004519 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004520 size_t output2_size = 0;
4521 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004522 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004523
Gilles Peskine8817f612018-12-18 00:18:46 +01004524 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004525
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004526 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4527 psa_set_key_algorithm( &attributes, alg );
4528 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004529
Ronald Cron5425a212020-08-04 14:58:35 +02004530 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4531 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004532 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004533 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004534
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004535 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4536 output1, output1_size,
4537 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004538 TEST_LE_U( output1_length,
4539 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4540 TEST_LE_U( output1_length,
4541 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004542
4543 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004544 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004545
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004546 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4547 output2, output2_size,
4548 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004549 TEST_LE_U( output2_length,
4550 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4551 TEST_LE_U( output2_length,
4552 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004553
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004554 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004555
4556exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004557 mbedtls_free( output1 );
4558 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004559 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004560 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004561}
4562/* END_CASE */
4563
4564/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004565void cipher_verify_output_multipart( int alg_arg,
4566 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004567 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004568 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004569 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004570{
Ronald Cron5425a212020-08-04 14:58:35 +02004571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004572 psa_key_type_t key_type = key_type_arg;
4573 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004574 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004575 unsigned char iv[16] = {0};
4576 size_t iv_size = 16;
4577 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004578 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004579 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004580 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004581 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004582 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004583 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004584 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004585 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4586 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004588
Gilles Peskine8817f612018-12-18 00:18:46 +01004589 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004590
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004591 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4592 psa_set_key_algorithm( &attributes, alg );
4593 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004594
Ronald Cron5425a212020-08-04 14:58:35 +02004595 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4596 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004597
Ronald Cron5425a212020-08-04 14:58:35 +02004598 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4599 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004600
Steven Cooreman177deba2020-09-07 17:14:14 +02004601 if( alg != PSA_ALG_ECB_NO_PADDING )
4602 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004603 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4604 iv, iv_size,
4605 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004606 }
4607
gabor-mezei-armceface22021-01-21 12:26:17 +01004608 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004609 TEST_LE_U( output1_buffer_size,
4610 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004611 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004612
Gilles Peskine7be11a72022-04-14 00:12:57 +02004613 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004614
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4616 output1, output1_buffer_size,
4617 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004618 TEST_LE_U( function_output_length,
4619 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4620 TEST_LE_U( function_output_length,
4621 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004622 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004623
Gilles Peskine8817f612018-12-18 00:18:46 +01004624 PSA_ASSERT( psa_cipher_update( &operation1,
4625 input->x + first_part_size,
4626 input->len - first_part_size,
4627 output1, output1_buffer_size,
4628 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004629 TEST_LE_U( function_output_length,
4630 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4631 alg,
4632 input->len - first_part_size ) );
4633 TEST_LE_U( function_output_length,
4634 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004635 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004636
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 PSA_ASSERT( psa_cipher_finish( &operation1,
4638 output1 + output1_length,
4639 output1_buffer_size - output1_length,
4640 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004641 TEST_LE_U( function_output_length,
4642 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4643 TEST_LE_U( function_output_length,
4644 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004645 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004646
Gilles Peskine8817f612018-12-18 00:18:46 +01004647 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004648
Gilles Peskine048b7f02018-06-08 14:20:49 +02004649 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004650 TEST_LE_U( output2_buffer_size,
4651 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4652 TEST_LE_U( output2_buffer_size,
4653 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004654 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004655
Steven Cooreman177deba2020-09-07 17:14:14 +02004656 if( iv_length > 0 )
4657 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004658 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4659 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004660 }
Moran Pekerded84402018-06-06 16:36:50 +03004661
Gilles Peskine8817f612018-12-18 00:18:46 +01004662 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4663 output2, output2_buffer_size,
4664 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004665 TEST_LE_U( function_output_length,
4666 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4667 TEST_LE_U( function_output_length,
4668 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004669 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004670
Gilles Peskine8817f612018-12-18 00:18:46 +01004671 PSA_ASSERT( psa_cipher_update( &operation2,
4672 output1 + first_part_size,
4673 output1_length - first_part_size,
4674 output2, output2_buffer_size,
4675 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004676 TEST_LE_U( function_output_length,
4677 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4678 alg,
4679 output1_length - first_part_size ) );
4680 TEST_LE_U( function_output_length,
4681 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004682 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004683
Gilles Peskine8817f612018-12-18 00:18:46 +01004684 PSA_ASSERT( psa_cipher_finish( &operation2,
4685 output2 + output2_length,
4686 output2_buffer_size - output2_length,
4687 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004688 TEST_LE_U( function_output_length,
4689 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4690 TEST_LE_U( function_output_length,
4691 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004692 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004693
Gilles Peskine8817f612018-12-18 00:18:46 +01004694 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004695
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004696 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004697
4698exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004699 psa_cipher_abort( &operation1 );
4700 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004701 mbedtls_free( output1 );
4702 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004703 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004704 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004705}
4706/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004707
Gilles Peskine20035e32018-02-03 22:44:14 +01004708/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004709void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004710 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004711 data_t *nonce,
4712 data_t *additional_data,
4713 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004714 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004715{
Ronald Cron5425a212020-08-04 14:58:35 +02004716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004717 psa_key_type_t key_type = key_type_arg;
4718 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004719 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004720 unsigned char *output_data = NULL;
4721 size_t output_size = 0;
4722 size_t output_length = 0;
4723 unsigned char *output_data2 = NULL;
4724 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004725 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004726 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004728
Gilles Peskine8817f612018-12-18 00:18:46 +01004729 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004730
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004731 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4732 psa_set_key_algorithm( &attributes, alg );
4733 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004734
Gilles Peskine049c7532019-05-15 20:22:09 +02004735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004736 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004737 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4738 key_bits = psa_get_key_bits( &attributes );
4739
4740 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4741 alg );
4742 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4743 * should be exact. */
4744 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4745 expected_result != PSA_ERROR_NOT_SUPPORTED )
4746 {
4747 TEST_EQUAL( output_size,
4748 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004749 TEST_LE_U( output_size,
4750 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004751 }
4752 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004753
Steven Cooremanf49478b2021-02-15 15:19:25 +01004754 status = psa_aead_encrypt( key, alg,
4755 nonce->x, nonce->len,
4756 additional_data->x,
4757 additional_data->len,
4758 input_data->x, input_data->len,
4759 output_data, output_size,
4760 &output_length );
4761
4762 /* If the operation is not supported, just skip and not fail in case the
4763 * encryption involves a common limitation of cryptography hardwares and
4764 * an alternative implementation. */
4765 if( status == PSA_ERROR_NOT_SUPPORTED )
4766 {
4767 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4768 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4769 }
4770
4771 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004772
4773 if( PSA_SUCCESS == expected_result )
4774 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004775 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004776
Gilles Peskine003a4a92019-05-14 16:09:40 +02004777 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4778 * should be exact. */
4779 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004780 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004781
Gilles Peskine7be11a72022-04-14 00:12:57 +02004782 TEST_LE_U( input_data->len,
4783 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004784
Ronald Cron5425a212020-08-04 14:58:35 +02004785 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004786 nonce->x, nonce->len,
4787 additional_data->x,
4788 additional_data->len,
4789 output_data, output_length,
4790 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004791 &output_length2 ),
4792 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004793
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004794 ASSERT_COMPARE( input_data->x, input_data->len,
4795 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004796 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004797
Gilles Peskinea1cac842018-06-11 19:33:02 +02004798exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004799 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800 mbedtls_free( output_data );
4801 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004802 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004803}
4804/* END_CASE */
4805
4806/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004807void aead_encrypt( int key_type_arg, data_t *key_data,
4808 int alg_arg,
4809 data_t *nonce,
4810 data_t *additional_data,
4811 data_t *input_data,
4812 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004813{
Ronald Cron5425a212020-08-04 14:58:35 +02004814 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004815 psa_key_type_t key_type = key_type_arg;
4816 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004817 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004818 unsigned char *output_data = NULL;
4819 size_t output_size = 0;
4820 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004822 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004823
Gilles Peskine8817f612018-12-18 00:18:46 +01004824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4827 psa_set_key_algorithm( &attributes, alg );
4828 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004829
Gilles Peskine049c7532019-05-15 20:22:09 +02004830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004831 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004832 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4833 key_bits = psa_get_key_bits( &attributes );
4834
4835 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4836 alg );
4837 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4838 * should be exact. */
4839 TEST_EQUAL( output_size,
4840 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004841 TEST_LE_U( output_size,
4842 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004843 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004844
Steven Cooremand588ea12021-01-11 19:36:04 +01004845 status = psa_aead_encrypt( key, alg,
4846 nonce->x, nonce->len,
4847 additional_data->x, additional_data->len,
4848 input_data->x, input_data->len,
4849 output_data, output_size,
4850 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004851
Ronald Cron28a45ed2021-02-09 20:35:42 +01004852 /* If the operation is not supported, just skip and not fail in case the
4853 * encryption involves a common limitation of cryptography hardwares and
4854 * an alternative implementation. */
4855 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004856 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004857 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4858 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004859 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004860
4861 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004862 ASSERT_COMPARE( expected_result->x, expected_result->len,
4863 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004864
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004866 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004868 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869}
4870/* END_CASE */
4871
4872/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004873void aead_decrypt( int key_type_arg, data_t *key_data,
4874 int alg_arg,
4875 data_t *nonce,
4876 data_t *additional_data,
4877 data_t *input_data,
4878 data_t *expected_data,
4879 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004880{
Ronald Cron5425a212020-08-04 14:58:35 +02004881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004882 psa_key_type_t key_type = key_type_arg;
4883 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004884 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004885 unsigned char *output_data = NULL;
4886 size_t output_size = 0;
4887 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004889 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004890 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004891
Gilles Peskine8817f612018-12-18 00:18:46 +01004892 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004893
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004894 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4895 psa_set_key_algorithm( &attributes, alg );
4896 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004897
Gilles Peskine049c7532019-05-15 20:22:09 +02004898 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004899 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004900 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4901 key_bits = psa_get_key_bits( &attributes );
4902
4903 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4904 alg );
4905 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4906 expected_result != PSA_ERROR_NOT_SUPPORTED )
4907 {
4908 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4909 * should be exact. */
4910 TEST_EQUAL( output_size,
4911 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004912 TEST_LE_U( output_size,
4913 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004914 }
4915 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004916
Steven Cooremand588ea12021-01-11 19:36:04 +01004917 status = psa_aead_decrypt( key, alg,
4918 nonce->x, nonce->len,
4919 additional_data->x,
4920 additional_data->len,
4921 input_data->x, input_data->len,
4922 output_data, output_size,
4923 &output_length );
4924
Ronald Cron28a45ed2021-02-09 20:35:42 +01004925 /* If the operation is not supported, just skip and not fail in case the
4926 * decryption involves a common limitation of cryptography hardwares and
4927 * an alternative implementation. */
4928 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004929 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004930 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4931 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004932 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004933
4934 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004935
Gilles Peskine2d277862018-06-18 15:41:12 +02004936 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004937 ASSERT_COMPARE( expected_data->x, expected_data->len,
4938 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004939
Gilles Peskinea1cac842018-06-11 19:33:02 +02004940exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004941 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004942 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004943 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004944}
4945/* END_CASE */
4946
4947/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004948void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4949 int alg_arg,
4950 data_t *nonce,
4951 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004952 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004953 int do_set_lengths,
4954 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004955{
Paul Elliottd3f82412021-06-16 16:52:21 +01004956 size_t ad_part_len = 0;
4957 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004958 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004959
Paul Elliott32f46ba2021-09-23 18:24:36 +01004960 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004961 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004962 mbedtls_test_set_step( ad_part_len );
4963
4964 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004965 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004966 if( ad_part_len & 0x01 )
4967 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4968 else
4969 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004970 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004971
4972 /* Split ad into length(ad_part_len) parts. */
4973 if( !aead_multipart_internal_func( key_type_arg, key_data,
4974 alg_arg, nonce,
4975 additional_data,
4976 ad_part_len,
4977 input_data, -1,
4978 set_lengths_method,
4979 expected_output,
4980 1, 0 ) )
4981 break;
4982
4983 /* length(0) part, length(ad_part_len) part, length(0) part... */
4984 mbedtls_test_set_step( 1000 + ad_part_len );
4985
4986 if( !aead_multipart_internal_func( key_type_arg, key_data,
4987 alg_arg, nonce,
4988 additional_data,
4989 ad_part_len,
4990 input_data, -1,
4991 set_lengths_method,
4992 expected_output,
4993 1, 1 ) )
4994 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004995 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004996
Paul Elliott32f46ba2021-09-23 18:24:36 +01004997 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004998 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999 /* Split data into length(data_part_len) parts. */
5000 mbedtls_test_set_step( 2000 + data_part_len );
5001
5002 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005003 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005004 if( data_part_len & 0x01 )
5005 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5006 else
5007 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005008 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005009
Paul Elliott32f46ba2021-09-23 18:24:36 +01005010 if( !aead_multipart_internal_func( key_type_arg, key_data,
5011 alg_arg, nonce,
5012 additional_data, -1,
5013 input_data, data_part_len,
5014 set_lengths_method,
5015 expected_output,
5016 1, 0 ) )
5017 break;
5018
5019 /* length(0) part, length(data_part_len) part, length(0) part... */
5020 mbedtls_test_set_step( 3000 + data_part_len );
5021
5022 if( !aead_multipart_internal_func( key_type_arg, key_data,
5023 alg_arg, nonce,
5024 additional_data, -1,
5025 input_data, data_part_len,
5026 set_lengths_method,
5027 expected_output,
5028 1, 1 ) )
5029 break;
5030 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005031
Paul Elliott8fc45162021-06-23 16:06:01 +01005032 /* Goto is required to silence warnings about unused labels, as we
5033 * don't actually do any test assertions in this function. */
5034 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005035}
5036/* END_CASE */
5037
5038/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005039void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5040 int alg_arg,
5041 data_t *nonce,
5042 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005043 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005044 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005045 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005046{
Paul Elliottd3f82412021-06-16 16:52:21 +01005047 size_t ad_part_len = 0;
5048 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005049 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005050
Paul Elliott32f46ba2021-09-23 18:24:36 +01005051 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005052 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005053 /* Split ad into length(ad_part_len) parts. */
5054 mbedtls_test_set_step( ad_part_len );
5055
5056 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005057 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005058 if( ad_part_len & 0x01 )
5059 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5060 else
5061 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005062 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063
5064 if( !aead_multipart_internal_func( key_type_arg, key_data,
5065 alg_arg, nonce,
5066 additional_data,
5067 ad_part_len,
5068 input_data, -1,
5069 set_lengths_method,
5070 expected_output,
5071 0, 0 ) )
5072 break;
5073
5074 /* length(0) part, length(ad_part_len) part, length(0) part... */
5075 mbedtls_test_set_step( 1000 + ad_part_len );
5076
5077 if( !aead_multipart_internal_func( key_type_arg, key_data,
5078 alg_arg, nonce,
5079 additional_data,
5080 ad_part_len,
5081 input_data, -1,
5082 set_lengths_method,
5083 expected_output,
5084 0, 1 ) )
5085 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005086 }
5087
Paul Elliott32f46ba2021-09-23 18:24:36 +01005088 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005089 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005090 /* Split data into length(data_part_len) parts. */
5091 mbedtls_test_set_step( 2000 + data_part_len );
5092
5093 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005094 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005095 if( data_part_len & 0x01 )
5096 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5097 else
5098 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005099 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005100
5101 if( !aead_multipart_internal_func( key_type_arg, key_data,
5102 alg_arg, nonce,
5103 additional_data, -1,
5104 input_data, data_part_len,
5105 set_lengths_method,
5106 expected_output,
5107 0, 0 ) )
5108 break;
5109
5110 /* length(0) part, length(data_part_len) part, length(0) part... */
5111 mbedtls_test_set_step( 3000 + data_part_len );
5112
5113 if( !aead_multipart_internal_func( key_type_arg, key_data,
5114 alg_arg, nonce,
5115 additional_data, -1,
5116 input_data, data_part_len,
5117 set_lengths_method,
5118 expected_output,
5119 0, 1 ) )
5120 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005121 }
5122
Paul Elliott8fc45162021-06-23 16:06:01 +01005123 /* Goto is required to silence warnings about unused labels, as we
5124 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005125 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005126}
5127/* END_CASE */
5128
5129/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005130void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5131 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005132 int nonce_length,
5133 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005134 data_t *additional_data,
5135 data_t *input_data,
5136 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005137{
5138
5139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5140 psa_key_type_t key_type = key_type_arg;
5141 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005142 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005143 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5145 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005146 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005147 size_t actual_nonce_length = 0;
5148 size_t expected_nonce_length = expected_nonce_length_arg;
5149 unsigned char *output = NULL;
5150 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005151 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005152 size_t ciphertext_size = 0;
5153 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005154 size_t tag_length = 0;
5155 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005156
5157 PSA_ASSERT( psa_crypto_init( ) );
5158
5159 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5160 psa_set_key_algorithm( & attributes, alg );
5161 psa_set_key_type( & attributes, key_type );
5162
5163 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5164 &key ) );
5165
5166 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5167
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005168 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5169
Paul Elliottf1277632021-08-24 18:11:37 +01005170 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005171
Paul Elliottf1277632021-08-24 18:11:37 +01005172 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005173
Gilles Peskine7be11a72022-04-14 00:12:57 +02005174 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005175
Paul Elliottf1277632021-08-24 18:11:37 +01005176 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005177
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005178 status = psa_aead_encrypt_setup( &operation, key, alg );
5179
5180 /* If the operation is not supported, just skip and not fail in case the
5181 * encryption involves a common limitation of cryptography hardwares and
5182 * an alternative implementation. */
5183 if( status == PSA_ERROR_NOT_SUPPORTED )
5184 {
5185 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005186 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005187 }
5188
5189 PSA_ASSERT( status );
5190
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005191 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005192 nonce_length,
5193 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005194
Paul Elliott693bf312021-07-23 17:40:41 +01005195 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005196
Paul Elliottf1277632021-08-24 18:11:37 +01005197 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005198
Paul Elliott88ecbe12021-09-22 17:23:03 +01005199 if( expected_status == PSA_SUCCESS )
5200 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5201 alg ) );
5202
Gilles Peskine7be11a72022-04-14 00:12:57 +02005203 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005204
Paul Elliott693bf312021-07-23 17:40:41 +01005205 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005206 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005207 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005208 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5209 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005210
5211 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5212 additional_data->len ) );
5213
5214 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005215 output, output_size,
5216 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005217
Paul Elliottf1277632021-08-24 18:11:37 +01005218 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5219 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005220 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5221 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005222
5223exit:
5224 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005225 mbedtls_free( output );
5226 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005227 psa_aead_abort( &operation );
5228 PSA_DONE( );
5229}
5230/* END_CASE */
5231
5232/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005233void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5234 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005235 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005236 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005237 data_t *additional_data,
5238 data_t *input_data,
5239 int expected_status_arg )
5240{
5241
5242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5243 psa_key_type_t key_type = key_type_arg;
5244 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005245 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005246 uint8_t *nonce_buffer = NULL;
5247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5248 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5249 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005250 unsigned char *output = NULL;
5251 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005252 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005253 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005254 size_t ciphertext_size = 0;
5255 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005256 size_t tag_length = 0;
5257 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005258 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005259 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005260
5261 PSA_ASSERT( psa_crypto_init( ) );
5262
5263 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5264 psa_set_key_algorithm( &attributes, alg );
5265 psa_set_key_type( &attributes, key_type );
5266
5267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5268 &key ) );
5269
5270 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5271
5272 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5273
Paul Elliott6f0e7202021-08-25 12:57:18 +01005274 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005275
Paul Elliott6f0e7202021-08-25 12:57:18 +01005276 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005277
Gilles Peskine7be11a72022-04-14 00:12:57 +02005278 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005279
Paul Elliott6f0e7202021-08-25 12:57:18 +01005280 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005281
Paul Elliott863864a2021-07-23 17:28:31 +01005282 status = psa_aead_encrypt_setup( &operation, key, alg );
5283
5284 /* If the operation is not supported, just skip and not fail in case the
5285 * encryption involves a common limitation of cryptography hardwares and
5286 * an alternative implementation. */
5287 if( status == PSA_ERROR_NOT_SUPPORTED )
5288 {
5289 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005290 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005291 }
5292
5293 PSA_ASSERT( status );
5294
Paul Elliott4023ffd2021-09-10 16:21:22 +01005295 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5296 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005297 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005298 /* Arbitrary size buffer, to test zero length valid buffer. */
5299 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005300 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005301 }
5302 else
5303 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005304 /* If length is zero, then this will return NULL. */
5305 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005306 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005307
Paul Elliott4023ffd2021-09-10 16:21:22 +01005308 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005309 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005310 for( index = 0; index < nonce_length - 1; ++index )
5311 {
5312 nonce_buffer[index] = 'a' + index;
5313 }
Paul Elliott66696b52021-08-16 18:42:41 +01005314 }
Paul Elliott863864a2021-07-23 17:28:31 +01005315 }
5316
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005317 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5318 {
5319 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5320 input_data->len ) );
5321 }
5322
Paul Elliott6f0e7202021-08-25 12:57:18 +01005323 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005324
Paul Elliott693bf312021-07-23 17:40:41 +01005325 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005326
5327 if( expected_status == PSA_SUCCESS )
5328 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005329 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5330 {
5331 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5332 input_data->len ) );
5333 }
5334 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5335 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005336
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005337 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5338 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5339 additional_data->len ),
5340 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005341
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005342 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005343 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005344 &ciphertext_length ),
5345 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005346
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005347 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005348 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005349 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5350 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005351 }
5352
5353exit:
5354 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005355 mbedtls_free( output );
5356 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005357 mbedtls_free( nonce_buffer );
5358 psa_aead_abort( &operation );
5359 PSA_DONE( );
5360}
5361/* END_CASE */
5362
5363/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005364void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5365 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005366 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005367 data_t *nonce,
5368 data_t *additional_data,
5369 data_t *input_data,
5370 int expected_status_arg )
5371{
5372
5373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5374 psa_key_type_t key_type = key_type_arg;
5375 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005376 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5378 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5379 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005380 unsigned char *output = NULL;
5381 unsigned char *ciphertext = NULL;
5382 size_t output_size = output_size_arg;
5383 size_t ciphertext_size = 0;
5384 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005385 size_t tag_length = 0;
5386 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5387
5388 PSA_ASSERT( psa_crypto_init( ) );
5389
5390 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5391 psa_set_key_algorithm( &attributes, alg );
5392 psa_set_key_type( &attributes, key_type );
5393
5394 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5395 &key ) );
5396
5397 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5398
Paul Elliottc6d11d02021-09-01 12:04:23 +01005399 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005400
Paul Elliottc6d11d02021-09-01 12:04:23 +01005401 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005402
Paul Elliottc6d11d02021-09-01 12:04:23 +01005403 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005404
Paul Elliott43fbda62021-07-23 18:30:59 +01005405 status = psa_aead_encrypt_setup( &operation, key, alg );
5406
5407 /* If the operation is not supported, just skip and not fail in case the
5408 * encryption involves a common limitation of cryptography hardwares and
5409 * an alternative implementation. */
5410 if( status == PSA_ERROR_NOT_SUPPORTED )
5411 {
5412 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5413 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5414 }
5415
5416 PSA_ASSERT( status );
5417
Paul Elliott47b9a142021-10-07 15:04:57 +01005418 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5419 input_data->len ) );
5420
Paul Elliott43fbda62021-07-23 18:30:59 +01005421 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5422
5423 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5424 additional_data->len ) );
5425
5426 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005427 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005428
5429 TEST_EQUAL( status, expected_status );
5430
5431 if( expected_status == PSA_SUCCESS )
5432 {
5433 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005434 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5435 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005436 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5437 }
5438
5439exit:
5440 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005441 mbedtls_free( output );
5442 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005443 psa_aead_abort( &operation );
5444 PSA_DONE( );
5445}
5446/* END_CASE */
5447
Paul Elliott91b021e2021-07-23 18:52:31 +01005448/* BEGIN_CASE */
5449void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5450 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005451 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005452 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005453 data_t *nonce,
5454 data_t *additional_data,
5455 data_t *input_data,
5456 int expected_status_arg )
5457{
5458
5459 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5460 psa_key_type_t key_type = key_type_arg;
5461 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005462 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5464 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5465 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005466 unsigned char *ciphertext = NULL;
5467 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005468 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005469 size_t ciphertext_size = 0;
5470 size_t ciphertext_length = 0;
5471 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005472 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005473 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005474
5475 PSA_ASSERT( psa_crypto_init( ) );
5476
5477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5478 psa_set_key_algorithm( &attributes, alg );
5479 psa_set_key_type( &attributes, key_type );
5480
5481 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5482 &key ) );
5483
5484 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5485
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005486 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005487
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005488 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005489
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005490 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005491
Paul Elliott719c1322021-09-13 18:27:22 +01005492 ASSERT_ALLOC( tag_buffer, tag_size );
5493
Paul Elliott91b021e2021-07-23 18:52:31 +01005494 status = psa_aead_encrypt_setup( &operation, key, alg );
5495
5496 /* If the operation is not supported, just skip and not fail in case the
5497 * encryption involves a common limitation of cryptography hardwares and
5498 * an alternative implementation. */
5499 if( status == PSA_ERROR_NOT_SUPPORTED )
5500 {
5501 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5502 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5503 }
5504
5505 PSA_ASSERT( status );
5506
5507 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5508
Paul Elliott76bda482021-10-07 17:07:23 +01005509 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5510 input_data->len ) );
5511
Paul Elliott91b021e2021-07-23 18:52:31 +01005512 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5513 additional_data->len ) );
5514
5515 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005516 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005517
5518 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005519 status = psa_aead_finish( &operation, finish_ciphertext,
5520 finish_ciphertext_size,
5521 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005522 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005523
5524 TEST_EQUAL( status, expected_status );
5525
5526exit:
5527 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005528 mbedtls_free( ciphertext );
5529 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005530 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005531 psa_aead_abort( &operation );
5532 PSA_DONE( );
5533}
5534/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005535
5536/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005537void aead_multipart_verify( int key_type_arg, data_t *key_data,
5538 int alg_arg,
5539 data_t *nonce,
5540 data_t *additional_data,
5541 data_t *input_data,
5542 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005543 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005544 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005545 int expected_status_arg )
5546{
5547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5548 psa_key_type_t key_type = key_type_arg;
5549 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005550 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5552 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5553 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005554 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005555 unsigned char *plaintext = NULL;
5556 unsigned char *finish_plaintext = NULL;
5557 size_t plaintext_size = 0;
5558 size_t plaintext_length = 0;
5559 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005560 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005561 unsigned char *tag_buffer = NULL;
5562 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005563
5564 PSA_ASSERT( psa_crypto_init( ) );
5565
5566 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5567 psa_set_key_algorithm( &attributes, alg );
5568 psa_set_key_type( &attributes, key_type );
5569
5570 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5571 &key ) );
5572
5573 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5574
5575 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5576 input_data->len );
5577
5578 ASSERT_ALLOC( plaintext, plaintext_size );
5579
5580 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5581
5582 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5583
Paul Elliott9961a662021-09-17 19:19:02 +01005584 status = psa_aead_decrypt_setup( &operation, key, alg );
5585
5586 /* If the operation is not supported, just skip and not fail in case the
5587 * encryption involves a common limitation of cryptography hardwares and
5588 * an alternative implementation. */
5589 if( status == PSA_ERROR_NOT_SUPPORTED )
5590 {
5591 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5592 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5593 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005594 TEST_EQUAL( status, expected_setup_status );
5595
5596 if( status != PSA_SUCCESS )
5597 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005598
5599 PSA_ASSERT( status );
5600
5601 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5602
Paul Elliottfec6f372021-10-06 17:15:02 +01005603 status = psa_aead_set_lengths( &operation, additional_data->len,
5604 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005605 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005606
Paul Elliott9961a662021-09-17 19:19:02 +01005607 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5608 additional_data->len ) );
5609
5610 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5611 input_data->len,
5612 plaintext, plaintext_size,
5613 &plaintext_length ) );
5614
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005615 if( tag_usage == USE_GIVEN_TAG )
5616 {
5617 tag_buffer = tag->x;
5618 tag_size = tag->len;
5619 }
5620
Paul Elliott9961a662021-09-17 19:19:02 +01005621 status = psa_aead_verify( &operation, finish_plaintext,
5622 verify_plaintext_size,
5623 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005624 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005625
5626 TEST_EQUAL( status, expected_status );
5627
5628exit:
5629 psa_destroy_key( key );
5630 mbedtls_free( plaintext );
5631 mbedtls_free( finish_plaintext );
5632 psa_aead_abort( &operation );
5633 PSA_DONE( );
5634}
5635/* END_CASE */
5636
Paul Elliott9961a662021-09-17 19:19:02 +01005637/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005638void aead_multipart_setup( int key_type_arg, data_t *key_data,
5639 int alg_arg, int expected_status_arg )
5640{
5641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5642 psa_key_type_t key_type = key_type_arg;
5643 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005644 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5646 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5647 psa_status_t expected_status = expected_status_arg;
5648
5649 PSA_ASSERT( psa_crypto_init( ) );
5650
5651 psa_set_key_usage_flags( &attributes,
5652 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5653 psa_set_key_algorithm( &attributes, alg );
5654 psa_set_key_type( &attributes, key_type );
5655
5656 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5657 &key ) );
5658
Paul Elliott5221ef62021-09-19 17:33:03 +01005659 status = psa_aead_encrypt_setup( &operation, key, alg );
5660
5661 TEST_EQUAL( status, expected_status );
5662
5663 psa_aead_abort( &operation );
5664
Paul Elliott5221ef62021-09-19 17:33:03 +01005665 status = psa_aead_decrypt_setup( &operation, key, alg );
5666
5667 TEST_EQUAL(status, expected_status );
5668
5669exit:
5670 psa_destroy_key( key );
5671 psa_aead_abort( &operation );
5672 PSA_DONE( );
5673}
5674/* END_CASE */
5675
5676/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005677void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5678 int alg_arg,
5679 data_t *nonce,
5680 data_t *additional_data,
5681 data_t *input_data )
5682{
5683 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5684 psa_key_type_t key_type = key_type_arg;
5685 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005686 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687 unsigned char *output_data = NULL;
5688 unsigned char *final_data = NULL;
5689 size_t output_size = 0;
5690 size_t finish_output_size = 0;
5691 size_t output_length = 0;
5692 size_t key_bits = 0;
5693 size_t tag_length = 0;
5694 size_t tag_size = 0;
5695 size_t nonce_length = 0;
5696 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5697 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5698 size_t output_part_length = 0;
5699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5700
5701 PSA_ASSERT( psa_crypto_init( ) );
5702
5703 psa_set_key_usage_flags( & attributes,
5704 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5705 psa_set_key_algorithm( & attributes, alg );
5706 psa_set_key_type( & attributes, key_type );
5707
5708 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5709 &key ) );
5710
5711 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5712 key_bits = psa_get_key_bits( &attributes );
5713
5714 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5715
Gilles Peskine7be11a72022-04-14 00:12:57 +02005716 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
5718 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5719
5720 ASSERT_ALLOC( output_data, output_size );
5721
5722 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5723
Gilles Peskine7be11a72022-04-14 00:12:57 +02005724 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005725
5726 ASSERT_ALLOC( final_data, finish_output_size );
5727
5728 /* Test all operations error without calling setup first. */
5729
Paul Elliottc23a9a02021-06-21 18:32:46 +01005730 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5731 PSA_ERROR_BAD_STATE );
5732
5733 psa_aead_abort( &operation );
5734
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5736 PSA_AEAD_NONCE_MAX_SIZE,
5737 &nonce_length ),
5738 PSA_ERROR_BAD_STATE );
5739
5740 psa_aead_abort( &operation );
5741
Paul Elliott481be342021-07-16 17:38:47 +01005742 /* ------------------------------------------------------- */
5743
Paul Elliottc23a9a02021-06-21 18:32:46 +01005744 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5745 input_data->len ),
5746 PSA_ERROR_BAD_STATE );
5747
5748 psa_aead_abort( &operation );
5749
Paul Elliott481be342021-07-16 17:38:47 +01005750 /* ------------------------------------------------------- */
5751
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5753 additional_data->len ),
5754 PSA_ERROR_BAD_STATE );
5755
5756 psa_aead_abort( &operation );
5757
Paul Elliott481be342021-07-16 17:38:47 +01005758 /* ------------------------------------------------------- */
5759
Paul Elliottc23a9a02021-06-21 18:32:46 +01005760 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5761 input_data->len, output_data,
5762 output_size, &output_length ),
5763 PSA_ERROR_BAD_STATE );
5764
5765 psa_aead_abort( &operation );
5766
Paul Elliott481be342021-07-16 17:38:47 +01005767 /* ------------------------------------------------------- */
5768
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5770 finish_output_size,
5771 &output_part_length,
5772 tag_buffer, tag_length,
5773 &tag_size ),
5774 PSA_ERROR_BAD_STATE );
5775
5776 psa_aead_abort( &operation );
5777
Paul Elliott481be342021-07-16 17:38:47 +01005778 /* ------------------------------------------------------- */
5779
Paul Elliottc23a9a02021-06-21 18:32:46 +01005780 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5781 finish_output_size,
5782 &output_part_length,
5783 tag_buffer,
5784 tag_length ),
5785 PSA_ERROR_BAD_STATE );
5786
5787 psa_aead_abort( &operation );
5788
5789 /* Test for double setups. */
5790
Paul Elliottc23a9a02021-06-21 18:32:46 +01005791 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5792
5793 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5794 PSA_ERROR_BAD_STATE );
5795
5796 psa_aead_abort( &operation );
5797
Paul Elliott481be342021-07-16 17:38:47 +01005798 /* ------------------------------------------------------- */
5799
Paul Elliottc23a9a02021-06-21 18:32:46 +01005800 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5801
5802 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5803 PSA_ERROR_BAD_STATE );
5804
5805 psa_aead_abort( &operation );
5806
Paul Elliott374a2be2021-07-16 17:53:40 +01005807 /* ------------------------------------------------------- */
5808
Paul Elliott374a2be2021-07-16 17:53:40 +01005809 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5810
5811 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5812 PSA_ERROR_BAD_STATE );
5813
5814 psa_aead_abort( &operation );
5815
5816 /* ------------------------------------------------------- */
5817
Paul Elliott374a2be2021-07-16 17:53:40 +01005818 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5819
5820 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5821 PSA_ERROR_BAD_STATE );
5822
5823 psa_aead_abort( &operation );
5824
Paul Elliottc23a9a02021-06-21 18:32:46 +01005825 /* Test for not setting a nonce. */
5826
Paul Elliottc23a9a02021-06-21 18:32:46 +01005827 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5828
5829 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5830 additional_data->len ),
5831 PSA_ERROR_BAD_STATE );
5832
5833 psa_aead_abort( &operation );
5834
Paul Elliott7f628422021-09-01 12:08:29 +01005835 /* ------------------------------------------------------- */
5836
Paul Elliott7f628422021-09-01 12:08:29 +01005837 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5838
5839 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5840 input_data->len, output_data,
5841 output_size, &output_length ),
5842 PSA_ERROR_BAD_STATE );
5843
5844 psa_aead_abort( &operation );
5845
Paul Elliottbdc2c682021-09-21 18:37:10 +01005846 /* ------------------------------------------------------- */
5847
Paul Elliottbdc2c682021-09-21 18:37:10 +01005848 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5849
5850 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5851 finish_output_size,
5852 &output_part_length,
5853 tag_buffer, tag_length,
5854 &tag_size ),
5855 PSA_ERROR_BAD_STATE );
5856
5857 psa_aead_abort( &operation );
5858
5859 /* ------------------------------------------------------- */
5860
Paul Elliottbdc2c682021-09-21 18:37:10 +01005861 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5862
5863 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5864 finish_output_size,
5865 &output_part_length,
5866 tag_buffer,
5867 tag_length ),
5868 PSA_ERROR_BAD_STATE );
5869
5870 psa_aead_abort( &operation );
5871
Paul Elliottc23a9a02021-06-21 18:32:46 +01005872 /* Test for double setting nonce. */
5873
Paul Elliottc23a9a02021-06-21 18:32:46 +01005874 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5875
5876 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5877
5878 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5879 PSA_ERROR_BAD_STATE );
5880
5881 psa_aead_abort( &operation );
5882
Paul Elliott374a2be2021-07-16 17:53:40 +01005883 /* Test for double generating nonce. */
5884
Paul Elliott374a2be2021-07-16 17:53:40 +01005885 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5886
5887 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5888 PSA_AEAD_NONCE_MAX_SIZE,
5889 &nonce_length ) );
5890
5891 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5892 PSA_AEAD_NONCE_MAX_SIZE,
5893 &nonce_length ),
5894 PSA_ERROR_BAD_STATE );
5895
5896
5897 psa_aead_abort( &operation );
5898
5899 /* Test for generate nonce then set and vice versa */
5900
Paul Elliott374a2be2021-07-16 17:53:40 +01005901 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5902
5903 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5904 PSA_AEAD_NONCE_MAX_SIZE,
5905 &nonce_length ) );
5906
5907 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5908 PSA_ERROR_BAD_STATE );
5909
5910 psa_aead_abort( &operation );
5911
Andrzej Kurekad837522021-12-15 15:28:49 +01005912 /* Test for generating nonce after calling set lengths */
5913
5914 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5915
5916 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5917 input_data->len ) );
5918
5919 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5920 PSA_AEAD_NONCE_MAX_SIZE,
5921 &nonce_length ) );
5922
5923 psa_aead_abort( &operation );
5924
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005925 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005926
5927 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5928
5929 if( operation.alg == PSA_ALG_CCM )
5930 {
5931 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5932 input_data->len ),
5933 PSA_ERROR_INVALID_ARGUMENT );
5934 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5935 PSA_AEAD_NONCE_MAX_SIZE,
5936 &nonce_length ),
5937 PSA_ERROR_BAD_STATE );
5938 }
5939 else
5940 {
5941 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5942 input_data->len ) );
5943 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5944 PSA_AEAD_NONCE_MAX_SIZE,
5945 &nonce_length ) );
5946 }
5947
5948 psa_aead_abort( &operation );
5949
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005950 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005951#if SIZE_MAX > UINT32_MAX
5952 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5953
5954 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5955 {
5956 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5957 input_data->len ),
5958 PSA_ERROR_INVALID_ARGUMENT );
5959 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5960 PSA_AEAD_NONCE_MAX_SIZE,
5961 &nonce_length ),
5962 PSA_ERROR_BAD_STATE );
5963 }
5964 else
5965 {
5966 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5967 input_data->len ) );
5968 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5969 PSA_AEAD_NONCE_MAX_SIZE,
5970 &nonce_length ) );
5971 }
5972
5973 psa_aead_abort( &operation );
5974#endif
5975
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005976 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005977
5978 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5979
5980 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5981 PSA_AEAD_NONCE_MAX_SIZE,
5982 &nonce_length ) );
5983
5984 if( operation.alg == PSA_ALG_CCM )
5985 {
5986 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5987 input_data->len ),
5988 PSA_ERROR_INVALID_ARGUMENT );
5989 }
5990 else
5991 {
5992 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5993 input_data->len ) );
5994 }
5995
5996 psa_aead_abort( &operation );
5997
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005998 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005999 /* Test for setting nonce after calling set lengths */
6000
6001 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6002
6003 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6004 input_data->len ) );
6005
6006 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6007
6008 psa_aead_abort( &operation );
6009
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006010 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006011
6012 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6013
6014 if( operation.alg == PSA_ALG_CCM )
6015 {
6016 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6017 input_data->len ),
6018 PSA_ERROR_INVALID_ARGUMENT );
6019 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6020 PSA_ERROR_BAD_STATE );
6021 }
6022 else
6023 {
6024 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6025 input_data->len ) );
6026 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6027 }
6028
6029 psa_aead_abort( &operation );
6030
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006031 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006032#if SIZE_MAX > UINT32_MAX
6033 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6034
6035 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6036 {
6037 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6038 input_data->len ),
6039 PSA_ERROR_INVALID_ARGUMENT );
6040 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6041 PSA_ERROR_BAD_STATE );
6042 }
6043 else
6044 {
6045 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6046 input_data->len ) );
6047 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6048 }
6049
6050 psa_aead_abort( &operation );
6051#endif
6052
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006053 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006054
6055 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6056
6057 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6058
6059 if( operation.alg == PSA_ALG_CCM )
6060 {
6061 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6062 input_data->len ),
6063 PSA_ERROR_INVALID_ARGUMENT );
6064 }
6065 else
6066 {
6067 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6068 input_data->len ) );
6069 }
6070
6071 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006072
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006073 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006074#if SIZE_MAX > UINT32_MAX
6075 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6076
6077 if( operation.alg == PSA_ALG_GCM )
6078 {
6079 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6080 SIZE_MAX ),
6081 PSA_ERROR_INVALID_ARGUMENT );
6082 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6083 PSA_ERROR_BAD_STATE );
6084 }
6085 else if ( operation.alg != PSA_ALG_CCM )
6086 {
6087 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6088 SIZE_MAX ) );
6089 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6090 }
6091
6092 psa_aead_abort( &operation );
6093
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006094 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006095 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6096
6097 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6098
6099 if( operation.alg == PSA_ALG_GCM )
6100 {
6101 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6102 SIZE_MAX ),
6103 PSA_ERROR_INVALID_ARGUMENT );
6104 }
6105 else if ( operation.alg != PSA_ALG_CCM )
6106 {
6107 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6108 SIZE_MAX ) );
6109 }
6110
6111 psa_aead_abort( &operation );
6112#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006113
6114 /* ------------------------------------------------------- */
6115
Paul Elliott374a2be2021-07-16 17:53:40 +01006116 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6117
6118 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6119
6120 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6121 PSA_AEAD_NONCE_MAX_SIZE,
6122 &nonce_length ),
6123 PSA_ERROR_BAD_STATE );
6124
6125 psa_aead_abort( &operation );
6126
Paul Elliott7220cae2021-06-22 17:25:57 +01006127 /* Test for generating nonce in decrypt setup. */
6128
Paul Elliott7220cae2021-06-22 17:25:57 +01006129 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6130
6131 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6132 PSA_AEAD_NONCE_MAX_SIZE,
6133 &nonce_length ),
6134 PSA_ERROR_BAD_STATE );
6135
6136 psa_aead_abort( &operation );
6137
Paul Elliottc23a9a02021-06-21 18:32:46 +01006138 /* Test for setting lengths twice. */
6139
Paul Elliottc23a9a02021-06-21 18:32:46 +01006140 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6141
6142 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6143
6144 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6145 input_data->len ) );
6146
6147 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6148 input_data->len ),
6149 PSA_ERROR_BAD_STATE );
6150
6151 psa_aead_abort( &operation );
6152
Andrzej Kurekad837522021-12-15 15:28:49 +01006153 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006154
Paul Elliottc23a9a02021-06-21 18:32:46 +01006155 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6156
6157 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6158
Andrzej Kurekad837522021-12-15 15:28:49 +01006159 if( operation.alg == PSA_ALG_CCM )
6160 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006161
Andrzej Kurekad837522021-12-15 15:28:49 +01006162 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6163 additional_data->len ),
6164 PSA_ERROR_BAD_STATE );
6165 }
6166 else
6167 {
6168 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6169 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006170
Andrzej Kurekad837522021-12-15 15:28:49 +01006171 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6172 input_data->len ),
6173 PSA_ERROR_BAD_STATE );
6174 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006175 psa_aead_abort( &operation );
6176
6177 /* ------------------------------------------------------- */
6178
Paul Elliottf94bd992021-09-19 18:15:59 +01006179 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6180
6181 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6182
Andrzej Kurekad837522021-12-15 15:28:49 +01006183 if( operation.alg == PSA_ALG_CCM )
6184 {
6185 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6186 input_data->len, output_data,
6187 output_size, &output_length ),
6188 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006189
Andrzej Kurekad837522021-12-15 15:28:49 +01006190 }
6191 else
6192 {
6193 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6194 input_data->len, output_data,
6195 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006196
Andrzej Kurekad837522021-12-15 15:28:49 +01006197 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6198 input_data->len ),
6199 PSA_ERROR_BAD_STATE );
6200 }
6201 psa_aead_abort( &operation );
6202
6203 /* ------------------------------------------------------- */
6204
6205 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6206
6207 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6208
6209 if( operation.alg == PSA_ALG_CCM )
6210 {
6211 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6212 finish_output_size,
6213 &output_part_length,
6214 tag_buffer, tag_length,
6215 &tag_size ) );
6216 }
6217 else
6218 {
6219 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6220 finish_output_size,
6221 &output_part_length,
6222 tag_buffer, tag_length,
6223 &tag_size ) );
6224
6225 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6226 input_data->len ),
6227 PSA_ERROR_BAD_STATE );
6228 }
6229 psa_aead_abort( &operation );
6230
6231 /* Test for setting lengths after generating nonce + already starting data. */
6232
6233 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6234
6235 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6236 PSA_AEAD_NONCE_MAX_SIZE,
6237 &nonce_length ) );
6238 if( operation.alg == PSA_ALG_CCM )
6239 {
6240
6241 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6242 additional_data->len ),
6243 PSA_ERROR_BAD_STATE );
6244 }
6245 else
6246 {
6247 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6248 additional_data->len ) );
6249
6250 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6251 input_data->len ),
6252 PSA_ERROR_BAD_STATE );
6253 }
6254 psa_aead_abort( &operation );
6255
6256 /* ------------------------------------------------------- */
6257
6258 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6259
6260 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6261 PSA_AEAD_NONCE_MAX_SIZE,
6262 &nonce_length ) );
6263 if( operation.alg == PSA_ALG_CCM )
6264 {
6265 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6266 input_data->len, output_data,
6267 output_size, &output_length ),
6268 PSA_ERROR_BAD_STATE );
6269
6270 }
6271 else
6272 {
6273 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6274 input_data->len, output_data,
6275 output_size, &output_length ) );
6276
6277 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6278 input_data->len ),
6279 PSA_ERROR_BAD_STATE );
6280 }
6281 psa_aead_abort( &operation );
6282
6283 /* ------------------------------------------------------- */
6284
6285 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6286
6287 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6288 PSA_AEAD_NONCE_MAX_SIZE,
6289 &nonce_length ) );
6290 if( operation.alg == PSA_ALG_CCM )
6291 {
6292 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6293 finish_output_size,
6294 &output_part_length,
6295 tag_buffer, tag_length,
6296 &tag_size ) );
6297 }
6298 else
6299 {
6300 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6301 finish_output_size,
6302 &output_part_length,
6303 tag_buffer, tag_length,
6304 &tag_size ) );
6305
6306 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6307 input_data->len ),
6308 PSA_ERROR_BAD_STATE );
6309 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006310 psa_aead_abort( &operation );
6311
Paul Elliott243080c2021-07-21 19:01:17 +01006312 /* Test for not sending any additional data or data after setting non zero
6313 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006314
Paul Elliottc23a9a02021-06-21 18:32:46 +01006315 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6316
6317 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6318
6319 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6320 input_data->len ) );
6321
6322 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6323 finish_output_size,
6324 &output_part_length,
6325 tag_buffer, tag_length,
6326 &tag_size ),
6327 PSA_ERROR_INVALID_ARGUMENT );
6328
6329 psa_aead_abort( &operation );
6330
Paul Elliott243080c2021-07-21 19:01:17 +01006331 /* Test for not sending any additional data or data after setting non-zero
6332 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006333
Paul Elliottc23a9a02021-06-21 18:32:46 +01006334 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6335
6336 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6337
6338 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6339 input_data->len ) );
6340
6341 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6342 finish_output_size,
6343 &output_part_length,
6344 tag_buffer,
6345 tag_length ),
6346 PSA_ERROR_INVALID_ARGUMENT );
6347
6348 psa_aead_abort( &operation );
6349
Paul Elliott243080c2021-07-21 19:01:17 +01006350 /* Test for not sending any additional data after setting a non-zero length
6351 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006352
Paul Elliottc23a9a02021-06-21 18:32:46 +01006353 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6354
6355 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6356
6357 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6358 input_data->len ) );
6359
6360 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6361 input_data->len, output_data,
6362 output_size, &output_length ),
6363 PSA_ERROR_INVALID_ARGUMENT );
6364
6365 psa_aead_abort( &operation );
6366
Paul Elliottf94bd992021-09-19 18:15:59 +01006367 /* Test for not sending any data after setting a non-zero length for it.*/
6368
Paul Elliottf94bd992021-09-19 18:15:59 +01006369 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6370
6371 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6372
6373 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6374 input_data->len ) );
6375
6376 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6377 additional_data->len ) );
6378
6379 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6380 finish_output_size,
6381 &output_part_length,
6382 tag_buffer, tag_length,
6383 &tag_size ),
6384 PSA_ERROR_INVALID_ARGUMENT );
6385
6386 psa_aead_abort( &operation );
6387
Paul Elliottb0450fe2021-09-01 15:06:26 +01006388 /* Test for sending too much additional data after setting lengths. */
6389
Paul Elliottb0450fe2021-09-01 15:06:26 +01006390 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6391
6392 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6393
6394 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6395
6396
6397 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6398 additional_data->len ),
6399 PSA_ERROR_INVALID_ARGUMENT );
6400
6401 psa_aead_abort( &operation );
6402
Paul Elliotta2a09b02021-09-22 14:56:40 +01006403 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006404
6405 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6406
6407 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6408
6409 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6410 input_data->len ) );
6411
6412 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6413 additional_data->len ) );
6414
6415 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6416 1 ),
6417 PSA_ERROR_INVALID_ARGUMENT );
6418
6419 psa_aead_abort( &operation );
6420
Paul Elliottb0450fe2021-09-01 15:06:26 +01006421 /* Test for sending too much data after setting lengths. */
6422
Paul Elliottb0450fe2021-09-01 15:06:26 +01006423 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6424
6425 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6426
6427 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6428
6429 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6430 input_data->len, output_data,
6431 output_size, &output_length ),
6432 PSA_ERROR_INVALID_ARGUMENT );
6433
6434 psa_aead_abort( &operation );
6435
Paul Elliotta2a09b02021-09-22 14:56:40 +01006436 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006437
6438 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6439
6440 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6441
6442 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6443 input_data->len ) );
6444
6445 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6446 additional_data->len ) );
6447
6448 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6449 input_data->len, output_data,
6450 output_size, &output_length ) );
6451
6452 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6453 1, output_data,
6454 output_size, &output_length ),
6455 PSA_ERROR_INVALID_ARGUMENT );
6456
6457 psa_aead_abort( &operation );
6458
Paul Elliottc23a9a02021-06-21 18:32:46 +01006459 /* Test sending additional data after data. */
6460
Paul Elliottc23a9a02021-06-21 18:32:46 +01006461 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6462
6463 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6464
Andrzej Kurekad837522021-12-15 15:28:49 +01006465 if( operation.alg != PSA_ALG_CCM )
6466 {
6467 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6468 input_data->len, output_data,
6469 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006470
Andrzej Kurekad837522021-12-15 15:28:49 +01006471 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6472 additional_data->len ),
6473 PSA_ERROR_BAD_STATE );
6474 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006475 psa_aead_abort( &operation );
6476
Paul Elliott534d0b42021-06-22 19:15:20 +01006477 /* Test calling finish on decryption. */
6478
Paul Elliott534d0b42021-06-22 19:15:20 +01006479 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6480
6481 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6482
6483 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6484 finish_output_size,
6485 &output_part_length,
6486 tag_buffer, tag_length,
6487 &tag_size ),
6488 PSA_ERROR_BAD_STATE );
6489
6490 psa_aead_abort( &operation );
6491
6492 /* Test calling verify on encryption. */
6493
Paul Elliott534d0b42021-06-22 19:15:20 +01006494 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6495
6496 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6497
6498 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6499 finish_output_size,
6500 &output_part_length,
6501 tag_buffer,
6502 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006503 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006504
6505 psa_aead_abort( &operation );
6506
6507
Paul Elliottc23a9a02021-06-21 18:32:46 +01006508exit:
6509 psa_destroy_key( key );
6510 psa_aead_abort( &operation );
6511 mbedtls_free( output_data );
6512 mbedtls_free( final_data );
6513 PSA_DONE( );
6514}
6515/* END_CASE */
6516
6517/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006518void signature_size( int type_arg,
6519 int bits,
6520 int alg_arg,
6521 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006522{
6523 psa_key_type_t type = type_arg;
6524 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006525 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006526
Gilles Peskinefe11b722018-12-18 00:24:04 +01006527 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006528
Gilles Peskinee59236f2018-01-27 23:32:46 +01006529exit:
6530 ;
6531}
6532/* END_CASE */
6533
6534/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006535void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6536 int alg_arg, data_t *input_data,
6537 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006538{
Ronald Cron5425a212020-08-04 14:58:35 +02006539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006540 psa_key_type_t key_type = key_type_arg;
6541 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006542 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006543 unsigned char *signature = NULL;
6544 size_t signature_size;
6545 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006547
Gilles Peskine8817f612018-12-18 00:18:46 +01006548 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006549
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006550 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006551 psa_set_key_algorithm( &attributes, alg );
6552 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006553
Gilles Peskine049c7532019-05-15 20:22:09 +02006554 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006555 &key ) );
6556 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006557 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006558
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006559 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006560 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006561 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006562 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006563 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006564 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006565 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006566
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006567 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006568 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006569 input_data->x, input_data->len,
6570 signature, signature_size,
6571 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006572 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006573 ASSERT_COMPARE( output_data->x, output_data->len,
6574 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006575
6576exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006577 /*
6578 * Key attributes may have been returned by psa_get_key_attributes()
6579 * thus reset them as required.
6580 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006581 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006582
Ronald Cron5425a212020-08-04 14:58:35 +02006583 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006584 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006585 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006586}
6587/* END_CASE */
6588
6589/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006590void sign_hash_fail( int key_type_arg, data_t *key_data,
6591 int alg_arg, data_t *input_data,
6592 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006593{
Ronald Cron5425a212020-08-04 14:58:35 +02006594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006595 psa_key_type_t key_type = key_type_arg;
6596 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006597 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006598 psa_status_t actual_status;
6599 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006600 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006601 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006603
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006604 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006605
Gilles Peskine8817f612018-12-18 00:18:46 +01006606 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006607
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006608 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006609 psa_set_key_algorithm( &attributes, alg );
6610 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006611
Gilles Peskine049c7532019-05-15 20:22:09 +02006612 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006613 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006614
Ronald Cron5425a212020-08-04 14:58:35 +02006615 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006616 input_data->x, input_data->len,
6617 signature, signature_size,
6618 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006619 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006620 /* The value of *signature_length is unspecified on error, but
6621 * whatever it is, it should be less than signature_size, so that
6622 * if the caller tries to read *signature_length bytes without
6623 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006624 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006625
6626exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006627 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006628 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006629 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006630 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006631}
6632/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006633
6634/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006635void sign_verify_hash( int key_type_arg, data_t *key_data,
6636 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006637{
Ronald Cron5425a212020-08-04 14:58:35 +02006638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006639 psa_key_type_t key_type = key_type_arg;
6640 psa_algorithm_t alg = alg_arg;
6641 size_t key_bits;
6642 unsigned char *signature = NULL;
6643 size_t signature_size;
6644 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006646
Gilles Peskine8817f612018-12-18 00:18:46 +01006647 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006648
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006649 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006650 psa_set_key_algorithm( &attributes, alg );
6651 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006652
Gilles Peskine049c7532019-05-15 20:22:09 +02006653 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006654 &key ) );
6655 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006656 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006657
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006658 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006659 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006660 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006661 key_bits, alg );
6662 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006663 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006664 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006665
6666 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006667 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006668 input_data->x, input_data->len,
6669 signature, signature_size,
6670 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006671 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006672 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006673 TEST_ASSERT( signature_length > 0 );
6674
6675 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006676 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006677 input_data->x, input_data->len,
6678 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006679
6680 if( input_data->len != 0 )
6681 {
6682 /* Flip a bit in the input and verify that the signature is now
6683 * detected as invalid. Flip a bit at the beginning, not at the end,
6684 * because ECDSA may ignore the last few bits of the input. */
6685 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006686 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006687 input_data->x, input_data->len,
6688 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006689 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006690 }
6691
6692exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006693 /*
6694 * Key attributes may have been returned by psa_get_key_attributes()
6695 * thus reset them as required.
6696 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006697 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006698
Ronald Cron5425a212020-08-04 14:58:35 +02006699 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006700 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006701 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006702}
6703/* END_CASE */
6704
6705/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006706void verify_hash( int key_type_arg, data_t *key_data,
6707 int alg_arg, data_t *hash_data,
6708 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006709{
Ronald Cron5425a212020-08-04 14:58:35 +02006710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006711 psa_key_type_t key_type = key_type_arg;
6712 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006713 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006714
Gilles Peskine7be11a72022-04-14 00:12:57 +02006715 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006716
Gilles Peskine8817f612018-12-18 00:18:46 +01006717 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006718
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006719 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006720 psa_set_key_algorithm( &attributes, alg );
6721 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006722
Gilles Peskine049c7532019-05-15 20:22:09 +02006723 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006724 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006725
Ronald Cron5425a212020-08-04 14:58:35 +02006726 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006727 hash_data->x, hash_data->len,
6728 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006729
itayzafrir5c753392018-05-08 11:18:38 +03006730exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006731 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006732 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006733 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006734}
6735/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006736
6737/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006738void verify_hash_fail( int key_type_arg, data_t *key_data,
6739 int alg_arg, data_t *hash_data,
6740 data_t *signature_data,
6741 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006742{
Ronald Cron5425a212020-08-04 14:58:35 +02006743 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006744 psa_key_type_t key_type = key_type_arg;
6745 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006746 psa_status_t actual_status;
6747 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006749
Gilles Peskine8817f612018-12-18 00:18:46 +01006750 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006751
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006753 psa_set_key_algorithm( &attributes, alg );
6754 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006755
Gilles Peskine049c7532019-05-15 20:22:09 +02006756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006757 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006758
Ronald Cron5425a212020-08-04 14:58:35 +02006759 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006760 hash_data->x, hash_data->len,
6761 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006762 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006763
6764exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006765 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006766 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006767 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006768}
6769/* END_CASE */
6770
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006771/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006772void sign_message_deterministic( int key_type_arg,
6773 data_t *key_data,
6774 int alg_arg,
6775 data_t *input_data,
6776 data_t *output_data )
6777{
6778 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6779 psa_key_type_t key_type = key_type_arg;
6780 psa_algorithm_t alg = alg_arg;
6781 size_t key_bits;
6782 unsigned char *signature = NULL;
6783 size_t signature_size;
6784 size_t signature_length = 0xdeadbeef;
6785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6786
6787 PSA_ASSERT( psa_crypto_init( ) );
6788
6789 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6790 psa_set_key_algorithm( &attributes, alg );
6791 psa_set_key_type( &attributes, key_type );
6792
6793 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6794 &key ) );
6795 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6796 key_bits = psa_get_key_bits( &attributes );
6797
6798 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6799 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006800 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006801 ASSERT_ALLOC( signature, signature_size );
6802
6803 PSA_ASSERT( psa_sign_message( key, alg,
6804 input_data->x, input_data->len,
6805 signature, signature_size,
6806 &signature_length ) );
6807
6808 ASSERT_COMPARE( output_data->x, output_data->len,
6809 signature, signature_length );
6810
6811exit:
6812 psa_reset_key_attributes( &attributes );
6813
6814 psa_destroy_key( key );
6815 mbedtls_free( signature );
6816 PSA_DONE( );
6817
6818}
6819/* END_CASE */
6820
6821/* BEGIN_CASE */
6822void sign_message_fail( int key_type_arg,
6823 data_t *key_data,
6824 int alg_arg,
6825 data_t *input_data,
6826 int signature_size_arg,
6827 int expected_status_arg )
6828{
6829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6830 psa_key_type_t key_type = key_type_arg;
6831 psa_algorithm_t alg = alg_arg;
6832 size_t signature_size = signature_size_arg;
6833 psa_status_t actual_status;
6834 psa_status_t expected_status = expected_status_arg;
6835 unsigned char *signature = NULL;
6836 size_t signature_length = 0xdeadbeef;
6837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6838
6839 ASSERT_ALLOC( signature, signature_size );
6840
6841 PSA_ASSERT( psa_crypto_init( ) );
6842
6843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6844 psa_set_key_algorithm( &attributes, alg );
6845 psa_set_key_type( &attributes, key_type );
6846
6847 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6848 &key ) );
6849
6850 actual_status = psa_sign_message( key, alg,
6851 input_data->x, input_data->len,
6852 signature, signature_size,
6853 &signature_length );
6854 TEST_EQUAL( actual_status, expected_status );
6855 /* The value of *signature_length is unspecified on error, but
6856 * whatever it is, it should be less than signature_size, so that
6857 * if the caller tries to read *signature_length bytes without
6858 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006859 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006860
6861exit:
6862 psa_reset_key_attributes( &attributes );
6863 psa_destroy_key( key );
6864 mbedtls_free( signature );
6865 PSA_DONE( );
6866}
6867/* END_CASE */
6868
6869/* BEGIN_CASE */
6870void sign_verify_message( int key_type_arg,
6871 data_t *key_data,
6872 int alg_arg,
6873 data_t *input_data )
6874{
6875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6876 psa_key_type_t key_type = key_type_arg;
6877 psa_algorithm_t alg = alg_arg;
6878 size_t key_bits;
6879 unsigned char *signature = NULL;
6880 size_t signature_size;
6881 size_t signature_length = 0xdeadbeef;
6882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6883
6884 PSA_ASSERT( psa_crypto_init( ) );
6885
6886 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6887 PSA_KEY_USAGE_VERIFY_MESSAGE );
6888 psa_set_key_algorithm( &attributes, alg );
6889 psa_set_key_type( &attributes, key_type );
6890
6891 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6892 &key ) );
6893 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6894 key_bits = psa_get_key_bits( &attributes );
6895
6896 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6897 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006898 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006899 ASSERT_ALLOC( signature, signature_size );
6900
6901 PSA_ASSERT( psa_sign_message( key, alg,
6902 input_data->x, input_data->len,
6903 signature, signature_size,
6904 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006905 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006906 TEST_ASSERT( signature_length > 0 );
6907
6908 PSA_ASSERT( psa_verify_message( key, alg,
6909 input_data->x, input_data->len,
6910 signature, signature_length ) );
6911
6912 if( input_data->len != 0 )
6913 {
6914 /* Flip a bit in the input and verify that the signature is now
6915 * detected as invalid. Flip a bit at the beginning, not at the end,
6916 * because ECDSA may ignore the last few bits of the input. */
6917 input_data->x[0] ^= 1;
6918 TEST_EQUAL( psa_verify_message( key, alg,
6919 input_data->x, input_data->len,
6920 signature, signature_length ),
6921 PSA_ERROR_INVALID_SIGNATURE );
6922 }
6923
6924exit:
6925 psa_reset_key_attributes( &attributes );
6926
6927 psa_destroy_key( key );
6928 mbedtls_free( signature );
6929 PSA_DONE( );
6930}
6931/* END_CASE */
6932
6933/* BEGIN_CASE */
6934void verify_message( int key_type_arg,
6935 data_t *key_data,
6936 int alg_arg,
6937 data_t *input_data,
6938 data_t *signature_data )
6939{
6940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6941 psa_key_type_t key_type = key_type_arg;
6942 psa_algorithm_t alg = alg_arg;
6943 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6944
Gilles Peskine7be11a72022-04-14 00:12:57 +02006945 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006946
6947 PSA_ASSERT( psa_crypto_init( ) );
6948
6949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6950 psa_set_key_algorithm( &attributes, alg );
6951 psa_set_key_type( &attributes, key_type );
6952
6953 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6954 &key ) );
6955
6956 PSA_ASSERT( psa_verify_message( key, alg,
6957 input_data->x, input_data->len,
6958 signature_data->x, signature_data->len ) );
6959
6960exit:
6961 psa_reset_key_attributes( &attributes );
6962 psa_destroy_key( key );
6963 PSA_DONE( );
6964}
6965/* END_CASE */
6966
6967/* BEGIN_CASE */
6968void verify_message_fail( int key_type_arg,
6969 data_t *key_data,
6970 int alg_arg,
6971 data_t *hash_data,
6972 data_t *signature_data,
6973 int expected_status_arg )
6974{
6975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6976 psa_key_type_t key_type = key_type_arg;
6977 psa_algorithm_t alg = alg_arg;
6978 psa_status_t actual_status;
6979 psa_status_t expected_status = expected_status_arg;
6980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6981
6982 PSA_ASSERT( psa_crypto_init( ) );
6983
6984 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6985 psa_set_key_algorithm( &attributes, alg );
6986 psa_set_key_type( &attributes, key_type );
6987
6988 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6989 &key ) );
6990
6991 actual_status = psa_verify_message( key, alg,
6992 hash_data->x, hash_data->len,
6993 signature_data->x,
6994 signature_data->len );
6995 TEST_EQUAL( actual_status, expected_status );
6996
6997exit:
6998 psa_reset_key_attributes( &attributes );
6999 psa_destroy_key( key );
7000 PSA_DONE( );
7001}
7002/* END_CASE */
7003
7004/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007005void asymmetric_encrypt( int key_type_arg,
7006 data_t *key_data,
7007 int alg_arg,
7008 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007009 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007010 int expected_output_length_arg,
7011 int expected_status_arg )
7012{
Ronald Cron5425a212020-08-04 14:58:35 +02007013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007014 psa_key_type_t key_type = key_type_arg;
7015 psa_algorithm_t alg = alg_arg;
7016 size_t expected_output_length = expected_output_length_arg;
7017 size_t key_bits;
7018 unsigned char *output = NULL;
7019 size_t output_size;
7020 size_t output_length = ~0;
7021 psa_status_t actual_status;
7022 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007024
Gilles Peskine8817f612018-12-18 00:18:46 +01007025 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007026
Gilles Peskine656896e2018-06-29 19:12:28 +02007027 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007028 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7029 psa_set_key_algorithm( &attributes, alg );
7030 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007031 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007032 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007033
7034 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007035 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007036 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007037
Gilles Peskine656896e2018-06-29 19:12:28 +02007038 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007039 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007040 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007041
7042 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007043 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007044 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007045 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007046 output, output_size,
7047 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007048 TEST_EQUAL( actual_status, expected_status );
7049 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007050
Gilles Peskine68428122018-06-30 18:42:41 +02007051 /* If the label is empty, the test framework puts a non-null pointer
7052 * in label->x. Test that a null pointer works as well. */
7053 if( label->len == 0 )
7054 {
7055 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007056 if( output_size != 0 )
7057 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007058 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007059 input_data->x, input_data->len,
7060 NULL, label->len,
7061 output, output_size,
7062 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007063 TEST_EQUAL( actual_status, expected_status );
7064 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007065 }
7066
Gilles Peskine656896e2018-06-29 19:12:28 +02007067exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007068 /*
7069 * Key attributes may have been returned by psa_get_key_attributes()
7070 * thus reset them as required.
7071 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007072 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007073
Ronald Cron5425a212020-08-04 14:58:35 +02007074 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007075 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007076 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007077}
7078/* END_CASE */
7079
7080/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007081void asymmetric_encrypt_decrypt( int key_type_arg,
7082 data_t *key_data,
7083 int alg_arg,
7084 data_t *input_data,
7085 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007086{
Ronald Cron5425a212020-08-04 14:58:35 +02007087 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007088 psa_key_type_t key_type = key_type_arg;
7089 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007090 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007091 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007092 size_t output_size;
7093 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007094 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007095 size_t output2_size;
7096 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007098
Gilles Peskine8817f612018-12-18 00:18:46 +01007099 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007100
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7102 psa_set_key_algorithm( &attributes, alg );
7103 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007104
Gilles Peskine049c7532019-05-15 20:22:09 +02007105 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007106 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007107
7108 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007109 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007110 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007111
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007112 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007113 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007114 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007115
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007116 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007117 TEST_LE_U( output2_size,
7118 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7119 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007120 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007121
Gilles Peskineeebd7382018-06-08 18:11:54 +02007122 /* We test encryption by checking that encrypt-then-decrypt gives back
7123 * the original plaintext because of the non-optional random
7124 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007125 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007126 input_data->x, input_data->len,
7127 label->x, label->len,
7128 output, output_size,
7129 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007130 /* We don't know what ciphertext length to expect, but check that
7131 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007132 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007133
Ronald Cron5425a212020-08-04 14:58:35 +02007134 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007135 output, output_length,
7136 label->x, label->len,
7137 output2, output2_size,
7138 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007139 ASSERT_COMPARE( input_data->x, input_data->len,
7140 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007141
7142exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007143 /*
7144 * Key attributes may have been returned by psa_get_key_attributes()
7145 * thus reset them as required.
7146 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007147 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007148
Ronald Cron5425a212020-08-04 14:58:35 +02007149 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007150 mbedtls_free( output );
7151 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007152 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007153}
7154/* END_CASE */
7155
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007156/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007157void asymmetric_decrypt( int key_type_arg,
7158 data_t *key_data,
7159 int alg_arg,
7160 data_t *input_data,
7161 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007162 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007163{
Ronald Cron5425a212020-08-04 14:58:35 +02007164 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007165 psa_key_type_t key_type = key_type_arg;
7166 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007167 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007168 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007169 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007170 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007172
Gilles Peskine8817f612018-12-18 00:18:46 +01007173 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007174
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007175 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7176 psa_set_key_algorithm( &attributes, alg );
7177 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007178
Gilles Peskine049c7532019-05-15 20:22:09 +02007179 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007180 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007181
gabor-mezei-armceface22021-01-21 12:26:17 +01007182 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7183 key_bits = psa_get_key_bits( &attributes );
7184
7185 /* Determine the maximum ciphertext length */
7186 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007187 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007188 ASSERT_ALLOC( output, output_size );
7189
Ronald Cron5425a212020-08-04 14:58:35 +02007190 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007191 input_data->x, input_data->len,
7192 label->x, label->len,
7193 output,
7194 output_size,
7195 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007196 ASSERT_COMPARE( expected_data->x, expected_data->len,
7197 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007198
Gilles Peskine68428122018-06-30 18:42:41 +02007199 /* If the label is empty, the test framework puts a non-null pointer
7200 * in label->x. Test that a null pointer works as well. */
7201 if( label->len == 0 )
7202 {
7203 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007204 if( output_size != 0 )
7205 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007206 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007207 input_data->x, input_data->len,
7208 NULL, label->len,
7209 output,
7210 output_size,
7211 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007212 ASSERT_COMPARE( expected_data->x, expected_data->len,
7213 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007214 }
7215
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007216exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007217 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007218 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007219 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007220 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007221}
7222/* END_CASE */
7223
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007224/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007225void asymmetric_decrypt_fail( int key_type_arg,
7226 data_t *key_data,
7227 int alg_arg,
7228 data_t *input_data,
7229 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007230 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007231 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007232{
Ronald Cron5425a212020-08-04 14:58:35 +02007233 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007234 psa_key_type_t key_type = key_type_arg;
7235 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007236 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007237 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007238 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007239 psa_status_t actual_status;
7240 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007242
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007243 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007244
Gilles Peskine8817f612018-12-18 00:18:46 +01007245 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007247 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7248 psa_set_key_algorithm( &attributes, alg );
7249 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007250
Gilles Peskine049c7532019-05-15 20:22:09 +02007251 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007252 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007253
Ronald Cron5425a212020-08-04 14:58:35 +02007254 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007255 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007256 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007257 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007258 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007259 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007260 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261
Gilles Peskine68428122018-06-30 18:42:41 +02007262 /* If the label is empty, the test framework puts a non-null pointer
7263 * in label->x. Test that a null pointer works as well. */
7264 if( label->len == 0 )
7265 {
7266 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007267 if( output_size != 0 )
7268 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007269 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007270 input_data->x, input_data->len,
7271 NULL, label->len,
7272 output, output_size,
7273 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007274 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007275 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007276 }
7277
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007278exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007279 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007280 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007281 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007282 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007283}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007284/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007285
7286/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007287void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007288{
7289 /* Test each valid way of initializing the object, except for `= {0}`, as
7290 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7291 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007292 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007293 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007294 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7295 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7296 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007297
7298 memset( &zero, 0, sizeof( zero ) );
7299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007300 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007301 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007302 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007303 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007304 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007305 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007306 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007307
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007308 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007309 PSA_ASSERT( psa_key_derivation_abort(&func) );
7310 PSA_ASSERT( psa_key_derivation_abort(&init) );
7311 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007312}
7313/* END_CASE */
7314
Janos Follath16de4a42019-06-13 16:32:24 +01007315/* BEGIN_CASE */
7316void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007317{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007318 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007319 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007320 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007321
Gilles Peskine8817f612018-12-18 00:18:46 +01007322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007323
Janos Follath16de4a42019-06-13 16:32:24 +01007324 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007325 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007326
7327exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007328 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007329 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007330}
7331/* END_CASE */
7332
Janos Follathaf3c2a02019-06-12 12:34:34 +01007333/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007334void derive_set_capacity( int alg_arg, int capacity_arg,
7335 int expected_status_arg )
7336{
7337 psa_algorithm_t alg = alg_arg;
7338 size_t capacity = capacity_arg;
7339 psa_status_t expected_status = expected_status_arg;
7340 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7341
7342 PSA_ASSERT( psa_crypto_init( ) );
7343
7344 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7345
7346 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7347 expected_status );
7348
7349exit:
7350 psa_key_derivation_abort( &operation );
7351 PSA_DONE( );
7352}
7353/* END_CASE */
7354
7355/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007356void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007357 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007358 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007359 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007360 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007361 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007362 int expected_status_arg3,
7363 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007364{
7365 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007366 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7367 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007368 psa_status_t expected_statuses[] = {expected_status_arg1,
7369 expected_status_arg2,
7370 expected_status_arg3};
7371 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007372 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7373 MBEDTLS_SVC_KEY_ID_INIT,
7374 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007375 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7377 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007378 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007379 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007380 psa_status_t expected_output_status = expected_output_status_arg;
7381 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007382
7383 PSA_ASSERT( psa_crypto_init( ) );
7384
7385 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7386 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007387
7388 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7389
7390 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7391 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007392 mbedtls_test_set_step( i );
7393 if( steps[i] == 0 )
7394 {
7395 /* Skip this step */
7396 }
7397 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007398 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007399 psa_set_key_type( &attributes, key_types[i] );
7400 PSA_ASSERT( psa_import_key( &attributes,
7401 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007402 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007403 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7404 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7405 {
7406 // When taking a private key as secret input, use key agreement
7407 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007408 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7409 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007410 expected_statuses[i] );
7411 }
7412 else
7413 {
7414 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007415 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007416 expected_statuses[i] );
7417 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007418 }
7419 else
7420 {
7421 TEST_EQUAL( psa_key_derivation_input_bytes(
7422 &operation, steps[i],
7423 inputs[i]->x, inputs[i]->len ),
7424 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007425 }
7426 }
7427
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007428 if( output_key_type != PSA_KEY_TYPE_NONE )
7429 {
7430 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007431 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007432 psa_set_key_bits( &attributes, 8 );
7433 actual_output_status =
7434 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007435 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007436 }
7437 else
7438 {
7439 uint8_t buffer[1];
7440 actual_output_status =
7441 psa_key_derivation_output_bytes( &operation,
7442 buffer, sizeof( buffer ) );
7443 }
7444 TEST_EQUAL( actual_output_status, expected_output_status );
7445
Janos Follathaf3c2a02019-06-12 12:34:34 +01007446exit:
7447 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007448 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7449 psa_destroy_key( keys[i] );
7450 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007451 PSA_DONE( );
7452}
7453/* END_CASE */
7454
Janos Follathd958bb72019-07-03 15:02:16 +01007455/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007456void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007457{
Janos Follathd958bb72019-07-03 15:02:16 +01007458 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007459 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007460 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007461 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007462 unsigned char input1[] = "Input 1";
7463 size_t input1_length = sizeof( input1 );
7464 unsigned char input2[] = "Input 2";
7465 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007466 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007467 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007468 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7469 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7470 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007472
Gilles Peskine8817f612018-12-18 00:18:46 +01007473 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007474
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007475 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7476 psa_set_key_algorithm( &attributes, alg );
7477 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007478
Gilles Peskine73676cb2019-05-15 20:15:10 +02007479 PSA_ASSERT( psa_import_key( &attributes,
7480 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007481 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007482
7483 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007484 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7485 input1, input1_length,
7486 input2, input2_length,
7487 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007488 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007489
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007490 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007491 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007492 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007493
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007494 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007495
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007496 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007497 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007498
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007499exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007500 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007501 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007502 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007503}
7504/* END_CASE */
7505
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007506/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007507void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007508{
7509 uint8_t output_buffer[16];
7510 size_t buffer_size = 16;
7511 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007512 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007513
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007514 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7515 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007516 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007517
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007518 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007519 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007520
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007521 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007522
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007523 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7524 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007525 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007526
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007527 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007528 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007529
7530exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007531 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007532}
7533/* END_CASE */
7534
7535/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007536void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007537 int step1_arg, data_t *input1, int expected_status_arg1,
7538 int step2_arg, data_t *input2, int expected_status_arg2,
7539 int step3_arg, data_t *input3, int expected_status_arg3,
7540 int step4_arg, data_t *input4, int expected_status_arg4,
7541 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007542 int requested_capacity_arg,
7543 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007544 data_t *expected_output2,
7545 int other_key_input_type,
7546 int key_input_type,
7547 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007548{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007549 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007550 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7551 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007552 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7553 MBEDTLS_SVC_KEY_ID_INIT,
7554 MBEDTLS_SVC_KEY_ID_INIT,
7555 MBEDTLS_SVC_KEY_ID_INIT};
7556 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7557 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007558 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007559 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007560 uint8_t *expected_outputs[2] =
7561 {expected_output1->x, expected_output2->x};
7562 size_t output_sizes[2] =
7563 {expected_output1->len, expected_output2->len};
7564 size_t output_buffer_size = 0;
7565 uint8_t *output_buffer = NULL;
7566 size_t expected_capacity;
7567 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007568 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7569 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7570 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7571 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007572 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007573 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007574 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007575
7576 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7577 {
7578 if( output_sizes[i] > output_buffer_size )
7579 output_buffer_size = output_sizes[i];
7580 if( output_sizes[i] == 0 )
7581 expected_outputs[i] = NULL;
7582 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007583 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007584 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007585
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007586 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007587 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7588 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7589 requested_capacity ) );
7590 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007591 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007592 switch( steps[i] )
7593 {
7594 case 0:
7595 break;
7596 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007597 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007598 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007599 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007600 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007601 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007602 inputs[i]->x, inputs[i]->len ),
7603 statuses[i] );
7604
7605 if( statuses[i] != PSA_SUCCESS )
7606 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007607 break;
7608 case 1: // input key
7609 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7610 psa_set_key_algorithm( &attributes1, alg );
7611 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7612
7613 PSA_ASSERT( psa_import_key( &attributes1,
7614 inputs[i]->x, inputs[i]->len,
7615 &keys[i] ) );
7616
Przemek Stekiel38647de2022-04-19 13:27:47 +02007617 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007618 {
7619 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007620 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7621 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007622 }
7623
Przemek Stekiel38647de2022-04-19 13:27:47 +02007624 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7625 steps[i],
7626 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007627 break;
7628 default:
7629 TEST_ASSERT( ! "default case not supported" );
7630 break;
7631 }
7632 break;
7633 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007634 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007635 {
7636 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007637 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7638 steps[i],
7639 inputs[i]->x,
7640 inputs[i]->len ),
7641 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007642 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007643 case 1: // input key, type DERIVE
7644 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007645 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7646 psa_set_key_algorithm( &attributes2, alg );
7647 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7648
7649 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007650 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007651 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7652
7653 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007654 inputs[i]->x, inputs[i]->len,
7655 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007656
Przemek Stekiel38647de2022-04-19 13:27:47 +02007657 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7658 steps[i],
7659 keys[i] ),
7660 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007661 break;
7662 case 2: // key agreement
7663 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7664 psa_set_key_algorithm( &attributes3, alg );
7665 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7666
7667 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007668 inputs[i]->x, inputs[i]->len,
7669 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007670
7671 TEST_EQUAL( psa_key_derivation_key_agreement(
7672 &operation,
7673 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7674 keys[i], key_agreement_peer_key->x,
7675 key_agreement_peer_key->len ), statuses[i] );
7676 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007677 default:
7678 TEST_ASSERT( ! "default case not supported" );
7679 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007680 }
7681
Przemek Stekiel38647de2022-04-19 13:27:47 +02007682 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007683 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007684 break;
7685 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007686 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007687 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007688 inputs[i]->x, inputs[i]->len ), statuses[i] );
7689
7690 if( statuses[i] != PSA_SUCCESS )
7691 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007692 break;
7693 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007694 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007695
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007696 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007697 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007698 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007699 expected_capacity = requested_capacity;
7700
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007701 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007702 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007703 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7704
7705 /* For output key derivation secret must be provided using
7706 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007707 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007708 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007709
7710 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7711 psa_set_key_algorithm( &attributes4, alg );
7712 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007713 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007714
7715 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007716 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007717 }
7718 else // output bytes
7719 {
7720 /* Expansion phase. */
7721 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007722 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007723 /* Read some bytes. */
7724 status = psa_key_derivation_output_bytes( &operation,
7725 output_buffer, output_sizes[i] );
7726 if( expected_capacity == 0 && output_sizes[i] == 0 )
7727 {
7728 /* Reading 0 bytes when 0 bytes are available can go either way. */
7729 TEST_ASSERT( status == PSA_SUCCESS ||
7730 status == PSA_ERROR_INSUFFICIENT_DATA );
7731 continue;
7732 }
7733 else if( expected_capacity == 0 ||
7734 output_sizes[i] > expected_capacity )
7735 {
7736 /* Capacity exceeded. */
7737 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7738 expected_capacity = 0;
7739 continue;
7740 }
7741 /* Success. Check the read data. */
7742 PSA_ASSERT( status );
7743 if( output_sizes[i] != 0 )
7744 ASSERT_COMPARE( output_buffer, output_sizes[i],
7745 expected_outputs[i], output_sizes[i] );
7746 /* Check the operation status. */
7747 expected_capacity -= output_sizes[i];
7748 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7749 &current_capacity ) );
7750 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007751 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007752 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007753 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007754
7755exit:
7756 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007757 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007758 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7759 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007760 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007761 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007762}
7763/* END_CASE */
7764
7765/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007766void derive_full( int alg_arg,
7767 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007768 data_t *input1,
7769 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007770 int requested_capacity_arg )
7771{
Ronald Cron5425a212020-08-04 14:58:35 +02007772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007773 psa_algorithm_t alg = alg_arg;
7774 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007775 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007776 unsigned char output_buffer[16];
7777 size_t expected_capacity = requested_capacity;
7778 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007780
Gilles Peskine8817f612018-12-18 00:18:46 +01007781 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007782
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007783 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7784 psa_set_key_algorithm( &attributes, alg );
7785 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007786
Gilles Peskine049c7532019-05-15 20:22:09 +02007787 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007788 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007789
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007790 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7791 input1->x, input1->len,
7792 input2->x, input2->len,
7793 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007794 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007795
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007796 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007797 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007798 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007799
7800 /* Expansion phase. */
7801 while( current_capacity > 0 )
7802 {
7803 size_t read_size = sizeof( output_buffer );
7804 if( read_size > current_capacity )
7805 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007806 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007807 output_buffer,
7808 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007809 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007810 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007811 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007812 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007813 }
7814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007815 /* Check that the operation refuses to go over capacity. */
7816 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007817 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007818
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007819 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007820
7821exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007822 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007823 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007824 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007825}
7826/* END_CASE */
7827
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007828/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007829void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007830 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007831 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007832 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007833 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007834{
7835 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7836 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007837 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007838 uint8_t *output_buffer = NULL;
7839 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007840 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7841 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7842 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007843
7844 ASSERT_ALLOC( output_buffer, expected_output->len );
7845 PSA_ASSERT( psa_crypto_init() );
7846
7847 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007848 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007849 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007850
7851 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007852 step, input->x, input->len ),
7853 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007854
7855 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7856 goto exit;
7857
7858 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7859 expected_output->len );
7860
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007861 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007862 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7863 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7864 expected_output->len );
7865
7866exit:
7867 mbedtls_free( output_buffer );
7868 psa_key_derivation_abort( &operation );
7869 PSA_DONE();
7870}
7871/* END_CASE */
7872
Janos Follathe60c9052019-07-03 13:51:30 +01007873/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007874void derive_key_exercise( int alg_arg,
7875 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007876 data_t *input1,
7877 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007878 int derived_type_arg,
7879 int derived_bits_arg,
7880 int derived_usage_arg,
7881 int derived_alg_arg )
7882{
Ronald Cron5425a212020-08-04 14:58:35 +02007883 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7884 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007885 psa_algorithm_t alg = alg_arg;
7886 psa_key_type_t derived_type = derived_type_arg;
7887 size_t derived_bits = derived_bits_arg;
7888 psa_key_usage_t derived_usage = derived_usage_arg;
7889 psa_algorithm_t derived_alg = derived_alg_arg;
7890 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007891 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007893 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007894
Gilles Peskine8817f612018-12-18 00:18:46 +01007895 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007896
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007897 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7898 psa_set_key_algorithm( &attributes, alg );
7899 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007900 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007901 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007902
7903 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007904 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7905 input1->x, input1->len,
7906 input2->x, input2->len,
7907 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007908 goto exit;
7909
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007910 psa_set_key_usage_flags( &attributes, derived_usage );
7911 psa_set_key_algorithm( &attributes, derived_alg );
7912 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007913 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007914 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007915 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007916
7917 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007918 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007919 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7920 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007921
7922 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007923 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007924 goto exit;
7925
7926exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007927 /*
7928 * Key attributes may have been returned by psa_get_key_attributes()
7929 * thus reset them as required.
7930 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007931 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007932
7933 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007934 psa_destroy_key( base_key );
7935 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007936 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007937}
7938/* END_CASE */
7939
Janos Follath42fd8882019-07-03 14:17:09 +01007940/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007941void derive_key_export( int alg_arg,
7942 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007943 data_t *input1,
7944 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007945 int bytes1_arg,
7946 int bytes2_arg )
7947{
Ronald Cron5425a212020-08-04 14:58:35 +02007948 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7949 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007950 psa_algorithm_t alg = alg_arg;
7951 size_t bytes1 = bytes1_arg;
7952 size_t bytes2 = bytes2_arg;
7953 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007954 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007955 uint8_t *output_buffer = NULL;
7956 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007957 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7958 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007959 size_t length;
7960
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007961 ASSERT_ALLOC( output_buffer, capacity );
7962 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007963 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007964
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007965 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7966 psa_set_key_algorithm( &base_attributes, alg );
7967 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007968 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007969 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007970
7971 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007972 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7973 input1->x, input1->len,
7974 input2->x, input2->len,
7975 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007976 goto exit;
7977
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007978 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007979 output_buffer,
7980 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007981 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007982
7983 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007984 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7985 input1->x, input1->len,
7986 input2->x, input2->len,
7987 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007988 goto exit;
7989
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007990 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7991 psa_set_key_algorithm( &derived_attributes, 0 );
7992 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007993 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007994 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007995 &derived_key ) );
7996 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007997 export_buffer, bytes1,
7998 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007999 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02008000 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008001 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008002 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008003 &derived_key ) );
8004 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008005 export_buffer + bytes1, bytes2,
8006 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008007 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008008
8009 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008010 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8011 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008012
8013exit:
8014 mbedtls_free( output_buffer );
8015 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008016 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008017 psa_destroy_key( base_key );
8018 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008019 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008020}
8021/* END_CASE */
8022
8023/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008024void derive_key_type( int alg_arg,
8025 data_t *key_data,
8026 data_t *input1,
8027 data_t *input2,
8028 int key_type_arg, int bits_arg,
8029 data_t *expected_export )
8030{
8031 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8032 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8033 const psa_algorithm_t alg = alg_arg;
8034 const psa_key_type_t key_type = key_type_arg;
8035 const size_t bits = bits_arg;
8036 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8037 const size_t export_buffer_size =
8038 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8039 uint8_t *export_buffer = NULL;
8040 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8041 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8042 size_t export_length;
8043
8044 ASSERT_ALLOC( export_buffer, export_buffer_size );
8045 PSA_ASSERT( psa_crypto_init( ) );
8046
8047 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8048 psa_set_key_algorithm( &base_attributes, alg );
8049 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8050 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8051 &base_key ) );
8052
Przemek Stekielc85f0912022-03-08 11:37:54 +01008053 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008054 &operation, base_key, alg,
8055 input1->x, input1->len,
8056 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008057 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008058 goto exit;
8059
8060 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8061 psa_set_key_algorithm( &derived_attributes, 0 );
8062 psa_set_key_type( &derived_attributes, key_type );
8063 psa_set_key_bits( &derived_attributes, bits );
8064 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8065 &derived_key ) );
8066
8067 PSA_ASSERT( psa_export_key( derived_key,
8068 export_buffer, export_buffer_size,
8069 &export_length ) );
8070 ASSERT_COMPARE( export_buffer, export_length,
8071 expected_export->x, expected_export->len );
8072
8073exit:
8074 mbedtls_free( export_buffer );
8075 psa_key_derivation_abort( &operation );
8076 psa_destroy_key( base_key );
8077 psa_destroy_key( derived_key );
8078 PSA_DONE( );
8079}
8080/* END_CASE */
8081
8082/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008083void derive_key( int alg_arg,
8084 data_t *key_data, data_t *input1, data_t *input2,
8085 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008086 int expected_status_arg,
8087 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008088{
Ronald Cron5425a212020-08-04 14:58:35 +02008089 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8090 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008091 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008092 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008093 size_t bits = bits_arg;
8094 psa_status_t expected_status = expected_status_arg;
8095 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8096 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8097 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8098
8099 PSA_ASSERT( psa_crypto_init( ) );
8100
8101 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8102 psa_set_key_algorithm( &base_attributes, alg );
8103 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8104 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008105 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008106
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008107 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8108 input1->x, input1->len,
8109 input2->x, input2->len,
8110 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008111 goto exit;
8112
8113 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8114 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008115 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008116 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008117
8118 psa_status_t status =
8119 psa_key_derivation_output_key( &derived_attributes,
8120 &operation,
8121 &derived_key );
8122 if( is_large_output > 0 )
8123 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8124 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008125
8126exit:
8127 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008128 psa_destroy_key( base_key );
8129 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008130 PSA_DONE( );
8131}
8132/* END_CASE */
8133
8134/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008135void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008136 int our_key_type_arg, int our_key_alg_arg,
8137 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008138 int expected_status_arg )
8139{
Ronald Cron5425a212020-08-04 14:58:35 +02008140 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008141 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008142 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008143 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008144 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008146 psa_status_t expected_status = expected_status_arg;
8147 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008148
Gilles Peskine8817f612018-12-18 00:18:46 +01008149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008150
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008151 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008152 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008153 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008154 PSA_ASSERT( psa_import_key( &attributes,
8155 our_key_data->x, our_key_data->len,
8156 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008157
Gilles Peskine77f40d82019-04-11 21:27:06 +02008158 /* The tests currently include inputs that should fail at either step.
8159 * Test cases that fail at the setup step should be changed to call
8160 * key_derivation_setup instead, and this function should be renamed
8161 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008162 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008163 if( status == PSA_SUCCESS )
8164 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008165 TEST_EQUAL( psa_key_derivation_key_agreement(
8166 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8167 our_key,
8168 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008169 expected_status );
8170 }
8171 else
8172 {
8173 TEST_ASSERT( status == expected_status );
8174 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008175
8176exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008177 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008178 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008179 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008180}
8181/* END_CASE */
8182
8183/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008184void raw_key_agreement( int alg_arg,
8185 int our_key_type_arg, data_t *our_key_data,
8186 data_t *peer_key_data,
8187 data_t *expected_output )
8188{
Ronald Cron5425a212020-08-04 14:58:35 +02008189 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008190 psa_algorithm_t alg = alg_arg;
8191 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008193 unsigned char *output = NULL;
8194 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008195 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008196
Gilles Peskinef0cba732019-04-11 22:12:38 +02008197 PSA_ASSERT( psa_crypto_init( ) );
8198
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008199 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8200 psa_set_key_algorithm( &attributes, alg );
8201 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008202 PSA_ASSERT( psa_import_key( &attributes,
8203 our_key_data->x, our_key_data->len,
8204 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008205
gabor-mezei-armceface22021-01-21 12:26:17 +01008206 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8207 key_bits = psa_get_key_bits( &attributes );
8208
Gilles Peskine992bee82022-04-13 23:25:52 +02008209 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008210 TEST_LE_U( expected_output->len,
8211 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8212 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8213 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008214
8215 /* Good case with exact output size */
8216 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008217 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8218 peer_key_data->x, peer_key_data->len,
8219 output, expected_output->len,
8220 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008221 ASSERT_COMPARE( output, output_length,
8222 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008223 mbedtls_free( output );
8224 output = NULL;
8225 output_length = ~0;
8226
8227 /* Larger buffer */
8228 ASSERT_ALLOC( output, expected_output->len + 1 );
8229 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8230 peer_key_data->x, peer_key_data->len,
8231 output, expected_output->len + 1,
8232 &output_length ) );
8233 ASSERT_COMPARE( output, output_length,
8234 expected_output->x, expected_output->len );
8235 mbedtls_free( output );
8236 output = NULL;
8237 output_length = ~0;
8238
8239 /* Buffer too small */
8240 ASSERT_ALLOC( output, expected_output->len - 1 );
8241 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8242 peer_key_data->x, peer_key_data->len,
8243 output, expected_output->len - 1,
8244 &output_length ),
8245 PSA_ERROR_BUFFER_TOO_SMALL );
8246 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008247 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008248 mbedtls_free( output );
8249 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008250
8251exit:
8252 mbedtls_free( output );
8253 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008254 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008255}
8256/* END_CASE */
8257
8258/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008259void key_agreement_capacity( int alg_arg,
8260 int our_key_type_arg, data_t *our_key_data,
8261 data_t *peer_key_data,
8262 int expected_capacity_arg )
8263{
Ronald Cron5425a212020-08-04 14:58:35 +02008264 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008265 psa_algorithm_t alg = alg_arg;
8266 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008267 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008269 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008270 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008271
Gilles Peskine8817f612018-12-18 00:18:46 +01008272 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008273
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008274 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8275 psa_set_key_algorithm( &attributes, alg );
8276 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008277 PSA_ASSERT( psa_import_key( &attributes,
8278 our_key_data->x, our_key_data->len,
8279 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008280
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008281 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008282 PSA_ASSERT( psa_key_derivation_key_agreement(
8283 &operation,
8284 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8285 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008286 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8287 {
8288 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008289 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008290 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008291 NULL, 0 ) );
8292 }
Gilles Peskine59685592018-09-18 12:11:34 +02008293
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008294 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008295 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008296 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008297 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008298
Gilles Peskinebf491972018-10-25 22:36:12 +02008299 /* Test the actual capacity by reading the output. */
8300 while( actual_capacity > sizeof( output ) )
8301 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008302 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008303 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008304 actual_capacity -= sizeof( output );
8305 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008306 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008307 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008308 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008309 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008310
Gilles Peskine59685592018-09-18 12:11:34 +02008311exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008312 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008313 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008314 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008315}
8316/* END_CASE */
8317
8318/* BEGIN_CASE */
8319void key_agreement_output( int alg_arg,
8320 int our_key_type_arg, data_t *our_key_data,
8321 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008322 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008323{
Ronald Cron5425a212020-08-04 14:58:35 +02008324 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008325 psa_algorithm_t alg = alg_arg;
8326 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008327 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008328 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008329 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008330
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008331 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8332 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008333
Gilles Peskine8817f612018-12-18 00:18:46 +01008334 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008335
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008336 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8337 psa_set_key_algorithm( &attributes, alg );
8338 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008339 PSA_ASSERT( psa_import_key( &attributes,
8340 our_key_data->x, our_key_data->len,
8341 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008342
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008343 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008344 PSA_ASSERT( psa_key_derivation_key_agreement(
8345 &operation,
8346 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8347 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008348 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8349 {
8350 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008351 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008352 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008353 NULL, 0 ) );
8354 }
Gilles Peskine59685592018-09-18 12:11:34 +02008355
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008356 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008357 actual_output,
8358 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008359 ASSERT_COMPARE( actual_output, expected_output1->len,
8360 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008361 if( expected_output2->len != 0 )
8362 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008363 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008364 actual_output,
8365 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008366 ASSERT_COMPARE( actual_output, expected_output2->len,
8367 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008368 }
Gilles Peskine59685592018-09-18 12:11:34 +02008369
8370exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008371 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008372 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008373 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008374 mbedtls_free( actual_output );
8375}
8376/* END_CASE */
8377
8378/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008379void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008380{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008381 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008382 unsigned char *output = NULL;
8383 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008384 size_t i;
8385 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008386
Simon Butcher49f8e312020-03-03 15:51:50 +00008387 TEST_ASSERT( bytes_arg >= 0 );
8388
Gilles Peskine91892022021-02-08 19:50:26 +01008389 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008390 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008391
Gilles Peskine8817f612018-12-18 00:18:46 +01008392 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008393
Gilles Peskinea50d7392018-06-21 10:22:13 +02008394 /* Run several times, to ensure that every output byte will be
8395 * nonzero at least once with overwhelming probability
8396 * (2^(-8*number_of_runs)). */
8397 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008398 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008399 if( bytes != 0 )
8400 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008401 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008402
Gilles Peskinea50d7392018-06-21 10:22:13 +02008403 for( i = 0; i < bytes; i++ )
8404 {
8405 if( output[i] != 0 )
8406 ++changed[i];
8407 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008408 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008409
8410 /* Check that every byte was changed to nonzero at least once. This
8411 * validates that psa_generate_random is overwriting every byte of
8412 * the output buffer. */
8413 for( i = 0; i < bytes; i++ )
8414 {
8415 TEST_ASSERT( changed[i] != 0 );
8416 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008417
8418exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008419 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008420 mbedtls_free( output );
8421 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008422}
8423/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008424
8425/* BEGIN_CASE */
8426void generate_key( int type_arg,
8427 int bits_arg,
8428 int usage_arg,
8429 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008430 int expected_status_arg,
8431 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008432{
Ronald Cron5425a212020-08-04 14:58:35 +02008433 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008434 psa_key_type_t type = type_arg;
8435 psa_key_usage_t usage = usage_arg;
8436 size_t bits = bits_arg;
8437 psa_algorithm_t alg = alg_arg;
8438 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008440 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008441
Gilles Peskine8817f612018-12-18 00:18:46 +01008442 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008443
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008444 psa_set_key_usage_flags( &attributes, usage );
8445 psa_set_key_algorithm( &attributes, alg );
8446 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008447 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008448
8449 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008450 psa_status_t status = psa_generate_key( &attributes, &key );
8451
8452 if( is_large_key > 0 )
8453 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8454 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008455 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008456 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008457
8458 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008459 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008460 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8461 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008462
Gilles Peskine818ca122018-06-20 18:16:48 +02008463 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008464 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008465 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008466
8467exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008468 /*
8469 * Key attributes may have been returned by psa_get_key_attributes()
8470 * thus reset them as required.
8471 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008472 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008473
Ronald Cron5425a212020-08-04 14:58:35 +02008474 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008475 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008476}
8477/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008478
Ronald Cronee414c72021-03-18 18:50:08 +01008479/* 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 +02008480void generate_key_rsa( int bits_arg,
8481 data_t *e_arg,
8482 int expected_status_arg )
8483{
Ronald Cron5425a212020-08-04 14:58:35 +02008484 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008485 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008486 size_t bits = bits_arg;
8487 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8488 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8489 psa_status_t expected_status = expected_status_arg;
8490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8491 uint8_t *exported = NULL;
8492 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008493 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008494 size_t exported_length = SIZE_MAX;
8495 uint8_t *e_read_buffer = NULL;
8496 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008497 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008498 size_t e_read_length = SIZE_MAX;
8499
8500 if( e_arg->len == 0 ||
8501 ( e_arg->len == 3 &&
8502 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8503 {
8504 is_default_public_exponent = 1;
8505 e_read_size = 0;
8506 }
8507 ASSERT_ALLOC( e_read_buffer, e_read_size );
8508 ASSERT_ALLOC( exported, exported_size );
8509
8510 PSA_ASSERT( psa_crypto_init( ) );
8511
8512 psa_set_key_usage_flags( &attributes, usage );
8513 psa_set_key_algorithm( &attributes, alg );
8514 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8515 e_arg->x, e_arg->len ) );
8516 psa_set_key_bits( &attributes, bits );
8517
8518 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008519 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008520 if( expected_status != PSA_SUCCESS )
8521 goto exit;
8522
8523 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008524 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008525 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8526 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8527 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8528 e_read_buffer, e_read_size,
8529 &e_read_length ) );
8530 if( is_default_public_exponent )
8531 TEST_EQUAL( e_read_length, 0 );
8532 else
8533 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8534
8535 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008536 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008537 goto exit;
8538
8539 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008540 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008541 exported, exported_size,
8542 &exported_length ) );
8543 {
8544 uint8_t *p = exported;
8545 uint8_t *end = exported + exported_length;
8546 size_t len;
8547 /* RSAPublicKey ::= SEQUENCE {
8548 * modulus INTEGER, -- n
8549 * publicExponent INTEGER } -- e
8550 */
8551 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008552 MBEDTLS_ASN1_SEQUENCE |
8553 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008554 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008555 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8556 MBEDTLS_ASN1_INTEGER ) );
8557 if( len >= 1 && p[0] == 0 )
8558 {
8559 ++p;
8560 --len;
8561 }
8562 if( e_arg->len == 0 )
8563 {
8564 TEST_EQUAL( len, 3 );
8565 TEST_EQUAL( p[0], 1 );
8566 TEST_EQUAL( p[1], 0 );
8567 TEST_EQUAL( p[2], 1 );
8568 }
8569 else
8570 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8571 }
8572
8573exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008574 /*
8575 * Key attributes may have been returned by psa_get_key_attributes() or
8576 * set by psa_set_key_domain_parameters() thus reset them as required.
8577 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008578 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008579
Ronald Cron5425a212020-08-04 14:58:35 +02008580 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008581 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008582 mbedtls_free( e_read_buffer );
8583 mbedtls_free( exported );
8584}
8585/* END_CASE */
8586
Darryl Greend49a4992018-06-18 17:27:26 +01008587/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008588void persistent_key_load_key_from_storage( data_t *data,
8589 int type_arg, int bits_arg,
8590 int usage_flags_arg, int alg_arg,
8591 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008592{
Ronald Cron71016a92020-08-28 19:01:50 +02008593 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8596 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008597 psa_key_type_t type = type_arg;
8598 size_t bits = bits_arg;
8599 psa_key_usage_t usage_flags = usage_flags_arg;
8600 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008601 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008602 unsigned char *first_export = NULL;
8603 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008604 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008605 size_t first_exported_length;
8606 size_t second_exported_length;
8607
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008608 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8609 {
8610 ASSERT_ALLOC( first_export, export_size );
8611 ASSERT_ALLOC( second_export, export_size );
8612 }
Darryl Greend49a4992018-06-18 17:27:26 +01008613
Gilles Peskine8817f612018-12-18 00:18:46 +01008614 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008615
Gilles Peskinec87af662019-05-15 16:12:22 +02008616 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008617 psa_set_key_usage_flags( &attributes, usage_flags );
8618 psa_set_key_algorithm( &attributes, alg );
8619 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008620 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008621
Darryl Green0c6575a2018-11-07 16:05:30 +00008622 switch( generation_method )
8623 {
8624 case IMPORT_KEY:
8625 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008626 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008627 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008628 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008629
Darryl Green0c6575a2018-11-07 16:05:30 +00008630 case GENERATE_KEY:
8631 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008632 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008633 break;
8634
8635 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008636#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008637 {
8638 /* Create base key */
8639 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8640 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8641 psa_set_key_usage_flags( &base_attributes,
8642 PSA_KEY_USAGE_DERIVE );
8643 psa_set_key_algorithm( &base_attributes, derive_alg );
8644 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008645 PSA_ASSERT( psa_import_key( &base_attributes,
8646 data->x, data->len,
8647 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008648 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008649 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008650 PSA_ASSERT( psa_key_derivation_input_key(
8651 &operation,
8652 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008653 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008654 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008655 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008656 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8657 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008658 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008659 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008660 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008661 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008662 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008663#else
8664 TEST_ASSUME( ! "KDF not supported in this configuration" );
8665#endif
8666 break;
8667
8668 default:
8669 TEST_ASSERT( ! "generation_method not implemented in test" );
8670 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008671 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008672 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008673
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008674 /* Export the key if permitted by the key policy. */
8675 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8676 {
Ronald Cron5425a212020-08-04 14:58:35 +02008677 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008678 first_export, export_size,
8679 &first_exported_length ) );
8680 if( generation_method == IMPORT_KEY )
8681 ASSERT_COMPARE( data->x, data->len,
8682 first_export, first_exported_length );
8683 }
Darryl Greend49a4992018-06-18 17:27:26 +01008684
8685 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008686 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008687 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008688 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008689
Darryl Greend49a4992018-06-18 17:27:26 +01008690 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008691 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008692 TEST_ASSERT( mbedtls_svc_key_id_equal(
8693 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008694 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8695 PSA_KEY_LIFETIME_PERSISTENT );
8696 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8697 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008698 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008699 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008700 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008701
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008702 /* Export the key again if permitted by the key policy. */
8703 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008704 {
Ronald Cron5425a212020-08-04 14:58:35 +02008705 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008706 second_export, export_size,
8707 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008708 ASSERT_COMPARE( first_export, first_exported_length,
8709 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008710 }
8711
8712 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008713 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008714 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008715
8716exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008717 /*
8718 * Key attributes may have been returned by psa_get_key_attributes()
8719 * thus reset them as required.
8720 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008721 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008722
Darryl Greend49a4992018-06-18 17:27:26 +01008723 mbedtls_free( first_export );
8724 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008725 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008726 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008727 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008728 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008729}
8730/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008731
Neil Armstronga557cb82022-06-10 08:58:32 +02008732/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008733void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8734 int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008735 int input_first, data_t *pw_data,
Neil Armstrong2a73f212022-09-06 11:34:54 +02008736 int expected_status_setup_arg,
8737 int expected_status_set_role_arg,
8738 int expected_status_set_password_key_arg,
8739 int expected_status_input_output_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008740{
8741 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8742 psa_pake_operation_t operation = psa_pake_operation_init();
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008743 psa_pake_operation_t op_copy = psa_pake_operation_init();
Neil Armstrongd597bc72022-05-25 11:28:39 +02008744 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008745 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008746 psa_key_type_t key_type_pw = key_type_pw_arg;
8747 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008748 psa_algorithm_t hash_alg = hash_arg;
8749 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008750 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8751 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008752 psa_status_t expected_status_setup = expected_status_setup_arg;
8753 psa_status_t expected_status_set_role = expected_status_set_role_arg;
8754 psa_status_t expected_status_set_password_key =
8755 expected_status_set_password_key_arg;
8756 psa_status_t expected_status_input_output =
8757 expected_status_input_output_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008758 unsigned char *output_buffer = NULL;
8759 size_t output_len = 0;
8760
8761 PSA_INIT( );
8762
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008763 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8764 PSA_PAKE_STEP_KEY_SHARE);
8765 ASSERT_ALLOC( output_buffer, buf_size );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008766
8767 if( pw_data->len > 0 )
8768 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008769 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008770 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008771 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008772 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8773 &key ) );
8774 }
8775
8776 psa_pake_cs_set_algorithm( &cipher_suite, alg );
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008777 psa_pake_cs_set_primitive( &cipher_suite, primitive );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008778 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8779
Neil Armstrong645cccd2022-06-08 17:36:23 +02008780 PSA_ASSERT( psa_pake_abort( &operation ) );
8781
8782 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8783 PSA_ERROR_BAD_STATE );
8784 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8785 PSA_ERROR_BAD_STATE );
8786 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8787 PSA_ERROR_BAD_STATE );
8788 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8789 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008790 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8791 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008792 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008793 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008794 PSA_ERROR_BAD_STATE );
8795
8796 PSA_ASSERT( psa_pake_abort( &operation ) );
8797
Neil Armstrong2a73f212022-09-06 11:34:54 +02008798 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8799 expected_status_setup );
8800 if( expected_status_setup != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008801 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008802
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008803 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8804 PSA_ERROR_BAD_STATE );
8805
Neil Armstrong2a73f212022-09-06 11:34:54 +02008806 TEST_EQUAL( psa_pake_set_role( &operation, role),
8807 expected_status_set_role );
8808 if( expected_status_set_role != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008809 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008810
8811 if( pw_data->len > 0 )
8812 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008813 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8814 expected_status_set_password_key );
8815 if( expected_status_set_password_key != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008816 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008817 }
8818
Neil Armstrong707d9572022-06-08 17:31:49 +02008819 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8820 PSA_ERROR_INVALID_ARGUMENT );
8821 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8822 PSA_ERROR_INVALID_ARGUMENT );
8823
8824 const uint8_t unsupported_id[] = "abcd";
8825
8826 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8827 PSA_ERROR_NOT_SUPPORTED );
8828 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8829 PSA_ERROR_NOT_SUPPORTED );
8830
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008831 const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive,
8832 PSA_PAKE_STEP_KEY_SHARE );
8833 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive,
8834 PSA_PAKE_STEP_ZK_PUBLIC );
8835 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive,
8836 PSA_PAKE_STEP_ZK_PROOF );
8837
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008838 /* First round */
8839 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008840 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008841 /* Invalid parameters (input) */
8842 op_copy = operation;
8843 TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008844 NULL, 0 ),
8845 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008846 /* Invalid parameters (step) */
8847 op_copy = operation;
8848 TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008849 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008850 PSA_ERROR_INVALID_ARGUMENT );
8851 /* Invalid first step */
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008852 op_copy = operation;
8853 TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008854 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008855 PSA_ERROR_BAD_STATE );
8856
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008857 /* Possibly valid */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008858 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008859 output_buffer, size_key_share ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008860 expected_status_input_output);
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008861
Neil Armstrong2a73f212022-09-06 11:34:54 +02008862 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008863 {
8864 /* Buffer too large */
8865 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008866 output_buffer, size_zk_public + 1 ),
8867 PSA_ERROR_INVALID_ARGUMENT );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008868
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008869 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008870 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008871 output_buffer, size_zk_public ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008872 PSA_ERROR_BAD_STATE );
8873 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008874 }
8875 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008876 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008877 /* Invalid parameters (output) */
8878 op_copy = operation;
8879 TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008880 NULL, 0, NULL ),
8881 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008882 op_copy = operation;
8883 /* Invalid parameters (step) */
8884 TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008885 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008886 PSA_ERROR_INVALID_ARGUMENT );
8887 /* Invalid first step */
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008888 op_copy = operation;
8889 TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008890 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008891 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008892
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008893 /* Possibly valid */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008894 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008895 output_buffer, buf_size, &output_len ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008896 expected_status_input_output );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008897
Neil Armstrong2a73f212022-09-06 11:34:54 +02008898 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008899 {
8900 TEST_ASSERT( output_len > 0 );
8901
8902 /* Buffer too small */
8903 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008904 output_buffer, size_zk_public - 1, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008905 PSA_ERROR_BUFFER_TOO_SMALL );
8906
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008907 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008908 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008909 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008910 PSA_ERROR_BAD_STATE );
8911 }
8912 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008913
8914exit:
8915 PSA_ASSERT( psa_destroy_key( key ) );
8916 PSA_ASSERT( psa_pake_abort( &operation ) );
8917 mbedtls_free( output_buffer );
8918 PSA_DONE( );
8919}
8920/* END_CASE */
8921
Neil Armstronga557cb82022-06-10 08:58:32 +02008922/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008923void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8924 int client_input_first, int inject_error,
8925 data_t *pw_data )
8926{
8927 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8928 psa_pake_operation_t server = psa_pake_operation_init();
8929 psa_pake_operation_t client = psa_pake_operation_init();
8930 psa_algorithm_t alg = alg_arg;
8931 psa_algorithm_t hash_alg = hash_arg;
8932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8934
8935 PSA_INIT( );
8936
8937 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8938 psa_set_key_algorithm( &attributes, alg );
8939 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8940 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8941 &key ) );
8942
8943 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8944 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8945 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8946
8947
8948 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8949 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8950
8951 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8952 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8953
8954 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8955 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8956
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008957 ecjpake_do_round( alg, primitive_arg, &server, &client,
8958 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008959
8960 if( inject_error == 1 || inject_error == 2 )
8961 goto exit;
8962
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008963 ecjpake_do_round( alg, primitive_arg, &server, &client,
8964 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008965
8966exit:
8967 psa_destroy_key( key );
8968 psa_pake_abort( &server );
8969 psa_pake_abort( &client );
8970 PSA_DONE( );
8971}
8972/* END_CASE */
8973
8974/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008975void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008976 int derive_alg_arg, data_t *pw_data,
8977 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008978{
8979 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8980 psa_pake_operation_t server = psa_pake_operation_init();
8981 psa_pake_operation_t client = psa_pake_operation_init();
8982 psa_algorithm_t alg = alg_arg;
8983 psa_algorithm_t hash_alg = hash_arg;
8984 psa_algorithm_t derive_alg = derive_alg_arg;
8985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8987 psa_key_derivation_operation_t server_derive =
8988 PSA_KEY_DERIVATION_OPERATION_INIT;
8989 psa_key_derivation_operation_t client_derive =
8990 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008991
8992 PSA_INIT( );
8993
Neil Armstrongd597bc72022-05-25 11:28:39 +02008994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8995 psa_set_key_algorithm( &attributes, alg );
8996 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8997 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8998 &key ) );
8999
9000 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9001 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9002 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9003
Neil Armstrong1e855602022-06-15 11:32:11 +02009004 /* Get shared key */
9005 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
9006 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
9007
9008 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
9009 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
9010 {
9011 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
9012 PSA_KEY_DERIVATION_INPUT_SEED,
9013 (const uint8_t*) "", 0) );
9014 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
9015 PSA_KEY_DERIVATION_INPUT_SEED,
9016 (const uint8_t*) "", 0) );
9017 }
9018
Neil Armstrongd597bc72022-05-25 11:28:39 +02009019 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9020 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9021
9022 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9023 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9024
9025 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9026 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9027
Neil Armstrong1e855602022-06-15 11:32:11 +02009028 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9029 PSA_ERROR_BAD_STATE );
9030 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9031 PSA_ERROR_BAD_STATE );
9032
Neil Armstrongf983caf2022-06-15 15:27:48 +02009033 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009034 ecjpake_do_round( alg, primitive_arg, &server, &client,
9035 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009036
Neil Armstrong1e855602022-06-15 11:32:11 +02009037 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9038 PSA_ERROR_BAD_STATE );
9039 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9040 PSA_ERROR_BAD_STATE );
9041
Neil Armstrongf983caf2022-06-15 15:27:48 +02009042 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009043 ecjpake_do_round( alg, primitive_arg, &server, &client,
9044 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009045
Neil Armstrongd597bc72022-05-25 11:28:39 +02009046 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9047 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9048
9049exit:
9050 psa_key_derivation_abort( &server_derive );
9051 psa_key_derivation_abort( &client_derive );
9052 psa_destroy_key( key );
9053 psa_pake_abort( &server );
9054 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009055 PSA_DONE( );
9056}
9057/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009058
9059/* BEGIN_CASE */
9060void ecjpake_size_macros( )
9061{
9062 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9063 const size_t bits = 256;
9064 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9065 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9066 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9067 PSA_ECC_FAMILY_SECP_R1 );
9068
9069 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9070 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9071 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9072 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9073 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9074 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9075 /* The output for ZK_PROOF is the same bitsize as the curve */
9076 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9077 PSA_BITS_TO_BYTES( bits ) );
9078
9079 /* Input sizes are the same as output sizes */
9080 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9081 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9082 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9083 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9084 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9085 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9086
9087 /* These inequalities will always hold even when other PAKEs are added */
9088 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9089 PSA_PAKE_OUTPUT_MAX_SIZE );
9090 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9091 PSA_PAKE_OUTPUT_MAX_SIZE );
9092 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9093 PSA_PAKE_OUTPUT_MAX_SIZE );
9094 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9095 PSA_PAKE_INPUT_MAX_SIZE );
9096 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9097 PSA_PAKE_INPUT_MAX_SIZE );
9098 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9099 PSA_PAKE_INPUT_MAX_SIZE );
9100}
9101/* END_CASE */