blob: 1f3b3b64a6cdd95ccb01e627e68550169ce857e2 [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"
Gilles Peskine42649d92022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02009/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
10 * uses mbedtls_ctr_drbg internally. */
11#include "mbedtls/ctr_drbg.h"
12
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020013#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020014#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015
Gilles Peskine8e94efe2021-02-13 00:25:53 +010016#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010017#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010018#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053019#if defined(PSA_CRYPTO_DRIVER_TEST)
20#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053021#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
22#else
23#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053024#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020025#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010026
Gilles Peskine4023c012021-05-27 13:21:20 +020027/* If this comes up, it's a bug in the test code or in the test data. */
28#define UNUSED 0xdeadbeef
29
Dave Rodgman647791d2021-06-23 12:49:59 +010030/* Assert that an operation is (not) active.
31 * This serves as a proxy for checking if the operation is aborted. */
32#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
33#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010034
Przemek Stekiel7c795482022-11-15 22:26:12 +010035#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekielf82effa2022-11-21 15:10:32 +010036int ecjpake_operation_setup( psa_pake_operation_t *operation,
Przemek Stekiel7c795482022-11-15 22:26:12 +010037 psa_pake_cipher_suite_t *cipher_suite,
38 psa_pake_role_t role,
39 mbedtls_svc_key_id_t key,
40 size_t key_available )
41{
Przemek Stekielf82effa2022-11-21 15:10:32 +010042 PSA_ASSERT( psa_pake_abort( operation ) );
Przemek Stekiel7c795482022-11-15 22:26:12 +010043
Przemek Stekielf82effa2022-11-21 15:10:32 +010044 PSA_ASSERT( psa_pake_setup( operation, cipher_suite ) );
Przemek Stekiel7c795482022-11-15 22:26:12 +010045
Przemek Stekielf82effa2022-11-21 15:10:32 +010046 PSA_ASSERT( psa_pake_set_role( operation, role) );
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
48 if( key_available )
Przemek Stekielf82effa2022-11-21 15:10:32 +010049 PSA_ASSERT( psa_pake_set_password_key( operation, key ) );
50 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010051exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010052 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010053}
54#endif
55
Jaeden Amerof24c7f82018-06-27 17:20:43 +010056/** An invalid export length that will never be set by psa_export_key(). */
57static const size_t INVALID_EXPORT_LENGTH = ~0U;
58
Gilles Peskinea7aa4422018-08-14 15:17:54 +020059/** Test if a buffer contains a constant byte value.
60 *
61 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020062 *
63 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020064 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020065 * \param size Size of the buffer in bytes.
66 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020067 * \return 1 if the buffer is all-bits-zero.
68 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020069 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020070static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020071{
72 size_t i;
73 for( i = 0; i < size; i++ )
74 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020075 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020076 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020077 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020078 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020079}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010080#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020081/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
82static int asn1_write_10x( unsigned char **p,
83 unsigned char *start,
84 size_t bits,
85 unsigned char x )
86{
87 int ret;
88 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020089 if( bits == 0 )
90 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
91 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020092 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030093 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020094 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
95 *p -= len;
96 ( *p )[len-1] = x;
97 if( bits % 8 == 0 )
98 ( *p )[1] |= 1;
99 else
100 ( *p )[0] |= 1 << ( bits % 8 );
101 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
102 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
103 MBEDTLS_ASN1_INTEGER ) );
104 return( len );
105}
106
107static int construct_fake_rsa_key( unsigned char *buffer,
108 size_t buffer_size,
109 unsigned char **p,
110 size_t bits,
111 int keypair )
112{
113 size_t half_bits = ( bits + 1 ) / 2;
114 int ret;
115 int len = 0;
116 /* Construct something that looks like a DER encoding of
117 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
118 * RSAPrivateKey ::= SEQUENCE {
119 * version Version,
120 * modulus INTEGER, -- n
121 * publicExponent INTEGER, -- e
122 * privateExponent INTEGER, -- d
123 * prime1 INTEGER, -- p
124 * prime2 INTEGER, -- q
125 * exponent1 INTEGER, -- d mod (p-1)
126 * exponent2 INTEGER, -- d mod (q-1)
127 * coefficient INTEGER, -- (inverse of q) mod p
128 * otherPrimeInfos OtherPrimeInfos OPTIONAL
129 * }
130 * Or, for a public key, the same structure with only
131 * version, modulus and publicExponent.
132 */
133 *p = buffer + buffer_size;
134 if( keypair )
135 {
136 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
137 asn1_write_10x( p, buffer, half_bits, 1 ) );
138 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
139 asn1_write_10x( p, buffer, half_bits, 1 ) );
140 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
141 asn1_write_10x( p, buffer, half_bits, 1 ) );
142 MBEDTLS_ASN1_CHK_ADD( len, /* q */
143 asn1_write_10x( p, buffer, half_bits, 1 ) );
144 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
145 asn1_write_10x( p, buffer, half_bits, 3 ) );
146 MBEDTLS_ASN1_CHK_ADD( len, /* d */
147 asn1_write_10x( p, buffer, bits, 1 ) );
148 }
149 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
150 asn1_write_10x( p, buffer, 17, 1 ) );
151 MBEDTLS_ASN1_CHK_ADD( len, /* n */
152 asn1_write_10x( p, buffer, bits, 1 ) );
153 if( keypair )
154 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
155 mbedtls_asn1_write_int( p, buffer, 0 ) );
156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
157 {
158 const unsigned char tag =
159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
161 }
162 return( len );
163}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100164#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200165
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166int exercise_mac_setup( psa_key_type_t key_type,
167 const unsigned char *key_bytes,
168 size_t key_length,
169 psa_algorithm_t alg,
170 psa_mac_operation_t *operation,
171 psa_status_t *status )
172{
Ronald Cron5425a212020-08-04 14:58:35 +0200173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_set_key_algorithm( &attributes, alg );
178 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200179 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180
Ronald Cron5425a212020-08-04 14:58:35 +0200181 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100182 /* Whether setup succeeded or failed, abort must succeed. */
183 PSA_ASSERT( psa_mac_abort( operation ) );
184 /* If setup failed, reproduce the failure, so that the caller can
185 * test the resulting state of the operation object. */
186 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100187 {
Ronald Cron5425a212020-08-04 14:58:35 +0200188 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100189 }
190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 1 );
193
194exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 0 );
197}
198
199int exercise_cipher_setup( psa_key_type_t key_type,
200 const unsigned char *key_bytes,
201 size_t key_length,
202 psa_algorithm_t alg,
203 psa_cipher_operation_t *operation,
204 psa_status_t *status )
205{
Ronald Cron5425a212020-08-04 14:58:35 +0200206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100208
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200209 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
210 psa_set_key_algorithm( &attributes, alg );
211 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200212 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100213
Ronald Cron5425a212020-08-04 14:58:35 +0200214 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100215 /* Whether setup succeeded or failed, abort must succeed. */
216 PSA_ASSERT( psa_cipher_abort( operation ) );
217 /* If setup failed, reproduce the failure, so that the caller can
218 * test the resulting state of the operation object. */
219 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220 {
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100223 }
224
Ronald Cron5425a212020-08-04 14:58:35 +0200225 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100226 return( 1 );
227
228exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200229 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 return( 0 );
231}
232
Ronald Cron5425a212020-08-04 14:58:35 +0200233static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234{
235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200236 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 uint8_t buffer[1];
238 size_t length;
239 int ok = 0;
240
Ronald Cronecfb2372020-07-23 17:13:42 +0200241 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
243 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
244 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200245 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000246 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200247 TEST_EQUAL(
248 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
249 TEST_EQUAL(
250 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200251 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200252 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
253 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
254 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
255 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
256
Ronald Cron5425a212020-08-04 14:58:35 +0200257 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000258 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200259 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200260 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000261 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200262
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200263 ok = 1;
264
265exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100266 /*
267 * Key attributes may have been returned by psa_get_key_attributes()
268 * thus reset them as required.
269 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200270 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100271
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200272 return( ok );
273}
274
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200275/* Assert that a key isn't reported as having a slot number. */
276#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
277#define ASSERT_NO_SLOT_NUMBER( attributes ) \
278 do \
279 { \
280 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
281 TEST_EQUAL( psa_get_key_slot_number( \
282 attributes, \
283 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
284 PSA_ERROR_INVALID_ARGUMENT ); \
285 } \
286 while( 0 )
287#else /* MBEDTLS_PSA_CRYPTO_SE_C */
288#define ASSERT_NO_SLOT_NUMBER( attributes ) \
289 ( (void) 0 )
290#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
291
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100292/* An overapproximation of the amount of storage needed for a key of the
293 * given type and with the given content. The API doesn't make it easy
294 * to find a good value for the size. The current implementation doesn't
295 * care about the value anyway. */
296#define KEY_BITS_FROM_DATA( type, data ) \
297 ( data )->len
298
Darryl Green0c6575a2018-11-07 16:05:30 +0000299typedef enum {
300 IMPORT_KEY = 0,
301 GENERATE_KEY = 1,
302 DERIVE_KEY = 2
303} generate_method;
304
Paul Elliott33746aa2021-09-15 16:40:40 +0100305typedef enum
306{
307 DO_NOT_SET_LENGTHS = 0,
308 SET_LENGTHS_BEFORE_NONCE = 1,
309 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100310} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100311
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100312typedef enum
313{
314 USE_NULL_TAG = 0,
315 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100316} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100317
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318/*!
319 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100320 * \param key_type_arg Type of key passed in
321 * \param key_data The encryption / decryption key data
322 * \param alg_arg The type of algorithm used
323 * \param nonce Nonce data
324 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100325 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326 * feed additional data in to be encrypted /
327 * decrypted. If -1, no chunking.
328 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100329 * \param data_part_len_arg If not -1, the length of chunks to feed
330 * the data in to be encrypted / decrypted. If
331 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100332 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100333 * expected here, this controls whether or not
334 * to set lengths, and in what order with
335 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100338 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100339 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 */
342static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
343 int alg_arg,
344 data_t *nonce,
345 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100346 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100348 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100349 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100350 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100351 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100352 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100353{
354 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
355 psa_key_type_t key_type = key_type_arg;
356 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100357 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 unsigned char *output_data = NULL;
359 unsigned char *part_data = NULL;
360 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100362 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100363 size_t output_size = 0;
364 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 size_t output_length = 0;
366 size_t key_bits = 0;
367 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100368 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100369 size_t part_length = 0;
370 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100371 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100372 size_t ad_part_len = 0;
373 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100374 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
376 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
377
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100378 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100379 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380
Paul Elliottd3f82412021-06-16 16:52:21 +0100381 PSA_ASSERT( psa_crypto_init( ) );
382
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383 if( is_encrypt )
384 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
385 else
386 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
387
Paul Elliottd3f82412021-06-16 16:52:21 +0100388 psa_set_key_algorithm( &attributes, alg );
389 psa_set_key_type( &attributes, key_type );
390
391 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
392 &key ) );
393
394 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
395 key_bits = psa_get_key_bits( &attributes );
396
397 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
398
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100399 if( is_encrypt )
400 {
401 /* Tag gets written at end of buffer. */
402 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
403 ( input_data->len +
404 tag_length ) );
405 data_true_size = input_data->len;
406 }
407 else
408 {
409 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
410 ( input_data->len -
411 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100412
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100413 /* Do not want to attempt to decrypt tag. */
414 data_true_size = input_data->len - tag_length;
415 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100416
417 ASSERT_ALLOC( output_data, output_size );
418
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100419 if( is_encrypt )
420 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100421 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200422 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100423 }
424 else
425 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100426 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200427 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100428 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100429
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100430 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100431
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100432 if( is_encrypt )
433 status = psa_aead_encrypt_setup( &operation, key, alg );
434 else
435 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100436
437 /* If the operation is not supported, just skip and not fail in case the
438 * encryption involves a common limitation of cryptography hardwares and
439 * an alternative implementation. */
440 if( status == PSA_ERROR_NOT_SUPPORTED )
441 {
442 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
444 }
445
446 PSA_ASSERT( status );
447
Paul Elliott33746aa2021-09-15 16:40:40 +0100448 if( set_lengths_method == DO_NOT_SET_LENGTHS )
449 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
450 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100452 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
453 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100454 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
455 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100456 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100457 {
458 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
459
Paul Elliott33746aa2021-09-15 16:40:40 +0100460 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
461 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100463
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 {
466 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100468
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100469 for( part_offset = 0, part_count = 0;
470 part_offset < additional_data->len;
471 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100472 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100473 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100474 {
Paul Elliott329d5382021-07-22 17:10:45 +0100475 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100477 else if( additional_data->len - part_offset < ad_part_len )
478 {
479 part_length = additional_data->len - part_offset;
480 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 else
482 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100483 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100484 }
485
486 PSA_ASSERT( psa_aead_update_ad( &operation,
487 additional_data->x + part_offset,
488 part_length ) );
489
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 }
491 }
492 else
493 {
494 /* Pass additional data in one go. */
495 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
496 additional_data->len ) );
497 }
498
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100499 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 {
501 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100502 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100503 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100504 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100505
506 ASSERT_ALLOC( part_data, part_data_size );
507
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100508 for( part_offset = 0, part_count = 0;
509 part_offset < data_true_size;
510 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100512 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 {
Paul Elliott329d5382021-07-22 17:10:45 +0100514 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100516 else if( ( data_true_size - part_offset ) < data_part_len )
517 {
518 part_length = ( data_true_size - part_offset );
519 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 else
521 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100522 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100523 }
524
525 PSA_ASSERT( psa_aead_update( &operation,
526 ( input_data->x + part_offset ),
527 part_length, part_data,
528 part_data_size,
529 &output_part_length ) );
530
531 if( output_data && output_part_length )
532 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100533 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100534 output_part_length );
535 }
536
Paul Elliottd3f82412021-06-16 16:52:21 +0100537 output_length += output_part_length;
538 }
539 }
540 else
541 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100542 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100543 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100544 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100545 output_size, &output_length ) );
546 }
547
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100548 if( is_encrypt )
549 PSA_ASSERT( psa_aead_finish( &operation, final_data,
550 final_output_size,
551 &output_part_length,
552 tag_buffer, tag_length,
553 &tag_size ) );
554 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100555 {
Paul Elliott9961a662021-09-17 19:19:02 +0100556 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100557 final_output_size,
558 &output_part_length,
559 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100560 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100561 }
562
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100563 if( output_data && output_part_length )
564 memcpy( ( output_data + output_length ), final_data,
565 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100566
567 output_length += output_part_length;
568
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100569
570 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
571 * should be exact.*/
572 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100573 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100574 TEST_EQUAL( tag_length, tag_size );
575
576 if( output_data && tag_length )
577 memcpy( ( output_data + output_length ), tag_buffer,
578 tag_length );
579
580 output_length += tag_length;
581
582 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200583 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
584 input_data->len ) );
585 TEST_LE_U( output_length,
586 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100587 }
588 else
589 {
590 TEST_EQUAL( output_length,
591 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
592 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200593 TEST_LE_U( output_length,
594 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100595 }
596
Paul Elliottd3f82412021-06-16 16:52:21 +0100597
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100598 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100599 output_data, output_length );
600
Paul Elliottd3f82412021-06-16 16:52:21 +0100601
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100602 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100603
604exit:
605 psa_destroy_key( key );
606 psa_aead_abort( &operation );
607 mbedtls_free( output_data );
608 mbedtls_free( part_data );
609 mbedtls_free( final_data );
610 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100611
612 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100613}
614
Neil Armstrong4766f992022-02-28 16:23:59 +0100615/*!
616 * \brief Internal Function for MAC multipart tests.
617 * \param key_type_arg Type of key passed in
618 * \param key_data The encryption / decryption key data
619 * \param alg_arg The type of algorithm used
620 * \param input_data Data to encrypt / decrypt
621 * \param data_part_len_arg If not -1, the length of chunks to feed
622 * the data in to be encrypted / decrypted. If
623 * -1, no chunking
624 * \param expected_output Expected output
625 * \param is_verify If non-zero this is an verify operation.
626 * \param do_zero_parts If non-zero, interleave zero length chunks
627 * with normal length chunks.
628 * \return int Zero on failure, non-zero on success.
629 */
630static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
631 int alg_arg,
632 data_t *input_data,
633 int data_part_len_arg,
634 data_t *expected_output,
635 int is_verify,
636 int do_zero_parts )
637{
638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
639 psa_key_type_t key_type = key_type_arg;
640 psa_algorithm_t alg = alg_arg;
641 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
642 unsigned char mac[PSA_MAC_MAX_SIZE];
643 size_t part_offset = 0;
644 size_t part_length = 0;
645 size_t data_part_len = 0;
646 size_t mac_len = 0;
647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
648 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
649
650 int test_ok = 0;
651 size_t part_count = 0;
652
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100653 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100654
655 if( is_verify )
656 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
657 else
658 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
659
660 psa_set_key_algorithm( &attributes, alg );
661 psa_set_key_type( &attributes, key_type );
662
663 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
664 &key ) );
665
666 if( is_verify )
667 status = psa_mac_verify_setup( &operation, key, alg );
668 else
669 status = psa_mac_sign_setup( &operation, key, alg );
670
671 PSA_ASSERT( status );
672
673 if( data_part_len_arg != -1 )
674 {
675 /* Pass data in parts */
676 data_part_len = ( size_t ) data_part_len_arg;
677
678 for( part_offset = 0, part_count = 0;
679 part_offset < input_data->len;
680 part_offset += part_length, part_count++ )
681 {
682 if( do_zero_parts && ( part_count & 0x01 ) )
683 {
684 part_length = 0;
685 }
686 else if( ( input_data->len - part_offset ) < data_part_len )
687 {
688 part_length = ( input_data->len - part_offset );
689 }
690 else
691 {
692 part_length = data_part_len;
693 }
694
695 PSA_ASSERT( psa_mac_update( &operation,
696 ( input_data->x + part_offset ),
697 part_length ) );
698 }
699 }
700 else
701 {
702 /* Pass all data in one go. */
703 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
704 input_data->len ) );
705 }
706
707 if( is_verify )
708 {
709 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
710 expected_output->len ) );
711 }
712 else
713 {
714 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
715 PSA_MAC_MAX_SIZE, &mac_len ) );
716
717 ASSERT_COMPARE( expected_output->x, expected_output->len,
718 mac, mac_len );
719 }
720
721 test_ok = 1;
722
723exit:
724 psa_destroy_key( key );
725 psa_mac_abort( &operation );
726 PSA_DONE( );
727
728 return( test_ok );
729}
730
Neil Armstrong75673ab2022-06-15 17:39:01 +0200731#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200732static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
733 psa_pake_operation_t *server,
734 psa_pake_operation_t *client,
735 int client_input_first,
736 int round, int inject_error )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737{
738 unsigned char *buffer0 = NULL, *buffer1 = NULL;
739 size_t buffer_length = (
740 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
741 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
742 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200743 /* The output should be exactly this size according to the spec */
744 const size_t expected_size_key_share =
745 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
746 /* The output should be exactly this size according to the spec */
747 const size_t expected_size_zk_public =
748 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
749 /* The output can be smaller: the spec allows stripping leading zeroes */
750 const size_t max_expected_size_zk_proof =
751 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200752 size_t buffer0_off = 0;
753 size_t buffer1_off = 0;
754 size_t s_g1_len, s_g2_len, s_a_len;
755 size_t s_g1_off, s_g2_off, s_a_off;
756 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
757 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
758 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
759 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
760 size_t c_g1_len, c_g2_len, c_a_len;
761 size_t c_g1_off, c_g2_off, c_a_off;
762 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
763 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
764 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
765 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
766 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200767 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200768
769 ASSERT_ALLOC( buffer0, buffer_length );
770 ASSERT_ALLOC( buffer1, buffer_length );
771
772 switch( round )
773 {
774 case 1:
775 /* Server first round Output */
776 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
777 buffer0 + buffer0_off,
778 512 - buffer0_off, &s_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200779 TEST_EQUAL( s_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200780 s_g1_off = buffer0_off;
781 buffer0_off += s_g1_len;
782 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
783 buffer0 + buffer0_off,
784 512 - buffer0_off, &s_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200785 TEST_EQUAL( s_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200786 s_x1_pk_off = buffer0_off;
787 buffer0_off += s_x1_pk_len;
788 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
789 buffer0 + buffer0_off,
790 512 - buffer0_off, &s_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200791 TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200792 s_x1_pr_off = buffer0_off;
793 buffer0_off += s_x1_pr_len;
794 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
795 buffer0 + buffer0_off,
796 512 - buffer0_off, &s_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200797 TEST_EQUAL( s_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200798 s_g2_off = buffer0_off;
799 buffer0_off += s_g2_len;
800 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
801 buffer0 + buffer0_off,
802 512 - buffer0_off, &s_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200803 TEST_EQUAL( s_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200804 s_x2_pk_off = buffer0_off;
805 buffer0_off += s_x2_pk_len;
806 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
807 buffer0 + buffer0_off,
808 512 - buffer0_off, &s_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200809 TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200810 s_x2_pr_off = buffer0_off;
811 buffer0_off += s_x2_pr_len;
812
813 if( inject_error == 1 )
814 {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500815 buffer0[s_x1_pr_off + 8] ^= 1;
816 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200817 expected_status = PSA_ERROR_DATA_INVALID;
818 }
819
Neil Armstrong51009d72022-09-05 17:59:54 +0200820 /*
821 * When injecting errors in inputs, the implementation is
822 * free to detect it right away of with a delay.
823 * This permits delaying the error until the end of the input
824 * sequence, if no error appears then, this will be treated
825 * as an error.
826 */
827
Neil Armstrongf983caf2022-06-15 15:27:48 +0200828 if( client_input_first == 1 )
829 {
830 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200831 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
832 buffer0 + s_g1_off, s_g1_len );
833 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200834 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200835 TEST_EQUAL( status, expected_status );
836 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200837 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200838 else
839 {
840 TEST_EQUAL( status, PSA_SUCCESS );
841 }
842
843 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
844 buffer0 + s_x1_pk_off,
845 s_x1_pk_len );
846 if( inject_error == 1 && status != PSA_SUCCESS )
847 {
848 TEST_EQUAL( status, expected_status );
849 break;
850 }
851 else
852 {
853 TEST_EQUAL( status, PSA_SUCCESS );
854 }
855
856 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
857 buffer0 + s_x1_pr_off,
858 s_x1_pr_len );
859 if( inject_error == 1 && status != PSA_SUCCESS )
860 {
861 TEST_EQUAL( status, expected_status );
862 break;
863 }
864 else
865 {
866 TEST_EQUAL( status, PSA_SUCCESS );
867 }
868
869 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
870 buffer0 + s_g2_off,
871 s_g2_len );
872 if( inject_error == 1 && status != PSA_SUCCESS )
873 {
874 TEST_EQUAL( status, expected_status );
875 break;
876 }
877 else
878 {
879 TEST_EQUAL( status, PSA_SUCCESS );
880 }
881
882 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
883 buffer0 + s_x2_pk_off,
884 s_x2_pk_len );
885 if( inject_error == 1 && status != PSA_SUCCESS )
886 {
887 TEST_EQUAL( status, expected_status );
888 break;
889 }
890 else
891 {
892 TEST_EQUAL( status, PSA_SUCCESS );
893 }
894
895 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
896 buffer0 + s_x2_pr_off,
897 s_x2_pr_len );
898 if( inject_error == 1 && status != PSA_SUCCESS )
899 {
900 TEST_EQUAL( status, expected_status );
901 break;
902 }
903 else
904 {
905 TEST_EQUAL( status, PSA_SUCCESS );
906 }
907
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200908 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200909 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200910 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200911 }
912
913 /* Client first round Output */
914 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
915 buffer1 + buffer1_off,
916 512 - buffer1_off, &c_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200917 TEST_EQUAL( c_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200918 c_g1_off = buffer1_off;
919 buffer1_off += c_g1_len;
920 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
921 buffer1 + buffer1_off,
922 512 - buffer1_off, &c_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200923 TEST_EQUAL( c_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200924 c_x1_pk_off = buffer1_off;
925 buffer1_off += c_x1_pk_len;
926 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
927 buffer1 + buffer1_off,
928 512 - buffer1_off, &c_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200929 TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200930 c_x1_pr_off = buffer1_off;
931 buffer1_off += c_x1_pr_len;
932 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
933 buffer1 + buffer1_off,
934 512 - buffer1_off, &c_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200935 TEST_EQUAL( c_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200936 c_g2_off = buffer1_off;
937 buffer1_off += c_g2_len;
938 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
939 buffer1 + buffer1_off,
940 512 - buffer1_off, &c_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200941 TEST_EQUAL( c_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200942 c_x2_pk_off = buffer1_off;
943 buffer1_off += c_x2_pk_len;
944 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
945 buffer1 + buffer1_off,
946 512 - buffer1_off, &c_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200947 TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200948 c_x2_pr_off = buffer1_off;
949 buffer1_off += c_x2_pr_len;
950
951 if( client_input_first == 0 )
952 {
953 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200954 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
955 buffer0 + s_g1_off, s_g1_len );
956 if( inject_error == 1 && status != PSA_SUCCESS )
957 {
958 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200959 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200960 }
961 else
962 {
963 TEST_EQUAL( status, PSA_SUCCESS );
964 }
965
966 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
967 buffer0 + s_x1_pk_off,
968 s_x1_pk_len );
969 if( inject_error == 1 && status != PSA_SUCCESS )
970 {
971 TEST_EQUAL( status, expected_status );
972 break;
973 }
974 else
975 {
976 TEST_EQUAL( status, PSA_SUCCESS );
977 }
978
979 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
980 buffer0 + s_x1_pr_off,
981 s_x1_pr_len );
982 if( inject_error == 1 && status != PSA_SUCCESS )
983 {
984 TEST_EQUAL( status, expected_status );
985 break;
986 }
987 else
988 {
989 TEST_EQUAL( status, PSA_SUCCESS );
990 }
991
992 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
993 buffer0 + s_g2_off,
994 s_g2_len );
995 if( inject_error == 1 && status != PSA_SUCCESS )
996 {
997 TEST_EQUAL( status, expected_status );
998 break;
999 }
1000 else
1001 {
1002 TEST_EQUAL( status, PSA_SUCCESS );
1003 }
1004
1005 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1006 buffer0 + s_x2_pk_off,
1007 s_x2_pk_len );
1008 if( inject_error == 1 && status != PSA_SUCCESS )
1009 {
1010 TEST_EQUAL( status, expected_status );
1011 break;
1012 }
1013 else
1014 {
1015 TEST_EQUAL( status, PSA_SUCCESS );
1016 }
1017
1018 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1019 buffer0 + s_x2_pr_off,
1020 s_x2_pr_len );
1021 if( inject_error == 1 && status != PSA_SUCCESS )
1022 {
1023 TEST_EQUAL( status, expected_status );
1024 break;
1025 }
1026 else
1027 {
1028 TEST_EQUAL( status, PSA_SUCCESS );
1029 }
1030
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001031 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001032 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001033 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001034 }
1035
1036 if( inject_error == 2 )
1037 {
Andrzej Kurekc0182042022-11-08 08:12:56 -05001038 buffer1[c_x1_pr_off + 12] ^= 1;
1039 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001040 expected_status = PSA_ERROR_DATA_INVALID;
1041 }
1042
1043 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001044 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1045 buffer1 + c_g1_off, c_g1_len );
1046 if( inject_error == 2 && status != PSA_SUCCESS )
1047 {
1048 TEST_EQUAL( status, expected_status );
1049 break;
1050 }
1051 else
1052 {
1053 TEST_EQUAL( status, PSA_SUCCESS );
1054 }
1055
1056 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1057 buffer1 + c_x1_pk_off, c_x1_pk_len );
1058 if( inject_error == 2 && status != PSA_SUCCESS )
1059 {
1060 TEST_EQUAL( status, expected_status );
1061 break;
1062 }
1063 else
1064 {
1065 TEST_EQUAL( status, PSA_SUCCESS );
1066 }
1067
1068 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1069 buffer1 + c_x1_pr_off, c_x1_pr_len );
1070 if( inject_error == 2 && status != PSA_SUCCESS )
1071 {
1072 TEST_EQUAL( status, expected_status );
1073 break;
1074 }
1075 else
1076 {
1077 TEST_EQUAL( status, PSA_SUCCESS );
1078 }
1079
1080 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1081 buffer1 + c_g2_off, c_g2_len );
1082 if( inject_error == 2 && status != PSA_SUCCESS )
1083 {
1084 TEST_EQUAL( status, expected_status );
1085 break;
1086 }
1087 else
1088 {
1089 TEST_EQUAL( status, PSA_SUCCESS );
1090 }
1091
1092 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1093 buffer1 + c_x2_pk_off, c_x2_pk_len );
1094 if( inject_error == 2 && status != PSA_SUCCESS )
1095 {
1096 TEST_EQUAL( status, expected_status );
1097 break;
1098 }
1099 else
1100 {
1101 TEST_EQUAL( status, PSA_SUCCESS );
1102 }
1103
1104 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1105 buffer1 + c_x2_pr_off, c_x2_pr_len );
1106 if( inject_error == 2 && status != PSA_SUCCESS )
1107 {
1108 TEST_EQUAL( status, expected_status );
1109 break;
1110 }
1111 else
1112 {
1113 TEST_EQUAL( status, PSA_SUCCESS );
1114 }
1115
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001116 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001117 if( inject_error == 2 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001118 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001119
1120 break;
1121
1122 case 2:
1123 /* Server second round Output */
1124 buffer0_off = 0;
1125
1126 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1127 buffer0 + buffer0_off,
1128 512 - buffer0_off, &s_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001129 TEST_EQUAL( s_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001130 s_a_off = buffer0_off;
1131 buffer0_off += s_a_len;
1132 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1133 buffer0 + buffer0_off,
1134 512 - buffer0_off, &s_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001135 TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001136 s_x2s_pk_off = buffer0_off;
1137 buffer0_off += s_x2s_pk_len;
1138 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1139 buffer0 + buffer0_off,
1140 512 - buffer0_off, &s_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001141 TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001142 s_x2s_pr_off = buffer0_off;
1143 buffer0_off += s_x2s_pr_len;
1144
1145 if( inject_error == 3 )
1146 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001147 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001148 expected_status = PSA_ERROR_DATA_INVALID;
1149 }
1150
1151 if( client_input_first == 1 )
1152 {
1153 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001154 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1155 buffer0 + s_a_off, s_a_len );
1156 if( inject_error == 3 && status != PSA_SUCCESS )
1157 {
1158 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001159 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001160 }
1161 else
1162 {
1163 TEST_EQUAL( status, PSA_SUCCESS );
1164 }
1165
1166 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1167 buffer0 + s_x2s_pk_off,
1168 s_x2s_pk_len );
1169 if( inject_error == 3 && status != PSA_SUCCESS )
1170 {
1171 TEST_EQUAL( status, expected_status );
1172 break;
1173 }
1174 else
1175 {
1176 TEST_EQUAL( status, PSA_SUCCESS );
1177 }
1178
1179 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1180 buffer0 + s_x2s_pr_off,
1181 s_x2s_pr_len );
1182 if( inject_error == 3 && status != PSA_SUCCESS )
1183 {
1184 TEST_EQUAL( status, expected_status );
1185 break;
1186 }
1187 else
1188 {
1189 TEST_EQUAL( status, PSA_SUCCESS );
1190 }
1191
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001192 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001193 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001194 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001195 }
1196
1197 /* Client second round Output */
1198 buffer1_off = 0;
1199
1200 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1201 buffer1 + buffer1_off,
1202 512 - buffer1_off, &c_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001203 TEST_EQUAL( c_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001204 c_a_off = buffer1_off;
1205 buffer1_off += c_a_len;
1206 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1207 buffer1 + buffer1_off,
1208 512 - buffer1_off, &c_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001209 TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001210 c_x2s_pk_off = buffer1_off;
1211 buffer1_off += c_x2s_pk_len;
1212 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1213 buffer1 + buffer1_off,
1214 512 - buffer1_off, &c_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001215 TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001216 c_x2s_pr_off = buffer1_off;
1217 buffer1_off += c_x2s_pr_len;
1218
1219 if( client_input_first == 0 )
1220 {
1221 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001222 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1223 buffer0 + s_a_off, s_a_len );
1224 if( inject_error == 3 && status != PSA_SUCCESS )
1225 {
1226 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001227 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001228 }
1229 else
1230 {
1231 TEST_EQUAL( status, PSA_SUCCESS );
1232 }
1233
1234 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1235 buffer0 + s_x2s_pk_off,
1236 s_x2s_pk_len );
1237 if( inject_error == 3 && status != PSA_SUCCESS )
1238 {
1239 TEST_EQUAL( status, expected_status );
1240 break;
1241 }
1242 else
1243 {
1244 TEST_EQUAL( status, PSA_SUCCESS );
1245 }
1246
1247 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1248 buffer0 + s_x2s_pr_off,
1249 s_x2s_pr_len );
1250 if( inject_error == 3 && status != PSA_SUCCESS )
1251 {
1252 TEST_EQUAL( status, expected_status );
1253 break;
1254 }
1255 else
1256 {
1257 TEST_EQUAL( status, PSA_SUCCESS );
1258 }
1259
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001260 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001261 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001262 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001263 }
1264
1265 if( inject_error == 4 )
1266 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001267 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001268 expected_status = PSA_ERROR_DATA_INVALID;
1269 }
1270
1271 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001272 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1273 buffer1 + c_a_off, c_a_len );
1274 if( inject_error == 4 && status != PSA_SUCCESS )
1275 {
1276 TEST_EQUAL( status, expected_status );
1277 break;
1278 }
1279 else
1280 {
1281 TEST_EQUAL( status, PSA_SUCCESS );
1282 }
1283
1284 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1285 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1286 if( inject_error == 4 && status != PSA_SUCCESS )
1287 {
1288 TEST_EQUAL( status, expected_status );
1289 break;
1290 }
1291 else
1292 {
1293 TEST_EQUAL( status, PSA_SUCCESS );
1294 }
1295
1296 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1297 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1298 if( inject_error == 4 && status != PSA_SUCCESS )
1299 {
1300 TEST_EQUAL( status, expected_status );
1301 break;
1302 }
1303 else
1304 {
1305 TEST_EQUAL( status, PSA_SUCCESS );
1306 }
1307
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001308 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001309 if( inject_error == 4 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001310 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001311
1312 break;
1313
1314 }
1315
Neil Armstrongf983caf2022-06-15 15:27:48 +02001316exit:
1317 mbedtls_free( buffer0 );
1318 mbedtls_free( buffer1 );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001319}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001320#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001321
Gilles Peskinee59236f2018-01-27 23:32:46 +01001322/* END_HEADER */
1323
1324/* BEGIN_DEPENDENCIES
1325 * depends_on:MBEDTLS_PSA_CRYPTO_C
1326 * END_DEPENDENCIES
1327 */
1328
1329/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001330void static_checks( )
1331{
1332 size_t max_truncated_mac_size =
1333 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1334
1335 /* Check that the length for a truncated MAC always fits in the algorithm
1336 * encoding. The shifted mask is the maximum truncated value. The
1337 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001338 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001339}
1340/* END_CASE */
1341
1342/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001343void import_with_policy( int type_arg,
1344 int usage_arg, int alg_arg,
1345 int expected_status_arg )
1346{
1347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1348 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001350 psa_key_type_t type = type_arg;
1351 psa_key_usage_t usage = usage_arg;
1352 psa_algorithm_t alg = alg_arg;
1353 psa_status_t expected_status = expected_status_arg;
1354 const uint8_t key_material[16] = {0};
1355 psa_status_t status;
1356
1357 PSA_ASSERT( psa_crypto_init( ) );
1358
1359 psa_set_key_type( &attributes, type );
1360 psa_set_key_usage_flags( &attributes, usage );
1361 psa_set_key_algorithm( &attributes, alg );
1362
1363 status = psa_import_key( &attributes,
1364 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001365 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001366 TEST_EQUAL( status, expected_status );
1367 if( status != PSA_SUCCESS )
1368 goto exit;
1369
Ronald Cron5425a212020-08-04 14:58:35 +02001370 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001371 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001372 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001373 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001374 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001375 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001376
Ronald Cron5425a212020-08-04 14:58:35 +02001377 PSA_ASSERT( psa_destroy_key( key ) );
1378 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001379
1380exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001381 /*
1382 * Key attributes may have been returned by psa_get_key_attributes()
1383 * thus reset them as required.
1384 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001385 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001386
1387 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001388 PSA_DONE( );
1389}
1390/* END_CASE */
1391
1392/* BEGIN_CASE */
1393void import_with_data( data_t *data, int type_arg,
1394 int attr_bits_arg,
1395 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001396{
1397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1398 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001399 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001400 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001401 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001402 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001404
Gilles Peskine8817f612018-12-18 00:18:46 +01001405 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001406
Gilles Peskine4747d192019-04-17 15:05:45 +02001407 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001408 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001409
Ronald Cron5425a212020-08-04 14:58:35 +02001410 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001411 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001412 if( status != PSA_SUCCESS )
1413 goto exit;
1414
Ronald Cron5425a212020-08-04 14:58:35 +02001415 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001416 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001417 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001418 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001419 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001420
Ronald Cron5425a212020-08-04 14:58:35 +02001421 PSA_ASSERT( psa_destroy_key( key ) );
1422 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001423
1424exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001425 /*
1426 * Key attributes may have been returned by psa_get_key_attributes()
1427 * thus reset them as required.
1428 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001429 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001430
1431 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001432 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001433}
1434/* END_CASE */
1435
1436/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001437void import_large_key( int type_arg, int byte_size_arg,
1438 int expected_status_arg )
1439{
1440 psa_key_type_t type = type_arg;
1441 size_t byte_size = byte_size_arg;
1442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1443 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001444 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001445 psa_status_t status;
1446 uint8_t *buffer = NULL;
1447 size_t buffer_size = byte_size + 1;
1448 size_t n;
1449
Steven Cooreman69967ce2021-01-18 18:01:08 +01001450 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001451 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001452 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001453 memset( buffer, 'K', byte_size );
1454
1455 PSA_ASSERT( psa_crypto_init( ) );
1456
1457 /* Try importing the key */
1458 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1459 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001460 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001461 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001462 TEST_EQUAL( status, expected_status );
1463
1464 if( status == PSA_SUCCESS )
1465 {
Ronald Cron5425a212020-08-04 14:58:35 +02001466 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001467 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1468 TEST_EQUAL( psa_get_key_bits( &attributes ),
1469 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001470 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001471 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001472 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001473 for( n = 0; n < byte_size; n++ )
1474 TEST_EQUAL( buffer[n], 'K' );
1475 for( n = byte_size; n < buffer_size; n++ )
1476 TEST_EQUAL( buffer[n], 0 );
1477 }
1478
1479exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001480 /*
1481 * Key attributes may have been returned by psa_get_key_attributes()
1482 * thus reset them as required.
1483 */
1484 psa_reset_key_attributes( &attributes );
1485
Ronald Cron5425a212020-08-04 14:58:35 +02001486 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001487 PSA_DONE( );
1488 mbedtls_free( buffer );
1489}
1490/* END_CASE */
1491
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001492/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001493void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1494{
Ronald Cron5425a212020-08-04 14:58:35 +02001495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001496 size_t bits = bits_arg;
1497 psa_status_t expected_status = expected_status_arg;
1498 psa_status_t status;
1499 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001500 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001501 size_t buffer_size = /* Slight overapproximations */
1502 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001503 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001504 unsigned char *p;
1505 int ret;
1506 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001508
Gilles Peskine8817f612018-12-18 00:18:46 +01001509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001510 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001511
1512 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1513 bits, keypair ) ) >= 0 );
1514 length = ret;
1515
1516 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001517 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001518 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001519 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001520
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001521 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001522 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001523
1524exit:
1525 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001526 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001527}
1528/* END_CASE */
1529
1530/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001531void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001532 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001533 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301534 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001535 int expected_bits,
1536 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001537 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538 int canonical_input )
1539{
Ronald Cron5425a212020-08-04 14:58:35 +02001540 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001541 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001542 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001543 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301545 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001546 unsigned char *exported = NULL;
1547 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001548 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001549 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001552 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553
Moran Pekercb088e72018-07-17 17:36:59 +03001554 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001555 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001557 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001559
Archana4d7ae1d2021-07-07 02:50:22 +05301560 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 psa_set_key_usage_flags( &attributes, usage_arg );
1562 psa_set_key_algorithm( &attributes, alg );
1563 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001564
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001566 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567
1568 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001569 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001570 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1571 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001572 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001573
1574 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001575 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001576 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001577
1578 /* The exported length must be set by psa_export_key() to a value between 0
1579 * and export_size. On errors, the exported length must be 0. */
1580 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1581 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001582 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001583
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001584 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001585 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001586 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001587 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001588 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001589 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001590 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591
Gilles Peskineea38a922021-02-13 00:05:16 +01001592 /* Run sanity checks on the exported key. For non-canonical inputs,
1593 * this validates the canonical representations. For canonical inputs,
1594 * this doesn't directly validate the implementation, but it still helps
1595 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301596 if( !psa_key_lifetime_is_external( lifetime ) )
1597 {
1598 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1599 goto exit;
1600 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001601
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001602 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001603 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001604 else
1605 {
Ronald Cron5425a212020-08-04 14:58:35 +02001606 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001607 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001608 &key2 ) );
1609 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 reexported,
1611 export_size,
1612 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001613 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301614 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001615 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001617 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301618 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301619 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001620 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001621
1622destroy:
1623 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001624 PSA_ASSERT( psa_destroy_key( key ) );
1625 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001626
1627exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001628 /*
1629 * Key attributes may have been returned by psa_get_key_attributes()
1630 * thus reset them as required.
1631 */
1632 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301633 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001634 mbedtls_free( exported );
1635 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001636 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001637}
1638/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001639
Moran Pekerf709f4a2018-06-06 17:26:04 +03001640/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001641void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001642 int type_arg,
1643 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301644 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001645 int export_size_delta,
1646 int expected_export_status_arg,
1647 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001648{
Ronald Cron5425a212020-08-04 14:58:35 +02001649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001650 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001651 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001652 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001653 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301654 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001655 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001656 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001657 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001659
Gilles Peskine8817f612018-12-18 00:18:46 +01001660 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001661
Archana4d7ae1d2021-07-07 02:50:22 +05301662 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001663 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1664 psa_set_key_algorithm( &attributes, alg );
1665 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001666
1667 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001668 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001669
Gilles Peskine49c25912018-10-29 15:15:31 +01001670 /* Export the public key */
1671 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001672 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001673 exported, export_size,
1674 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001675 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001676 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001677 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001678 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001679 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001680 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001681 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001682 TEST_LE_U( expected_public_key->len,
1683 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1684 TEST_LE_U( expected_public_key->len,
1685 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1686 TEST_LE_U( expected_public_key->len,
1687 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001688 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1689 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001690 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001691exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001692 /*
1693 * Key attributes may have been returned by psa_get_key_attributes()
1694 * thus reset them as required.
1695 */
1696 psa_reset_key_attributes( &attributes );
1697
itayzafrir3e02b3b2018-06-12 17:06:52 +03001698 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001699 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001700 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001701}
1702/* END_CASE */
1703
Gilles Peskine20035e32018-02-03 22:44:14 +01001704/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001705void import_and_exercise_key( data_t *data,
1706 int type_arg,
1707 int bits_arg,
1708 int alg_arg )
1709{
Ronald Cron5425a212020-08-04 14:58:35 +02001710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001711 psa_key_type_t type = type_arg;
1712 size_t bits = bits_arg;
1713 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001714 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001716 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001717
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001719
Gilles Peskine4747d192019-04-17 15:05:45 +02001720 psa_set_key_usage_flags( &attributes, usage );
1721 psa_set_key_algorithm( &attributes, alg );
1722 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001723
1724 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001725 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001726
1727 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001728 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001729 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1730 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001731
1732 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001733 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001734 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001735
Ronald Cron5425a212020-08-04 14:58:35 +02001736 PSA_ASSERT( psa_destroy_key( key ) );
1737 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001738
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001739exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001740 /*
1741 * Key attributes may have been returned by psa_get_key_attributes()
1742 * thus reset them as required.
1743 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001744 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001745
1746 psa_reset_key_attributes( &attributes );
1747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001748 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001749}
1750/* END_CASE */
1751
1752/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001753void effective_key_attributes( int type_arg, int expected_type_arg,
1754 int bits_arg, int expected_bits_arg,
1755 int usage_arg, int expected_usage_arg,
1756 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001757{
Ronald Cron5425a212020-08-04 14:58:35 +02001758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001759 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001760 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001761 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001762 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001763 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001764 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001765 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001766 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001768
Gilles Peskine8817f612018-12-18 00:18:46 +01001769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001770
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001771 psa_set_key_usage_flags( &attributes, usage );
1772 psa_set_key_algorithm( &attributes, alg );
1773 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001774 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001775
Ronald Cron5425a212020-08-04 14:58:35 +02001776 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001777 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001778
Ronald Cron5425a212020-08-04 14:58:35 +02001779 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001780 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1781 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1782 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1783 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001784
1785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001786 /*
1787 * Key attributes may have been returned by psa_get_key_attributes()
1788 * thus reset them as required.
1789 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001790 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001791
1792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001793 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001794}
1795/* END_CASE */
1796
1797/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001798void check_key_policy( int type_arg, int bits_arg,
1799 int usage_arg, int alg_arg )
1800{
1801 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001802 usage_arg,
1803 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001804 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001805 goto exit;
1806}
1807/* END_CASE */
1808
1809/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001810void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001811{
1812 /* Test each valid way of initializing the object, except for `= {0}`, as
1813 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1814 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001815 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001816 psa_key_attributes_t func = psa_key_attributes_init( );
1817 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1818 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001819
1820 memset( &zero, 0, sizeof( zero ) );
1821
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001822 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1823 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1824 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001826 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1827 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1828 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1829
1830 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1831 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1832 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1833
1834 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1835 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1836 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1837
1838 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1839 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1840 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001841}
1842/* END_CASE */
1843
1844/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001845void mac_key_policy( int policy_usage_arg,
1846 int policy_alg_arg,
1847 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001848 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001849 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001850 int expected_status_sign_arg,
1851 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001852{
Ronald Cron5425a212020-08-04 14:58:35 +02001853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001854 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001855 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001856 psa_key_type_t key_type = key_type_arg;
1857 psa_algorithm_t policy_alg = policy_alg_arg;
1858 psa_algorithm_t exercise_alg = exercise_alg_arg;
1859 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001860 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001861 psa_status_t expected_status_sign = expected_status_sign_arg;
1862 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001863 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001864
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001866
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001867 psa_set_key_usage_flags( &attributes, policy_usage );
1868 psa_set_key_algorithm( &attributes, policy_alg );
1869 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001870
Gilles Peskine049c7532019-05-15 20:22:09 +02001871 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001872 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001873
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001874 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1875 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001876
Ronald Cron5425a212020-08-04 14:58:35 +02001877 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001878 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001879
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001880 /* Calculate the MAC, one-shot case. */
1881 uint8_t input[128] = {0};
1882 size_t mac_len;
1883 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1884 input, 128,
1885 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1886 expected_status_sign );
1887
Neil Armstrong3af9b972022-02-07 12:20:21 +01001888 /* Calculate the MAC, multi-part case. */
1889 PSA_ASSERT( psa_mac_abort( &operation ) );
1890 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1891 if( status == PSA_SUCCESS )
1892 {
1893 status = psa_mac_update( &operation, input, 128 );
1894 if( status == PSA_SUCCESS )
1895 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1896 &mac_len ),
1897 expected_status_sign );
1898 else
1899 TEST_EQUAL( status, expected_status_sign );
1900 }
1901 else
1902 {
1903 TEST_EQUAL( status, expected_status_sign );
1904 }
1905 PSA_ASSERT( psa_mac_abort( &operation ) );
1906
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001907 /* Verify correct MAC, one-shot case. */
1908 status = psa_mac_verify( key, exercise_alg, input, 128,
1909 mac, mac_len );
1910
1911 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1912 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001913 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001914 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001915
Neil Armstrong3af9b972022-02-07 12:20:21 +01001916 /* Verify correct MAC, multi-part case. */
1917 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1918 if( status == PSA_SUCCESS )
1919 {
1920 status = psa_mac_update( &operation, input, 128 );
1921 if( status == PSA_SUCCESS )
1922 {
1923 status = psa_mac_verify_finish( &operation, mac, mac_len );
1924 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1925 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1926 else
1927 TEST_EQUAL( status, expected_status_verify );
1928 }
1929 else
1930 {
1931 TEST_EQUAL( status, expected_status_verify );
1932 }
1933 }
1934 else
1935 {
1936 TEST_EQUAL( status, expected_status_verify );
1937 }
1938
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001939 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001940
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001942 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001943 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944
1945exit:
1946 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001947 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001948 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001949}
1950/* END_CASE */
1951
1952/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001953void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001954 int policy_alg,
1955 int key_type,
1956 data_t *key_data,
1957 int exercise_alg )
1958{
Ronald Cron5425a212020-08-04 14:58:35 +02001959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001961 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001962 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001963 size_t output_buffer_size = 0;
1964 size_t input_buffer_size = 0;
1965 size_t output_length = 0;
1966 uint8_t *output = NULL;
1967 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968 psa_status_t status;
1969
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001970 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1971 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1972 input_buffer_size );
1973
1974 ASSERT_ALLOC( input, input_buffer_size );
1975 ASSERT_ALLOC( output, output_buffer_size );
1976
Gilles Peskine8817f612018-12-18 00:18:46 +01001977 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001978
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001979 psa_set_key_usage_flags( &attributes, policy_usage );
1980 psa_set_key_algorithm( &attributes, policy_alg );
1981 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982
Gilles Peskine049c7532019-05-15 20:22:09 +02001983 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001984 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001986 /* Check if no key usage flag implication is done */
1987 TEST_EQUAL( policy_usage,
1988 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001989
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001990 /* Encrypt check, one-shot */
1991 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1992 output, output_buffer_size,
1993 &output_length);
1994 if( policy_alg == exercise_alg &&
1995 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1996 PSA_ASSERT( status );
1997 else
1998 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1999
2000 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002001 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002 if( policy_alg == exercise_alg &&
2003 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002004 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002006 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002007 psa_cipher_abort( &operation );
2008
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002009 /* Decrypt check, one-shot */
2010 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
2011 input, input_buffer_size,
2012 &output_length);
2013 if( policy_alg == exercise_alg &&
2014 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
2015 PSA_ASSERT( status );
2016 else
2017 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2018
2019 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002020 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021 if( policy_alg == exercise_alg &&
2022 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002023 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002025 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026
2027exit:
2028 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002029 mbedtls_free( input );
2030 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002031 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002032 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033}
2034/* END_CASE */
2035
2036/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002037void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038 int policy_alg,
2039 int key_type,
2040 data_t *key_data,
2041 int nonce_length_arg,
2042 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002043 int exercise_alg,
2044 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045{
Ronald Cron5425a212020-08-04 14:58:35 +02002046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002048 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002049 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002051 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002052 unsigned char nonce[16] = {0};
2053 size_t nonce_length = nonce_length_arg;
2054 unsigned char tag[16];
2055 size_t tag_length = tag_length_arg;
2056 size_t output_length;
2057
Gilles Peskine7be11a72022-04-14 00:12:57 +02002058 TEST_LE_U( nonce_length, sizeof( nonce ) );
2059 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002060
Gilles Peskine8817f612018-12-18 00:18:46 +01002061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002062
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002063 psa_set_key_usage_flags( &attributes, policy_usage );
2064 psa_set_key_algorithm( &attributes, policy_alg );
2065 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066
Gilles Peskine049c7532019-05-15 20:22:09 +02002067 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002068 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002070 /* Check if no key usage implication is done */
2071 TEST_EQUAL( policy_usage,
2072 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002073
Neil Armstrong752d8112022-02-07 14:51:11 +01002074 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002075 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002076 nonce, nonce_length,
2077 NULL, 0,
2078 NULL, 0,
2079 tag, tag_length,
2080 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002081 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2082 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002084 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085
Neil Armstrong752d8112022-02-07 14:51:11 +01002086 /* Encrypt check, multi-part */
2087 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2088 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2089 TEST_EQUAL( status, expected_status );
2090 else
2091 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2092
2093 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002095 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096 nonce, nonce_length,
2097 NULL, 0,
2098 tag, tag_length,
2099 NULL, 0,
2100 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002101 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2102 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2103 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002104 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002106 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107
Neil Armstrong752d8112022-02-07 14:51:11 +01002108 /* Decrypt check, multi-part */
2109 PSA_ASSERT( psa_aead_abort( &operation ) );
2110 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2111 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2112 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2113 else
2114 TEST_EQUAL( status, expected_status );
2115
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002117 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002118 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002119 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002120}
2121/* END_CASE */
2122
2123/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002124void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125 int policy_alg,
2126 int key_type,
2127 data_t *key_data,
2128 int exercise_alg )
2129{
Ronald Cron5425a212020-08-04 14:58:35 +02002130 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002132 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002133 psa_status_t status;
2134 size_t key_bits;
2135 size_t buffer_length;
2136 unsigned char *buffer = NULL;
2137 size_t output_length;
2138
Gilles Peskine8817f612018-12-18 00:18:46 +01002139 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002141 psa_set_key_usage_flags( &attributes, policy_usage );
2142 psa_set_key_algorithm( &attributes, policy_alg );
2143 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002144
Gilles Peskine049c7532019-05-15 20:22:09 +02002145 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002146 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002148 /* Check if no key usage implication is done */
2149 TEST_EQUAL( policy_usage,
2150 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002151
Ronald Cron5425a212020-08-04 14:58:35 +02002152 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002153 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002154 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2155 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002156 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002157
Ronald Cron5425a212020-08-04 14:58:35 +02002158 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159 NULL, 0,
2160 NULL, 0,
2161 buffer, buffer_length,
2162 &output_length );
2163 if( policy_alg == exercise_alg &&
2164 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002165 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002166 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002167 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002168
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002169 if( buffer_length != 0 )
2170 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002171 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002172 buffer, buffer_length,
2173 NULL, 0,
2174 buffer, buffer_length,
2175 &output_length );
2176 if( policy_alg == exercise_alg &&
2177 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002178 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002180 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002181
2182exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002183 /*
2184 * Key attributes may have been returned by psa_get_key_attributes()
2185 * thus reset them as required.
2186 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002187 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002188
2189 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002190 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191 mbedtls_free( buffer );
2192}
2193/* END_CASE */
2194
2195/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002196void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002197 int policy_alg,
2198 int key_type,
2199 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002200 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002201 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002202 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203{
Ronald Cron5425a212020-08-04 14:58:35 +02002204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002206 psa_key_usage_t policy_usage = policy_usage_arg;
2207 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002209 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2210 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2211 * compatible with the policy and `payload_length_arg` is supposed to be
2212 * a valid input length to sign. If `payload_length_arg <= 0`,
2213 * `exercise_alg` is supposed to be forbidden by the policy. */
2214 int compatible_alg = payload_length_arg > 0;
2215 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002216 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002217 size_t signature_length;
2218
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002219 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002220 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002221 TEST_EQUAL( expected_usage,
2222 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002223
Gilles Peskine8817f612018-12-18 00:18:46 +01002224 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002225
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002226 psa_set_key_usage_flags( &attributes, policy_usage );
2227 psa_set_key_algorithm( &attributes, policy_alg );
2228 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002229
Gilles Peskine049c7532019-05-15 20:22:09 +02002230 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002231 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002232
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002233 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2234
Ronald Cron5425a212020-08-04 14:58:35 +02002235 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002236 payload, payload_length,
2237 signature, sizeof( signature ),
2238 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002239 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002241 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002242 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002243
2244 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002245 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002246 payload, payload_length,
2247 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002248 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002249 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002250 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002251 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002252
Gilles Peskinef7b41372021-09-22 16:15:05 +02002253 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002254 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002255 {
2256 status = psa_sign_message( key, exercise_alg,
2257 payload, payload_length,
2258 signature, sizeof( signature ),
2259 &signature_length );
2260 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2261 PSA_ASSERT( status );
2262 else
2263 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2264
2265 memset( signature, 0, sizeof( signature ) );
2266 status = psa_verify_message( key, exercise_alg,
2267 payload, payload_length,
2268 signature, sizeof( signature ) );
2269 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2270 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2271 else
2272 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2273 }
2274
Gilles Peskined5b33222018-06-18 22:20:03 +02002275exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002277 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002278}
2279/* END_CASE */
2280
Janos Follathba3fab92019-06-11 14:50:16 +01002281/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002282void derive_key_policy( int policy_usage,
2283 int policy_alg,
2284 int key_type,
2285 data_t *key_data,
2286 int exercise_alg )
2287{
Ronald Cron5425a212020-08-04 14:58:35 +02002288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002290 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002291 psa_status_t status;
2292
Gilles Peskine8817f612018-12-18 00:18:46 +01002293 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002294
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002295 psa_set_key_usage_flags( &attributes, policy_usage );
2296 psa_set_key_algorithm( &attributes, policy_alg );
2297 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002298
Gilles Peskine049c7532019-05-15 20:22:09 +02002299 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002300 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002301
Janos Follathba3fab92019-06-11 14:50:16 +01002302 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2303
2304 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2305 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002306 {
Janos Follathba3fab92019-06-11 14:50:16 +01002307 PSA_ASSERT( psa_key_derivation_input_bytes(
2308 &operation,
2309 PSA_KEY_DERIVATION_INPUT_SEED,
2310 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002311 }
Janos Follathba3fab92019-06-11 14:50:16 +01002312
2313 status = psa_key_derivation_input_key( &operation,
2314 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002315 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002316
Gilles Peskineea0fb492018-07-12 17:17:20 +02002317 if( policy_alg == exercise_alg &&
2318 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002319 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002320 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002321 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002322
2323exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002324 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002325 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002326 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002327}
2328/* END_CASE */
2329
2330/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002331void agreement_key_policy( int policy_usage,
2332 int policy_alg,
2333 int key_type_arg,
2334 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002335 int exercise_alg,
2336 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002337{
Ronald Cron5425a212020-08-04 14:58:35 +02002338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002340 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002341 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002342 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002343 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002344
Gilles Peskine8817f612018-12-18 00:18:46 +01002345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002347 psa_set_key_usage_flags( &attributes, policy_usage );
2348 psa_set_key_algorithm( &attributes, policy_alg );
2349 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002350
Gilles Peskine049c7532019-05-15 20:22:09 +02002351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002352 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002353
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002354 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002355 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002356
Steven Cooremance48e852020-10-05 16:02:45 +02002357 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002358
2359exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002360 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002361 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002362 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002363}
2364/* END_CASE */
2365
2366/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002367void key_policy_alg2( int key_type_arg, data_t *key_data,
2368 int usage_arg, int alg_arg, int alg2_arg )
2369{
Ronald Cron5425a212020-08-04 14:58:35 +02002370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002371 psa_key_type_t key_type = key_type_arg;
2372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2373 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2374 psa_key_usage_t usage = usage_arg;
2375 psa_algorithm_t alg = alg_arg;
2376 psa_algorithm_t alg2 = alg2_arg;
2377
2378 PSA_ASSERT( psa_crypto_init( ) );
2379
2380 psa_set_key_usage_flags( &attributes, usage );
2381 psa_set_key_algorithm( &attributes, alg );
2382 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2383 psa_set_key_type( &attributes, key_type );
2384 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002385 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002386
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002387 /* Update the usage flags to obtain implicit usage flags */
2388 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002389 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002390 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2391 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2392 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2393
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002394 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002395 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002396 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002397 goto exit;
2398
2399exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002400 /*
2401 * Key attributes may have been returned by psa_get_key_attributes()
2402 * thus reset them as required.
2403 */
2404 psa_reset_key_attributes( &got_attributes );
2405
Ronald Cron5425a212020-08-04 14:58:35 +02002406 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002407 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002408}
2409/* END_CASE */
2410
2411/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002412void raw_agreement_key_policy( int policy_usage,
2413 int policy_alg,
2414 int key_type_arg,
2415 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002416 int exercise_alg,
2417 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002418{
Ronald Cron5425a212020-08-04 14:58:35 +02002419 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002421 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002422 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002423 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002424 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002425
2426 PSA_ASSERT( psa_crypto_init( ) );
2427
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002428 psa_set_key_usage_flags( &attributes, policy_usage );
2429 psa_set_key_algorithm( &attributes, policy_alg );
2430 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002431
Gilles Peskine049c7532019-05-15 20:22:09 +02002432 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002433 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002434
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002435 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002436
Steven Cooremance48e852020-10-05 16:02:45 +02002437 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002438
2439exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002440 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002441 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002442 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002443}
2444/* END_CASE */
2445
2446/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002447void copy_success( int source_usage_arg,
2448 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302449 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002450 int type_arg, data_t *material,
2451 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002452 int target_usage_arg,
2453 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302454 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002455 int expected_usage_arg,
2456 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002457{
Gilles Peskineca25db92019-04-19 11:43:08 +02002458 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2459 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002460 psa_key_usage_t expected_usage = expected_usage_arg;
2461 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002462 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302463 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2464 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002465 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2466 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002467 uint8_t *export_buffer = NULL;
2468
Gilles Peskine57ab7212019-01-28 13:03:09 +01002469 PSA_ASSERT( psa_crypto_init( ) );
2470
Gilles Peskineca25db92019-04-19 11:43:08 +02002471 /* Prepare the source key. */
2472 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2473 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002474 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002475 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302476 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002477 PSA_ASSERT( psa_import_key( &source_attributes,
2478 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002479 &source_key ) );
2480 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002481
Gilles Peskineca25db92019-04-19 11:43:08 +02002482 /* Prepare the target attributes. */
2483 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002484 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002485 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002486 }
Archana8a180362021-07-05 02:18:48 +05302487 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002488
Gilles Peskineca25db92019-04-19 11:43:08 +02002489 if( target_usage_arg != -1 )
2490 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2491 if( target_alg_arg != -1 )
2492 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002493 if( target_alg2_arg != -1 )
2494 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002495
Archana8a180362021-07-05 02:18:48 +05302496
Gilles Peskine57ab7212019-01-28 13:03:09 +01002497 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002498 PSA_ASSERT( psa_copy_key( source_key,
2499 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002500
2501 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002502 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002503
2504 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002505 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002506 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2507 psa_get_key_type( &target_attributes ) );
2508 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2509 psa_get_key_bits( &target_attributes ) );
2510 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2511 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002512 TEST_EQUAL( expected_alg2,
2513 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002514 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2515 {
2516 size_t length;
2517 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002518 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002519 material->len, &length ) );
2520 ASSERT_COMPARE( material->x, material->len,
2521 export_buffer, length );
2522 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002523
Archana8a180362021-07-05 02:18:48 +05302524 if( !psa_key_lifetime_is_external( target_lifetime ) )
2525 {
2526 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2527 goto exit;
2528 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2529 goto exit;
2530 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002531
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002533
2534exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002535 /*
2536 * Source and target key attributes may have been returned by
2537 * psa_get_key_attributes() thus reset them as required.
2538 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002539 psa_reset_key_attributes( &source_attributes );
2540 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002541
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002542 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002543 mbedtls_free( export_buffer );
2544}
2545/* END_CASE */
2546
2547/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002548void copy_fail( int source_usage_arg,
2549 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302550 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002551 int type_arg, data_t *material,
2552 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002553 int target_usage_arg,
2554 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002555 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002556 int expected_status_arg )
2557{
2558 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2559 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002560 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2561 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002562 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002563
2564 PSA_ASSERT( psa_crypto_init( ) );
2565
2566 /* Prepare the source key. */
2567 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2568 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002569 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002570 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302571 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002572 PSA_ASSERT( psa_import_key( &source_attributes,
2573 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002574 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002575
2576 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002577 psa_set_key_id( &target_attributes, key_id );
2578 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002579 psa_set_key_type( &target_attributes, target_type_arg );
2580 psa_set_key_bits( &target_attributes, target_bits_arg );
2581 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2582 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002583 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002584
2585 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002586 TEST_EQUAL( psa_copy_key( source_key,
2587 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002588 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002589
Ronald Cron5425a212020-08-04 14:58:35 +02002590 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002591
Gilles Peskine4a644642019-05-03 17:14:08 +02002592exit:
2593 psa_reset_key_attributes( &source_attributes );
2594 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002595 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002596}
2597/* END_CASE */
2598
2599/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002600void hash_operation_init( )
2601{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002602 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002603 /* Test each valid way of initializing the object, except for `= {0}`, as
2604 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2605 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002606 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002607 psa_hash_operation_t func = psa_hash_operation_init( );
2608 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2609 psa_hash_operation_t zero;
2610
2611 memset( &zero, 0, sizeof( zero ) );
2612
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002613 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002614 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2615 PSA_ERROR_BAD_STATE );
2616 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2617 PSA_ERROR_BAD_STATE );
2618 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2619 PSA_ERROR_BAD_STATE );
2620
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002621 /* A default hash operation should be abortable without error. */
2622 PSA_ASSERT( psa_hash_abort( &func ) );
2623 PSA_ASSERT( psa_hash_abort( &init ) );
2624 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002625}
2626/* END_CASE */
2627
2628/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629void hash_setup( int alg_arg,
2630 int expected_status_arg )
2631{
2632 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002633 uint8_t *output = NULL;
2634 size_t output_size = 0;
2635 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002636 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002637 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002638 psa_status_t status;
2639
Gilles Peskine8817f612018-12-18 00:18:46 +01002640 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641
Neil Armstrongedb20862022-02-07 15:47:44 +01002642 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002643 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002644 ASSERT_ALLOC( output, output_size );
2645
2646 status = psa_hash_compute( alg, NULL, 0,
2647 output, output_size, &output_length );
2648 TEST_EQUAL( status, expected_status );
2649
2650 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002651 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002652 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002653
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002654 /* Whether setup succeeded or failed, abort must succeed. */
2655 PSA_ASSERT( psa_hash_abort( &operation ) );
2656
2657 /* If setup failed, reproduce the failure, so as to
2658 * test the resulting state of the operation object. */
2659 if( status != PSA_SUCCESS )
2660 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2661
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002662 /* Now the operation object should be reusable. */
2663#if defined(KNOWN_SUPPORTED_HASH_ALG)
2664 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2665 PSA_ASSERT( psa_hash_abort( &operation ) );
2666#endif
2667
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002668exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002669 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002670 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002671}
2672/* END_CASE */
2673
2674/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002675void hash_compute_fail( int alg_arg, data_t *input,
2676 int output_size_arg, int expected_status_arg )
2677{
2678 psa_algorithm_t alg = alg_arg;
2679 uint8_t *output = NULL;
2680 size_t output_size = output_size_arg;
2681 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002682 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002683 psa_status_t expected_status = expected_status_arg;
2684 psa_status_t status;
2685
2686 ASSERT_ALLOC( output, output_size );
2687
2688 PSA_ASSERT( psa_crypto_init( ) );
2689
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002690 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002691 status = psa_hash_compute( alg, input->x, input->len,
2692 output, output_size, &output_length );
2693 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002694 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002695
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002696 /* Hash Compute, multi-part */
2697 status = psa_hash_setup( &operation, alg );
2698 if( status == PSA_SUCCESS )
2699 {
2700 status = psa_hash_update( &operation, input->x, input->len );
2701 if( status == PSA_SUCCESS )
2702 {
2703 status = psa_hash_finish( &operation, output, output_size,
2704 &output_length );
2705 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002706 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002707 else
2708 TEST_EQUAL( status, expected_status );
2709 }
2710 else
2711 {
2712 TEST_EQUAL( status, expected_status );
2713 }
2714 }
2715 else
2716 {
2717 TEST_EQUAL( status, expected_status );
2718 }
2719
Gilles Peskine0a749c82019-11-28 19:33:58 +01002720exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002721 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002722 mbedtls_free( output );
2723 PSA_DONE( );
2724}
2725/* END_CASE */
2726
2727/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002728void hash_compare_fail( int alg_arg, data_t *input,
2729 data_t *reference_hash,
2730 int expected_status_arg )
2731{
2732 psa_algorithm_t alg = alg_arg;
2733 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002734 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002735 psa_status_t status;
2736
2737 PSA_ASSERT( psa_crypto_init( ) );
2738
Neil Armstrong55a1be12022-02-07 11:23:20 +01002739 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002740 status = psa_hash_compare( alg, input->x, input->len,
2741 reference_hash->x, reference_hash->len );
2742 TEST_EQUAL( status, expected_status );
2743
Neil Armstrong55a1be12022-02-07 11:23:20 +01002744 /* Hash Compare, multi-part */
2745 status = psa_hash_setup( &operation, alg );
2746 if( status == PSA_SUCCESS )
2747 {
2748 status = psa_hash_update( &operation, input->x, input->len );
2749 if( status == PSA_SUCCESS )
2750 {
2751 status = psa_hash_verify( &operation, reference_hash->x,
2752 reference_hash->len );
2753 TEST_EQUAL( status, expected_status );
2754 }
2755 else
2756 {
2757 TEST_EQUAL( status, expected_status );
2758 }
2759 }
2760 else
2761 {
2762 TEST_EQUAL( status, expected_status );
2763 }
2764
Gilles Peskine88e08462020-01-28 20:43:00 +01002765exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002766 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002767 PSA_DONE( );
2768}
2769/* END_CASE */
2770
2771/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002772void hash_compute_compare( int alg_arg, data_t *input,
2773 data_t *expected_output )
2774{
2775 psa_algorithm_t alg = alg_arg;
2776 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2777 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002778 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002779 size_t i;
2780
2781 PSA_ASSERT( psa_crypto_init( ) );
2782
Neil Armstrongca30a002022-02-07 11:40:23 +01002783 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002784 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002785 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002786 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002787 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002788 ASSERT_COMPARE( output, output_length,
2789 expected_output->x, expected_output->len );
2790
Neil Armstrongca30a002022-02-07 11:40:23 +01002791 /* Compute with tight buffer, multi-part */
2792 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2793 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2794 PSA_ASSERT( psa_hash_finish( &operation, output,
2795 PSA_HASH_LENGTH( alg ),
2796 &output_length ) );
2797 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2798 ASSERT_COMPARE( output, output_length,
2799 expected_output->x, expected_output->len );
2800
2801 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002802 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2803 output, sizeof( output ),
2804 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002805 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002806 ASSERT_COMPARE( output, output_length,
2807 expected_output->x, expected_output->len );
2808
Neil Armstrongca30a002022-02-07 11:40:23 +01002809 /* Compute with larger buffer, multi-part */
2810 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2811 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2812 PSA_ASSERT( psa_hash_finish( &operation, output,
2813 sizeof( output ), &output_length ) );
2814 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2815 ASSERT_COMPARE( output, output_length,
2816 expected_output->x, expected_output->len );
2817
2818 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002819 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2820 output, output_length ) );
2821
Neil Armstrongca30a002022-02-07 11:40:23 +01002822 /* Compare with correct hash, multi-part */
2823 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2824 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2825 PSA_ASSERT( psa_hash_verify( &operation, output,
2826 output_length ) );
2827
2828 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002829 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2830 output, output_length + 1 ),
2831 PSA_ERROR_INVALID_SIGNATURE );
2832
Neil Armstrongca30a002022-02-07 11:40:23 +01002833 /* Compare with trailing garbage, multi-part */
2834 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2835 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2836 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2837 PSA_ERROR_INVALID_SIGNATURE );
2838
2839 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002840 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2841 output, output_length - 1 ),
2842 PSA_ERROR_INVALID_SIGNATURE );
2843
Neil Armstrongca30a002022-02-07 11:40:23 +01002844 /* Compare with truncated hash, multi-part */
2845 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2846 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2847 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2848 PSA_ERROR_INVALID_SIGNATURE );
2849
Gilles Peskine0a749c82019-11-28 19:33:58 +01002850 /* Compare with corrupted value */
2851 for( i = 0; i < output_length; i++ )
2852 {
Chris Jones9634bb12021-01-20 15:56:42 +00002853 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002854 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002855
2856 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002857 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2858 output, output_length ),
2859 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002860
2861 /* Multi-Part */
2862 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2863 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2864 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2865 PSA_ERROR_INVALID_SIGNATURE );
2866
Gilles Peskine0a749c82019-11-28 19:33:58 +01002867 output[i] ^= 1;
2868 }
2869
2870exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002871 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002872 PSA_DONE( );
2873}
2874/* END_CASE */
2875
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002876/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002877void hash_bad_order( )
2878{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002879 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002880 unsigned char input[] = "";
2881 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002882 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002883 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2884 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2885 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002886 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002887 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002888 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002889
Gilles Peskine8817f612018-12-18 00:18:46 +01002890 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002891
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002892 /* Call setup twice in a row. */
2893 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002894 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002895 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2896 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002897 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002898 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002899 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002900
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002901 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002902 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002903 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002904 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002905
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002906 /* Check that update calls abort on error. */
2907 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002908 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002909 ASSERT_OPERATION_IS_ACTIVE( operation );
2910 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2911 PSA_ERROR_BAD_STATE );
2912 ASSERT_OPERATION_IS_INACTIVE( operation );
2913 PSA_ASSERT( psa_hash_abort( &operation ) );
2914 ASSERT_OPERATION_IS_INACTIVE( operation );
2915
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002916 /* Call update after finish. */
2917 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2918 PSA_ASSERT( psa_hash_finish( &operation,
2919 hash, sizeof( hash ), &hash_len ) );
2920 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002921 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002922 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002923
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002924 /* Call verify without calling setup beforehand. */
2925 TEST_EQUAL( psa_hash_verify( &operation,
2926 valid_hash, sizeof( valid_hash ) ),
2927 PSA_ERROR_BAD_STATE );
2928 PSA_ASSERT( psa_hash_abort( &operation ) );
2929
2930 /* Call verify after finish. */
2931 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2932 PSA_ASSERT( psa_hash_finish( &operation,
2933 hash, sizeof( hash ), &hash_len ) );
2934 TEST_EQUAL( psa_hash_verify( &operation,
2935 valid_hash, sizeof( valid_hash ) ),
2936 PSA_ERROR_BAD_STATE );
2937 PSA_ASSERT( psa_hash_abort( &operation ) );
2938
2939 /* Call verify twice in a row. */
2940 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002941 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002942 PSA_ASSERT( psa_hash_verify( &operation,
2943 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002944 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002945 TEST_EQUAL( psa_hash_verify( &operation,
2946 valid_hash, sizeof( valid_hash ) ),
2947 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002948 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002949 PSA_ASSERT( psa_hash_abort( &operation ) );
2950
2951 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002952 TEST_EQUAL( psa_hash_finish( &operation,
2953 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002954 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002955 PSA_ASSERT( psa_hash_abort( &operation ) );
2956
2957 /* Call finish twice in a row. */
2958 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2959 PSA_ASSERT( psa_hash_finish( &operation,
2960 hash, sizeof( hash ), &hash_len ) );
2961 TEST_EQUAL( psa_hash_finish( &operation,
2962 hash, sizeof( hash ), &hash_len ),
2963 PSA_ERROR_BAD_STATE );
2964 PSA_ASSERT( psa_hash_abort( &operation ) );
2965
2966 /* Call finish after calling verify. */
2967 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2968 PSA_ASSERT( psa_hash_verify( &operation,
2969 valid_hash, sizeof( valid_hash ) ) );
2970 TEST_EQUAL( psa_hash_finish( &operation,
2971 hash, sizeof( hash ), &hash_len ),
2972 PSA_ERROR_BAD_STATE );
2973 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002974
2975exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002976 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002977}
2978/* END_CASE */
2979
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002980/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002981void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002982{
2983 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002984 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2985 * appended to it */
2986 unsigned char hash[] = {
2987 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2988 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2989 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002990 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002991 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002992
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002994
itayzafrir27e69452018-11-01 14:26:34 +02002995 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002997 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002998 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002999 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003000 ASSERT_OPERATION_IS_INACTIVE( operation );
3001 PSA_ASSERT( psa_hash_abort( &operation ) );
3002 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03003003
itayzafrir27e69452018-11-01 14:26:34 +02003004 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01003005 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003006 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003007 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03003008
itayzafrir27e69452018-11-01 14:26:34 +02003009 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003010 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003011 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003012 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03003013
itayzafrirec93d302018-10-18 18:01:10 +03003014exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003015 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03003016}
3017/* END_CASE */
3018
Ronald Cronee414c72021-03-18 18:50:08 +01003019/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003020void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03003021{
3022 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003023 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003024 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003025 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003026 size_t hash_len;
3027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003029
itayzafrir58028322018-10-25 10:22:01 +03003030 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003032 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003033 hash, expected_size - 1, &hash_len ),
3034 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003035
3036exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003037 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003038}
3039/* END_CASE */
3040
Ronald Cronee414c72021-03-18 18:50:08 +01003041/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003042void hash_clone_source_state( )
3043{
3044 psa_algorithm_t alg = PSA_ALG_SHA_256;
3045 unsigned char hash[PSA_HASH_MAX_SIZE];
3046 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3047 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3048 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3049 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3050 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3051 size_t hash_len;
3052
3053 PSA_ASSERT( psa_crypto_init( ) );
3054 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3055
3056 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3057 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3058 PSA_ASSERT( psa_hash_finish( &op_finished,
3059 hash, sizeof( hash ), &hash_len ) );
3060 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3061 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3062
3063 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3064 PSA_ERROR_BAD_STATE );
3065
3066 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3067 PSA_ASSERT( psa_hash_finish( &op_init,
3068 hash, sizeof( hash ), &hash_len ) );
3069 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3070 PSA_ASSERT( psa_hash_finish( &op_finished,
3071 hash, sizeof( hash ), &hash_len ) );
3072 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3073 PSA_ASSERT( psa_hash_finish( &op_aborted,
3074 hash, sizeof( hash ), &hash_len ) );
3075
3076exit:
3077 psa_hash_abort( &op_source );
3078 psa_hash_abort( &op_init );
3079 psa_hash_abort( &op_setup );
3080 psa_hash_abort( &op_finished );
3081 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003082 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003083}
3084/* END_CASE */
3085
Ronald Cronee414c72021-03-18 18:50:08 +01003086/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003087void hash_clone_target_state( )
3088{
3089 psa_algorithm_t alg = PSA_ALG_SHA_256;
3090 unsigned char hash[PSA_HASH_MAX_SIZE];
3091 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3092 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3093 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3094 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3095 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3096 size_t hash_len;
3097
3098 PSA_ASSERT( psa_crypto_init( ) );
3099
3100 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3101 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3102 PSA_ASSERT( psa_hash_finish( &op_finished,
3103 hash, sizeof( hash ), &hash_len ) );
3104 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3105 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3106
3107 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3108 PSA_ASSERT( psa_hash_finish( &op_target,
3109 hash, sizeof( hash ), &hash_len ) );
3110
3111 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3112 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3113 PSA_ERROR_BAD_STATE );
3114 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3115 PSA_ERROR_BAD_STATE );
3116
3117exit:
3118 psa_hash_abort( &op_target );
3119 psa_hash_abort( &op_init );
3120 psa_hash_abort( &op_setup );
3121 psa_hash_abort( &op_finished );
3122 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003123 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003124}
3125/* END_CASE */
3126
itayzafrir58028322018-10-25 10:22:01 +03003127/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003128void mac_operation_init( )
3129{
Jaeden Amero252ef282019-02-15 14:05:35 +00003130 const uint8_t input[1] = { 0 };
3131
Jaeden Amero769ce272019-01-04 11:48:03 +00003132 /* Test each valid way of initializing the object, except for `= {0}`, as
3133 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3134 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003135 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003136 psa_mac_operation_t func = psa_mac_operation_init( );
3137 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3138 psa_mac_operation_t zero;
3139
3140 memset( &zero, 0, sizeof( zero ) );
3141
Jaeden Amero252ef282019-02-15 14:05:35 +00003142 /* A freshly-initialized MAC operation should not be usable. */
3143 TEST_EQUAL( psa_mac_update( &func,
3144 input, sizeof( input ) ),
3145 PSA_ERROR_BAD_STATE );
3146 TEST_EQUAL( psa_mac_update( &init,
3147 input, sizeof( input ) ),
3148 PSA_ERROR_BAD_STATE );
3149 TEST_EQUAL( psa_mac_update( &zero,
3150 input, sizeof( input ) ),
3151 PSA_ERROR_BAD_STATE );
3152
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003153 /* A default MAC operation should be abortable without error. */
3154 PSA_ASSERT( psa_mac_abort( &func ) );
3155 PSA_ASSERT( psa_mac_abort( &init ) );
3156 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003157}
3158/* END_CASE */
3159
3160/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003161void mac_setup( int key_type_arg,
3162 data_t *key,
3163 int alg_arg,
3164 int expected_status_arg )
3165{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003166 psa_key_type_t key_type = key_type_arg;
3167 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003168 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003169 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003170 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3171#if defined(KNOWN_SUPPORTED_MAC_ALG)
3172 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3173#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003174
Gilles Peskine8817f612018-12-18 00:18:46 +01003175 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003176
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003177 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3178 &operation, &status ) )
3179 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003180 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003181
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003182 /* The operation object should be reusable. */
3183#if defined(KNOWN_SUPPORTED_MAC_ALG)
3184 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3185 smoke_test_key_data,
3186 sizeof( smoke_test_key_data ),
3187 KNOWN_SUPPORTED_MAC_ALG,
3188 &operation, &status ) )
3189 goto exit;
3190 TEST_EQUAL( status, PSA_SUCCESS );
3191#endif
3192
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003193exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003194 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003195}
3196/* END_CASE */
3197
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003198/* 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 +00003199void mac_bad_order( )
3200{
Ronald Cron5425a212020-08-04 14:58:35 +02003201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003202 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3203 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003204 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003205 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3206 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3207 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003209 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3210 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3211 size_t sign_mac_length = 0;
3212 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3213 const uint8_t verify_mac[] = {
3214 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3215 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3216 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3217
3218 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003220 psa_set_key_algorithm( &attributes, alg );
3221 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003222
Ronald Cron5425a212020-08-04 14:58:35 +02003223 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3224 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003225
Jaeden Amero252ef282019-02-15 14:05:35 +00003226 /* Call update without calling setup beforehand. */
3227 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3228 PSA_ERROR_BAD_STATE );
3229 PSA_ASSERT( psa_mac_abort( &operation ) );
3230
3231 /* Call sign finish without calling setup beforehand. */
3232 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3233 &sign_mac_length),
3234 PSA_ERROR_BAD_STATE );
3235 PSA_ASSERT( psa_mac_abort( &operation ) );
3236
3237 /* Call verify finish without calling setup beforehand. */
3238 TEST_EQUAL( psa_mac_verify_finish( &operation,
3239 verify_mac, sizeof( verify_mac ) ),
3240 PSA_ERROR_BAD_STATE );
3241 PSA_ASSERT( psa_mac_abort( &operation ) );
3242
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003243 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003244 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003245 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003246 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003247 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003248 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003249 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003250 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003251
Jaeden Amero252ef282019-02-15 14:05:35 +00003252 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003253 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003254 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3255 PSA_ASSERT( psa_mac_sign_finish( &operation,
3256 sign_mac, sizeof( sign_mac ),
3257 &sign_mac_length ) );
3258 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3259 PSA_ERROR_BAD_STATE );
3260 PSA_ASSERT( psa_mac_abort( &operation ) );
3261
3262 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003263 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003264 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3265 PSA_ASSERT( psa_mac_verify_finish( &operation,
3266 verify_mac, sizeof( verify_mac ) ) );
3267 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3268 PSA_ERROR_BAD_STATE );
3269 PSA_ASSERT( psa_mac_abort( &operation ) );
3270
3271 /* Call sign finish twice in a row. */
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 ) ) );
3274 PSA_ASSERT( psa_mac_sign_finish( &operation,
3275 sign_mac, sizeof( sign_mac ),
3276 &sign_mac_length ) );
3277 TEST_EQUAL( psa_mac_sign_finish( &operation,
3278 sign_mac, sizeof( sign_mac ),
3279 &sign_mac_length ),
3280 PSA_ERROR_BAD_STATE );
3281 PSA_ASSERT( psa_mac_abort( &operation ) );
3282
3283 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003284 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003285 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3286 PSA_ASSERT( psa_mac_verify_finish( &operation,
3287 verify_mac, sizeof( verify_mac ) ) );
3288 TEST_EQUAL( psa_mac_verify_finish( &operation,
3289 verify_mac, sizeof( verify_mac ) ),
3290 PSA_ERROR_BAD_STATE );
3291 PSA_ASSERT( psa_mac_abort( &operation ) );
3292
3293 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003294 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003295 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003296 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003297 TEST_EQUAL( psa_mac_verify_finish( &operation,
3298 verify_mac, sizeof( verify_mac ) ),
3299 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003300 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003301 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003302 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003303
3304 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003305 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003306 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003307 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003308 TEST_EQUAL( psa_mac_sign_finish( &operation,
3309 sign_mac, sizeof( sign_mac ),
3310 &sign_mac_length ),
3311 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003312 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003313 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003314 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003315
Ronald Cron5425a212020-08-04 14:58:35 +02003316 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003317
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003318exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003319 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003320}
3321/* END_CASE */
3322
3323/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003324void mac_sign_verify_multi( int key_type_arg,
3325 data_t *key_data,
3326 int alg_arg,
3327 data_t *input,
3328 int is_verify,
3329 data_t *expected_mac )
3330{
3331 size_t data_part_len = 0;
3332
3333 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3334 {
3335 /* Split data into length(data_part_len) parts. */
3336 mbedtls_test_set_step( 2000 + data_part_len );
3337
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003338 if( mac_multipart_internal_func( key_type_arg, key_data,
3339 alg_arg,
3340 input, data_part_len,
3341 expected_mac,
3342 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003343 break;
3344
3345 /* length(0) part, length(data_part_len) part, length(0) part... */
3346 mbedtls_test_set_step( 3000 + data_part_len );
3347
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003348 if( mac_multipart_internal_func( key_type_arg, key_data,
3349 alg_arg,
3350 input, data_part_len,
3351 expected_mac,
3352 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003353 break;
3354 }
3355
3356 /* Goto is required to silence warnings about unused labels, as we
3357 * don't actually do any test assertions in this function. */
3358 goto exit;
3359}
3360/* END_CASE */
3361
3362/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003364 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003365 int alg_arg,
3366 data_t *input,
3367 data_t *expected_mac )
3368{
Ronald Cron5425a212020-08-04 14:58:35 +02003369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003370 psa_key_type_t key_type = key_type_arg;
3371 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003372 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003374 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003375 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003376 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003377 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003378 const size_t output_sizes_to_test[] = {
3379 0,
3380 1,
3381 expected_mac->len - 1,
3382 expected_mac->len,
3383 expected_mac->len + 1,
3384 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
Gilles Peskine7be11a72022-04-14 00:12:57 +02003386 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003387 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003388 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003389
Gilles Peskine8817f612018-12-18 00:18:46 +01003390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003391
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003392 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003393 psa_set_key_algorithm( &attributes, alg );
3394 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003395
Ronald Cron5425a212020-08-04 14:58:35 +02003396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3397 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003398
Gilles Peskine8b356b52020-08-25 23:44:59 +02003399 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3400 {
3401 const size_t output_size = output_sizes_to_test[i];
3402 psa_status_t expected_status =
3403 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3404 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003405
Chris Jones9634bb12021-01-20 15:56:42 +00003406 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003407 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003408
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003409 /* Calculate the MAC, one-shot case. */
3410 TEST_EQUAL( psa_mac_compute( key, alg,
3411 input->x, input->len,
3412 actual_mac, output_size, &mac_length ),
3413 expected_status );
3414 if( expected_status == PSA_SUCCESS )
3415 {
3416 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3417 actual_mac, mac_length );
3418 }
3419
3420 if( output_size > 0 )
3421 memset( actual_mac, 0, output_size );
3422
3423 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003424 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003425 PSA_ASSERT( psa_mac_update( &operation,
3426 input->x, input->len ) );
3427 TEST_EQUAL( psa_mac_sign_finish( &operation,
3428 actual_mac, output_size,
3429 &mac_length ),
3430 expected_status );
3431 PSA_ASSERT( psa_mac_abort( &operation ) );
3432
3433 if( expected_status == PSA_SUCCESS )
3434 {
3435 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3436 actual_mac, mac_length );
3437 }
3438 mbedtls_free( actual_mac );
3439 actual_mac = NULL;
3440 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003441
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003442exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003443 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003444 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003445 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003446 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003447}
3448/* END_CASE */
3449
3450/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003451void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003452 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003453 int alg_arg,
3454 data_t *input,
3455 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456{
Ronald Cron5425a212020-08-04 14:58:35 +02003457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003458 psa_key_type_t key_type = key_type_arg;
3459 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003460 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003462 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003463
Gilles Peskine7be11a72022-04-14 00:12:57 +02003464 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003465
Gilles Peskine8817f612018-12-18 00:18:46 +01003466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003467
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003468 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003469 psa_set_key_algorithm( &attributes, alg );
3470 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003471
Ronald Cron5425a212020-08-04 14:58:35 +02003472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3473 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003474
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003475 /* Verify correct MAC, one-shot case. */
3476 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3477 expected_mac->x, expected_mac->len ) );
3478
3479 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003480 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003481 PSA_ASSERT( psa_mac_update( &operation,
3482 input->x, input->len ) );
3483 PSA_ASSERT( psa_mac_verify_finish( &operation,
3484 expected_mac->x,
3485 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003486
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003487 /* Test a MAC that's too short, one-shot case. */
3488 TEST_EQUAL( psa_mac_verify( key, alg,
3489 input->x, input->len,
3490 expected_mac->x,
3491 expected_mac->len - 1 ),
3492 PSA_ERROR_INVALID_SIGNATURE );
3493
3494 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003495 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003496 PSA_ASSERT( psa_mac_update( &operation,
3497 input->x, input->len ) );
3498 TEST_EQUAL( psa_mac_verify_finish( &operation,
3499 expected_mac->x,
3500 expected_mac->len - 1 ),
3501 PSA_ERROR_INVALID_SIGNATURE );
3502
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003503 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003504 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3505 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003506 TEST_EQUAL( psa_mac_verify( key, alg,
3507 input->x, input->len,
3508 perturbed_mac, expected_mac->len + 1 ),
3509 PSA_ERROR_INVALID_SIGNATURE );
3510
3511 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003512 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003513 PSA_ASSERT( psa_mac_update( &operation,
3514 input->x, input->len ) );
3515 TEST_EQUAL( psa_mac_verify_finish( &operation,
3516 perturbed_mac,
3517 expected_mac->len + 1 ),
3518 PSA_ERROR_INVALID_SIGNATURE );
3519
3520 /* Test changing one byte. */
3521 for( size_t i = 0; i < expected_mac->len; i++ )
3522 {
Chris Jones9634bb12021-01-20 15:56:42 +00003523 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003524 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003525
3526 TEST_EQUAL( psa_mac_verify( key, alg,
3527 input->x, input->len,
3528 perturbed_mac, expected_mac->len ),
3529 PSA_ERROR_INVALID_SIGNATURE );
3530
Ronald Cron5425a212020-08-04 14:58:35 +02003531 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003532 PSA_ASSERT( psa_mac_update( &operation,
3533 input->x, input->len ) );
3534 TEST_EQUAL( psa_mac_verify_finish( &operation,
3535 perturbed_mac,
3536 expected_mac->len ),
3537 PSA_ERROR_INVALID_SIGNATURE );
3538 perturbed_mac[i] ^= 1;
3539 }
3540
Gilles Peskine8c9def32018-02-08 10:02:12 +01003541exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003542 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003543 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003544 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003545 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003546}
3547/* END_CASE */
3548
3549/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003550void cipher_operation_init( )
3551{
Jaeden Ameroab439972019-02-15 14:12:05 +00003552 const uint8_t input[1] = { 0 };
3553 unsigned char output[1] = { 0 };
3554 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003555 /* Test each valid way of initializing the object, except for `= {0}`, as
3556 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3557 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003558 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003559 psa_cipher_operation_t func = psa_cipher_operation_init( );
3560 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3561 psa_cipher_operation_t zero;
3562
3563 memset( &zero, 0, sizeof( zero ) );
3564
Jaeden Ameroab439972019-02-15 14:12:05 +00003565 /* A freshly-initialized cipher operation should not be usable. */
3566 TEST_EQUAL( psa_cipher_update( &func,
3567 input, sizeof( input ),
3568 output, sizeof( output ),
3569 &output_length ),
3570 PSA_ERROR_BAD_STATE );
3571 TEST_EQUAL( psa_cipher_update( &init,
3572 input, sizeof( input ),
3573 output, sizeof( output ),
3574 &output_length ),
3575 PSA_ERROR_BAD_STATE );
3576 TEST_EQUAL( psa_cipher_update( &zero,
3577 input, sizeof( input ),
3578 output, sizeof( output ),
3579 &output_length ),
3580 PSA_ERROR_BAD_STATE );
3581
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003582 /* A default cipher operation should be abortable without error. */
3583 PSA_ASSERT( psa_cipher_abort( &func ) );
3584 PSA_ASSERT( psa_cipher_abort( &init ) );
3585 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003586}
3587/* END_CASE */
3588
3589/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003590void cipher_setup( int key_type_arg,
3591 data_t *key,
3592 int alg_arg,
3593 int expected_status_arg )
3594{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003595 psa_key_type_t key_type = key_type_arg;
3596 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003597 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003598 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003599 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003600#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003601 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3602#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003603
Gilles Peskine8817f612018-12-18 00:18:46 +01003604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003605
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003606 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3607 &operation, &status ) )
3608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003609 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003610
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003611 /* The operation object should be reusable. */
3612#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3613 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3614 smoke_test_key_data,
3615 sizeof( smoke_test_key_data ),
3616 KNOWN_SUPPORTED_CIPHER_ALG,
3617 &operation, &status ) )
3618 goto exit;
3619 TEST_EQUAL( status, PSA_SUCCESS );
3620#endif
3621
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003622exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003623 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003624 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003625}
3626/* END_CASE */
3627
Ronald Cronee414c72021-03-18 18:50:08 +01003628/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003629void cipher_bad_order( )
3630{
Ronald Cron5425a212020-08-04 14:58:35 +02003631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003632 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3633 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003635 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003636 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003637 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003638 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3639 0xaa, 0xaa, 0xaa, 0xaa };
3640 const uint8_t text[] = {
3641 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3642 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003643 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003644 size_t length = 0;
3645
3646 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3648 psa_set_key_algorithm( &attributes, alg );
3649 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003650 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3651 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003652
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003653 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003654 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003655 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003656 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003657 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003658 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003659 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003660 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003661
3662 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003663 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003664 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003665 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003666 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003667 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003668 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003669 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003670
Jaeden Ameroab439972019-02-15 14:12:05 +00003671 /* Generate an IV without calling setup beforehand. */
3672 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3673 buffer, sizeof( buffer ),
3674 &length ),
3675 PSA_ERROR_BAD_STATE );
3676 PSA_ASSERT( psa_cipher_abort( &operation ) );
3677
3678 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003679 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003680 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3681 buffer, sizeof( buffer ),
3682 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003683 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003684 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3685 buffer, sizeof( buffer ),
3686 &length ),
3687 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003688 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003689 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003690 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003691
3692 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003693 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003694 PSA_ASSERT( psa_cipher_set_iv( &operation,
3695 iv, sizeof( iv ) ) );
3696 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3697 buffer, sizeof( buffer ),
3698 &length ),
3699 PSA_ERROR_BAD_STATE );
3700 PSA_ASSERT( psa_cipher_abort( &operation ) );
3701
3702 /* Set an IV without calling setup beforehand. */
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 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003709 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003710 PSA_ASSERT( psa_cipher_set_iv( &operation,
3711 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003712 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003713 TEST_EQUAL( psa_cipher_set_iv( &operation,
3714 iv, sizeof( iv ) ),
3715 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003716 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003717 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003718 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003719
3720 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003721 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003722 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3723 buffer, sizeof( buffer ),
3724 &length ) );
3725 TEST_EQUAL( psa_cipher_set_iv( &operation,
3726 iv, sizeof( iv ) ),
3727 PSA_ERROR_BAD_STATE );
3728 PSA_ASSERT( psa_cipher_abort( &operation ) );
3729
3730 /* Call update without calling setup beforehand. */
3731 TEST_EQUAL( psa_cipher_update( &operation,
3732 text, sizeof( text ),
3733 buffer, sizeof( buffer ),
3734 &length ),
3735 PSA_ERROR_BAD_STATE );
3736 PSA_ASSERT( psa_cipher_abort( &operation ) );
3737
3738 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003739 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003740 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003741 TEST_EQUAL( psa_cipher_update( &operation,
3742 text, sizeof( text ),
3743 buffer, sizeof( buffer ),
3744 &length ),
3745 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003746 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003747 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003748 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003749
3750 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003751 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003752 PSA_ASSERT( psa_cipher_set_iv( &operation,
3753 iv, sizeof( iv ) ) );
3754 PSA_ASSERT( psa_cipher_finish( &operation,
3755 buffer, sizeof( buffer ), &length ) );
3756 TEST_EQUAL( psa_cipher_update( &operation,
3757 text, sizeof( text ),
3758 buffer, sizeof( buffer ),
3759 &length ),
3760 PSA_ERROR_BAD_STATE );
3761 PSA_ASSERT( psa_cipher_abort( &operation ) );
3762
3763 /* Call finish without calling setup beforehand. */
3764 TEST_EQUAL( psa_cipher_finish( &operation,
3765 buffer, sizeof( buffer ), &length ),
3766 PSA_ERROR_BAD_STATE );
3767 PSA_ASSERT( psa_cipher_abort( &operation ) );
3768
3769 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003770 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003771 /* Not calling update means we are encrypting an empty buffer, which is OK
3772 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003773 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003774 TEST_EQUAL( psa_cipher_finish( &operation,
3775 buffer, sizeof( buffer ), &length ),
3776 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003777 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003778 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003779 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003780
3781 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003782 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003783 PSA_ASSERT( psa_cipher_set_iv( &operation,
3784 iv, sizeof( iv ) ) );
3785 PSA_ASSERT( psa_cipher_finish( &operation,
3786 buffer, sizeof( buffer ), &length ) );
3787 TEST_EQUAL( psa_cipher_finish( &operation,
3788 buffer, sizeof( buffer ), &length ),
3789 PSA_ERROR_BAD_STATE );
3790 PSA_ASSERT( psa_cipher_abort( &operation ) );
3791
Ronald Cron5425a212020-08-04 14:58:35 +02003792 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003793
Jaeden Ameroab439972019-02-15 14:12:05 +00003794exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003795 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003796 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003797}
3798/* END_CASE */
3799
3800/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003801void cipher_encrypt_fail( int alg_arg,
3802 int key_type_arg,
3803 data_t *key_data,
3804 data_t *input,
3805 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003806{
Ronald Cron5425a212020-08-04 14:58:35 +02003807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003808 psa_status_t status;
3809 psa_key_type_t key_type = key_type_arg;
3810 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003811 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003812 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3813 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3814 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003815 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003816 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003817 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003818 size_t function_output_length;
3819 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3821
3822 if ( PSA_ERROR_BAD_STATE != expected_status )
3823 {
3824 PSA_ASSERT( psa_crypto_init( ) );
3825
3826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3827 psa_set_key_algorithm( &attributes, alg );
3828 psa_set_key_type( &attributes, key_type );
3829
3830 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3831 input->len );
3832 ASSERT_ALLOC( output, output_buffer_size );
3833
3834 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3835 &key ) );
3836 }
3837
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003838 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003839 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3840 output_buffer_size, &output_length );
3841
3842 TEST_EQUAL( status, expected_status );
3843
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003844 /* Encrypt, multi-part */
3845 status = psa_cipher_encrypt_setup( &operation, key, alg );
3846 if( status == PSA_SUCCESS )
3847 {
3848 if( alg != PSA_ALG_ECB_NO_PADDING )
3849 {
3850 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3851 iv, iv_size,
3852 &iv_length ) );
3853 }
3854
3855 status = psa_cipher_update( &operation, input->x, input->len,
3856 output, output_buffer_size,
3857 &function_output_length );
3858 if( status == PSA_SUCCESS )
3859 {
3860 output_length += function_output_length;
3861
3862 status = psa_cipher_finish( &operation, output + output_length,
3863 output_buffer_size - output_length,
3864 &function_output_length );
3865
3866 TEST_EQUAL( status, expected_status );
3867 }
3868 else
3869 {
3870 TEST_EQUAL( status, expected_status );
3871 }
3872 }
3873 else
3874 {
3875 TEST_EQUAL( status, expected_status );
3876 }
3877
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003878exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003879 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003880 mbedtls_free( output );
3881 psa_destroy_key( key );
3882 PSA_DONE( );
3883}
3884/* END_CASE */
3885
3886/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003887void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3888 data_t *input, int iv_length,
3889 int expected_result )
3890{
3891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3892 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3894 size_t output_buffer_size = 0;
3895 unsigned char *output = NULL;
3896
3897 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3898 ASSERT_ALLOC( output, output_buffer_size );
3899
3900 PSA_ASSERT( psa_crypto_init( ) );
3901
3902 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3903 psa_set_key_algorithm( &attributes, alg );
3904 psa_set_key_type( &attributes, key_type );
3905
3906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3907 &key ) );
3908 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3909 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3910 iv_length ) );
3911
3912exit:
3913 psa_cipher_abort( &operation );
3914 mbedtls_free( output );
3915 psa_destroy_key( key );
3916 PSA_DONE( );
3917}
3918/* END_CASE */
3919
3920/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003921void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3922 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003923{
3924 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3925 psa_key_type_t key_type = key_type_arg;
3926 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003927 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3928 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003929 unsigned char *output = NULL;
3930 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003931 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3933
3934 PSA_ASSERT( psa_crypto_init( ) );
3935
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003936 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003937 TEST_LE_U( ciphertext->len,
3938 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3939 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003940 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003941 TEST_LE_U( plaintext->len,
3942 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3943 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3944 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003945
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003946
3947 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003948 psa_set_key_usage_flags( &attributes,
3949 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003950 psa_set_key_algorithm( &attributes, alg );
3951 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003952 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3953 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003954 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3955 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003956 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003957
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003958 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003959 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3960 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3961 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003962 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3963 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003964 PSA_ERROR_BAD_STATE );
3965
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003966 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003967 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3968 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003969 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003970 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003971 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3972 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003973 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003974 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003975
Gilles Peskine286c3142022-04-20 17:09:38 +02003976 /* Multipart encryption */
3977 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3978 output_length = 0;
3979 length = ~0;
3980 PSA_ASSERT( psa_cipher_update( &operation,
3981 plaintext->x, plaintext->len,
3982 output, output_buffer_size,
3983 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003984 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003985 output_length += length;
3986 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine42649d92022-11-23 14:15:57 +01003987 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003988 output_buffer_size - output_length,
3989 &length ) );
3990 output_length += length;
3991 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003992 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003993
Gilles Peskine286c3142022-04-20 17:09:38 +02003994 /* Multipart encryption */
3995 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3996 output_length = 0;
3997 length = ~0;
3998 PSA_ASSERT( psa_cipher_update( &operation,
3999 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004000 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02004001 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004002 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02004003 output_length += length;
4004 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine42649d92022-11-23 14:15:57 +01004005 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine286c3142022-04-20 17:09:38 +02004006 output_buffer_size - output_length,
4007 &length ) );
4008 output_length += length;
4009 ASSERT_COMPARE( plaintext->x, plaintext->len,
4010 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004011
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004012 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004013 output_length = ~0;
4014 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
4015 output, output_buffer_size,
4016 &output_length ) );
4017 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
4018 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004019
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004020 /* One-shot decryption */
4021 output_length = ~0;
4022 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4023 output, output_buffer_size,
4024 &output_length ) );
4025 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004026 output, output_length );
4027
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004028exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004029 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004030 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004031 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004032 psa_destroy_key( key );
4033 PSA_DONE( );
4034}
4035/* END_CASE */
4036
4037/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004038void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4039{
4040 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4041 psa_algorithm_t alg = alg_arg;
4042 psa_key_type_t key_type = key_type_arg;
4043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4044 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4045 psa_status_t status;
4046
4047 PSA_ASSERT( psa_crypto_init( ) );
4048
4049 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4050 psa_set_key_algorithm( &attributes, alg );
4051 psa_set_key_type( &attributes, key_type );
4052
4053 /* Usage of either of these two size macros would cause divide by zero
4054 * with incorrect key types previously. Input length should be irrelevant
4055 * here. */
4056 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4057 0 );
4058 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4059
4060
4061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4062 &key ) );
4063
4064 /* Should fail due to invalid alg type (to support invalid key type).
4065 * Encrypt or decrypt will end up in the same place. */
4066 status = psa_cipher_encrypt_setup( &operation, key, alg );
4067
4068 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4069
4070exit:
4071 psa_cipher_abort( &operation );
4072 psa_destroy_key( key );
4073 PSA_DONE( );
4074}
4075/* END_CASE */
4076
4077/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004078void cipher_encrypt_validation( int alg_arg,
4079 int key_type_arg,
4080 data_t *key_data,
4081 data_t *input )
4082{
4083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4084 psa_key_type_t key_type = key_type_arg;
4085 psa_algorithm_t alg = alg_arg;
4086 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4087 unsigned char *output1 = NULL;
4088 size_t output1_buffer_size = 0;
4089 size_t output1_length = 0;
4090 unsigned char *output2 = NULL;
4091 size_t output2_buffer_size = 0;
4092 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004093 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004094 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004096
Gilles Peskine8817f612018-12-18 00:18:46 +01004097 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004098
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004099 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4100 psa_set_key_algorithm( &attributes, alg );
4101 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004102
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004103 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4104 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4105 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4106 ASSERT_ALLOC( output1, output1_buffer_size );
4107 ASSERT_ALLOC( output2, output2_buffer_size );
4108
Ronald Cron5425a212020-08-04 14:58:35 +02004109 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4110 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004111
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004112 /* The one-shot cipher encryption uses generated iv so validating
4113 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004114 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4115 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004116 TEST_LE_U( output1_length,
4117 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4118 TEST_LE_U( output1_length,
4119 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004120
4121 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4122 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004123
Gilles Peskine8817f612018-12-18 00:18:46 +01004124 PSA_ASSERT( psa_cipher_update( &operation,
4125 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004126 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004127 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004128 TEST_LE_U( function_output_length,
4129 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4130 TEST_LE_U( function_output_length,
4131 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004132 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004133
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004134 PSA_ASSERT( psa_cipher_finish( &operation,
4135 output2 + output2_length,
4136 output2_buffer_size - output2_length,
4137 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004138 TEST_LE_U( function_output_length,
4139 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4140 TEST_LE_U( function_output_length,
4141 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004142 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004143
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004144 PSA_ASSERT( psa_cipher_abort( &operation ) );
4145 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4146 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004147
Gilles Peskine50e586b2018-06-08 14:28:46 +02004148exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004149 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004150 mbedtls_free( output1 );
4151 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004152 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004153 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004154}
4155/* END_CASE */
4156
4157/* BEGIN_CASE */
4158void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004159 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004160 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004161 int first_part_size_arg,
4162 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004163 data_t *expected_output,
4164 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004165{
Ronald Cron5425a212020-08-04 14:58:35 +02004166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004167 psa_key_type_t key_type = key_type_arg;
4168 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004169 psa_status_t status;
4170 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004171 size_t first_part_size = first_part_size_arg;
4172 size_t output1_length = output1_length_arg;
4173 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004174 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004175 size_t output_buffer_size = 0;
4176 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004177 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004178 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004180
Gilles Peskine8817f612018-12-18 00:18:46 +01004181 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004182
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004183 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4184 psa_set_key_algorithm( &attributes, alg );
4185 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004186
Ronald Cron5425a212020-08-04 14:58:35 +02004187 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4188 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004189
Ronald Cron5425a212020-08-04 14:58:35 +02004190 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004191
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004192 if( iv->len > 0 )
4193 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004194 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004195 }
4196
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004197 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4198 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004199 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004200
Gilles Peskine7be11a72022-04-14 00:12:57 +02004201 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004202 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4203 output, output_buffer_size,
4204 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004205 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004206 TEST_LE_U( function_output_length,
4207 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4208 TEST_LE_U( function_output_length,
4209 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004210 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004211
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004212 if( first_part_size < input->len )
4213 {
4214 PSA_ASSERT( psa_cipher_update( &operation,
4215 input->x + first_part_size,
4216 input->len - first_part_size,
4217 ( output_buffer_size == 0 ? NULL :
4218 output + total_output_length ),
4219 output_buffer_size - total_output_length,
4220 &function_output_length ) );
4221 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004222 TEST_LE_U( function_output_length,
4223 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4224 alg,
4225 input->len - first_part_size ) );
4226 TEST_LE_U( function_output_length,
4227 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004228 total_output_length += function_output_length;
4229 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004230
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004231 status = psa_cipher_finish( &operation,
4232 ( output_buffer_size == 0 ? NULL :
4233 output + total_output_length ),
4234 output_buffer_size - total_output_length,
4235 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004236 TEST_LE_U( function_output_length,
4237 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4238 TEST_LE_U( function_output_length,
4239 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004240 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004241 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004242
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004243 if( expected_status == PSA_SUCCESS )
4244 {
4245 PSA_ASSERT( psa_cipher_abort( &operation ) );
4246
4247 ASSERT_COMPARE( expected_output->x, expected_output->len,
4248 output, total_output_length );
4249 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004250
4251exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004252 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004253 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004254 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004255 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004256}
4257/* END_CASE */
4258
4259/* BEGIN_CASE */
4260void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004261 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004262 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004263 int first_part_size_arg,
4264 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004265 data_t *expected_output,
4266 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267{
Ronald Cron5425a212020-08-04 14:58:35 +02004268 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004269 psa_key_type_t key_type = key_type_arg;
4270 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004271 psa_status_t status;
4272 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004273 size_t first_part_size = first_part_size_arg;
4274 size_t output1_length = output1_length_arg;
4275 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004276 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004277 size_t output_buffer_size = 0;
4278 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004279 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004280 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004282
Gilles Peskine8817f612018-12-18 00:18:46 +01004283 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004284
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4286 psa_set_key_algorithm( &attributes, alg );
4287 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004288
Ronald Cron5425a212020-08-04 14:58:35 +02004289 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4290 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004291
Ronald Cron5425a212020-08-04 14:58:35 +02004292 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004293
Steven Cooreman177deba2020-09-07 17:14:14 +02004294 if( iv->len > 0 )
4295 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004296 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004297 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004298
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004299 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4300 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004301 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004302
Gilles Peskine7be11a72022-04-14 00:12:57 +02004303 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004304 PSA_ASSERT( psa_cipher_update( &operation,
4305 input->x, first_part_size,
4306 output, output_buffer_size,
4307 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004308 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004309 TEST_LE_U( function_output_length,
4310 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4311 TEST_LE_U( function_output_length,
4312 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004313 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004314
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004315 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004316 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004317 PSA_ASSERT( psa_cipher_update( &operation,
4318 input->x + first_part_size,
4319 input->len - first_part_size,
4320 ( output_buffer_size == 0 ? NULL :
4321 output + total_output_length ),
4322 output_buffer_size - total_output_length,
4323 &function_output_length ) );
4324 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004325 TEST_LE_U( function_output_length,
4326 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4327 alg,
4328 input->len - first_part_size ) );
4329 TEST_LE_U( function_output_length,
4330 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004331 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004332 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004333
Gilles Peskine50e586b2018-06-08 14:28:46 +02004334 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004335 ( output_buffer_size == 0 ? NULL :
4336 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004337 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004338 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004339 TEST_LE_U( function_output_length,
4340 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4341 TEST_LE_U( function_output_length,
4342 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004343 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004344 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004345
4346 if( expected_status == PSA_SUCCESS )
4347 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004348 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004349
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004350 ASSERT_COMPARE( expected_output->x, expected_output->len,
4351 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004352 }
4353
Gilles Peskine50e586b2018-06-08 14:28:46 +02004354exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004355 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004356 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004357 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004358 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004359}
4360/* END_CASE */
4361
Gilles Peskine50e586b2018-06-08 14:28:46 +02004362/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004363void cipher_decrypt_fail( int alg_arg,
4364 int key_type_arg,
4365 data_t *key_data,
4366 data_t *iv,
4367 data_t *input_arg,
4368 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004369{
4370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4371 psa_status_t status;
4372 psa_key_type_t key_type = key_type_arg;
4373 psa_algorithm_t alg = alg_arg;
4374 psa_status_t expected_status = expected_status_arg;
4375 unsigned char *input = NULL;
4376 size_t input_buffer_size = 0;
4377 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004378 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004379 size_t output_buffer_size = 0;
4380 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004381 size_t function_output_length;
4382 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4384
4385 if ( PSA_ERROR_BAD_STATE != expected_status )
4386 {
4387 PSA_ASSERT( psa_crypto_init( ) );
4388
4389 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4390 psa_set_key_algorithm( &attributes, alg );
4391 psa_set_key_type( &attributes, key_type );
4392
4393 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4394 &key ) );
4395 }
4396
4397 /* Allocate input buffer and copy the iv and the plaintext */
4398 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4399 if ( input_buffer_size > 0 )
4400 {
4401 ASSERT_ALLOC( input, input_buffer_size );
4402 memcpy( input, iv->x, iv->len );
4403 memcpy( input + iv->len, input_arg->x, input_arg->len );
4404 }
4405
4406 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4407 ASSERT_ALLOC( output, output_buffer_size );
4408
Neil Armstrong66a479f2022-02-07 15:41:19 +01004409 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004410 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4411 output_buffer_size, &output_length );
4412 TEST_EQUAL( status, expected_status );
4413
Neil Armstrong66a479f2022-02-07 15:41:19 +01004414 /* Decrypt, multi-part */
4415 status = psa_cipher_decrypt_setup( &operation, key, alg );
4416 if( status == PSA_SUCCESS )
4417 {
4418 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4419 input_arg->len ) +
4420 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4421 ASSERT_ALLOC( output_multi, output_buffer_size );
4422
4423 if( iv->len > 0 )
4424 {
4425 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4426
4427 if( status != PSA_SUCCESS )
4428 TEST_EQUAL( status, expected_status );
4429 }
4430
4431 if( status == PSA_SUCCESS )
4432 {
4433 status = psa_cipher_update( &operation,
4434 input_arg->x, input_arg->len,
4435 output_multi, output_buffer_size,
4436 &function_output_length );
4437 if( status == PSA_SUCCESS )
4438 {
4439 output_length = function_output_length;
4440
4441 status = psa_cipher_finish( &operation,
4442 output_multi + output_length,
4443 output_buffer_size - output_length,
4444 &function_output_length );
4445
4446 TEST_EQUAL( status, expected_status );
4447 }
4448 else
4449 {
4450 TEST_EQUAL( status, expected_status );
4451 }
4452 }
4453 else
4454 {
4455 TEST_EQUAL( status, expected_status );
4456 }
4457 }
4458 else
4459 {
4460 TEST_EQUAL( status, expected_status );
4461 }
4462
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004463exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004464 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004465 mbedtls_free( input );
4466 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004467 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004468 psa_destroy_key( key );
4469 PSA_DONE( );
4470}
4471/* END_CASE */
4472
4473/* BEGIN_CASE */
4474void cipher_decrypt( int alg_arg,
4475 int key_type_arg,
4476 data_t *key_data,
4477 data_t *iv,
4478 data_t *input_arg,
4479 data_t *expected_output )
4480{
4481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4482 psa_key_type_t key_type = key_type_arg;
4483 psa_algorithm_t alg = alg_arg;
4484 unsigned char *input = NULL;
4485 size_t input_buffer_size = 0;
4486 unsigned char *output = NULL;
4487 size_t output_buffer_size = 0;
4488 size_t output_length = 0;
4489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4490
4491 PSA_ASSERT( psa_crypto_init( ) );
4492
4493 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4494 psa_set_key_algorithm( &attributes, alg );
4495 psa_set_key_type( &attributes, key_type );
4496
4497 /* Allocate input buffer and copy the iv and the plaintext */
4498 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4499 if ( input_buffer_size > 0 )
4500 {
4501 ASSERT_ALLOC( input, input_buffer_size );
4502 memcpy( input, iv->x, iv->len );
4503 memcpy( input + iv->len, input_arg->x, input_arg->len );
4504 }
4505
4506 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4507 ASSERT_ALLOC( output, output_buffer_size );
4508
4509 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4510 &key ) );
4511
4512 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4513 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004514 TEST_LE_U( output_length,
4515 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4516 TEST_LE_U( output_length,
4517 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004518
4519 ASSERT_COMPARE( expected_output->x, expected_output->len,
4520 output, output_length );
4521exit:
4522 mbedtls_free( input );
4523 mbedtls_free( output );
4524 psa_destroy_key( key );
4525 PSA_DONE( );
4526}
4527/* END_CASE */
4528
4529/* BEGIN_CASE */
4530void cipher_verify_output( int alg_arg,
4531 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004532 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004533 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004534{
Ronald Cron5425a212020-08-04 14:58:35 +02004535 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004536 psa_key_type_t key_type = key_type_arg;
4537 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004538 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004539 size_t output1_size = 0;
4540 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004541 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004542 size_t output2_size = 0;
4543 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004545
Gilles Peskine8817f612018-12-18 00:18:46 +01004546 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004547
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4549 psa_set_key_algorithm( &attributes, alg );
4550 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004551
Ronald Cron5425a212020-08-04 14:58:35 +02004552 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4553 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004554 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004555 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004556
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004557 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4558 output1, output1_size,
4559 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004560 TEST_LE_U( output1_length,
4561 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4562 TEST_LE_U( output1_length,
4563 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004564
4565 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004566 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004567
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004568 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4569 output2, output2_size,
4570 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004571 TEST_LE_U( output2_length,
4572 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4573 TEST_LE_U( output2_length,
4574 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004575
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004576 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004577
4578exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004579 mbedtls_free( output1 );
4580 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004581 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004582 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004583}
4584/* END_CASE */
4585
4586/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004587void cipher_verify_output_multipart( int alg_arg,
4588 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004589 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004590 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004591 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004592{
Ronald Cron5425a212020-08-04 14:58:35 +02004593 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004594 psa_key_type_t key_type = key_type_arg;
4595 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004596 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004597 unsigned char iv[16] = {0};
4598 size_t iv_size = 16;
4599 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004600 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004601 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004602 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004603 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004604 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004605 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004606 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004607 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4608 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004610
Gilles Peskine8817f612018-12-18 00:18:46 +01004611 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004612
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004613 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4614 psa_set_key_algorithm( &attributes, alg );
4615 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004616
Ronald Cron5425a212020-08-04 14:58:35 +02004617 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4618 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004619
Ronald Cron5425a212020-08-04 14:58:35 +02004620 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4621 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004622
Steven Cooreman177deba2020-09-07 17:14:14 +02004623 if( alg != PSA_ALG_ECB_NO_PADDING )
4624 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004625 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4626 iv, iv_size,
4627 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004628 }
4629
gabor-mezei-armceface22021-01-21 12:26:17 +01004630 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004631 TEST_LE_U( output1_buffer_size,
4632 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004633 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004634
Gilles Peskine7be11a72022-04-14 00:12:57 +02004635 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004636
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4638 output1, output1_buffer_size,
4639 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004640 TEST_LE_U( function_output_length,
4641 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4642 TEST_LE_U( function_output_length,
4643 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004644 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004645
Gilles Peskine8817f612018-12-18 00:18:46 +01004646 PSA_ASSERT( psa_cipher_update( &operation1,
4647 input->x + first_part_size,
4648 input->len - first_part_size,
4649 output1, output1_buffer_size,
4650 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004651 TEST_LE_U( function_output_length,
4652 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4653 alg,
4654 input->len - first_part_size ) );
4655 TEST_LE_U( function_output_length,
4656 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004657 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004658
Gilles Peskine8817f612018-12-18 00:18:46 +01004659 PSA_ASSERT( psa_cipher_finish( &operation1,
4660 output1 + output1_length,
4661 output1_buffer_size - output1_length,
4662 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004663 TEST_LE_U( function_output_length,
4664 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4665 TEST_LE_U( function_output_length,
4666 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004667 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004668
Gilles Peskine8817f612018-12-18 00:18:46 +01004669 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004670
Gilles Peskine048b7f02018-06-08 14:20:49 +02004671 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004672 TEST_LE_U( output2_buffer_size,
4673 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4674 TEST_LE_U( output2_buffer_size,
4675 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004676 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004677
Steven Cooreman177deba2020-09-07 17:14:14 +02004678 if( iv_length > 0 )
4679 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004680 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4681 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004682 }
Moran Pekerded84402018-06-06 16:36:50 +03004683
Gilles Peskine8817f612018-12-18 00:18:46 +01004684 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4685 output2, output2_buffer_size,
4686 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004687 TEST_LE_U( function_output_length,
4688 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4689 TEST_LE_U( function_output_length,
4690 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004691 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004692
Gilles Peskine8817f612018-12-18 00:18:46 +01004693 PSA_ASSERT( psa_cipher_update( &operation2,
4694 output1 + first_part_size,
4695 output1_length - first_part_size,
4696 output2, output2_buffer_size,
4697 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004698 TEST_LE_U( function_output_length,
4699 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4700 alg,
4701 output1_length - first_part_size ) );
4702 TEST_LE_U( function_output_length,
4703 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004704 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004705
Gilles Peskine8817f612018-12-18 00:18:46 +01004706 PSA_ASSERT( psa_cipher_finish( &operation2,
4707 output2 + output2_length,
4708 output2_buffer_size - output2_length,
4709 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004710 TEST_LE_U( function_output_length,
4711 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4712 TEST_LE_U( function_output_length,
4713 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004714 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004715
Gilles Peskine8817f612018-12-18 00:18:46 +01004716 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004717
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004718 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004719
4720exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004721 psa_cipher_abort( &operation1 );
4722 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004723 mbedtls_free( output1 );
4724 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004725 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004726 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004727}
4728/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004729
Gilles Peskine20035e32018-02-03 22:44:14 +01004730/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004731void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004732 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004733 data_t *nonce,
4734 data_t *additional_data,
4735 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004736 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004737{
Ronald Cron5425a212020-08-04 14:58:35 +02004738 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004739 psa_key_type_t key_type = key_type_arg;
4740 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004741 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004742 unsigned char *output_data = NULL;
4743 size_t output_size = 0;
4744 size_t output_length = 0;
4745 unsigned char *output_data2 = NULL;
4746 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004747 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004748 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004750
Gilles Peskine8817f612018-12-18 00:18:46 +01004751 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004752
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004753 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4754 psa_set_key_algorithm( &attributes, alg );
4755 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004756
Gilles Peskine049c7532019-05-15 20:22:09 +02004757 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004758 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004759 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4760 key_bits = psa_get_key_bits( &attributes );
4761
4762 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4763 alg );
4764 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4765 * should be exact. */
4766 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4767 expected_result != PSA_ERROR_NOT_SUPPORTED )
4768 {
4769 TEST_EQUAL( output_size,
4770 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004771 TEST_LE_U( output_size,
4772 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004773 }
4774 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775
Steven Cooremanf49478b2021-02-15 15:19:25 +01004776 status = psa_aead_encrypt( key, alg,
4777 nonce->x, nonce->len,
4778 additional_data->x,
4779 additional_data->len,
4780 input_data->x, input_data->len,
4781 output_data, output_size,
4782 &output_length );
4783
4784 /* If the operation is not supported, just skip and not fail in case the
4785 * encryption involves a common limitation of cryptography hardwares and
4786 * an alternative implementation. */
4787 if( status == PSA_ERROR_NOT_SUPPORTED )
4788 {
4789 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4790 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4791 }
4792
4793 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794
4795 if( PSA_SUCCESS == expected_result )
4796 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004797 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004798
Gilles Peskine003a4a92019-05-14 16:09:40 +02004799 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4800 * should be exact. */
4801 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004802 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004803
Gilles Peskine7be11a72022-04-14 00:12:57 +02004804 TEST_LE_U( input_data->len,
4805 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004806
Ronald Cron5425a212020-08-04 14:58:35 +02004807 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004808 nonce->x, nonce->len,
4809 additional_data->x,
4810 additional_data->len,
4811 output_data, output_length,
4812 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004813 &output_length2 ),
4814 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004815
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004816 ASSERT_COMPARE( input_data->x, input_data->len,
4817 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004818 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004819
Gilles Peskinea1cac842018-06-11 19:33:02 +02004820exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004821 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004822 mbedtls_free( output_data );
4823 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004824 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004825}
4826/* END_CASE */
4827
4828/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004829void aead_encrypt( int key_type_arg, data_t *key_data,
4830 int alg_arg,
4831 data_t *nonce,
4832 data_t *additional_data,
4833 data_t *input_data,
4834 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004835{
Ronald Cron5425a212020-08-04 14:58:35 +02004836 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004837 psa_key_type_t key_type = key_type_arg;
4838 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004839 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004840 unsigned char *output_data = NULL;
4841 size_t output_size = 0;
4842 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004844 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004845
Gilles Peskine8817f612018-12-18 00:18:46 +01004846 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004847
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004848 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4849 psa_set_key_algorithm( &attributes, alg );
4850 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004851
Gilles Peskine049c7532019-05-15 20:22:09 +02004852 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004853 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004854 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4855 key_bits = psa_get_key_bits( &attributes );
4856
4857 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4858 alg );
4859 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4860 * should be exact. */
4861 TEST_EQUAL( output_size,
4862 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004863 TEST_LE_U( output_size,
4864 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004865 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004866
Steven Cooremand588ea12021-01-11 19:36:04 +01004867 status = psa_aead_encrypt( key, alg,
4868 nonce->x, nonce->len,
4869 additional_data->x, additional_data->len,
4870 input_data->x, input_data->len,
4871 output_data, output_size,
4872 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004873
Ronald Cron28a45ed2021-02-09 20:35:42 +01004874 /* If the operation is not supported, just skip and not fail in case the
4875 * encryption involves a common limitation of cryptography hardwares and
4876 * an alternative implementation. */
4877 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004878 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004879 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4880 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004881 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004882
4883 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004884 ASSERT_COMPARE( expected_result->x, expected_result->len,
4885 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004886
Gilles Peskinea1cac842018-06-11 19:33:02 +02004887exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004888 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004889 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004890 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004891}
4892/* END_CASE */
4893
4894/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004895void aead_decrypt( int key_type_arg, data_t *key_data,
4896 int alg_arg,
4897 data_t *nonce,
4898 data_t *additional_data,
4899 data_t *input_data,
4900 data_t *expected_data,
4901 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004902{
Ronald Cron5425a212020-08-04 14:58:35 +02004903 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004904 psa_key_type_t key_type = key_type_arg;
4905 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004906 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004907 unsigned char *output_data = NULL;
4908 size_t output_size = 0;
4909 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004911 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004912 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004913
Gilles Peskine8817f612018-12-18 00:18:46 +01004914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4917 psa_set_key_algorithm( &attributes, alg );
4918 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004919
Gilles Peskine049c7532019-05-15 20:22:09 +02004920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004921 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004922 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4923 key_bits = psa_get_key_bits( &attributes );
4924
4925 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4926 alg );
4927 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4928 expected_result != PSA_ERROR_NOT_SUPPORTED )
4929 {
4930 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4931 * should be exact. */
4932 TEST_EQUAL( output_size,
4933 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004934 TEST_LE_U( output_size,
4935 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004936 }
4937 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004938
Steven Cooremand588ea12021-01-11 19:36:04 +01004939 status = psa_aead_decrypt( key, alg,
4940 nonce->x, nonce->len,
4941 additional_data->x,
4942 additional_data->len,
4943 input_data->x, input_data->len,
4944 output_data, output_size,
4945 &output_length );
4946
Ronald Cron28a45ed2021-02-09 20:35:42 +01004947 /* If the operation is not supported, just skip and not fail in case the
4948 * decryption involves a common limitation of cryptography hardwares and
4949 * an alternative implementation. */
4950 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004951 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004952 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4953 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004954 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004955
4956 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004957
Gilles Peskine2d277862018-06-18 15:41:12 +02004958 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004959 ASSERT_COMPARE( expected_data->x, expected_data->len,
4960 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004961
Gilles Peskinea1cac842018-06-11 19:33:02 +02004962exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004963 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004964 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004965 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004966}
4967/* END_CASE */
4968
4969/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004970void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4971 int alg_arg,
4972 data_t *nonce,
4973 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004974 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004975 int do_set_lengths,
4976 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004977{
Paul Elliottd3f82412021-06-16 16:52:21 +01004978 size_t ad_part_len = 0;
4979 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004980 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004981
Paul Elliott32f46ba2021-09-23 18:24:36 +01004982 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004983 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004984 mbedtls_test_set_step( ad_part_len );
4985
4986 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004987 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004988 if( ad_part_len & 0x01 )
4989 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4990 else
4991 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004992 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004993
4994 /* Split ad into length(ad_part_len) parts. */
4995 if( !aead_multipart_internal_func( key_type_arg, key_data,
4996 alg_arg, nonce,
4997 additional_data,
4998 ad_part_len,
4999 input_data, -1,
5000 set_lengths_method,
5001 expected_output,
5002 1, 0 ) )
5003 break;
5004
5005 /* length(0) part, length(ad_part_len) part, length(0) part... */
5006 mbedtls_test_set_step( 1000 + ad_part_len );
5007
5008 if( !aead_multipart_internal_func( key_type_arg, key_data,
5009 alg_arg, nonce,
5010 additional_data,
5011 ad_part_len,
5012 input_data, -1,
5013 set_lengths_method,
5014 expected_output,
5015 1, 1 ) )
5016 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005017 }
Paul Elliottd3f82412021-06-16 16:52:21 +01005018
Paul Elliott32f46ba2021-09-23 18:24:36 +01005019 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005020 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005021 /* Split data into length(data_part_len) parts. */
5022 mbedtls_test_set_step( 2000 + data_part_len );
5023
5024 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005025 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005026 if( data_part_len & 0x01 )
5027 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5028 else
5029 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005030 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005031
Paul Elliott32f46ba2021-09-23 18:24:36 +01005032 if( !aead_multipart_internal_func( key_type_arg, key_data,
5033 alg_arg, nonce,
5034 additional_data, -1,
5035 input_data, data_part_len,
5036 set_lengths_method,
5037 expected_output,
5038 1, 0 ) )
5039 break;
5040
5041 /* length(0) part, length(data_part_len) part, length(0) part... */
5042 mbedtls_test_set_step( 3000 + data_part_len );
5043
5044 if( !aead_multipart_internal_func( key_type_arg, key_data,
5045 alg_arg, nonce,
5046 additional_data, -1,
5047 input_data, data_part_len,
5048 set_lengths_method,
5049 expected_output,
5050 1, 1 ) )
5051 break;
5052 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005053
Paul Elliott8fc45162021-06-23 16:06:01 +01005054 /* Goto is required to silence warnings about unused labels, as we
5055 * don't actually do any test assertions in this function. */
5056 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005057}
5058/* END_CASE */
5059
5060/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005061void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5062 int alg_arg,
5063 data_t *nonce,
5064 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005065 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005066 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005067 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005068{
Paul Elliottd3f82412021-06-16 16:52:21 +01005069 size_t ad_part_len = 0;
5070 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005071 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005072
Paul Elliott32f46ba2021-09-23 18:24:36 +01005073 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005074 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005075 /* Split ad into length(ad_part_len) parts. */
5076 mbedtls_test_set_step( ad_part_len );
5077
5078 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005079 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005080 if( ad_part_len & 0x01 )
5081 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5082 else
5083 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005084 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005085
5086 if( !aead_multipart_internal_func( key_type_arg, key_data,
5087 alg_arg, nonce,
5088 additional_data,
5089 ad_part_len,
5090 input_data, -1,
5091 set_lengths_method,
5092 expected_output,
5093 0, 0 ) )
5094 break;
5095
5096 /* length(0) part, length(ad_part_len) part, length(0) part... */
5097 mbedtls_test_set_step( 1000 + ad_part_len );
5098
5099 if( !aead_multipart_internal_func( key_type_arg, key_data,
5100 alg_arg, nonce,
5101 additional_data,
5102 ad_part_len,
5103 input_data, -1,
5104 set_lengths_method,
5105 expected_output,
5106 0, 1 ) )
5107 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005108 }
5109
Paul Elliott32f46ba2021-09-23 18:24:36 +01005110 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005111 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005112 /* Split data into length(data_part_len) parts. */
5113 mbedtls_test_set_step( 2000 + data_part_len );
5114
5115 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005116 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005117 if( data_part_len & 0x01 )
5118 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5119 else
5120 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005121 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005122
5123 if( !aead_multipart_internal_func( key_type_arg, key_data,
5124 alg_arg, nonce,
5125 additional_data, -1,
5126 input_data, data_part_len,
5127 set_lengths_method,
5128 expected_output,
5129 0, 0 ) )
5130 break;
5131
5132 /* length(0) part, length(data_part_len) part, length(0) part... */
5133 mbedtls_test_set_step( 3000 + data_part_len );
5134
5135 if( !aead_multipart_internal_func( key_type_arg, key_data,
5136 alg_arg, nonce,
5137 additional_data, -1,
5138 input_data, data_part_len,
5139 set_lengths_method,
5140 expected_output,
5141 0, 1 ) )
5142 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005143 }
5144
Paul Elliott8fc45162021-06-23 16:06:01 +01005145 /* Goto is required to silence warnings about unused labels, as we
5146 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005147 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005148}
5149/* END_CASE */
5150
5151/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005152void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5153 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005154 int nonce_length,
5155 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005156 data_t *additional_data,
5157 data_t *input_data,
5158 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005159{
5160
5161 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5162 psa_key_type_t key_type = key_type_arg;
5163 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005164 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005165 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5167 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005168 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005169 size_t actual_nonce_length = 0;
5170 size_t expected_nonce_length = expected_nonce_length_arg;
5171 unsigned char *output = NULL;
5172 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005173 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005174 size_t ciphertext_size = 0;
5175 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005176 size_t tag_length = 0;
5177 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005178
5179 PSA_ASSERT( psa_crypto_init( ) );
5180
5181 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5182 psa_set_key_algorithm( & attributes, alg );
5183 psa_set_key_type( & attributes, key_type );
5184
5185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5186 &key ) );
5187
5188 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5189
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005190 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5191
Paul Elliottf1277632021-08-24 18:11:37 +01005192 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005193
Paul Elliottf1277632021-08-24 18:11:37 +01005194 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005195
Gilles Peskine7be11a72022-04-14 00:12:57 +02005196 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005197
Paul Elliottf1277632021-08-24 18:11:37 +01005198 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005199
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005200 status = psa_aead_encrypt_setup( &operation, key, alg );
5201
5202 /* If the operation is not supported, just skip and not fail in case the
5203 * encryption involves a common limitation of cryptography hardwares and
5204 * an alternative implementation. */
5205 if( status == PSA_ERROR_NOT_SUPPORTED )
5206 {
5207 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005208 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005209 }
5210
5211 PSA_ASSERT( status );
5212
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005213 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005214 nonce_length,
5215 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005216
Paul Elliott693bf312021-07-23 17:40:41 +01005217 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005218
Paul Elliottf1277632021-08-24 18:11:37 +01005219 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005220
Paul Elliott88ecbe12021-09-22 17:23:03 +01005221 if( expected_status == PSA_SUCCESS )
5222 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5223 alg ) );
5224
Gilles Peskine7be11a72022-04-14 00:12:57 +02005225 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005226
Paul Elliott693bf312021-07-23 17:40:41 +01005227 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005228 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005229 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005230 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5231 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005232
5233 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5234 additional_data->len ) );
5235
5236 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005237 output, output_size,
5238 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005239
Paul Elliottf1277632021-08-24 18:11:37 +01005240 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5241 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005242 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5243 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005244
5245exit:
5246 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005247 mbedtls_free( output );
5248 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005249 psa_aead_abort( &operation );
5250 PSA_DONE( );
5251}
5252/* END_CASE */
5253
5254/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005255void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5256 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005257 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005258 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005259 data_t *additional_data,
5260 data_t *input_data,
5261 int expected_status_arg )
5262{
5263
5264 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5265 psa_key_type_t key_type = key_type_arg;
5266 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005267 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005268 uint8_t *nonce_buffer = NULL;
5269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5270 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5271 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005272 unsigned char *output = NULL;
5273 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005274 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005275 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005276 size_t ciphertext_size = 0;
5277 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005278 size_t tag_length = 0;
5279 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005280 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005281 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005282
5283 PSA_ASSERT( psa_crypto_init( ) );
5284
5285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5286 psa_set_key_algorithm( &attributes, alg );
5287 psa_set_key_type( &attributes, key_type );
5288
5289 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5290 &key ) );
5291
5292 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5293
5294 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5295
Paul Elliott6f0e7202021-08-25 12:57:18 +01005296 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005297
Paul Elliott6f0e7202021-08-25 12:57:18 +01005298 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005299
Gilles Peskine7be11a72022-04-14 00:12:57 +02005300 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005301
Paul Elliott6f0e7202021-08-25 12:57:18 +01005302 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005303
Paul Elliott863864a2021-07-23 17:28:31 +01005304 status = psa_aead_encrypt_setup( &operation, key, alg );
5305
5306 /* If the operation is not supported, just skip and not fail in case the
5307 * encryption involves a common limitation of cryptography hardwares and
5308 * an alternative implementation. */
5309 if( status == PSA_ERROR_NOT_SUPPORTED )
5310 {
5311 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005312 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005313 }
5314
5315 PSA_ASSERT( status );
5316
Paul Elliott4023ffd2021-09-10 16:21:22 +01005317 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5318 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005319 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005320 /* Arbitrary size buffer, to test zero length valid buffer. */
5321 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005322 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005323 }
5324 else
5325 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005326 /* If length is zero, then this will return NULL. */
5327 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005328 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005329
Paul Elliott4023ffd2021-09-10 16:21:22 +01005330 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005331 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005332 for( index = 0; index < nonce_length - 1; ++index )
5333 {
5334 nonce_buffer[index] = 'a' + index;
5335 }
Paul Elliott66696b52021-08-16 18:42:41 +01005336 }
Paul Elliott863864a2021-07-23 17:28:31 +01005337 }
5338
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005339 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5340 {
5341 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5342 input_data->len ) );
5343 }
5344
Paul Elliott6f0e7202021-08-25 12:57:18 +01005345 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005346
Paul Elliott693bf312021-07-23 17:40:41 +01005347 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005348
5349 if( expected_status == PSA_SUCCESS )
5350 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005351 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5352 {
5353 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5354 input_data->len ) );
5355 }
5356 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5357 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005358
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005359 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5360 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5361 additional_data->len ),
5362 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005363
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005364 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005365 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005366 &ciphertext_length ),
5367 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005368
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005369 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005370 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005371 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5372 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005373 }
5374
5375exit:
5376 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005377 mbedtls_free( output );
5378 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005379 mbedtls_free( nonce_buffer );
5380 psa_aead_abort( &operation );
5381 PSA_DONE( );
5382}
5383/* END_CASE */
5384
5385/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005386void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5387 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005388 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005389 data_t *nonce,
5390 data_t *additional_data,
5391 data_t *input_data,
5392 int expected_status_arg )
5393{
5394
5395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5396 psa_key_type_t key_type = key_type_arg;
5397 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005398 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5400 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5401 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005402 unsigned char *output = NULL;
5403 unsigned char *ciphertext = NULL;
5404 size_t output_size = output_size_arg;
5405 size_t ciphertext_size = 0;
5406 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005407 size_t tag_length = 0;
5408 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5409
5410 PSA_ASSERT( psa_crypto_init( ) );
5411
5412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5413 psa_set_key_algorithm( &attributes, alg );
5414 psa_set_key_type( &attributes, key_type );
5415
5416 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5417 &key ) );
5418
5419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5420
Paul Elliottc6d11d02021-09-01 12:04:23 +01005421 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005422
Paul Elliottc6d11d02021-09-01 12:04:23 +01005423 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005424
Paul Elliottc6d11d02021-09-01 12:04:23 +01005425 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005426
Paul Elliott43fbda62021-07-23 18:30:59 +01005427 status = psa_aead_encrypt_setup( &operation, key, alg );
5428
5429 /* If the operation is not supported, just skip and not fail in case the
5430 * encryption involves a common limitation of cryptography hardwares and
5431 * an alternative implementation. */
5432 if( status == PSA_ERROR_NOT_SUPPORTED )
5433 {
5434 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5435 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5436 }
5437
5438 PSA_ASSERT( status );
5439
Paul Elliott47b9a142021-10-07 15:04:57 +01005440 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5441 input_data->len ) );
5442
Paul Elliott43fbda62021-07-23 18:30:59 +01005443 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5444
5445 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5446 additional_data->len ) );
5447
5448 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005449 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005450
5451 TEST_EQUAL( status, expected_status );
5452
5453 if( expected_status == PSA_SUCCESS )
5454 {
5455 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005456 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5457 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005458 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5459 }
5460
5461exit:
5462 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005463 mbedtls_free( output );
5464 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005465 psa_aead_abort( &operation );
5466 PSA_DONE( );
5467}
5468/* END_CASE */
5469
Paul Elliott91b021e2021-07-23 18:52:31 +01005470/* BEGIN_CASE */
5471void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5472 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005473 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005474 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005475 data_t *nonce,
5476 data_t *additional_data,
5477 data_t *input_data,
5478 int expected_status_arg )
5479{
5480
5481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5482 psa_key_type_t key_type = key_type_arg;
5483 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005484 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5486 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5487 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005488 unsigned char *ciphertext = NULL;
5489 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005490 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005491 size_t ciphertext_size = 0;
5492 size_t ciphertext_length = 0;
5493 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005494 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005495 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005496
5497 PSA_ASSERT( psa_crypto_init( ) );
5498
5499 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5500 psa_set_key_algorithm( &attributes, alg );
5501 psa_set_key_type( &attributes, key_type );
5502
5503 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5504 &key ) );
5505
5506 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5507
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005508 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005509
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005510 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005511
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005512 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005513
Paul Elliott719c1322021-09-13 18:27:22 +01005514 ASSERT_ALLOC( tag_buffer, tag_size );
5515
Paul Elliott91b021e2021-07-23 18:52:31 +01005516 status = psa_aead_encrypt_setup( &operation, key, alg );
5517
5518 /* If the operation is not supported, just skip and not fail in case the
5519 * encryption involves a common limitation of cryptography hardwares and
5520 * an alternative implementation. */
5521 if( status == PSA_ERROR_NOT_SUPPORTED )
5522 {
5523 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5524 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5525 }
5526
5527 PSA_ASSERT( status );
5528
5529 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5530
Paul Elliott76bda482021-10-07 17:07:23 +01005531 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5532 input_data->len ) );
5533
Paul Elliott91b021e2021-07-23 18:52:31 +01005534 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5535 additional_data->len ) );
5536
5537 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005538 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005539
5540 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005541 status = psa_aead_finish( &operation, finish_ciphertext,
5542 finish_ciphertext_size,
5543 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005544 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005545
5546 TEST_EQUAL( status, expected_status );
5547
5548exit:
5549 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005550 mbedtls_free( ciphertext );
5551 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005552 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005553 psa_aead_abort( &operation );
5554 PSA_DONE( );
5555}
5556/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005557
5558/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005559void aead_multipart_verify( int key_type_arg, data_t *key_data,
5560 int alg_arg,
5561 data_t *nonce,
5562 data_t *additional_data,
5563 data_t *input_data,
5564 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005565 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005566 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005567 int expected_status_arg )
5568{
5569 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5570 psa_key_type_t key_type = key_type_arg;
5571 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005572 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5574 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5575 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005576 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005577 unsigned char *plaintext = NULL;
5578 unsigned char *finish_plaintext = NULL;
5579 size_t plaintext_size = 0;
5580 size_t plaintext_length = 0;
5581 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005582 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005583 unsigned char *tag_buffer = NULL;
5584 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005585
5586 PSA_ASSERT( psa_crypto_init( ) );
5587
5588 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5589 psa_set_key_algorithm( &attributes, alg );
5590 psa_set_key_type( &attributes, key_type );
5591
5592 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5593 &key ) );
5594
5595 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5596
5597 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5598 input_data->len );
5599
5600 ASSERT_ALLOC( plaintext, plaintext_size );
5601
5602 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5603
5604 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5605
Paul Elliott9961a662021-09-17 19:19:02 +01005606 status = psa_aead_decrypt_setup( &operation, key, alg );
5607
5608 /* If the operation is not supported, just skip and not fail in case the
5609 * encryption involves a common limitation of cryptography hardwares and
5610 * an alternative implementation. */
5611 if( status == PSA_ERROR_NOT_SUPPORTED )
5612 {
5613 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5614 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5615 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005616 TEST_EQUAL( status, expected_setup_status );
5617
5618 if( status != PSA_SUCCESS )
5619 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005620
5621 PSA_ASSERT( status );
5622
5623 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5624
Paul Elliottfec6f372021-10-06 17:15:02 +01005625 status = psa_aead_set_lengths( &operation, additional_data->len,
5626 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005627 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005628
Paul Elliott9961a662021-09-17 19:19:02 +01005629 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5630 additional_data->len ) );
5631
5632 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5633 input_data->len,
5634 plaintext, plaintext_size,
5635 &plaintext_length ) );
5636
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005637 if( tag_usage == USE_GIVEN_TAG )
5638 {
5639 tag_buffer = tag->x;
5640 tag_size = tag->len;
5641 }
5642
Paul Elliott9961a662021-09-17 19:19:02 +01005643 status = psa_aead_verify( &operation, finish_plaintext,
5644 verify_plaintext_size,
5645 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005646 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005647
5648 TEST_EQUAL( status, expected_status );
5649
5650exit:
5651 psa_destroy_key( key );
5652 mbedtls_free( plaintext );
5653 mbedtls_free( finish_plaintext );
5654 psa_aead_abort( &operation );
5655 PSA_DONE( );
5656}
5657/* END_CASE */
5658
Paul Elliott9961a662021-09-17 19:19:02 +01005659/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005660void aead_multipart_setup( int key_type_arg, data_t *key_data,
5661 int alg_arg, int expected_status_arg )
5662{
5663 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5664 psa_key_type_t key_type = key_type_arg;
5665 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005666 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5668 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5669 psa_status_t expected_status = expected_status_arg;
5670
5671 PSA_ASSERT( psa_crypto_init( ) );
5672
5673 psa_set_key_usage_flags( &attributes,
5674 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5675 psa_set_key_algorithm( &attributes, alg );
5676 psa_set_key_type( &attributes, key_type );
5677
5678 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5679 &key ) );
5680
Paul Elliott5221ef62021-09-19 17:33:03 +01005681 status = psa_aead_encrypt_setup( &operation, key, alg );
5682
5683 TEST_EQUAL( status, expected_status );
5684
5685 psa_aead_abort( &operation );
5686
Paul Elliott5221ef62021-09-19 17:33:03 +01005687 status = psa_aead_decrypt_setup( &operation, key, alg );
5688
5689 TEST_EQUAL(status, expected_status );
5690
5691exit:
5692 psa_destroy_key( key );
5693 psa_aead_abort( &operation );
5694 PSA_DONE( );
5695}
5696/* END_CASE */
5697
5698/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5700 int alg_arg,
5701 data_t *nonce,
5702 data_t *additional_data,
5703 data_t *input_data )
5704{
5705 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5706 psa_key_type_t key_type = key_type_arg;
5707 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005708 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005709 unsigned char *output_data = NULL;
5710 unsigned char *final_data = NULL;
5711 size_t output_size = 0;
5712 size_t finish_output_size = 0;
5713 size_t output_length = 0;
5714 size_t key_bits = 0;
5715 size_t tag_length = 0;
5716 size_t tag_size = 0;
5717 size_t nonce_length = 0;
5718 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5719 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5720 size_t output_part_length = 0;
5721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5722
5723 PSA_ASSERT( psa_crypto_init( ) );
5724
5725 psa_set_key_usage_flags( & attributes,
5726 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5727 psa_set_key_algorithm( & attributes, alg );
5728 psa_set_key_type( & attributes, key_type );
5729
5730 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5731 &key ) );
5732
5733 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5734 key_bits = psa_get_key_bits( &attributes );
5735
5736 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5737
Gilles Peskine7be11a72022-04-14 00:12:57 +02005738 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005739
5740 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5741
5742 ASSERT_ALLOC( output_data, output_size );
5743
5744 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5745
Gilles Peskine7be11a72022-04-14 00:12:57 +02005746 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005747
5748 ASSERT_ALLOC( final_data, finish_output_size );
5749
5750 /* Test all operations error without calling setup first. */
5751
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5753 PSA_ERROR_BAD_STATE );
5754
5755 psa_aead_abort( &operation );
5756
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5758 PSA_AEAD_NONCE_MAX_SIZE,
5759 &nonce_length ),
5760 PSA_ERROR_BAD_STATE );
5761
5762 psa_aead_abort( &operation );
5763
Paul Elliott481be342021-07-16 17:38:47 +01005764 /* ------------------------------------------------------- */
5765
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5767 input_data->len ),
5768 PSA_ERROR_BAD_STATE );
5769
5770 psa_aead_abort( &operation );
5771
Paul Elliott481be342021-07-16 17:38:47 +01005772 /* ------------------------------------------------------- */
5773
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5775 additional_data->len ),
5776 PSA_ERROR_BAD_STATE );
5777
5778 psa_aead_abort( &operation );
5779
Paul Elliott481be342021-07-16 17:38:47 +01005780 /* ------------------------------------------------------- */
5781
Paul Elliottc23a9a02021-06-21 18:32:46 +01005782 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5783 input_data->len, output_data,
5784 output_size, &output_length ),
5785 PSA_ERROR_BAD_STATE );
5786
5787 psa_aead_abort( &operation );
5788
Paul Elliott481be342021-07-16 17:38:47 +01005789 /* ------------------------------------------------------- */
5790
Paul Elliottc23a9a02021-06-21 18:32:46 +01005791 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5792 finish_output_size,
5793 &output_part_length,
5794 tag_buffer, tag_length,
5795 &tag_size ),
5796 PSA_ERROR_BAD_STATE );
5797
5798 psa_aead_abort( &operation );
5799
Paul Elliott481be342021-07-16 17:38:47 +01005800 /* ------------------------------------------------------- */
5801
Paul Elliottc23a9a02021-06-21 18:32:46 +01005802 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5803 finish_output_size,
5804 &output_part_length,
5805 tag_buffer,
5806 tag_length ),
5807 PSA_ERROR_BAD_STATE );
5808
5809 psa_aead_abort( &operation );
5810
5811 /* Test for double setups. */
5812
Paul Elliottc23a9a02021-06-21 18:32:46 +01005813 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5814
5815 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5816 PSA_ERROR_BAD_STATE );
5817
5818 psa_aead_abort( &operation );
5819
Paul Elliott481be342021-07-16 17:38:47 +01005820 /* ------------------------------------------------------- */
5821
Paul Elliottc23a9a02021-06-21 18:32:46 +01005822 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5823
5824 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5825 PSA_ERROR_BAD_STATE );
5826
5827 psa_aead_abort( &operation );
5828
Paul Elliott374a2be2021-07-16 17:53:40 +01005829 /* ------------------------------------------------------- */
5830
Paul Elliott374a2be2021-07-16 17:53:40 +01005831 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5832
5833 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5834 PSA_ERROR_BAD_STATE );
5835
5836 psa_aead_abort( &operation );
5837
5838 /* ------------------------------------------------------- */
5839
Paul Elliott374a2be2021-07-16 17:53:40 +01005840 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5841
5842 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5843 PSA_ERROR_BAD_STATE );
5844
5845 psa_aead_abort( &operation );
5846
Paul Elliottc23a9a02021-06-21 18:32:46 +01005847 /* Test for not setting a nonce. */
5848
Paul Elliottc23a9a02021-06-21 18:32:46 +01005849 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5850
5851 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5852 additional_data->len ),
5853 PSA_ERROR_BAD_STATE );
5854
5855 psa_aead_abort( &operation );
5856
Paul Elliott7f628422021-09-01 12:08:29 +01005857 /* ------------------------------------------------------- */
5858
Paul Elliott7f628422021-09-01 12:08:29 +01005859 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5860
5861 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5862 input_data->len, output_data,
5863 output_size, &output_length ),
5864 PSA_ERROR_BAD_STATE );
5865
5866 psa_aead_abort( &operation );
5867
Paul Elliottbdc2c682021-09-21 18:37:10 +01005868 /* ------------------------------------------------------- */
5869
Paul Elliottbdc2c682021-09-21 18:37:10 +01005870 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5871
5872 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5873 finish_output_size,
5874 &output_part_length,
5875 tag_buffer, tag_length,
5876 &tag_size ),
5877 PSA_ERROR_BAD_STATE );
5878
5879 psa_aead_abort( &operation );
5880
5881 /* ------------------------------------------------------- */
5882
Paul Elliottbdc2c682021-09-21 18:37:10 +01005883 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5884
5885 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5886 finish_output_size,
5887 &output_part_length,
5888 tag_buffer,
5889 tag_length ),
5890 PSA_ERROR_BAD_STATE );
5891
5892 psa_aead_abort( &operation );
5893
Paul Elliottc23a9a02021-06-21 18:32:46 +01005894 /* Test for double setting nonce. */
5895
Paul Elliottc23a9a02021-06-21 18:32:46 +01005896 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5897
5898 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5899
5900 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5901 PSA_ERROR_BAD_STATE );
5902
5903 psa_aead_abort( &operation );
5904
Paul Elliott374a2be2021-07-16 17:53:40 +01005905 /* Test for double generating nonce. */
5906
Paul Elliott374a2be2021-07-16 17:53:40 +01005907 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5908
5909 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5910 PSA_AEAD_NONCE_MAX_SIZE,
5911 &nonce_length ) );
5912
5913 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5914 PSA_AEAD_NONCE_MAX_SIZE,
5915 &nonce_length ),
5916 PSA_ERROR_BAD_STATE );
5917
5918
5919 psa_aead_abort( &operation );
5920
5921 /* Test for generate nonce then set and vice versa */
5922
Paul Elliott374a2be2021-07-16 17:53:40 +01005923 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5924
5925 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5926 PSA_AEAD_NONCE_MAX_SIZE,
5927 &nonce_length ) );
5928
5929 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5930 PSA_ERROR_BAD_STATE );
5931
5932 psa_aead_abort( &operation );
5933
Andrzej Kurekad837522021-12-15 15:28:49 +01005934 /* Test for generating nonce after calling set lengths */
5935
5936 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5937
5938 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5939 input_data->len ) );
5940
5941 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5942 PSA_AEAD_NONCE_MAX_SIZE,
5943 &nonce_length ) );
5944
5945 psa_aead_abort( &operation );
5946
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005947 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005948
5949 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5950
5951 if( operation.alg == PSA_ALG_CCM )
5952 {
5953 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5954 input_data->len ),
5955 PSA_ERROR_INVALID_ARGUMENT );
5956 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5957 PSA_AEAD_NONCE_MAX_SIZE,
5958 &nonce_length ),
5959 PSA_ERROR_BAD_STATE );
5960 }
5961 else
5962 {
5963 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5964 input_data->len ) );
5965 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5966 PSA_AEAD_NONCE_MAX_SIZE,
5967 &nonce_length ) );
5968 }
5969
5970 psa_aead_abort( &operation );
5971
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005972 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005973#if SIZE_MAX > UINT32_MAX
5974 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5975
5976 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5977 {
5978 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5979 input_data->len ),
5980 PSA_ERROR_INVALID_ARGUMENT );
5981 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5982 PSA_AEAD_NONCE_MAX_SIZE,
5983 &nonce_length ),
5984 PSA_ERROR_BAD_STATE );
5985 }
5986 else
5987 {
5988 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5989 input_data->len ) );
5990 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5991 PSA_AEAD_NONCE_MAX_SIZE,
5992 &nonce_length ) );
5993 }
5994
5995 psa_aead_abort( &operation );
5996#endif
5997
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005998 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005999
6000 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6001
6002 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6003 PSA_AEAD_NONCE_MAX_SIZE,
6004 &nonce_length ) );
6005
6006 if( operation.alg == PSA_ALG_CCM )
6007 {
6008 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6009 input_data->len ),
6010 PSA_ERROR_INVALID_ARGUMENT );
6011 }
6012 else
6013 {
6014 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6015 input_data->len ) );
6016 }
6017
6018 psa_aead_abort( &operation );
6019
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006020 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006021 /* Test for setting nonce after calling set lengths */
6022
6023 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6024
6025 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6026 input_data->len ) );
6027
6028 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6029
6030 psa_aead_abort( &operation );
6031
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006032 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006033
6034 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6035
6036 if( operation.alg == PSA_ALG_CCM )
6037 {
6038 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6039 input_data->len ),
6040 PSA_ERROR_INVALID_ARGUMENT );
6041 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6042 PSA_ERROR_BAD_STATE );
6043 }
6044 else
6045 {
6046 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6047 input_data->len ) );
6048 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6049 }
6050
6051 psa_aead_abort( &operation );
6052
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006053 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006054#if SIZE_MAX > UINT32_MAX
6055 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6056
6057 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6058 {
6059 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6060 input_data->len ),
6061 PSA_ERROR_INVALID_ARGUMENT );
6062 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6063 PSA_ERROR_BAD_STATE );
6064 }
6065 else
6066 {
6067 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6068 input_data->len ) );
6069 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6070 }
6071
6072 psa_aead_abort( &operation );
6073#endif
6074
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006075 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006076
6077 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6078
6079 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6080
6081 if( operation.alg == PSA_ALG_CCM )
6082 {
6083 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6084 input_data->len ),
6085 PSA_ERROR_INVALID_ARGUMENT );
6086 }
6087 else
6088 {
6089 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6090 input_data->len ) );
6091 }
6092
6093 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006094
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006095 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006096#if SIZE_MAX > UINT32_MAX
6097 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
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 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6105 PSA_ERROR_BAD_STATE );
6106 }
6107 else if ( operation.alg != PSA_ALG_CCM )
6108 {
6109 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6110 SIZE_MAX ) );
6111 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6112 }
6113
6114 psa_aead_abort( &operation );
6115
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006116 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006117 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6118
6119 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6120
6121 if( operation.alg == PSA_ALG_GCM )
6122 {
6123 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6124 SIZE_MAX ),
6125 PSA_ERROR_INVALID_ARGUMENT );
6126 }
6127 else if ( operation.alg != PSA_ALG_CCM )
6128 {
6129 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6130 SIZE_MAX ) );
6131 }
6132
6133 psa_aead_abort( &operation );
6134#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006135
6136 /* ------------------------------------------------------- */
6137
Paul Elliott374a2be2021-07-16 17:53:40 +01006138 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6139
6140 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6141
6142 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6143 PSA_AEAD_NONCE_MAX_SIZE,
6144 &nonce_length ),
6145 PSA_ERROR_BAD_STATE );
6146
6147 psa_aead_abort( &operation );
6148
Paul Elliott7220cae2021-06-22 17:25:57 +01006149 /* Test for generating nonce in decrypt setup. */
6150
Paul Elliott7220cae2021-06-22 17:25:57 +01006151 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6152
6153 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6154 PSA_AEAD_NONCE_MAX_SIZE,
6155 &nonce_length ),
6156 PSA_ERROR_BAD_STATE );
6157
6158 psa_aead_abort( &operation );
6159
Paul Elliottc23a9a02021-06-21 18:32:46 +01006160 /* Test for setting lengths twice. */
6161
Paul Elliottc23a9a02021-06-21 18:32:46 +01006162 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6163
6164 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6165
6166 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6167 input_data->len ) );
6168
6169 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6170 input_data->len ),
6171 PSA_ERROR_BAD_STATE );
6172
6173 psa_aead_abort( &operation );
6174
Andrzej Kurekad837522021-12-15 15:28:49 +01006175 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006176
Paul Elliottc23a9a02021-06-21 18:32:46 +01006177 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6178
6179 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6180
Andrzej Kurekad837522021-12-15 15:28:49 +01006181 if( operation.alg == PSA_ALG_CCM )
6182 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006183
Andrzej Kurekad837522021-12-15 15:28:49 +01006184 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6185 additional_data->len ),
6186 PSA_ERROR_BAD_STATE );
6187 }
6188 else
6189 {
6190 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6191 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006192
Andrzej Kurekad837522021-12-15 15:28:49 +01006193 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6194 input_data->len ),
6195 PSA_ERROR_BAD_STATE );
6196 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006197 psa_aead_abort( &operation );
6198
6199 /* ------------------------------------------------------- */
6200
Paul Elliottf94bd992021-09-19 18:15:59 +01006201 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6202
6203 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6204
Andrzej Kurekad837522021-12-15 15:28:49 +01006205 if( operation.alg == PSA_ALG_CCM )
6206 {
6207 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6208 input_data->len, output_data,
6209 output_size, &output_length ),
6210 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006211
Andrzej Kurekad837522021-12-15 15:28:49 +01006212 }
6213 else
6214 {
6215 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6216 input_data->len, output_data,
6217 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006218
Andrzej Kurekad837522021-12-15 15:28:49 +01006219 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6220 input_data->len ),
6221 PSA_ERROR_BAD_STATE );
6222 }
6223 psa_aead_abort( &operation );
6224
6225 /* ------------------------------------------------------- */
6226
6227 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6228
6229 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6230
6231 if( operation.alg == PSA_ALG_CCM )
6232 {
6233 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6234 finish_output_size,
6235 &output_part_length,
6236 tag_buffer, tag_length,
6237 &tag_size ) );
6238 }
6239 else
6240 {
6241 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6242 finish_output_size,
6243 &output_part_length,
6244 tag_buffer, tag_length,
6245 &tag_size ) );
6246
6247 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6248 input_data->len ),
6249 PSA_ERROR_BAD_STATE );
6250 }
6251 psa_aead_abort( &operation );
6252
6253 /* Test for setting lengths after generating nonce + already starting data. */
6254
6255 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6256
6257 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6258 PSA_AEAD_NONCE_MAX_SIZE,
6259 &nonce_length ) );
6260 if( operation.alg == PSA_ALG_CCM )
6261 {
6262
6263 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6264 additional_data->len ),
6265 PSA_ERROR_BAD_STATE );
6266 }
6267 else
6268 {
6269 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6270 additional_data->len ) );
6271
6272 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6273 input_data->len ),
6274 PSA_ERROR_BAD_STATE );
6275 }
6276 psa_aead_abort( &operation );
6277
6278 /* ------------------------------------------------------- */
6279
6280 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6281
6282 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6283 PSA_AEAD_NONCE_MAX_SIZE,
6284 &nonce_length ) );
6285 if( operation.alg == PSA_ALG_CCM )
6286 {
6287 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6288 input_data->len, output_data,
6289 output_size, &output_length ),
6290 PSA_ERROR_BAD_STATE );
6291
6292 }
6293 else
6294 {
6295 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6296 input_data->len, output_data,
6297 output_size, &output_length ) );
6298
6299 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6300 input_data->len ),
6301 PSA_ERROR_BAD_STATE );
6302 }
6303 psa_aead_abort( &operation );
6304
6305 /* ------------------------------------------------------- */
6306
6307 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6308
6309 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6310 PSA_AEAD_NONCE_MAX_SIZE,
6311 &nonce_length ) );
6312 if( operation.alg == PSA_ALG_CCM )
6313 {
6314 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6315 finish_output_size,
6316 &output_part_length,
6317 tag_buffer, tag_length,
6318 &tag_size ) );
6319 }
6320 else
6321 {
6322 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6323 finish_output_size,
6324 &output_part_length,
6325 tag_buffer, tag_length,
6326 &tag_size ) );
6327
6328 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6329 input_data->len ),
6330 PSA_ERROR_BAD_STATE );
6331 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006332 psa_aead_abort( &operation );
6333
Paul Elliott243080c2021-07-21 19:01:17 +01006334 /* Test for not sending any additional data or data after setting non zero
6335 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006336
Paul Elliottc23a9a02021-06-21 18:32:46 +01006337 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6338
6339 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6340
6341 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6342 input_data->len ) );
6343
6344 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6345 finish_output_size,
6346 &output_part_length,
6347 tag_buffer, tag_length,
6348 &tag_size ),
6349 PSA_ERROR_INVALID_ARGUMENT );
6350
6351 psa_aead_abort( &operation );
6352
Paul Elliott243080c2021-07-21 19:01:17 +01006353 /* Test for not sending any additional data or data after setting non-zero
6354 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006355
Paul Elliottc23a9a02021-06-21 18:32:46 +01006356 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6357
6358 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6359
6360 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6361 input_data->len ) );
6362
6363 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6364 finish_output_size,
6365 &output_part_length,
6366 tag_buffer,
6367 tag_length ),
6368 PSA_ERROR_INVALID_ARGUMENT );
6369
6370 psa_aead_abort( &operation );
6371
Paul Elliott243080c2021-07-21 19:01:17 +01006372 /* Test for not sending any additional data after setting a non-zero length
6373 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006374
Paul Elliottc23a9a02021-06-21 18:32:46 +01006375 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6376
6377 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6378
6379 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6380 input_data->len ) );
6381
6382 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6383 input_data->len, output_data,
6384 output_size, &output_length ),
6385 PSA_ERROR_INVALID_ARGUMENT );
6386
6387 psa_aead_abort( &operation );
6388
Paul Elliottf94bd992021-09-19 18:15:59 +01006389 /* Test for not sending any data after setting a non-zero length for it.*/
6390
Paul Elliottf94bd992021-09-19 18:15:59 +01006391 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6392
6393 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6394
6395 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6396 input_data->len ) );
6397
6398 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6399 additional_data->len ) );
6400
6401 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6402 finish_output_size,
6403 &output_part_length,
6404 tag_buffer, tag_length,
6405 &tag_size ),
6406 PSA_ERROR_INVALID_ARGUMENT );
6407
6408 psa_aead_abort( &operation );
6409
Paul Elliottb0450fe2021-09-01 15:06:26 +01006410 /* Test for sending too much additional data after setting lengths. */
6411
Paul Elliottb0450fe2021-09-01 15:06:26 +01006412 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6413
6414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6415
6416 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6417
6418
6419 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6420 additional_data->len ),
6421 PSA_ERROR_INVALID_ARGUMENT );
6422
6423 psa_aead_abort( &operation );
6424
Paul Elliotta2a09b02021-09-22 14:56:40 +01006425 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006426
6427 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6428
6429 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6430
6431 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6432 input_data->len ) );
6433
6434 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6435 additional_data->len ) );
6436
6437 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6438 1 ),
6439 PSA_ERROR_INVALID_ARGUMENT );
6440
6441 psa_aead_abort( &operation );
6442
Paul Elliottb0450fe2021-09-01 15:06:26 +01006443 /* Test for sending too much data after setting lengths. */
6444
Paul Elliottb0450fe2021-09-01 15:06:26 +01006445 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6446
6447 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6448
6449 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6450
6451 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6452 input_data->len, output_data,
6453 output_size, &output_length ),
6454 PSA_ERROR_INVALID_ARGUMENT );
6455
6456 psa_aead_abort( &operation );
6457
Paul Elliotta2a09b02021-09-22 14:56:40 +01006458 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006459
6460 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6461
6462 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6463
6464 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6465 input_data->len ) );
6466
6467 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6468 additional_data->len ) );
6469
6470 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6471 input_data->len, output_data,
6472 output_size, &output_length ) );
6473
6474 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6475 1, output_data,
6476 output_size, &output_length ),
6477 PSA_ERROR_INVALID_ARGUMENT );
6478
6479 psa_aead_abort( &operation );
6480
Paul Elliottc23a9a02021-06-21 18:32:46 +01006481 /* Test sending additional data after data. */
6482
Paul Elliottc23a9a02021-06-21 18:32:46 +01006483 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6484
6485 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6486
Andrzej Kurekad837522021-12-15 15:28:49 +01006487 if( operation.alg != PSA_ALG_CCM )
6488 {
6489 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6490 input_data->len, output_data,
6491 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006492
Andrzej Kurekad837522021-12-15 15:28:49 +01006493 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6494 additional_data->len ),
6495 PSA_ERROR_BAD_STATE );
6496 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006497 psa_aead_abort( &operation );
6498
Paul Elliott534d0b42021-06-22 19:15:20 +01006499 /* Test calling finish on decryption. */
6500
Paul Elliott534d0b42021-06-22 19:15:20 +01006501 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6502
6503 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6504
6505 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6506 finish_output_size,
6507 &output_part_length,
6508 tag_buffer, tag_length,
6509 &tag_size ),
6510 PSA_ERROR_BAD_STATE );
6511
6512 psa_aead_abort( &operation );
6513
6514 /* Test calling verify on encryption. */
6515
Paul Elliott534d0b42021-06-22 19:15:20 +01006516 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6517
6518 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6519
6520 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6521 finish_output_size,
6522 &output_part_length,
6523 tag_buffer,
6524 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006525 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006526
6527 psa_aead_abort( &operation );
6528
6529
Paul Elliottc23a9a02021-06-21 18:32:46 +01006530exit:
6531 psa_destroy_key( key );
6532 psa_aead_abort( &operation );
6533 mbedtls_free( output_data );
6534 mbedtls_free( final_data );
6535 PSA_DONE( );
6536}
6537/* END_CASE */
6538
6539/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006540void signature_size( int type_arg,
6541 int bits,
6542 int alg_arg,
6543 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006544{
6545 psa_key_type_t type = type_arg;
6546 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006547 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006548
Gilles Peskinefe11b722018-12-18 00:24:04 +01006549 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006550
Gilles Peskinee59236f2018-01-27 23:32:46 +01006551exit:
6552 ;
6553}
6554/* END_CASE */
6555
6556/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006557void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6558 int alg_arg, data_t *input_data,
6559 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006560{
Ronald Cron5425a212020-08-04 14:58:35 +02006561 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006562 psa_key_type_t key_type = key_type_arg;
6563 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006564 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006565 unsigned char *signature = NULL;
6566 size_t signature_size;
6567 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006569
Gilles Peskine8817f612018-12-18 00:18:46 +01006570 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006571
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006572 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006573 psa_set_key_algorithm( &attributes, alg );
6574 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006575
Gilles Peskine049c7532019-05-15 20:22:09 +02006576 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006577 &key ) );
6578 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006579 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006580
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006581 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006582 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006583 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006584 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006585 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006586 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006587 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006588
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006589 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006590 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006591 input_data->x, input_data->len,
6592 signature, signature_size,
6593 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006594 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006595 ASSERT_COMPARE( output_data->x, output_data->len,
6596 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006597
6598exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006599 /*
6600 * Key attributes may have been returned by psa_get_key_attributes()
6601 * thus reset them as required.
6602 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006603 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006604
Ronald Cron5425a212020-08-04 14:58:35 +02006605 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006606 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006607 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006608}
6609/* END_CASE */
6610
6611/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006612void sign_hash_fail( int key_type_arg, data_t *key_data,
6613 int alg_arg, data_t *input_data,
6614 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006615{
Ronald Cron5425a212020-08-04 14:58:35 +02006616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006617 psa_key_type_t key_type = key_type_arg;
6618 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006619 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006620 psa_status_t actual_status;
6621 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006622 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006623 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006625
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006626 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006627
Gilles Peskine8817f612018-12-18 00:18:46 +01006628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006629
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006631 psa_set_key_algorithm( &attributes, alg );
6632 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006633
Gilles Peskine049c7532019-05-15 20:22:09 +02006634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006635 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006636
Ronald Cron5425a212020-08-04 14:58:35 +02006637 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006638 input_data->x, input_data->len,
6639 signature, signature_size,
6640 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006641 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006642 /* The value of *signature_length is unspecified on error, but
6643 * whatever it is, it should be less than signature_size, so that
6644 * if the caller tries to read *signature_length bytes without
6645 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006646 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006647
6648exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006649 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006650 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006651 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006652 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006653}
6654/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006655
6656/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006657void sign_verify_hash( int key_type_arg, data_t *key_data,
6658 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006659{
Ronald Cron5425a212020-08-04 14:58:35 +02006660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006661 psa_key_type_t key_type = key_type_arg;
6662 psa_algorithm_t alg = alg_arg;
6663 size_t key_bits;
6664 unsigned char *signature = NULL;
6665 size_t signature_size;
6666 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006668
Gilles Peskine8817f612018-12-18 00:18:46 +01006669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006670
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006672 psa_set_key_algorithm( &attributes, alg );
6673 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006674
Gilles Peskine049c7532019-05-15 20:22:09 +02006675 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006676 &key ) );
6677 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006678 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006679
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006680 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006681 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006682 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006683 key_bits, alg );
6684 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006685 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006686 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006687
6688 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006689 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006690 input_data->x, input_data->len,
6691 signature, signature_size,
6692 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006693 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006694 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006695 TEST_ASSERT( signature_length > 0 );
6696
6697 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006698 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006699 input_data->x, input_data->len,
6700 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006701
6702 if( input_data->len != 0 )
6703 {
6704 /* Flip a bit in the input and verify that the signature is now
6705 * detected as invalid. Flip a bit at the beginning, not at the end,
6706 * because ECDSA may ignore the last few bits of the input. */
6707 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006708 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006709 input_data->x, input_data->len,
6710 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006711 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006712 }
6713
6714exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006715 /*
6716 * Key attributes may have been returned by psa_get_key_attributes()
6717 * thus reset them as required.
6718 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006719 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006720
Ronald Cron5425a212020-08-04 14:58:35 +02006721 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006722 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006723 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006724}
6725/* END_CASE */
6726
6727/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006728void verify_hash( int key_type_arg, data_t *key_data,
6729 int alg_arg, data_t *hash_data,
6730 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006731{
Ronald Cron5425a212020-08-04 14:58:35 +02006732 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006733 psa_key_type_t key_type = key_type_arg;
6734 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006735 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006736
Gilles Peskine7be11a72022-04-14 00:12:57 +02006737 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006738
Gilles Peskine8817f612018-12-18 00:18:46 +01006739 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006740
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006741 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006742 psa_set_key_algorithm( &attributes, alg );
6743 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006744
Gilles Peskine049c7532019-05-15 20:22:09 +02006745 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006746 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006747
Ronald Cron5425a212020-08-04 14:58:35 +02006748 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006749 hash_data->x, hash_data->len,
6750 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006751
itayzafrir5c753392018-05-08 11:18:38 +03006752exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006753 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006754 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006755 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006756}
6757/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006758
6759/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006760void verify_hash_fail( int key_type_arg, data_t *key_data,
6761 int alg_arg, data_t *hash_data,
6762 data_t *signature_data,
6763 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006764{
Ronald Cron5425a212020-08-04 14:58:35 +02006765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006766 psa_key_type_t key_type = key_type_arg;
6767 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006768 psa_status_t actual_status;
6769 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006771
Gilles Peskine8817f612018-12-18 00:18:46 +01006772 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006773
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006775 psa_set_key_algorithm( &attributes, alg );
6776 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006777
Gilles Peskine049c7532019-05-15 20:22:09 +02006778 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006779 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006780
Ronald Cron5425a212020-08-04 14:58:35 +02006781 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006782 hash_data->x, hash_data->len,
6783 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006784 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006785
6786exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006787 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006788 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006789 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006790}
6791/* END_CASE */
6792
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006793/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006794void sign_message_deterministic( int key_type_arg,
6795 data_t *key_data,
6796 int alg_arg,
6797 data_t *input_data,
6798 data_t *output_data )
6799{
6800 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6801 psa_key_type_t key_type = key_type_arg;
6802 psa_algorithm_t alg = alg_arg;
6803 size_t key_bits;
6804 unsigned char *signature = NULL;
6805 size_t signature_size;
6806 size_t signature_length = 0xdeadbeef;
6807 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6808
6809 PSA_ASSERT( psa_crypto_init( ) );
6810
6811 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6812 psa_set_key_algorithm( &attributes, alg );
6813 psa_set_key_type( &attributes, key_type );
6814
6815 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6816 &key ) );
6817 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6818 key_bits = psa_get_key_bits( &attributes );
6819
6820 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6821 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006822 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006823 ASSERT_ALLOC( signature, signature_size );
6824
6825 PSA_ASSERT( psa_sign_message( key, alg,
6826 input_data->x, input_data->len,
6827 signature, signature_size,
6828 &signature_length ) );
6829
6830 ASSERT_COMPARE( output_data->x, output_data->len,
6831 signature, signature_length );
6832
6833exit:
6834 psa_reset_key_attributes( &attributes );
6835
6836 psa_destroy_key( key );
6837 mbedtls_free( signature );
6838 PSA_DONE( );
6839
6840}
6841/* END_CASE */
6842
6843/* BEGIN_CASE */
6844void sign_message_fail( int key_type_arg,
6845 data_t *key_data,
6846 int alg_arg,
6847 data_t *input_data,
6848 int signature_size_arg,
6849 int expected_status_arg )
6850{
6851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6852 psa_key_type_t key_type = key_type_arg;
6853 psa_algorithm_t alg = alg_arg;
6854 size_t signature_size = signature_size_arg;
6855 psa_status_t actual_status;
6856 psa_status_t expected_status = expected_status_arg;
6857 unsigned char *signature = NULL;
6858 size_t signature_length = 0xdeadbeef;
6859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6860
6861 ASSERT_ALLOC( signature, signature_size );
6862
6863 PSA_ASSERT( psa_crypto_init( ) );
6864
6865 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6866 psa_set_key_algorithm( &attributes, alg );
6867 psa_set_key_type( &attributes, key_type );
6868
6869 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6870 &key ) );
6871
6872 actual_status = psa_sign_message( key, alg,
6873 input_data->x, input_data->len,
6874 signature, signature_size,
6875 &signature_length );
6876 TEST_EQUAL( actual_status, expected_status );
6877 /* The value of *signature_length is unspecified on error, but
6878 * whatever it is, it should be less than signature_size, so that
6879 * if the caller tries to read *signature_length bytes without
6880 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006881 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006882
6883exit:
6884 psa_reset_key_attributes( &attributes );
6885 psa_destroy_key( key );
6886 mbedtls_free( signature );
6887 PSA_DONE( );
6888}
6889/* END_CASE */
6890
6891/* BEGIN_CASE */
6892void sign_verify_message( int key_type_arg,
6893 data_t *key_data,
6894 int alg_arg,
6895 data_t *input_data )
6896{
6897 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6898 psa_key_type_t key_type = key_type_arg;
6899 psa_algorithm_t alg = alg_arg;
6900 size_t key_bits;
6901 unsigned char *signature = NULL;
6902 size_t signature_size;
6903 size_t signature_length = 0xdeadbeef;
6904 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6905
6906 PSA_ASSERT( psa_crypto_init( ) );
6907
6908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6909 PSA_KEY_USAGE_VERIFY_MESSAGE );
6910 psa_set_key_algorithm( &attributes, alg );
6911 psa_set_key_type( &attributes, key_type );
6912
6913 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6914 &key ) );
6915 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6916 key_bits = psa_get_key_bits( &attributes );
6917
6918 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6919 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006920 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006921 ASSERT_ALLOC( signature, signature_size );
6922
6923 PSA_ASSERT( psa_sign_message( key, alg,
6924 input_data->x, input_data->len,
6925 signature, signature_size,
6926 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006927 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006928 TEST_ASSERT( signature_length > 0 );
6929
6930 PSA_ASSERT( psa_verify_message( key, alg,
6931 input_data->x, input_data->len,
6932 signature, signature_length ) );
6933
6934 if( input_data->len != 0 )
6935 {
6936 /* Flip a bit in the input and verify that the signature is now
6937 * detected as invalid. Flip a bit at the beginning, not at the end,
6938 * because ECDSA may ignore the last few bits of the input. */
6939 input_data->x[0] ^= 1;
6940 TEST_EQUAL( psa_verify_message( key, alg,
6941 input_data->x, input_data->len,
6942 signature, signature_length ),
6943 PSA_ERROR_INVALID_SIGNATURE );
6944 }
6945
6946exit:
6947 psa_reset_key_attributes( &attributes );
6948
6949 psa_destroy_key( key );
6950 mbedtls_free( signature );
6951 PSA_DONE( );
6952}
6953/* END_CASE */
6954
6955/* BEGIN_CASE */
6956void verify_message( int key_type_arg,
6957 data_t *key_data,
6958 int alg_arg,
6959 data_t *input_data,
6960 data_t *signature_data )
6961{
6962 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6963 psa_key_type_t key_type = key_type_arg;
6964 psa_algorithm_t alg = alg_arg;
6965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6966
Gilles Peskine7be11a72022-04-14 00:12:57 +02006967 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006968
6969 PSA_ASSERT( psa_crypto_init( ) );
6970
6971 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6972 psa_set_key_algorithm( &attributes, alg );
6973 psa_set_key_type( &attributes, key_type );
6974
6975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6976 &key ) );
6977
6978 PSA_ASSERT( psa_verify_message( key, alg,
6979 input_data->x, input_data->len,
6980 signature_data->x, signature_data->len ) );
6981
6982exit:
6983 psa_reset_key_attributes( &attributes );
6984 psa_destroy_key( key );
6985 PSA_DONE( );
6986}
6987/* END_CASE */
6988
6989/* BEGIN_CASE */
6990void verify_message_fail( int key_type_arg,
6991 data_t *key_data,
6992 int alg_arg,
6993 data_t *hash_data,
6994 data_t *signature_data,
6995 int expected_status_arg )
6996{
6997 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6998 psa_key_type_t key_type = key_type_arg;
6999 psa_algorithm_t alg = alg_arg;
7000 psa_status_t actual_status;
7001 psa_status_t expected_status = expected_status_arg;
7002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7003
7004 PSA_ASSERT( psa_crypto_init( ) );
7005
7006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
7007 psa_set_key_algorithm( &attributes, alg );
7008 psa_set_key_type( &attributes, key_type );
7009
7010 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
7011 &key ) );
7012
7013 actual_status = psa_verify_message( key, alg,
7014 hash_data->x, hash_data->len,
7015 signature_data->x,
7016 signature_data->len );
7017 TEST_EQUAL( actual_status, expected_status );
7018
7019exit:
7020 psa_reset_key_attributes( &attributes );
7021 psa_destroy_key( key );
7022 PSA_DONE( );
7023}
7024/* END_CASE */
7025
7026/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007027void asymmetric_encrypt( int key_type_arg,
7028 data_t *key_data,
7029 int alg_arg,
7030 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007031 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007032 int expected_output_length_arg,
7033 int expected_status_arg )
7034{
Ronald Cron5425a212020-08-04 14:58:35 +02007035 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007036 psa_key_type_t key_type = key_type_arg;
7037 psa_algorithm_t alg = alg_arg;
7038 size_t expected_output_length = expected_output_length_arg;
7039 size_t key_bits;
7040 unsigned char *output = NULL;
7041 size_t output_size;
7042 size_t output_length = ~0;
7043 psa_status_t actual_status;
7044 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007045 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007046
Gilles Peskine8817f612018-12-18 00:18:46 +01007047 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007048
Gilles Peskine656896e2018-06-29 19:12:28 +02007049 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007050 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7051 psa_set_key_algorithm( &attributes, alg );
7052 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007053 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007054 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007055
7056 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007057 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007058 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007059
Gilles Peskine656896e2018-06-29 19:12:28 +02007060 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007061 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007062 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007063
7064 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007065 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007066 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007067 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007068 output, output_size,
7069 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007070 TEST_EQUAL( actual_status, expected_status );
7071 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007072
Gilles Peskine68428122018-06-30 18:42:41 +02007073 /* If the label is empty, the test framework puts a non-null pointer
7074 * in label->x. Test that a null pointer works as well. */
7075 if( label->len == 0 )
7076 {
7077 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007078 if( output_size != 0 )
7079 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007080 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007081 input_data->x, input_data->len,
7082 NULL, label->len,
7083 output, output_size,
7084 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007085 TEST_EQUAL( actual_status, expected_status );
7086 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007087 }
7088
Gilles Peskine656896e2018-06-29 19:12:28 +02007089exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007090 /*
7091 * Key attributes may have been returned by psa_get_key_attributes()
7092 * thus reset them as required.
7093 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007094 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007095
Ronald Cron5425a212020-08-04 14:58:35 +02007096 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007097 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007098 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007099}
7100/* END_CASE */
7101
7102/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007103void asymmetric_encrypt_decrypt( int key_type_arg,
7104 data_t *key_data,
7105 int alg_arg,
7106 data_t *input_data,
7107 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007108{
Ronald Cron5425a212020-08-04 14:58:35 +02007109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007110 psa_key_type_t key_type = key_type_arg;
7111 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007112 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007113 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007114 size_t output_size;
7115 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007116 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007117 size_t output2_size;
7118 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007120
Gilles Peskine8817f612018-12-18 00:18:46 +01007121 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007122
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007123 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7124 psa_set_key_algorithm( &attributes, alg );
7125 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007126
Gilles Peskine049c7532019-05-15 20:22:09 +02007127 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007128 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007129
7130 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007131 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007132 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007133
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007134 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007135 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007136 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007137
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007138 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007139 TEST_LE_U( output2_size,
7140 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7141 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007142 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007143
Gilles Peskineeebd7382018-06-08 18:11:54 +02007144 /* We test encryption by checking that encrypt-then-decrypt gives back
7145 * the original plaintext because of the non-optional random
7146 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007147 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007148 input_data->x, input_data->len,
7149 label->x, label->len,
7150 output, output_size,
7151 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007152 /* We don't know what ciphertext length to expect, but check that
7153 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007154 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007155
Ronald Cron5425a212020-08-04 14:58:35 +02007156 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007157 output, output_length,
7158 label->x, label->len,
7159 output2, output2_size,
7160 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007161 ASSERT_COMPARE( input_data->x, input_data->len,
7162 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007163
7164exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007165 /*
7166 * Key attributes may have been returned by psa_get_key_attributes()
7167 * thus reset them as required.
7168 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007169 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007170
Ronald Cron5425a212020-08-04 14:58:35 +02007171 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007172 mbedtls_free( output );
7173 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007174 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007175}
7176/* END_CASE */
7177
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007178/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007179void asymmetric_decrypt( int key_type_arg,
7180 data_t *key_data,
7181 int alg_arg,
7182 data_t *input_data,
7183 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007184 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007185{
Ronald Cron5425a212020-08-04 14:58:35 +02007186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007187 psa_key_type_t key_type = key_type_arg;
7188 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007189 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007190 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007191 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007192 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007194
Gilles Peskine8817f612018-12-18 00:18:46 +01007195 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007196
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007197 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7198 psa_set_key_algorithm( &attributes, alg );
7199 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007200
Gilles Peskine049c7532019-05-15 20:22:09 +02007201 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007202 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007203
gabor-mezei-armceface22021-01-21 12:26:17 +01007204 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7205 key_bits = psa_get_key_bits( &attributes );
7206
7207 /* Determine the maximum ciphertext length */
7208 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007209 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007210 ASSERT_ALLOC( output, output_size );
7211
Ronald Cron5425a212020-08-04 14:58:35 +02007212 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007213 input_data->x, input_data->len,
7214 label->x, label->len,
7215 output,
7216 output_size,
7217 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007218 ASSERT_COMPARE( expected_data->x, expected_data->len,
7219 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007220
Gilles Peskine68428122018-06-30 18:42:41 +02007221 /* If the label is empty, the test framework puts a non-null pointer
7222 * in label->x. Test that a null pointer works as well. */
7223 if( label->len == 0 )
7224 {
7225 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007226 if( output_size != 0 )
7227 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007228 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007229 input_data->x, input_data->len,
7230 NULL, label->len,
7231 output,
7232 output_size,
7233 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007234 ASSERT_COMPARE( expected_data->x, expected_data->len,
7235 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007236 }
7237
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007238exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007239 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007240 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007241 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007242 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007243}
7244/* END_CASE */
7245
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007246/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007247void asymmetric_decrypt_fail( int key_type_arg,
7248 data_t *key_data,
7249 int alg_arg,
7250 data_t *input_data,
7251 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007252 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007253 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007254{
Ronald Cron5425a212020-08-04 14:58:35 +02007255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256 psa_key_type_t key_type = key_type_arg;
7257 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007258 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007259 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007260 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261 psa_status_t actual_status;
7262 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007264
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007265 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007266
Gilles Peskine8817f612018-12-18 00:18:46 +01007267 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007268
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007269 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7270 psa_set_key_algorithm( &attributes, alg );
7271 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007272
Gilles Peskine049c7532019-05-15 20:22:09 +02007273 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007274 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007275
Ronald Cron5425a212020-08-04 14:58:35 +02007276 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007277 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007278 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007279 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007280 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007281 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007282 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007283
Gilles Peskine68428122018-06-30 18:42:41 +02007284 /* If the label is empty, the test framework puts a non-null pointer
7285 * in label->x. Test that a null pointer works as well. */
7286 if( label->len == 0 )
7287 {
7288 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007289 if( output_size != 0 )
7290 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007291 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007292 input_data->x, input_data->len,
7293 NULL, label->len,
7294 output, output_size,
7295 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007296 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007297 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007298 }
7299
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007300exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007301 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007302 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007303 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007304 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007305}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007306/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007307
7308/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007309void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007310{
7311 /* Test each valid way of initializing the object, except for `= {0}`, as
7312 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7313 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007314 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007315 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007316 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7317 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7318 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007319
7320 memset( &zero, 0, sizeof( zero ) );
7321
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007322 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007323 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007324 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007325 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007326 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007327 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007328 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007329
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007330 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007331 PSA_ASSERT( psa_key_derivation_abort(&func) );
7332 PSA_ASSERT( psa_key_derivation_abort(&init) );
7333 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007334}
7335/* END_CASE */
7336
Janos Follath16de4a42019-06-13 16:32:24 +01007337/* BEGIN_CASE */
7338void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007339{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007340 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007341 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007342 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007343
Gilles Peskine8817f612018-12-18 00:18:46 +01007344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007345
Janos Follath16de4a42019-06-13 16:32:24 +01007346 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007347 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007348
7349exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007350 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007351 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007352}
7353/* END_CASE */
7354
Janos Follathaf3c2a02019-06-12 12:34:34 +01007355/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007356void derive_set_capacity( int alg_arg, int capacity_arg,
7357 int expected_status_arg )
7358{
7359 psa_algorithm_t alg = alg_arg;
7360 size_t capacity = capacity_arg;
7361 psa_status_t expected_status = expected_status_arg;
7362 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7363
7364 PSA_ASSERT( psa_crypto_init( ) );
7365
7366 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7367
7368 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7369 expected_status );
7370
7371exit:
7372 psa_key_derivation_abort( &operation );
7373 PSA_DONE( );
7374}
7375/* END_CASE */
7376
7377/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007378void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007379 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007380 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007381 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007382 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007383 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007384 int expected_status_arg3,
7385 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007386{
7387 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007388 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7389 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007390 psa_status_t expected_statuses[] = {expected_status_arg1,
7391 expected_status_arg2,
7392 expected_status_arg3};
7393 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007394 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7395 MBEDTLS_SVC_KEY_ID_INIT,
7396 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007397 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7399 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007400 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007401 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007402 psa_status_t expected_output_status = expected_output_status_arg;
7403 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007404
7405 PSA_ASSERT( psa_crypto_init( ) );
7406
7407 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7408 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007409
7410 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7411
7412 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7413 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007414 mbedtls_test_set_step( i );
7415 if( steps[i] == 0 )
7416 {
7417 /* Skip this step */
7418 }
7419 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007420 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007421 psa_set_key_type( &attributes, key_types[i] );
7422 PSA_ASSERT( psa_import_key( &attributes,
7423 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007424 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007425 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7426 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7427 {
7428 // When taking a private key as secret input, use key agreement
7429 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007430 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7431 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007432 expected_statuses[i] );
7433 }
7434 else
7435 {
7436 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007437 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007438 expected_statuses[i] );
7439 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007440 }
7441 else
7442 {
7443 TEST_EQUAL( psa_key_derivation_input_bytes(
7444 &operation, steps[i],
7445 inputs[i]->x, inputs[i]->len ),
7446 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007447 }
7448 }
7449
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007450 if( output_key_type != PSA_KEY_TYPE_NONE )
7451 {
7452 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007453 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007454 psa_set_key_bits( &attributes, 8 );
7455 actual_output_status =
7456 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007457 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007458 }
7459 else
7460 {
7461 uint8_t buffer[1];
7462 actual_output_status =
7463 psa_key_derivation_output_bytes( &operation,
7464 buffer, sizeof( buffer ) );
7465 }
7466 TEST_EQUAL( actual_output_status, expected_output_status );
7467
Janos Follathaf3c2a02019-06-12 12:34:34 +01007468exit:
7469 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007470 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7471 psa_destroy_key( keys[i] );
7472 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007473 PSA_DONE( );
7474}
7475/* END_CASE */
7476
Janos Follathd958bb72019-07-03 15:02:16 +01007477/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007478void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007479{
Janos Follathd958bb72019-07-03 15:02:16 +01007480 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007482 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007483 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007484 unsigned char input1[] = "Input 1";
7485 size_t input1_length = sizeof( input1 );
7486 unsigned char input2[] = "Input 2";
7487 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007488 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007489 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007490 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7491 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7492 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007494
Gilles Peskine8817f612018-12-18 00:18:46 +01007495 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007496
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007497 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7498 psa_set_key_algorithm( &attributes, alg );
7499 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007500
Gilles Peskine73676cb2019-05-15 20:15:10 +02007501 PSA_ASSERT( psa_import_key( &attributes,
7502 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007503 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007504
7505 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007506 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7507 input1, input1_length,
7508 input2, input2_length,
7509 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007510 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007511
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007512 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007513 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007514 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007515
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007516 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007517
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007518 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007519 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007520
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007521exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007522 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007523 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007524 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007525}
7526/* END_CASE */
7527
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007528/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007529void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007530{
7531 uint8_t output_buffer[16];
7532 size_t buffer_size = 16;
7533 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007534 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007535
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007536 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7537 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007538 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007539
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007540 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007541 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007542
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007543 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007544
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007545 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7546 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007547 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007548
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007549 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007550 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007551
7552exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007553 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007554}
7555/* END_CASE */
7556
7557/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007558void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007559 int step1_arg, data_t *input1, int expected_status_arg1,
7560 int step2_arg, data_t *input2, int expected_status_arg2,
7561 int step3_arg, data_t *input3, int expected_status_arg3,
7562 int step4_arg, data_t *input4, int expected_status_arg4,
7563 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007564 int requested_capacity_arg,
7565 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007566 data_t *expected_output2,
7567 int other_key_input_type,
7568 int key_input_type,
7569 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007570{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007571 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007572 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7573 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007574 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7575 MBEDTLS_SVC_KEY_ID_INIT,
7576 MBEDTLS_SVC_KEY_ID_INIT,
7577 MBEDTLS_SVC_KEY_ID_INIT};
7578 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7579 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007580 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007581 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007582 uint8_t *expected_outputs[2] =
7583 {expected_output1->x, expected_output2->x};
7584 size_t output_sizes[2] =
7585 {expected_output1->len, expected_output2->len};
7586 size_t output_buffer_size = 0;
7587 uint8_t *output_buffer = NULL;
7588 size_t expected_capacity;
7589 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007590 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7591 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7592 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7593 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007594 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007595 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007596 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007597
7598 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7599 {
7600 if( output_sizes[i] > output_buffer_size )
7601 output_buffer_size = output_sizes[i];
7602 if( output_sizes[i] == 0 )
7603 expected_outputs[i] = NULL;
7604 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007605 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007606 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007607
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007608 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007609 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7610 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7611 requested_capacity ) );
7612 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007613 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007614 switch( steps[i] )
7615 {
7616 case 0:
7617 break;
7618 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007619 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007620 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007621 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007622 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007623 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007624 inputs[i]->x, inputs[i]->len ),
7625 statuses[i] );
7626
7627 if( statuses[i] != PSA_SUCCESS )
7628 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007629 break;
7630 case 1: // input key
7631 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7632 psa_set_key_algorithm( &attributes1, alg );
7633 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7634
7635 PSA_ASSERT( psa_import_key( &attributes1,
7636 inputs[i]->x, inputs[i]->len,
7637 &keys[i] ) );
7638
Przemek Stekiel38647de2022-04-19 13:27:47 +02007639 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007640 {
7641 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007642 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7643 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007644 }
7645
Przemek Stekiel38647de2022-04-19 13:27:47 +02007646 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7647 steps[i],
7648 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007649 break;
7650 default:
7651 TEST_ASSERT( ! "default case not supported" );
7652 break;
7653 }
7654 break;
7655 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007656 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007657 {
7658 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007659 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7660 steps[i],
7661 inputs[i]->x,
7662 inputs[i]->len ),
7663 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007664 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007665 case 1: // input key, type DERIVE
7666 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007667 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7668 psa_set_key_algorithm( &attributes2, alg );
7669 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7670
7671 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007672 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007673 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7674
7675 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007676 inputs[i]->x, inputs[i]->len,
7677 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007678
Przemek Stekiel38647de2022-04-19 13:27:47 +02007679 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7680 steps[i],
7681 keys[i] ),
7682 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007683 break;
7684 case 2: // key agreement
7685 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7686 psa_set_key_algorithm( &attributes3, alg );
7687 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7688
7689 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007690 inputs[i]->x, inputs[i]->len,
7691 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007692
7693 TEST_EQUAL( psa_key_derivation_key_agreement(
7694 &operation,
7695 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7696 keys[i], key_agreement_peer_key->x,
7697 key_agreement_peer_key->len ), statuses[i] );
7698 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007699 default:
7700 TEST_ASSERT( ! "default case not supported" );
7701 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007702 }
7703
Przemek Stekiel38647de2022-04-19 13:27:47 +02007704 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007705 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007706 break;
7707 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007708 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007709 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007710 inputs[i]->x, inputs[i]->len ), statuses[i] );
7711
7712 if( statuses[i] != PSA_SUCCESS )
7713 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007714 break;
7715 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007716 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007717
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007718 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007719 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007720 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007721 expected_capacity = requested_capacity;
7722
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007723 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007724 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007725 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7726
7727 /* For output key derivation secret must be provided using
7728 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007729 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007730 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007731
7732 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7733 psa_set_key_algorithm( &attributes4, alg );
7734 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007735 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007736
7737 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007738 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007739 }
7740 else // output bytes
7741 {
7742 /* Expansion phase. */
7743 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007744 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007745 /* Read some bytes. */
7746 status = psa_key_derivation_output_bytes( &operation,
7747 output_buffer, output_sizes[i] );
7748 if( expected_capacity == 0 && output_sizes[i] == 0 )
7749 {
7750 /* Reading 0 bytes when 0 bytes are available can go either way. */
7751 TEST_ASSERT( status == PSA_SUCCESS ||
7752 status == PSA_ERROR_INSUFFICIENT_DATA );
7753 continue;
7754 }
7755 else if( expected_capacity == 0 ||
7756 output_sizes[i] > expected_capacity )
7757 {
7758 /* Capacity exceeded. */
7759 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7760 expected_capacity = 0;
7761 continue;
7762 }
7763 /* Success. Check the read data. */
7764 PSA_ASSERT( status );
7765 if( output_sizes[i] != 0 )
7766 ASSERT_COMPARE( output_buffer, output_sizes[i],
7767 expected_outputs[i], output_sizes[i] );
7768 /* Check the operation status. */
7769 expected_capacity -= output_sizes[i];
7770 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7771 &current_capacity ) );
7772 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007773 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007774 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007775 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007776
7777exit:
7778 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007779 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007780 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7781 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007782 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007783 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007784}
7785/* END_CASE */
7786
7787/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007788void derive_full( int alg_arg,
7789 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007790 data_t *input1,
7791 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007792 int requested_capacity_arg )
7793{
Ronald Cron5425a212020-08-04 14:58:35 +02007794 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007795 psa_algorithm_t alg = alg_arg;
7796 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007797 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007798 unsigned char output_buffer[16];
7799 size_t expected_capacity = requested_capacity;
7800 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007801 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007802
Gilles Peskine8817f612018-12-18 00:18:46 +01007803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007804
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007805 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7806 psa_set_key_algorithm( &attributes, alg );
7807 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007808
Gilles Peskine049c7532019-05-15 20:22:09 +02007809 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007810 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007811
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007812 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7813 input1->x, input1->len,
7814 input2->x, input2->len,
7815 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007816 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007817
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007818 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007819 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007820 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007821
7822 /* Expansion phase. */
7823 while( current_capacity > 0 )
7824 {
7825 size_t read_size = sizeof( output_buffer );
7826 if( read_size > current_capacity )
7827 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007828 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007829 output_buffer,
7830 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007831 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007832 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007833 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007834 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007835 }
7836
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007837 /* Check that the operation refuses to go over capacity. */
7838 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007839 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007840
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007841 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007842
7843exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007844 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007845 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007846 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007847}
7848/* END_CASE */
7849
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007850/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007851void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007852 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007853 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007854 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007855 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007856{
7857 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7858 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007859 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007860 uint8_t *output_buffer = NULL;
7861 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007862 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7863 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7864 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007865
7866 ASSERT_ALLOC( output_buffer, expected_output->len );
7867 PSA_ASSERT( psa_crypto_init() );
7868
7869 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007870 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007871 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007872
7873 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007874 step, input->x, input->len ),
7875 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007876
7877 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7878 goto exit;
7879
7880 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7881 expected_output->len );
7882
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007883 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007884 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7885 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7886 expected_output->len );
7887
7888exit:
7889 mbedtls_free( output_buffer );
7890 psa_key_derivation_abort( &operation );
7891 PSA_DONE();
7892}
7893/* END_CASE */
7894
Janos Follathe60c9052019-07-03 13:51:30 +01007895/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007896void derive_key_exercise( int alg_arg,
7897 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007898 data_t *input1,
7899 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007900 int derived_type_arg,
7901 int derived_bits_arg,
7902 int derived_usage_arg,
7903 int derived_alg_arg )
7904{
Ronald Cron5425a212020-08-04 14:58:35 +02007905 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7906 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007907 psa_algorithm_t alg = alg_arg;
7908 psa_key_type_t derived_type = derived_type_arg;
7909 size_t derived_bits = derived_bits_arg;
7910 psa_key_usage_t derived_usage = derived_usage_arg;
7911 psa_algorithm_t derived_alg = derived_alg_arg;
7912 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007913 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007915 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007916
Gilles Peskine8817f612018-12-18 00:18:46 +01007917 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007918
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007919 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7920 psa_set_key_algorithm( &attributes, alg );
7921 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007922 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007923 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007924
7925 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007926 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7927 input1->x, input1->len,
7928 input2->x, input2->len,
7929 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007930 goto exit;
7931
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007932 psa_set_key_usage_flags( &attributes, derived_usage );
7933 psa_set_key_algorithm( &attributes, derived_alg );
7934 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007935 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007936 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007937 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007938
7939 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007940 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007941 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7942 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007943
7944 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007945 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007946 goto exit;
7947
7948exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007949 /*
7950 * Key attributes may have been returned by psa_get_key_attributes()
7951 * thus reset them as required.
7952 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007953 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007954
7955 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007956 psa_destroy_key( base_key );
7957 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007958 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007959}
7960/* END_CASE */
7961
Janos Follath42fd8882019-07-03 14:17:09 +01007962/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007963void derive_key_export( int alg_arg,
7964 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007965 data_t *input1,
7966 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007967 int bytes1_arg,
7968 int bytes2_arg )
7969{
Ronald Cron5425a212020-08-04 14:58:35 +02007970 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7971 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007972 psa_algorithm_t alg = alg_arg;
7973 size_t bytes1 = bytes1_arg;
7974 size_t bytes2 = bytes2_arg;
7975 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007976 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007977 uint8_t *output_buffer = NULL;
7978 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007979 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7980 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007981 size_t length;
7982
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007983 ASSERT_ALLOC( output_buffer, capacity );
7984 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007986
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007987 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7988 psa_set_key_algorithm( &base_attributes, alg );
7989 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007990 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007991 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007992
7993 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007994 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7995 input1->x, input1->len,
7996 input2->x, input2->len,
7997 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007998 goto exit;
7999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008000 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008001 output_buffer,
8002 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008003 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008004
8005 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008006 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8007 input1->x, input1->len,
8008 input2->x, input2->len,
8009 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008010 goto exit;
8011
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008012 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8013 psa_set_key_algorithm( &derived_attributes, 0 );
8014 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008015 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008016 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008017 &derived_key ) );
8018 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008019 export_buffer, bytes1,
8020 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008021 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02008022 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008023 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008024 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008025 &derived_key ) );
8026 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008027 export_buffer + bytes1, bytes2,
8028 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008029 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008030
8031 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008032 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8033 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008034
8035exit:
8036 mbedtls_free( output_buffer );
8037 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008038 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008039 psa_destroy_key( base_key );
8040 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008041 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008042}
8043/* END_CASE */
8044
8045/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008046void derive_key_type( int alg_arg,
8047 data_t *key_data,
8048 data_t *input1,
8049 data_t *input2,
8050 int key_type_arg, int bits_arg,
8051 data_t *expected_export )
8052{
8053 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8054 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8055 const psa_algorithm_t alg = alg_arg;
8056 const psa_key_type_t key_type = key_type_arg;
8057 const size_t bits = bits_arg;
8058 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8059 const size_t export_buffer_size =
8060 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8061 uint8_t *export_buffer = NULL;
8062 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8063 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8064 size_t export_length;
8065
8066 ASSERT_ALLOC( export_buffer, export_buffer_size );
8067 PSA_ASSERT( psa_crypto_init( ) );
8068
8069 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8070 psa_set_key_algorithm( &base_attributes, alg );
8071 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8072 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8073 &base_key ) );
8074
Przemek Stekielc85f0912022-03-08 11:37:54 +01008075 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008076 &operation, base_key, alg,
8077 input1->x, input1->len,
8078 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008079 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008080 goto exit;
8081
8082 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8083 psa_set_key_algorithm( &derived_attributes, 0 );
8084 psa_set_key_type( &derived_attributes, key_type );
8085 psa_set_key_bits( &derived_attributes, bits );
8086 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8087 &derived_key ) );
8088
8089 PSA_ASSERT( psa_export_key( derived_key,
8090 export_buffer, export_buffer_size,
8091 &export_length ) );
8092 ASSERT_COMPARE( export_buffer, export_length,
8093 expected_export->x, expected_export->len );
8094
8095exit:
8096 mbedtls_free( export_buffer );
8097 psa_key_derivation_abort( &operation );
8098 psa_destroy_key( base_key );
8099 psa_destroy_key( derived_key );
8100 PSA_DONE( );
8101}
8102/* END_CASE */
8103
8104/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008105void derive_key( int alg_arg,
8106 data_t *key_data, data_t *input1, data_t *input2,
8107 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008108 int expected_status_arg,
8109 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008110{
Ronald Cron5425a212020-08-04 14:58:35 +02008111 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8112 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008113 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008114 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008115 size_t bits = bits_arg;
8116 psa_status_t expected_status = expected_status_arg;
8117 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8118 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8119 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8120
8121 PSA_ASSERT( psa_crypto_init( ) );
8122
8123 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8124 psa_set_key_algorithm( &base_attributes, alg );
8125 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8126 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008127 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008128
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008129 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8130 input1->x, input1->len,
8131 input2->x, input2->len,
8132 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008133 goto exit;
8134
8135 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8136 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008137 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008138 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008139
8140 psa_status_t status =
8141 psa_key_derivation_output_key( &derived_attributes,
8142 &operation,
8143 &derived_key );
8144 if( is_large_output > 0 )
8145 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8146 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008147
8148exit:
8149 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008150 psa_destroy_key( base_key );
8151 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008152 PSA_DONE( );
8153}
8154/* END_CASE */
8155
8156/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008157void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008158 int our_key_type_arg, int our_key_alg_arg,
8159 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008160 int expected_status_arg )
8161{
Ronald Cron5425a212020-08-04 14:58:35 +02008162 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008163 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008164 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008165 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008166 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008168 psa_status_t expected_status = expected_status_arg;
8169 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008170
Gilles Peskine8817f612018-12-18 00:18:46 +01008171 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008172
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008174 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008175 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008176 PSA_ASSERT( psa_import_key( &attributes,
8177 our_key_data->x, our_key_data->len,
8178 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008179
Gilles Peskine77f40d82019-04-11 21:27:06 +02008180 /* The tests currently include inputs that should fail at either step.
8181 * Test cases that fail at the setup step should be changed to call
8182 * key_derivation_setup instead, and this function should be renamed
8183 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008184 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008185 if( status == PSA_SUCCESS )
8186 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008187 TEST_EQUAL( psa_key_derivation_key_agreement(
8188 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8189 our_key,
8190 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008191 expected_status );
8192 }
8193 else
8194 {
8195 TEST_ASSERT( status == expected_status );
8196 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008197
8198exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008199 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008200 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008201 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008202}
8203/* END_CASE */
8204
8205/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008206void raw_key_agreement( int alg_arg,
8207 int our_key_type_arg, data_t *our_key_data,
8208 data_t *peer_key_data,
8209 data_t *expected_output )
8210{
Ronald Cron5425a212020-08-04 14:58:35 +02008211 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008212 psa_algorithm_t alg = alg_arg;
8213 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008215 unsigned char *output = NULL;
8216 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008217 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008218
Gilles Peskinef0cba732019-04-11 22:12:38 +02008219 PSA_ASSERT( psa_crypto_init( ) );
8220
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008221 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8222 psa_set_key_algorithm( &attributes, alg );
8223 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008224 PSA_ASSERT( psa_import_key( &attributes,
8225 our_key_data->x, our_key_data->len,
8226 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008227
gabor-mezei-armceface22021-01-21 12:26:17 +01008228 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8229 key_bits = psa_get_key_bits( &attributes );
8230
Gilles Peskine992bee82022-04-13 23:25:52 +02008231 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008232 TEST_LE_U( expected_output->len,
8233 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8234 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8235 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008236
8237 /* Good case with exact output size */
8238 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008239 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8240 peer_key_data->x, peer_key_data->len,
8241 output, expected_output->len,
8242 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008243 ASSERT_COMPARE( output, output_length,
8244 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008245 mbedtls_free( output );
8246 output = NULL;
8247 output_length = ~0;
8248
8249 /* Larger buffer */
8250 ASSERT_ALLOC( output, expected_output->len + 1 );
8251 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8252 peer_key_data->x, peer_key_data->len,
8253 output, expected_output->len + 1,
8254 &output_length ) );
8255 ASSERT_COMPARE( output, output_length,
8256 expected_output->x, expected_output->len );
8257 mbedtls_free( output );
8258 output = NULL;
8259 output_length = ~0;
8260
8261 /* Buffer too small */
8262 ASSERT_ALLOC( output, expected_output->len - 1 );
8263 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8264 peer_key_data->x, peer_key_data->len,
8265 output, expected_output->len - 1,
8266 &output_length ),
8267 PSA_ERROR_BUFFER_TOO_SMALL );
8268 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008269 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008270 mbedtls_free( output );
8271 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008272
8273exit:
8274 mbedtls_free( output );
8275 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008276 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008277}
8278/* END_CASE */
8279
8280/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008281void key_agreement_capacity( int alg_arg,
8282 int our_key_type_arg, data_t *our_key_data,
8283 data_t *peer_key_data,
8284 int expected_capacity_arg )
8285{
Ronald Cron5425a212020-08-04 14:58:35 +02008286 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008287 psa_algorithm_t alg = alg_arg;
8288 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008289 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008291 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008292 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008293
Gilles Peskine8817f612018-12-18 00:18:46 +01008294 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008295
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008296 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8297 psa_set_key_algorithm( &attributes, alg );
8298 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008299 PSA_ASSERT( psa_import_key( &attributes,
8300 our_key_data->x, our_key_data->len,
8301 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008302
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008303 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008304 PSA_ASSERT( psa_key_derivation_key_agreement(
8305 &operation,
8306 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8307 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008308 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8309 {
8310 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008311 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008312 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008313 NULL, 0 ) );
8314 }
Gilles Peskine59685592018-09-18 12:11:34 +02008315
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008316 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008317 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008318 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008319 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008320
Gilles Peskinebf491972018-10-25 22:36:12 +02008321 /* Test the actual capacity by reading the output. */
8322 while( actual_capacity > sizeof( output ) )
8323 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008324 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008325 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008326 actual_capacity -= sizeof( output );
8327 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008328 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008329 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008330 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008331 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008332
Gilles Peskine59685592018-09-18 12:11:34 +02008333exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008334 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008335 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008336 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008337}
8338/* END_CASE */
8339
8340/* BEGIN_CASE */
8341void key_agreement_output( int alg_arg,
8342 int our_key_type_arg, data_t *our_key_data,
8343 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008344 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008345{
Ronald Cron5425a212020-08-04 14:58:35 +02008346 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008347 psa_algorithm_t alg = alg_arg;
8348 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008349 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008351 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008352
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008353 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8354 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008355
Gilles Peskine8817f612018-12-18 00:18:46 +01008356 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008357
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8359 psa_set_key_algorithm( &attributes, alg );
8360 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008361 PSA_ASSERT( psa_import_key( &attributes,
8362 our_key_data->x, our_key_data->len,
8363 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008364
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008365 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008366 PSA_ASSERT( psa_key_derivation_key_agreement(
8367 &operation,
8368 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8369 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008370 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8371 {
8372 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008373 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008374 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008375 NULL, 0 ) );
8376 }
Gilles Peskine59685592018-09-18 12:11:34 +02008377
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008378 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008379 actual_output,
8380 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008381 ASSERT_COMPARE( actual_output, expected_output1->len,
8382 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008383 if( expected_output2->len != 0 )
8384 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008385 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008386 actual_output,
8387 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008388 ASSERT_COMPARE( actual_output, expected_output2->len,
8389 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008390 }
Gilles Peskine59685592018-09-18 12:11:34 +02008391
8392exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008393 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008394 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008395 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008396 mbedtls_free( actual_output );
8397}
8398/* END_CASE */
8399
8400/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008401void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008402{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008403 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008404 unsigned char *output = NULL;
8405 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008406 size_t i;
8407 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008408
Simon Butcher49f8e312020-03-03 15:51:50 +00008409 TEST_ASSERT( bytes_arg >= 0 );
8410
Gilles Peskine91892022021-02-08 19:50:26 +01008411 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008412 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008413
Gilles Peskine8817f612018-12-18 00:18:46 +01008414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008415
Gilles Peskinea50d7392018-06-21 10:22:13 +02008416 /* Run several times, to ensure that every output byte will be
8417 * nonzero at least once with overwhelming probability
8418 * (2^(-8*number_of_runs)). */
8419 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008420 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008421 if( bytes != 0 )
8422 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008423 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008424
Gilles Peskinea50d7392018-06-21 10:22:13 +02008425 for( i = 0; i < bytes; i++ )
8426 {
8427 if( output[i] != 0 )
8428 ++changed[i];
8429 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008430 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008431
8432 /* Check that every byte was changed to nonzero at least once. This
8433 * validates that psa_generate_random is overwriting every byte of
8434 * the output buffer. */
8435 for( i = 0; i < bytes; i++ )
8436 {
8437 TEST_ASSERT( changed[i] != 0 );
8438 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008439
8440exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008441 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008442 mbedtls_free( output );
8443 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008444}
8445/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008446
8447/* BEGIN_CASE */
8448void generate_key( int type_arg,
8449 int bits_arg,
8450 int usage_arg,
8451 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008452 int expected_status_arg,
8453 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008454{
Ronald Cron5425a212020-08-04 14:58:35 +02008455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008456 psa_key_type_t type = type_arg;
8457 psa_key_usage_t usage = usage_arg;
8458 size_t bits = bits_arg;
8459 psa_algorithm_t alg = alg_arg;
8460 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008462 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008463
Gilles Peskine8817f612018-12-18 00:18:46 +01008464 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008465
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008466 psa_set_key_usage_flags( &attributes, usage );
8467 psa_set_key_algorithm( &attributes, alg );
8468 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008469 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008470
8471 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008472 psa_status_t status = psa_generate_key( &attributes, &key );
8473
8474 if( is_large_key > 0 )
8475 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8476 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008477 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008478 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008479
8480 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008481 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008482 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8483 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008484
Gilles Peskine818ca122018-06-20 18:16:48 +02008485 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008486 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008487 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008488
8489exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008490 /*
8491 * Key attributes may have been returned by psa_get_key_attributes()
8492 * thus reset them as required.
8493 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008494 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008495
Ronald Cron5425a212020-08-04 14:58:35 +02008496 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008497 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008498}
8499/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008500
Ronald Cronee414c72021-03-18 18:50:08 +01008501/* 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 +02008502void generate_key_rsa( int bits_arg,
8503 data_t *e_arg,
8504 int expected_status_arg )
8505{
Ronald Cron5425a212020-08-04 14:58:35 +02008506 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008507 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008508 size_t bits = bits_arg;
8509 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8510 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8511 psa_status_t expected_status = expected_status_arg;
8512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8513 uint8_t *exported = NULL;
8514 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008515 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008516 size_t exported_length = SIZE_MAX;
8517 uint8_t *e_read_buffer = NULL;
8518 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008519 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008520 size_t e_read_length = SIZE_MAX;
8521
8522 if( e_arg->len == 0 ||
8523 ( e_arg->len == 3 &&
8524 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8525 {
8526 is_default_public_exponent = 1;
8527 e_read_size = 0;
8528 }
8529 ASSERT_ALLOC( e_read_buffer, e_read_size );
8530 ASSERT_ALLOC( exported, exported_size );
8531
8532 PSA_ASSERT( psa_crypto_init( ) );
8533
8534 psa_set_key_usage_flags( &attributes, usage );
8535 psa_set_key_algorithm( &attributes, alg );
8536 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8537 e_arg->x, e_arg->len ) );
8538 psa_set_key_bits( &attributes, bits );
8539
8540 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008541 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008542 if( expected_status != PSA_SUCCESS )
8543 goto exit;
8544
8545 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008546 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008547 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8548 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8549 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8550 e_read_buffer, e_read_size,
8551 &e_read_length ) );
8552 if( is_default_public_exponent )
8553 TEST_EQUAL( e_read_length, 0 );
8554 else
8555 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8556
8557 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008558 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008559 goto exit;
8560
8561 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008562 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008563 exported, exported_size,
8564 &exported_length ) );
8565 {
8566 uint8_t *p = exported;
8567 uint8_t *end = exported + exported_length;
8568 size_t len;
8569 /* RSAPublicKey ::= SEQUENCE {
8570 * modulus INTEGER, -- n
8571 * publicExponent INTEGER } -- e
8572 */
8573 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008574 MBEDTLS_ASN1_SEQUENCE |
8575 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008576 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008577 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8578 MBEDTLS_ASN1_INTEGER ) );
8579 if( len >= 1 && p[0] == 0 )
8580 {
8581 ++p;
8582 --len;
8583 }
8584 if( e_arg->len == 0 )
8585 {
8586 TEST_EQUAL( len, 3 );
8587 TEST_EQUAL( p[0], 1 );
8588 TEST_EQUAL( p[1], 0 );
8589 TEST_EQUAL( p[2], 1 );
8590 }
8591 else
8592 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8593 }
8594
8595exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008596 /*
8597 * Key attributes may have been returned by psa_get_key_attributes() or
8598 * set by psa_set_key_domain_parameters() thus reset them as required.
8599 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008600 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008601
Ronald Cron5425a212020-08-04 14:58:35 +02008602 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008603 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008604 mbedtls_free( e_read_buffer );
8605 mbedtls_free( exported );
8606}
8607/* END_CASE */
8608
Darryl Greend49a4992018-06-18 17:27:26 +01008609/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008610void persistent_key_load_key_from_storage( data_t *data,
8611 int type_arg, int bits_arg,
8612 int usage_flags_arg, int alg_arg,
8613 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008614{
Ronald Cron71016a92020-08-28 19:01:50 +02008615 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8618 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008619 psa_key_type_t type = type_arg;
8620 size_t bits = bits_arg;
8621 psa_key_usage_t usage_flags = usage_flags_arg;
8622 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008623 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008624 unsigned char *first_export = NULL;
8625 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008626 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008627 size_t first_exported_length;
8628 size_t second_exported_length;
8629
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008630 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8631 {
8632 ASSERT_ALLOC( first_export, export_size );
8633 ASSERT_ALLOC( second_export, export_size );
8634 }
Darryl Greend49a4992018-06-18 17:27:26 +01008635
Gilles Peskine8817f612018-12-18 00:18:46 +01008636 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008637
Gilles Peskinec87af662019-05-15 16:12:22 +02008638 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008639 psa_set_key_usage_flags( &attributes, usage_flags );
8640 psa_set_key_algorithm( &attributes, alg );
8641 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008642 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008643
Darryl Green0c6575a2018-11-07 16:05:30 +00008644 switch( generation_method )
8645 {
8646 case IMPORT_KEY:
8647 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008648 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008649 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008650 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008651
Darryl Green0c6575a2018-11-07 16:05:30 +00008652 case GENERATE_KEY:
8653 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008654 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008655 break;
8656
8657 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008658#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008659 {
8660 /* Create base key */
8661 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8662 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8663 psa_set_key_usage_flags( &base_attributes,
8664 PSA_KEY_USAGE_DERIVE );
8665 psa_set_key_algorithm( &base_attributes, derive_alg );
8666 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008667 PSA_ASSERT( psa_import_key( &base_attributes,
8668 data->x, data->len,
8669 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008670 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008671 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008672 PSA_ASSERT( psa_key_derivation_input_key(
8673 &operation,
8674 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008675 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008676 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008677 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008678 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8679 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008680 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008681 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008682 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008683 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008684 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008685#else
8686 TEST_ASSUME( ! "KDF not supported in this configuration" );
8687#endif
8688 break;
8689
8690 default:
8691 TEST_ASSERT( ! "generation_method not implemented in test" );
8692 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008693 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008694 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008695
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008696 /* Export the key if permitted by the key policy. */
8697 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8698 {
Ronald Cron5425a212020-08-04 14:58:35 +02008699 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008700 first_export, export_size,
8701 &first_exported_length ) );
8702 if( generation_method == IMPORT_KEY )
8703 ASSERT_COMPARE( data->x, data->len,
8704 first_export, first_exported_length );
8705 }
Darryl Greend49a4992018-06-18 17:27:26 +01008706
8707 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008708 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008709 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008710 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008711
Darryl Greend49a4992018-06-18 17:27:26 +01008712 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008713 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008714 TEST_ASSERT( mbedtls_svc_key_id_equal(
8715 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008716 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8717 PSA_KEY_LIFETIME_PERSISTENT );
8718 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8719 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008720 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008721 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008722 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008723
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008724 /* Export the key again if permitted by the key policy. */
8725 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008726 {
Ronald Cron5425a212020-08-04 14:58:35 +02008727 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008728 second_export, export_size,
8729 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008730 ASSERT_COMPARE( first_export, first_exported_length,
8731 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008732 }
8733
8734 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008735 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008736 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008737
8738exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008739 /*
8740 * Key attributes may have been returned by psa_get_key_attributes()
8741 * thus reset them as required.
8742 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008743 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008744
Darryl Greend49a4992018-06-18 17:27:26 +01008745 mbedtls_free( first_export );
8746 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008747 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008748 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008749 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008750 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008751}
8752/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008753
Neil Armstronga557cb82022-06-10 08:58:32 +02008754/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008755void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8756 int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008757 int input_first, data_t *pw_data,
Neil Armstrong2a73f212022-09-06 11:34:54 +02008758 int expected_status_setup_arg,
8759 int expected_status_set_role_arg,
8760 int expected_status_set_password_key_arg,
8761 int expected_status_input_output_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008762{
8763 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8764 psa_pake_operation_t operation = psa_pake_operation_init();
8765 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008766 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008767 psa_key_type_t key_type_pw = key_type_pw_arg;
8768 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008769 psa_algorithm_t hash_alg = hash_arg;
8770 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008773 psa_status_t expected_status_setup = expected_status_setup_arg;
8774 psa_status_t expected_status_set_role = expected_status_set_role_arg;
8775 psa_status_t expected_status_set_password_key =
8776 expected_status_set_password_key_arg;
8777 psa_status_t expected_status_input_output =
8778 expected_status_input_output_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008779 unsigned char *output_buffer = NULL;
8780 size_t output_len = 0;
8781
8782 PSA_INIT( );
8783
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008784 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8785 PSA_PAKE_STEP_KEY_SHARE);
8786 ASSERT_ALLOC( output_buffer, buf_size );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008787
8788 if( pw_data->len > 0 )
8789 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008790 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008791 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008792 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008793 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8794 &key ) );
8795 }
8796
8797 psa_pake_cs_set_algorithm( &cipher_suite, alg );
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008798 psa_pake_cs_set_primitive( &cipher_suite, primitive );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008799 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8800
Neil Armstrong645cccd2022-06-08 17:36:23 +02008801 PSA_ASSERT( psa_pake_abort( &operation ) );
8802
8803 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8804 PSA_ERROR_BAD_STATE );
8805 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8806 PSA_ERROR_BAD_STATE );
8807 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8808 PSA_ERROR_BAD_STATE );
8809 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8810 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008811 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8812 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008813 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008814 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008815 PSA_ERROR_BAD_STATE );
8816
8817 PSA_ASSERT( psa_pake_abort( &operation ) );
8818
Neil Armstrong2a73f212022-09-06 11:34:54 +02008819 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8820 expected_status_setup );
8821 if( expected_status_setup != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008822 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008823
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008824 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8825 PSA_ERROR_BAD_STATE );
8826
Neil Armstrong2a73f212022-09-06 11:34:54 +02008827 TEST_EQUAL( psa_pake_set_role( &operation, role),
8828 expected_status_set_role );
8829 if( expected_status_set_role != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008830 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008831
8832 if( pw_data->len > 0 )
8833 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008834 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8835 expected_status_set_password_key );
8836 if( expected_status_set_password_key != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008837 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008838 }
8839
Neil Armstrong707d9572022-06-08 17:31:49 +02008840 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8841 PSA_ERROR_INVALID_ARGUMENT );
8842 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8843 PSA_ERROR_INVALID_ARGUMENT );
8844
8845 const uint8_t unsupported_id[] = "abcd";
8846
8847 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8848 PSA_ERROR_NOT_SUPPORTED );
8849 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8850 PSA_ERROR_NOT_SUPPORTED );
8851
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008852 const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive,
8853 PSA_PAKE_STEP_KEY_SHARE );
8854 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive,
8855 PSA_PAKE_STEP_ZK_PUBLIC );
8856 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive,
8857 PSA_PAKE_STEP_ZK_PROOF );
8858
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008859 /* First round */
8860 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008861 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008862 /* Invalid parameters (input) */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008863 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008864 NULL, 0 ),
8865 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008866 /* Invalid parameters (step) */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008867 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8868 key, pw_data->len ) , 0 );
Przemek Stekiel7c795482022-11-15 22:26:12 +01008869 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008870 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008871 PSA_ERROR_INVALID_ARGUMENT );
8872 /* Invalid first step */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008873 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8874 key, pw_data->len ), 0 );
Przemek Stekiel7c795482022-11-15 22:26:12 +01008875 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008876 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008877 PSA_ERROR_BAD_STATE );
8878
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008879 /* Possibly valid */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008880 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8881 key, pw_data->len ), 0 );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008882 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008883 output_buffer, size_key_share ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008884 expected_status_input_output);
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008885
Neil Armstrong2a73f212022-09-06 11:34:54 +02008886 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008887 {
8888 /* Buffer too large */
8889 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008890 output_buffer, size_zk_public + 1 ),
8891 PSA_ERROR_INVALID_ARGUMENT );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008892
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008893 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008894 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008895 output_buffer, size_zk_public ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008896 PSA_ERROR_BAD_STATE );
8897 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008898 }
8899 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008900 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008901 /* Invalid parameters (output) */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008902 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008903 NULL, 0, NULL ),
8904 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008905 /* Invalid parameters (step) */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008906 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8907 key, pw_data->len ), 0 );
Przemek Stekiel7c795482022-11-15 22:26:12 +01008908 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008909 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008910 PSA_ERROR_INVALID_ARGUMENT );
8911 /* Invalid first step */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008912 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8913 key, pw_data->len ), 0 );
Przemek Stekiel7c795482022-11-15 22:26:12 +01008914 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008915 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008916 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008917
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008918 /* Possibly valid */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008919 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8920 key, pw_data->len ), 0 );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008921 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008922 output_buffer, buf_size, &output_len ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008923 expected_status_input_output );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008924
Neil Armstrong2a73f212022-09-06 11:34:54 +02008925 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008926 {
8927 TEST_ASSERT( output_len > 0 );
8928
8929 /* Buffer too small */
8930 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008931 output_buffer, size_zk_public - 1, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008932 PSA_ERROR_BUFFER_TOO_SMALL );
8933
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008934 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008935 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008936 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008937 PSA_ERROR_BAD_STATE );
8938 }
8939 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008940
8941exit:
8942 PSA_ASSERT( psa_destroy_key( key ) );
8943 PSA_ASSERT( psa_pake_abort( &operation ) );
8944 mbedtls_free( output_buffer );
8945 PSA_DONE( );
8946}
8947/* END_CASE */
8948
Neil Armstronga557cb82022-06-10 08:58:32 +02008949/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008950void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8951 int client_input_first, int inject_error,
8952 data_t *pw_data )
8953{
8954 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8955 psa_pake_operation_t server = psa_pake_operation_init();
8956 psa_pake_operation_t client = psa_pake_operation_init();
8957 psa_algorithm_t alg = alg_arg;
8958 psa_algorithm_t hash_alg = hash_arg;
8959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8961
8962 PSA_INIT( );
8963
8964 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8965 psa_set_key_algorithm( &attributes, alg );
8966 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8967 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8968 &key ) );
8969
8970 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8971 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8972 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8973
8974
8975 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8976 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8977
8978 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8979 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8980
8981 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8982 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8983
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008984 ecjpake_do_round( alg, primitive_arg, &server, &client,
8985 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008986
8987 if( inject_error == 1 || inject_error == 2 )
8988 goto exit;
8989
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008990 ecjpake_do_round( alg, primitive_arg, &server, &client,
8991 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008992
8993exit:
8994 psa_destroy_key( key );
8995 psa_pake_abort( &server );
8996 psa_pake_abort( &client );
8997 PSA_DONE( );
8998}
8999/* END_CASE */
9000
9001/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009002void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02009003 int derive_alg_arg, data_t *pw_data,
Przemek Stekielcd356c32022-11-20 19:05:20 +01009004 int client_input_first, int destroy_key )
Neil Armstrongd597bc72022-05-25 11:28:39 +02009005{
9006 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9007 psa_pake_operation_t server = psa_pake_operation_init();
9008 psa_pake_operation_t client = psa_pake_operation_init();
9009 psa_algorithm_t alg = alg_arg;
9010 psa_algorithm_t hash_alg = hash_arg;
9011 psa_algorithm_t derive_alg = derive_alg_arg;
9012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9014 psa_key_derivation_operation_t server_derive =
9015 PSA_KEY_DERIVATION_OPERATION_INIT;
9016 psa_key_derivation_operation_t client_derive =
9017 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009018
9019 PSA_INIT( );
9020
Neil Armstrongd597bc72022-05-25 11:28:39 +02009021 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9022 psa_set_key_algorithm( &attributes, alg );
9023 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9024 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9025 &key ) );
9026
9027 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9028 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9029 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9030
Neil Armstrong1e855602022-06-15 11:32:11 +02009031 /* Get shared key */
9032 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
9033 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
9034
9035 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
9036 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
9037 {
9038 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
9039 PSA_KEY_DERIVATION_INPUT_SEED,
9040 (const uint8_t*) "", 0) );
9041 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
9042 PSA_KEY_DERIVATION_INPUT_SEED,
9043 (const uint8_t*) "", 0) );
9044 }
9045
Neil Armstrongd597bc72022-05-25 11:28:39 +02009046 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9047 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9048
9049 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9050 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9051
9052 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9053 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9054
Przemek Stekielcd356c32022-11-20 19:05:20 +01009055 if( destroy_key == 1 )
9056 psa_destroy_key( key );
9057
Neil Armstrong1e855602022-06-15 11:32:11 +02009058 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9059 PSA_ERROR_BAD_STATE );
9060 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9061 PSA_ERROR_BAD_STATE );
9062
Neil Armstrongf983caf2022-06-15 15:27:48 +02009063 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009064 ecjpake_do_round( alg, primitive_arg, &server, &client,
9065 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009066
Neil Armstrong1e855602022-06-15 11:32:11 +02009067 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9068 PSA_ERROR_BAD_STATE );
9069 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9070 PSA_ERROR_BAD_STATE );
9071
Neil Armstrongf983caf2022-06-15 15:27:48 +02009072 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009073 ecjpake_do_round( alg, primitive_arg, &server, &client,
9074 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009075
Neil Armstrongd597bc72022-05-25 11:28:39 +02009076 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9077 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9078
9079exit:
9080 psa_key_derivation_abort( &server_derive );
9081 psa_key_derivation_abort( &client_derive );
9082 psa_destroy_key( key );
9083 psa_pake_abort( &server );
9084 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009085 PSA_DONE( );
9086}
9087/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009088
9089/* BEGIN_CASE */
9090void ecjpake_size_macros( )
9091{
9092 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9093 const size_t bits = 256;
9094 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9095 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9096 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9097 PSA_ECC_FAMILY_SECP_R1 );
9098
9099 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9100 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9101 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9102 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9103 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9104 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9105 /* The output for ZK_PROOF is the same bitsize as the curve */
9106 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9107 PSA_BITS_TO_BYTES( bits ) );
9108
9109 /* Input sizes are the same as output sizes */
9110 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9111 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9112 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9113 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9114 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9115 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9116
9117 /* These inequalities will always hold even when other PAKEs are added */
9118 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9119 PSA_PAKE_OUTPUT_MAX_SIZE );
9120 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9121 PSA_PAKE_OUTPUT_MAX_SIZE );
9122 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9123 PSA_PAKE_OUTPUT_MAX_SIZE );
9124 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9125 PSA_PAKE_INPUT_MAX_SIZE );
9126 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9127 PSA_PAKE_INPUT_MAX_SIZE );
9128 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9129 PSA_PAKE_INPUT_MAX_SIZE );
9130}
9131/* END_CASE */