blob: dbbac764dd65306f144b36c3f3478203ac3311c9 [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
Tom Cosgrove1797b052022-12-04 17:19:59 +0000625 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100626 * \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
Valerio Setti1070aed2022-11-11 19:37:31 +01001322typedef enum
1323{
1324 INJECT_ERR_NONE = 0,
1325 INJECT_ERR_UNINITIALIZED_ACCESS,
1326 INJECT_ERR_DUPLICATE_SETUP,
1327 INJECT_ERR_INVALID_USER,
1328 INJECT_ERR_INVALID_PEER,
1329 INJECT_ERR_SET_USER,
1330 INJECT_ERR_SET_PEER,
1331 INJECT_EMPTY_IO_BUFFER,
1332 INJECT_UNKNOWN_STEP,
1333 INJECT_INVALID_FIRST_STEP,
1334 INJECT_WRONG_BUFFER_SIZE,
1335 INJECT_VALID_OPERATION_AFTER_FAILURE,
1336 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1337 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1338} ecjpake_injected_failure_t;
1339
Gilles Peskinee59236f2018-01-27 23:32:46 +01001340/* END_HEADER */
1341
1342/* BEGIN_DEPENDENCIES
1343 * depends_on:MBEDTLS_PSA_CRYPTO_C
1344 * END_DEPENDENCIES
1345 */
1346
1347/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001348void static_checks( )
1349{
1350 size_t max_truncated_mac_size =
1351 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1352
1353 /* Check that the length for a truncated MAC always fits in the algorithm
1354 * encoding. The shifted mask is the maximum truncated value. The
1355 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001356 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001357}
1358/* END_CASE */
1359
1360/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001361void import_with_policy( int type_arg,
1362 int usage_arg, int alg_arg,
1363 int expected_status_arg )
1364{
1365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1366 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001367 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001368 psa_key_type_t type = type_arg;
1369 psa_key_usage_t usage = usage_arg;
1370 psa_algorithm_t alg = alg_arg;
1371 psa_status_t expected_status = expected_status_arg;
1372 const uint8_t key_material[16] = {0};
1373 psa_status_t status;
1374
1375 PSA_ASSERT( psa_crypto_init( ) );
1376
1377 psa_set_key_type( &attributes, type );
1378 psa_set_key_usage_flags( &attributes, usage );
1379 psa_set_key_algorithm( &attributes, alg );
1380
1381 status = psa_import_key( &attributes,
1382 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001383 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001384 TEST_EQUAL( status, expected_status );
1385 if( status != PSA_SUCCESS )
1386 goto exit;
1387
Ronald Cron5425a212020-08-04 14:58:35 +02001388 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001389 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001390 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001391 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001392 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001393 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001394
Ronald Cron5425a212020-08-04 14:58:35 +02001395 PSA_ASSERT( psa_destroy_key( key ) );
1396 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001397
1398exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001399 /*
1400 * Key attributes may have been returned by psa_get_key_attributes()
1401 * thus reset them as required.
1402 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001403 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001404
1405 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001406 PSA_DONE( );
1407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
1411void import_with_data( data_t *data, int type_arg,
1412 int attr_bits_arg,
1413 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001414{
1415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1416 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001417 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001418 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001419 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001420 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001422
Gilles Peskine8817f612018-12-18 00:18:46 +01001423 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424
Gilles Peskine4747d192019-04-17 15:05:45 +02001425 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001426 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001427
Ronald Cron5425a212020-08-04 14:58:35 +02001428 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001429 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001430 if( status != PSA_SUCCESS )
1431 goto exit;
1432
Ronald Cron5425a212020-08-04 14:58:35 +02001433 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001434 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001435 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001436 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001437 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001438
Ronald Cron5425a212020-08-04 14:58:35 +02001439 PSA_ASSERT( psa_destroy_key( key ) );
1440 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001441
1442exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001443 /*
1444 * Key attributes may have been returned by psa_get_key_attributes()
1445 * thus reset them as required.
1446 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001447 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001448
1449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001451}
1452/* END_CASE */
1453
1454/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001455void import_large_key( int type_arg, int byte_size_arg,
1456 int expected_status_arg )
1457{
1458 psa_key_type_t type = type_arg;
1459 size_t byte_size = byte_size_arg;
1460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1461 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001462 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001463 psa_status_t status;
1464 uint8_t *buffer = NULL;
1465 size_t buffer_size = byte_size + 1;
1466 size_t n;
1467
Steven Cooreman69967ce2021-01-18 18:01:08 +01001468 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001469 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001470 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001471 memset( buffer, 'K', byte_size );
1472
1473 PSA_ASSERT( psa_crypto_init( ) );
1474
1475 /* Try importing the key */
1476 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1477 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001478 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001479 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001480 TEST_EQUAL( status, expected_status );
1481
1482 if( status == PSA_SUCCESS )
1483 {
Ronald Cron5425a212020-08-04 14:58:35 +02001484 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001485 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1486 TEST_EQUAL( psa_get_key_bits( &attributes ),
1487 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001488 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001489 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001490 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001491 for( n = 0; n < byte_size; n++ )
1492 TEST_EQUAL( buffer[n], 'K' );
1493 for( n = byte_size; n < buffer_size; n++ )
1494 TEST_EQUAL( buffer[n], 0 );
1495 }
1496
1497exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001498 /*
1499 * Key attributes may have been returned by psa_get_key_attributes()
1500 * thus reset them as required.
1501 */
1502 psa_reset_key_attributes( &attributes );
1503
Ronald Cron5425a212020-08-04 14:58:35 +02001504 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001505 PSA_DONE( );
1506 mbedtls_free( buffer );
1507}
1508/* END_CASE */
1509
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001510/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001511void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1512{
Ronald Cron5425a212020-08-04 14:58:35 +02001513 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001514 size_t bits = bits_arg;
1515 psa_status_t expected_status = expected_status_arg;
1516 psa_status_t status;
1517 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001518 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001519 size_t buffer_size = /* Slight overapproximations */
1520 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001521 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001522 unsigned char *p;
1523 int ret;
1524 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001526
Gilles Peskine8817f612018-12-18 00:18:46 +01001527 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001528 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001529
1530 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1531 bits, keypair ) ) >= 0 );
1532 length = ret;
1533
1534 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001535 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001536 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001537 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001538
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001539 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001540 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001541
1542exit:
1543 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001544 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001545}
1546/* END_CASE */
1547
1548/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001549void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001550 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001551 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301552 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553 int expected_bits,
1554 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001555 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556 int canonical_input )
1557{
Ronald Cron5425a212020-08-04 14:58:35 +02001558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001559 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001560 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001561 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001562 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301563 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564 unsigned char *exported = NULL;
1565 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001567 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001570 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001571
Moran Pekercb088e72018-07-17 17:36:59 +03001572 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001573 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001575 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001577
Archana4d7ae1d2021-07-07 02:50:22 +05301578 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 psa_set_key_usage_flags( &attributes, usage_arg );
1580 psa_set_key_algorithm( &attributes, alg );
1581 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001582
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001583 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001584 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585
1586 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001587 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001588 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1589 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001590 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591
1592 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001593 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001594 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001595
1596 /* The exported length must be set by psa_export_key() to a value between 0
1597 * and export_size. On errors, the exported length must be 0. */
1598 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1599 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001600 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001601
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001602 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001603 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001604 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001605 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001606 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001608 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609
Gilles Peskineea38a922021-02-13 00:05:16 +01001610 /* Run sanity checks on the exported key. For non-canonical inputs,
1611 * this validates the canonical representations. For canonical inputs,
1612 * this doesn't directly validate the implementation, but it still helps
1613 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301614 if( !psa_key_lifetime_is_external( lifetime ) )
1615 {
1616 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1617 goto exit;
1618 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001619
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001620 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001621 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001622 else
1623 {
Ronald Cron5425a212020-08-04 14:58:35 +02001624 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001625 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001626 &key2 ) );
1627 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001628 reexported,
1629 export_size,
1630 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001631 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301632 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001633 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001635 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301636 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301637 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001638 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001639
1640destroy:
1641 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001642 PSA_ASSERT( psa_destroy_key( key ) );
1643 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001644
1645exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001646 /*
1647 * Key attributes may have been returned by psa_get_key_attributes()
1648 * thus reset them as required.
1649 */
1650 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301651 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001652 mbedtls_free( exported );
1653 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001654 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001655}
1656/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001657
Moran Pekerf709f4a2018-06-06 17:26:04 +03001658/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001659void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001660 int type_arg,
1661 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301662 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001663 int export_size_delta,
1664 int expected_export_status_arg,
1665 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001666{
Ronald Cron5425a212020-08-04 14:58:35 +02001667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001668 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001669 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001670 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001671 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301672 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001673 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001674 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001675 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001677
Gilles Peskine8817f612018-12-18 00:18:46 +01001678 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001679
Archana4d7ae1d2021-07-07 02:50:22 +05301680 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1682 psa_set_key_algorithm( &attributes, alg );
1683 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001684
1685 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001686 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001687
Gilles Peskine49c25912018-10-29 15:15:31 +01001688 /* Export the public key */
1689 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001690 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001691 exported, export_size,
1692 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001693 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001694 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001695 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001696 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001697 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001698 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001699 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001700 TEST_LE_U( expected_public_key->len,
1701 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1702 TEST_LE_U( expected_public_key->len,
1703 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1704 TEST_LE_U( expected_public_key->len,
1705 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001706 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1707 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001708 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001709exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001710 /*
1711 * Key attributes may have been returned by psa_get_key_attributes()
1712 * thus reset them as required.
1713 */
1714 psa_reset_key_attributes( &attributes );
1715
itayzafrir3e02b3b2018-06-12 17:06:52 +03001716 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001717 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001718 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001719}
1720/* END_CASE */
1721
Gilles Peskine20035e32018-02-03 22:44:14 +01001722/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001723void import_and_exercise_key( data_t *data,
1724 int type_arg,
1725 int bits_arg,
1726 int alg_arg )
1727{
Ronald Cron5425a212020-08-04 14:58:35 +02001728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001729 psa_key_type_t type = type_arg;
1730 size_t bits = bits_arg;
1731 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001732 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001733 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001734 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001735
Gilles Peskine8817f612018-12-18 00:18:46 +01001736 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001737
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 psa_set_key_usage_flags( &attributes, usage );
1739 psa_set_key_algorithm( &attributes, alg );
1740 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001741
1742 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001743 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001744
1745 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001746 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001747 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1748 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001749
1750 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001751 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001752 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001753
Ronald Cron5425a212020-08-04 14:58:35 +02001754 PSA_ASSERT( psa_destroy_key( key ) );
1755 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001756
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001757exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001758 /*
1759 * Key attributes may have been returned by psa_get_key_attributes()
1760 * thus reset them as required.
1761 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001762 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001763
1764 psa_reset_key_attributes( &attributes );
1765 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001766 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001767}
1768/* END_CASE */
1769
1770/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001771void effective_key_attributes( int type_arg, int expected_type_arg,
1772 int bits_arg, int expected_bits_arg,
1773 int usage_arg, int expected_usage_arg,
1774 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001775{
Ronald Cron5425a212020-08-04 14:58:35 +02001776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001777 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001778 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001779 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001780 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001781 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001782 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001783 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001784 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001786
Gilles Peskine8817f612018-12-18 00:18:46 +01001787 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001788
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001789 psa_set_key_usage_flags( &attributes, usage );
1790 psa_set_key_algorithm( &attributes, alg );
1791 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001792 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001793
Ronald Cron5425a212020-08-04 14:58:35 +02001794 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001795 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001796
Ronald Cron5425a212020-08-04 14:58:35 +02001797 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001798 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1799 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1800 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1801 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001802
1803exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001804 /*
1805 * Key attributes may have been returned by psa_get_key_attributes()
1806 * thus reset them as required.
1807 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001808 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001809
1810 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001811 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001812}
1813/* END_CASE */
1814
1815/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001816void check_key_policy( int type_arg, int bits_arg,
1817 int usage_arg, int alg_arg )
1818{
1819 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001820 usage_arg,
1821 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001822 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001823 goto exit;
1824}
1825/* END_CASE */
1826
1827/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001828void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001829{
1830 /* Test each valid way of initializing the object, except for `= {0}`, as
1831 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1832 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001833 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001834 psa_key_attributes_t func = psa_key_attributes_init( );
1835 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1836 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001837
1838 memset( &zero, 0, sizeof( zero ) );
1839
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001840 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1841 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1842 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001843
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001844 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1845 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1846 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1847
1848 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1849 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1850 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1851
1852 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1853 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1854 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1855
1856 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1857 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1858 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001859}
1860/* END_CASE */
1861
1862/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001863void mac_key_policy( int policy_usage_arg,
1864 int policy_alg_arg,
1865 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001866 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001867 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001868 int expected_status_sign_arg,
1869 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001870{
Ronald Cron5425a212020-08-04 14:58:35 +02001871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001872 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001873 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001874 psa_key_type_t key_type = key_type_arg;
1875 psa_algorithm_t policy_alg = policy_alg_arg;
1876 psa_algorithm_t exercise_alg = exercise_alg_arg;
1877 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001878 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001879 psa_status_t expected_status_sign = expected_status_sign_arg;
1880 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001881 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001882
Gilles Peskine8817f612018-12-18 00:18:46 +01001883 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001884
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001885 psa_set_key_usage_flags( &attributes, policy_usage );
1886 psa_set_key_algorithm( &attributes, policy_alg );
1887 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001888
Gilles Peskine049c7532019-05-15 20:22:09 +02001889 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001890 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001891
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001892 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1893 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001894
Ronald Cron5425a212020-08-04 14:58:35 +02001895 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001896 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001897
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001898 /* Calculate the MAC, one-shot case. */
1899 uint8_t input[128] = {0};
1900 size_t mac_len;
1901 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1902 input, 128,
1903 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1904 expected_status_sign );
1905
Neil Armstrong3af9b972022-02-07 12:20:21 +01001906 /* Calculate the MAC, multi-part case. */
1907 PSA_ASSERT( psa_mac_abort( &operation ) );
1908 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1909 if( status == PSA_SUCCESS )
1910 {
1911 status = psa_mac_update( &operation, input, 128 );
1912 if( status == PSA_SUCCESS )
1913 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1914 &mac_len ),
1915 expected_status_sign );
1916 else
1917 TEST_EQUAL( status, expected_status_sign );
1918 }
1919 else
1920 {
1921 TEST_EQUAL( status, expected_status_sign );
1922 }
1923 PSA_ASSERT( psa_mac_abort( &operation ) );
1924
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001925 /* Verify correct MAC, one-shot case. */
1926 status = psa_mac_verify( key, exercise_alg, input, 128,
1927 mac, mac_len );
1928
1929 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1930 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001931 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001932 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001933
Neil Armstrong3af9b972022-02-07 12:20:21 +01001934 /* Verify correct MAC, multi-part case. */
1935 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1936 if( status == PSA_SUCCESS )
1937 {
1938 status = psa_mac_update( &operation, input, 128 );
1939 if( status == PSA_SUCCESS )
1940 {
1941 status = psa_mac_verify_finish( &operation, mac, mac_len );
1942 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1943 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1944 else
1945 TEST_EQUAL( status, expected_status_verify );
1946 }
1947 else
1948 {
1949 TEST_EQUAL( status, expected_status_verify );
1950 }
1951 }
1952 else
1953 {
1954 TEST_EQUAL( status, expected_status_verify );
1955 }
1956
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001958
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001960 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001961 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962
1963exit:
1964 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001965 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001966 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967}
1968/* END_CASE */
1969
1970/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001971void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001972 int policy_alg,
1973 int key_type,
1974 data_t *key_data,
1975 int exercise_alg )
1976{
Ronald Cron5425a212020-08-04 14:58:35 +02001977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001979 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001980 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001981 size_t output_buffer_size = 0;
1982 size_t input_buffer_size = 0;
1983 size_t output_length = 0;
1984 uint8_t *output = NULL;
1985 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 psa_status_t status;
1987
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001988 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1989 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1990 input_buffer_size );
1991
1992 ASSERT_ALLOC( input, input_buffer_size );
1993 ASSERT_ALLOC( output, output_buffer_size );
1994
Gilles Peskine8817f612018-12-18 00:18:46 +01001995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001996
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_set_key_usage_flags( &attributes, policy_usage );
1998 psa_set_key_algorithm( &attributes, policy_alg );
1999 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000
Gilles Peskine049c7532019-05-15 20:22:09 +02002001 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002002 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002004 /* Check if no key usage flag implication is done */
2005 TEST_EQUAL( policy_usage,
2006 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002007
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002008 /* Encrypt check, one-shot */
2009 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
2010 output, output_buffer_size,
2011 &output_length);
2012 if( policy_alg == exercise_alg &&
2013 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2014 PSA_ASSERT( status );
2015 else
2016 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2017
2018 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002019 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020 if( policy_alg == exercise_alg &&
2021 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002022 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002024 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025 psa_cipher_abort( &operation );
2026
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002027 /* Decrypt check, one-shot */
2028 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
2029 input, input_buffer_size,
2030 &output_length);
2031 if( policy_alg == exercise_alg &&
2032 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
2033 PSA_ASSERT( status );
2034 else
2035 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2036
2037 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002038 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039 if( policy_alg == exercise_alg &&
2040 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002041 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002042 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002043 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044
2045exit:
2046 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002047 mbedtls_free( input );
2048 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002049 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002050 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002051}
2052/* END_CASE */
2053
2054/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002055void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056 int policy_alg,
2057 int key_type,
2058 data_t *key_data,
2059 int nonce_length_arg,
2060 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002061 int exercise_alg,
2062 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063{
Ronald Cron5425a212020-08-04 14:58:35 +02002064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002066 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002067 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002069 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070 unsigned char nonce[16] = {0};
2071 size_t nonce_length = nonce_length_arg;
2072 unsigned char tag[16];
2073 size_t tag_length = tag_length_arg;
2074 size_t output_length;
2075
Gilles Peskine7be11a72022-04-14 00:12:57 +02002076 TEST_LE_U( nonce_length, sizeof( nonce ) );
2077 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002078
Gilles Peskine8817f612018-12-18 00:18:46 +01002079 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002080
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002081 psa_set_key_usage_flags( &attributes, policy_usage );
2082 psa_set_key_algorithm( &attributes, policy_alg );
2083 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
Gilles Peskine049c7532019-05-15 20:22:09 +02002085 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002086 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002088 /* Check if no key usage implication is done */
2089 TEST_EQUAL( policy_usage,
2090 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002091
Neil Armstrong752d8112022-02-07 14:51:11 +01002092 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002093 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094 nonce, nonce_length,
2095 NULL, 0,
2096 NULL, 0,
2097 tag, tag_length,
2098 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002099 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2100 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002102 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103
Neil Armstrong752d8112022-02-07 14:51:11 +01002104 /* Encrypt check, multi-part */
2105 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2106 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2107 TEST_EQUAL( status, expected_status );
2108 else
2109 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2110
2111 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002113 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002114 nonce, nonce_length,
2115 NULL, 0,
2116 tag, tag_length,
2117 NULL, 0,
2118 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002119 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2120 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2121 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002122 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002124 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Neil Armstrong752d8112022-02-07 14:51:11 +01002126 /* Decrypt check, multi-part */
2127 PSA_ASSERT( psa_aead_abort( &operation ) );
2128 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2129 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2130 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2131 else
2132 TEST_EQUAL( status, expected_status );
2133
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002134exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002135 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002136 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002137 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002138}
2139/* END_CASE */
2140
2141/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002142void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143 int policy_alg,
2144 int key_type,
2145 data_t *key_data,
2146 int exercise_alg )
2147{
Ronald Cron5425a212020-08-04 14:58:35 +02002148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002150 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002151 psa_status_t status;
2152 size_t key_bits;
2153 size_t buffer_length;
2154 unsigned char *buffer = NULL;
2155 size_t output_length;
2156
Gilles Peskine8817f612018-12-18 00:18:46 +01002157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_set_key_usage_flags( &attributes, policy_usage );
2160 psa_set_key_algorithm( &attributes, policy_alg );
2161 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002162
Gilles Peskine049c7532019-05-15 20:22:09 +02002163 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002164 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002166 /* Check if no key usage implication is done */
2167 TEST_EQUAL( policy_usage,
2168 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002169
Ronald Cron5425a212020-08-04 14:58:35 +02002170 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002171 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002172 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2173 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002174 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175
Ronald Cron5425a212020-08-04 14:58:35 +02002176 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177 NULL, 0,
2178 NULL, 0,
2179 buffer, buffer_length,
2180 &output_length );
2181 if( policy_alg == exercise_alg &&
2182 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002183 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002184 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002185 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002186
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002187 if( buffer_length != 0 )
2188 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002189 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002190 buffer, buffer_length,
2191 NULL, 0,
2192 buffer, buffer_length,
2193 &output_length );
2194 if( policy_alg == exercise_alg &&
2195 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002196 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002197 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002198 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002199
2200exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002201 /*
2202 * Key attributes may have been returned by psa_get_key_attributes()
2203 * thus reset them as required.
2204 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002205 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002206
2207 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002208 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002209 mbedtls_free( buffer );
2210}
2211/* END_CASE */
2212
2213/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002214void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002215 int policy_alg,
2216 int key_type,
2217 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002218 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002219 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002220 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002221{
Ronald Cron5425a212020-08-04 14:58:35 +02002222 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002224 psa_key_usage_t policy_usage = policy_usage_arg;
2225 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002226 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002227 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2228 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2229 * compatible with the policy and `payload_length_arg` is supposed to be
2230 * a valid input length to sign. If `payload_length_arg <= 0`,
2231 * `exercise_alg` is supposed to be forbidden by the policy. */
2232 int compatible_alg = payload_length_arg > 0;
2233 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002234 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002235 size_t signature_length;
2236
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002237 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002238 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002239 TEST_EQUAL( expected_usage,
2240 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002241
Gilles Peskine8817f612018-12-18 00:18:46 +01002242 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002243
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002244 psa_set_key_usage_flags( &attributes, policy_usage );
2245 psa_set_key_algorithm( &attributes, policy_alg );
2246 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002247
Gilles Peskine049c7532019-05-15 20:22:09 +02002248 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002249 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002250
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002251 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2252
Ronald Cron5425a212020-08-04 14:58:35 +02002253 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002254 payload, payload_length,
2255 signature, sizeof( signature ),
2256 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002257 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002258 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002259 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002260 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002261
2262 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002263 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002264 payload, payload_length,
2265 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002266 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002267 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002268 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002269 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002270
Gilles Peskinef7b41372021-09-22 16:15:05 +02002271 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002272 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002273 {
2274 status = psa_sign_message( key, exercise_alg,
2275 payload, payload_length,
2276 signature, sizeof( signature ),
2277 &signature_length );
2278 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2279 PSA_ASSERT( status );
2280 else
2281 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2282
2283 memset( signature, 0, sizeof( signature ) );
2284 status = psa_verify_message( key, exercise_alg,
2285 payload, payload_length,
2286 signature, sizeof( signature ) );
2287 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2288 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2289 else
2290 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2291 }
2292
Gilles Peskined5b33222018-06-18 22:20:03 +02002293exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002294 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002295 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002296}
2297/* END_CASE */
2298
Janos Follathba3fab92019-06-11 14:50:16 +01002299/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002300void derive_key_policy( int policy_usage,
2301 int policy_alg,
2302 int key_type,
2303 data_t *key_data,
2304 int exercise_alg )
2305{
Ronald Cron5425a212020-08-04 14:58:35 +02002306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002308 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002309 psa_status_t status;
2310
Gilles Peskine8817f612018-12-18 00:18:46 +01002311 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002312
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002313 psa_set_key_usage_flags( &attributes, policy_usage );
2314 psa_set_key_algorithm( &attributes, policy_alg );
2315 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002316
Gilles Peskine049c7532019-05-15 20:22:09 +02002317 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002318 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002319
Janos Follathba3fab92019-06-11 14:50:16 +01002320 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2321
2322 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2323 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002324 {
Janos Follathba3fab92019-06-11 14:50:16 +01002325 PSA_ASSERT( psa_key_derivation_input_bytes(
2326 &operation,
2327 PSA_KEY_DERIVATION_INPUT_SEED,
2328 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002329 }
Janos Follathba3fab92019-06-11 14:50:16 +01002330
2331 status = psa_key_derivation_input_key( &operation,
2332 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002333 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002334
Gilles Peskineea0fb492018-07-12 17:17:20 +02002335 if( policy_alg == exercise_alg &&
2336 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002337 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002338 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002339 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002340
2341exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002342 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002343 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002344 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002345}
2346/* END_CASE */
2347
2348/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002349void agreement_key_policy( int policy_usage,
2350 int policy_alg,
2351 int key_type_arg,
2352 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002353 int exercise_alg,
2354 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002355{
Ronald Cron5425a212020-08-04 14:58:35 +02002356 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002358 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002359 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002360 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002361 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002362
Gilles Peskine8817f612018-12-18 00:18:46 +01002363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002365 psa_set_key_usage_flags( &attributes, policy_usage );
2366 psa_set_key_algorithm( &attributes, policy_alg );
2367 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002368
Gilles Peskine049c7532019-05-15 20:22:09 +02002369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002370 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002371
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002372 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002373 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002374
Steven Cooremance48e852020-10-05 16:02:45 +02002375 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002376
2377exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002378 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002379 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002380 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002381}
2382/* END_CASE */
2383
2384/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002385void key_policy_alg2( int key_type_arg, data_t *key_data,
2386 int usage_arg, int alg_arg, int alg2_arg )
2387{
Ronald Cron5425a212020-08-04 14:58:35 +02002388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002389 psa_key_type_t key_type = key_type_arg;
2390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2391 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2392 psa_key_usage_t usage = usage_arg;
2393 psa_algorithm_t alg = alg_arg;
2394 psa_algorithm_t alg2 = alg2_arg;
2395
2396 PSA_ASSERT( psa_crypto_init( ) );
2397
2398 psa_set_key_usage_flags( &attributes, usage );
2399 psa_set_key_algorithm( &attributes, alg );
2400 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2401 psa_set_key_type( &attributes, key_type );
2402 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002403 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002404
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002405 /* Update the usage flags to obtain implicit usage flags */
2406 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002407 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002408 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2409 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2410 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2411
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002412 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002413 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002414 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002415 goto exit;
2416
2417exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002418 /*
2419 * Key attributes may have been returned by psa_get_key_attributes()
2420 * thus reset them as required.
2421 */
2422 psa_reset_key_attributes( &got_attributes );
2423
Ronald Cron5425a212020-08-04 14:58:35 +02002424 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002425 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002426}
2427/* END_CASE */
2428
2429/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002430void raw_agreement_key_policy( int policy_usage,
2431 int policy_alg,
2432 int key_type_arg,
2433 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002434 int exercise_alg,
2435 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002436{
Ronald Cron5425a212020-08-04 14:58:35 +02002437 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002439 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002440 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002441 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002442 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002443
2444 PSA_ASSERT( psa_crypto_init( ) );
2445
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002446 psa_set_key_usage_flags( &attributes, policy_usage );
2447 psa_set_key_algorithm( &attributes, policy_alg );
2448 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002449
Gilles Peskine049c7532019-05-15 20:22:09 +02002450 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002451 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002452
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002453 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002454
Steven Cooremance48e852020-10-05 16:02:45 +02002455 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002456
2457exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002458 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002459 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002460 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002461}
2462/* END_CASE */
2463
2464/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002465void copy_success( int source_usage_arg,
2466 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302467 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002468 int type_arg, data_t *material,
2469 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002470 int target_usage_arg,
2471 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302472 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002473 int expected_usage_arg,
2474 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002475{
Gilles Peskineca25db92019-04-19 11:43:08 +02002476 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2477 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478 psa_key_usage_t expected_usage = expected_usage_arg;
2479 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002480 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302481 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2482 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002483 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2484 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002485 uint8_t *export_buffer = NULL;
2486
Gilles Peskine57ab7212019-01-28 13:03:09 +01002487 PSA_ASSERT( psa_crypto_init( ) );
2488
Gilles Peskineca25db92019-04-19 11:43:08 +02002489 /* Prepare the source key. */
2490 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2491 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002492 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002493 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302494 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002495 PSA_ASSERT( psa_import_key( &source_attributes,
2496 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002497 &source_key ) );
2498 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002499
Gilles Peskineca25db92019-04-19 11:43:08 +02002500 /* Prepare the target attributes. */
2501 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002502 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002503 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002504 }
Archana8a180362021-07-05 02:18:48 +05302505 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002506
Gilles Peskineca25db92019-04-19 11:43:08 +02002507 if( target_usage_arg != -1 )
2508 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2509 if( target_alg_arg != -1 )
2510 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002511 if( target_alg2_arg != -1 )
2512 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002513
Archana8a180362021-07-05 02:18:48 +05302514
Gilles Peskine57ab7212019-01-28 13:03:09 +01002515 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002516 PSA_ASSERT( psa_copy_key( source_key,
2517 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002518
2519 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002520 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002521
2522 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002524 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2525 psa_get_key_type( &target_attributes ) );
2526 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2527 psa_get_key_bits( &target_attributes ) );
2528 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2529 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002530 TEST_EQUAL( expected_alg2,
2531 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002532 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2533 {
2534 size_t length;
2535 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002536 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002537 material->len, &length ) );
2538 ASSERT_COMPARE( material->x, material->len,
2539 export_buffer, length );
2540 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002541
Archana8a180362021-07-05 02:18:48 +05302542 if( !psa_key_lifetime_is_external( target_lifetime ) )
2543 {
2544 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2545 goto exit;
2546 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2547 goto exit;
2548 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002549
Ronald Cron5425a212020-08-04 14:58:35 +02002550 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002551
2552exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002553 /*
2554 * Source and target key attributes may have been returned by
2555 * psa_get_key_attributes() thus reset them as required.
2556 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002557 psa_reset_key_attributes( &source_attributes );
2558 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002559
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002560 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002561 mbedtls_free( export_buffer );
2562}
2563/* END_CASE */
2564
2565/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002566void copy_fail( int source_usage_arg,
2567 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302568 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002569 int type_arg, data_t *material,
2570 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002571 int target_usage_arg,
2572 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002573 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002574 int expected_status_arg )
2575{
2576 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2577 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002578 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2579 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002580 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002581
2582 PSA_ASSERT( psa_crypto_init( ) );
2583
2584 /* Prepare the source key. */
2585 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2586 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002587 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002588 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302589 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002590 PSA_ASSERT( psa_import_key( &source_attributes,
2591 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002592 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002593
2594 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002595 psa_set_key_id( &target_attributes, key_id );
2596 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002597 psa_set_key_type( &target_attributes, target_type_arg );
2598 psa_set_key_bits( &target_attributes, target_bits_arg );
2599 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2600 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002601 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002602
2603 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002604 TEST_EQUAL( psa_copy_key( source_key,
2605 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002606 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002607
Ronald Cron5425a212020-08-04 14:58:35 +02002608 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002609
Gilles Peskine4a644642019-05-03 17:14:08 +02002610exit:
2611 psa_reset_key_attributes( &source_attributes );
2612 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002613 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002614}
2615/* END_CASE */
2616
2617/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002618void hash_operation_init( )
2619{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002620 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002621 /* Test each valid way of initializing the object, except for `= {0}`, as
2622 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2623 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002624 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002625 psa_hash_operation_t func = psa_hash_operation_init( );
2626 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2627 psa_hash_operation_t zero;
2628
2629 memset( &zero, 0, sizeof( zero ) );
2630
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002631 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002632 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2633 PSA_ERROR_BAD_STATE );
2634 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2635 PSA_ERROR_BAD_STATE );
2636 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2637 PSA_ERROR_BAD_STATE );
2638
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002639 /* A default hash operation should be abortable without error. */
2640 PSA_ASSERT( psa_hash_abort( &func ) );
2641 PSA_ASSERT( psa_hash_abort( &init ) );
2642 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002643}
2644/* END_CASE */
2645
2646/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002647void hash_setup( int alg_arg,
2648 int expected_status_arg )
2649{
2650 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002651 uint8_t *output = NULL;
2652 size_t output_size = 0;
2653 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002654 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002655 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002656 psa_status_t status;
2657
Gilles Peskine8817f612018-12-18 00:18:46 +01002658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002659
Neil Armstrongedb20862022-02-07 15:47:44 +01002660 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002661 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002662 ASSERT_ALLOC( output, output_size );
2663
2664 status = psa_hash_compute( alg, NULL, 0,
2665 output, output_size, &output_length );
2666 TEST_EQUAL( status, expected_status );
2667
2668 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002669 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002670 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002671
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002672 /* Whether setup succeeded or failed, abort must succeed. */
2673 PSA_ASSERT( psa_hash_abort( &operation ) );
2674
2675 /* If setup failed, reproduce the failure, so as to
2676 * test the resulting state of the operation object. */
2677 if( status != PSA_SUCCESS )
2678 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2679
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002680 /* Now the operation object should be reusable. */
2681#if defined(KNOWN_SUPPORTED_HASH_ALG)
2682 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2683 PSA_ASSERT( psa_hash_abort( &operation ) );
2684#endif
2685
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002686exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002687 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002688 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689}
2690/* END_CASE */
2691
2692/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002693void hash_compute_fail( int alg_arg, data_t *input,
2694 int output_size_arg, int expected_status_arg )
2695{
2696 psa_algorithm_t alg = alg_arg;
2697 uint8_t *output = NULL;
2698 size_t output_size = output_size_arg;
2699 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002700 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002701 psa_status_t expected_status = expected_status_arg;
2702 psa_status_t status;
2703
2704 ASSERT_ALLOC( output, output_size );
2705
2706 PSA_ASSERT( psa_crypto_init( ) );
2707
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002708 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002709 status = psa_hash_compute( alg, input->x, input->len,
2710 output, output_size, &output_length );
2711 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002712 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002713
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002714 /* Hash Compute, multi-part */
2715 status = psa_hash_setup( &operation, alg );
2716 if( status == PSA_SUCCESS )
2717 {
2718 status = psa_hash_update( &operation, input->x, input->len );
2719 if( status == PSA_SUCCESS )
2720 {
2721 status = psa_hash_finish( &operation, output, output_size,
2722 &output_length );
2723 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002724 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002725 else
2726 TEST_EQUAL( status, expected_status );
2727 }
2728 else
2729 {
2730 TEST_EQUAL( status, expected_status );
2731 }
2732 }
2733 else
2734 {
2735 TEST_EQUAL( status, expected_status );
2736 }
2737
Gilles Peskine0a749c82019-11-28 19:33:58 +01002738exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002739 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002740 mbedtls_free( output );
2741 PSA_DONE( );
2742}
2743/* END_CASE */
2744
2745/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002746void hash_compare_fail( int alg_arg, data_t *input,
2747 data_t *reference_hash,
2748 int expected_status_arg )
2749{
2750 psa_algorithm_t alg = alg_arg;
2751 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002752 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002753 psa_status_t status;
2754
2755 PSA_ASSERT( psa_crypto_init( ) );
2756
Neil Armstrong55a1be12022-02-07 11:23:20 +01002757 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002758 status = psa_hash_compare( alg, input->x, input->len,
2759 reference_hash->x, reference_hash->len );
2760 TEST_EQUAL( status, expected_status );
2761
Neil Armstrong55a1be12022-02-07 11:23:20 +01002762 /* Hash Compare, multi-part */
2763 status = psa_hash_setup( &operation, alg );
2764 if( status == PSA_SUCCESS )
2765 {
2766 status = psa_hash_update( &operation, input->x, input->len );
2767 if( status == PSA_SUCCESS )
2768 {
2769 status = psa_hash_verify( &operation, reference_hash->x,
2770 reference_hash->len );
2771 TEST_EQUAL( status, expected_status );
2772 }
2773 else
2774 {
2775 TEST_EQUAL( status, expected_status );
2776 }
2777 }
2778 else
2779 {
2780 TEST_EQUAL( status, expected_status );
2781 }
2782
Gilles Peskine88e08462020-01-28 20:43:00 +01002783exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002784 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002785 PSA_DONE( );
2786}
2787/* END_CASE */
2788
2789/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002790void hash_compute_compare( int alg_arg, data_t *input,
2791 data_t *expected_output )
2792{
2793 psa_algorithm_t alg = alg_arg;
2794 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2795 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002796 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002797 size_t i;
2798
2799 PSA_ASSERT( psa_crypto_init( ) );
2800
Neil Armstrongca30a002022-02-07 11:40:23 +01002801 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002802 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002803 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002804 &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 tight 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 PSA_HASH_LENGTH( alg ),
2814 &output_length ) );
2815 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2816 ASSERT_COMPARE( output, output_length,
2817 expected_output->x, expected_output->len );
2818
2819 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002820 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2821 output, sizeof( output ),
2822 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002823 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002824 ASSERT_COMPARE( output, output_length,
2825 expected_output->x, expected_output->len );
2826
Neil Armstrongca30a002022-02-07 11:40:23 +01002827 /* Compute with larger buffer, multi-part */
2828 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2829 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2830 PSA_ASSERT( psa_hash_finish( &operation, output,
2831 sizeof( output ), &output_length ) );
2832 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2833 ASSERT_COMPARE( output, output_length,
2834 expected_output->x, expected_output->len );
2835
2836 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002837 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2838 output, output_length ) );
2839
Neil Armstrongca30a002022-02-07 11:40:23 +01002840 /* Compare with correct hash, multi-part */
2841 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2842 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2843 PSA_ASSERT( psa_hash_verify( &operation, output,
2844 output_length ) );
2845
2846 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002847 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2848 output, output_length + 1 ),
2849 PSA_ERROR_INVALID_SIGNATURE );
2850
Neil Armstrongca30a002022-02-07 11:40:23 +01002851 /* Compare with trailing garbage, multi-part */
2852 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2853 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2854 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2855 PSA_ERROR_INVALID_SIGNATURE );
2856
2857 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002858 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2859 output, output_length - 1 ),
2860 PSA_ERROR_INVALID_SIGNATURE );
2861
Neil Armstrongca30a002022-02-07 11:40:23 +01002862 /* Compare with truncated hash, multi-part */
2863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2864 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2865 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2866 PSA_ERROR_INVALID_SIGNATURE );
2867
Gilles Peskine0a749c82019-11-28 19:33:58 +01002868 /* Compare with corrupted value */
2869 for( i = 0; i < output_length; i++ )
2870 {
Chris Jones9634bb12021-01-20 15:56:42 +00002871 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002872 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002873
2874 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002875 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2876 output, output_length ),
2877 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002878
2879 /* Multi-Part */
2880 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2881 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2882 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2883 PSA_ERROR_INVALID_SIGNATURE );
2884
Gilles Peskine0a749c82019-11-28 19:33:58 +01002885 output[i] ^= 1;
2886 }
2887
2888exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002889 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002890 PSA_DONE( );
2891}
2892/* END_CASE */
2893
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002894/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002895void hash_bad_order( )
2896{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002897 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002898 unsigned char input[] = "";
2899 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002900 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002901 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2902 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2903 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002904 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002905 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002906 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002907
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002909
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002910 /* Call setup twice in a row. */
2911 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002912 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002913 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2914 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002915 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002916 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002917 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002918
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002919 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002920 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
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002924 /* Check that update calls abort on error. */
2925 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002926 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002927 ASSERT_OPERATION_IS_ACTIVE( operation );
2928 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2929 PSA_ERROR_BAD_STATE );
2930 ASSERT_OPERATION_IS_INACTIVE( operation );
2931 PSA_ASSERT( psa_hash_abort( &operation ) );
2932 ASSERT_OPERATION_IS_INACTIVE( operation );
2933
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002934 /* Call update after finish. */
2935 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2936 PSA_ASSERT( psa_hash_finish( &operation,
2937 hash, sizeof( hash ), &hash_len ) );
2938 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002939 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002940 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002941
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002942 /* Call verify without calling setup beforehand. */
2943 TEST_EQUAL( psa_hash_verify( &operation,
2944 valid_hash, sizeof( valid_hash ) ),
2945 PSA_ERROR_BAD_STATE );
2946 PSA_ASSERT( psa_hash_abort( &operation ) );
2947
2948 /* Call verify after finish. */
2949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2950 PSA_ASSERT( psa_hash_finish( &operation,
2951 hash, sizeof( hash ), &hash_len ) );
2952 TEST_EQUAL( psa_hash_verify( &operation,
2953 valid_hash, sizeof( valid_hash ) ),
2954 PSA_ERROR_BAD_STATE );
2955 PSA_ASSERT( psa_hash_abort( &operation ) );
2956
2957 /* Call verify twice in a row. */
2958 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002959 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002960 PSA_ASSERT( psa_hash_verify( &operation,
2961 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002962 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002963 TEST_EQUAL( psa_hash_verify( &operation,
2964 valid_hash, sizeof( valid_hash ) ),
2965 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002966 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002967 PSA_ASSERT( psa_hash_abort( &operation ) );
2968
2969 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002970 TEST_EQUAL( psa_hash_finish( &operation,
2971 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002972 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002973 PSA_ASSERT( psa_hash_abort( &operation ) );
2974
2975 /* Call finish twice in a row. */
2976 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2977 PSA_ASSERT( psa_hash_finish( &operation,
2978 hash, sizeof( hash ), &hash_len ) );
2979 TEST_EQUAL( psa_hash_finish( &operation,
2980 hash, sizeof( hash ), &hash_len ),
2981 PSA_ERROR_BAD_STATE );
2982 PSA_ASSERT( psa_hash_abort( &operation ) );
2983
2984 /* Call finish after calling verify. */
2985 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2986 PSA_ASSERT( psa_hash_verify( &operation,
2987 valid_hash, sizeof( valid_hash ) ) );
2988 TEST_EQUAL( psa_hash_finish( &operation,
2989 hash, sizeof( hash ), &hash_len ),
2990 PSA_ERROR_BAD_STATE );
2991 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002992
2993exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002994 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002995}
2996/* END_CASE */
2997
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002998/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002999void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03003000{
3001 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003002 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3003 * appended to it */
3004 unsigned char hash[] = {
3005 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3006 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
3007 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003008 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003009 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003010
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03003012
itayzafrir27e69452018-11-01 14:26:34 +02003013 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003015 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003016 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003017 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003018 ASSERT_OPERATION_IS_INACTIVE( operation );
3019 PSA_ASSERT( psa_hash_abort( &operation ) );
3020 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03003021
itayzafrir27e69452018-11-01 14:26:34 +02003022 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01003023 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003024 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003025 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03003026
itayzafrir27e69452018-11-01 14:26:34 +02003027 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003029 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003030 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03003031
itayzafrirec93d302018-10-18 18:01:10 +03003032exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003033 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03003034}
3035/* END_CASE */
3036
Ronald Cronee414c72021-03-18 18:50:08 +01003037/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003038void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03003039{
3040 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003041 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003042 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003043 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003044 size_t hash_len;
3045
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003047
itayzafrir58028322018-10-25 10:22:01 +03003048 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003049 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003050 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003051 hash, expected_size - 1, &hash_len ),
3052 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003053
3054exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003055 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003056}
3057/* END_CASE */
3058
Ronald Cronee414c72021-03-18 18:50:08 +01003059/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003060void hash_clone_source_state( )
3061{
3062 psa_algorithm_t alg = PSA_ALG_SHA_256;
3063 unsigned char hash[PSA_HASH_MAX_SIZE];
3064 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3065 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3066 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3067 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3068 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3069 size_t hash_len;
3070
3071 PSA_ASSERT( psa_crypto_init( ) );
3072 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3073
3074 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3075 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3076 PSA_ASSERT( psa_hash_finish( &op_finished,
3077 hash, sizeof( hash ), &hash_len ) );
3078 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3079 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3080
3081 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3082 PSA_ERROR_BAD_STATE );
3083
3084 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3085 PSA_ASSERT( psa_hash_finish( &op_init,
3086 hash, sizeof( hash ), &hash_len ) );
3087 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3088 PSA_ASSERT( psa_hash_finish( &op_finished,
3089 hash, sizeof( hash ), &hash_len ) );
3090 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3091 PSA_ASSERT( psa_hash_finish( &op_aborted,
3092 hash, sizeof( hash ), &hash_len ) );
3093
3094exit:
3095 psa_hash_abort( &op_source );
3096 psa_hash_abort( &op_init );
3097 psa_hash_abort( &op_setup );
3098 psa_hash_abort( &op_finished );
3099 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003100 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003101}
3102/* END_CASE */
3103
Ronald Cronee414c72021-03-18 18:50:08 +01003104/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003105void hash_clone_target_state( )
3106{
3107 psa_algorithm_t alg = PSA_ALG_SHA_256;
3108 unsigned char hash[PSA_HASH_MAX_SIZE];
3109 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3110 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3111 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3112 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3113 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3114 size_t hash_len;
3115
3116 PSA_ASSERT( psa_crypto_init( ) );
3117
3118 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3119 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3120 PSA_ASSERT( psa_hash_finish( &op_finished,
3121 hash, sizeof( hash ), &hash_len ) );
3122 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3123 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3124
3125 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3126 PSA_ASSERT( psa_hash_finish( &op_target,
3127 hash, sizeof( hash ), &hash_len ) );
3128
3129 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3130 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3131 PSA_ERROR_BAD_STATE );
3132 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3133 PSA_ERROR_BAD_STATE );
3134
3135exit:
3136 psa_hash_abort( &op_target );
3137 psa_hash_abort( &op_init );
3138 psa_hash_abort( &op_setup );
3139 psa_hash_abort( &op_finished );
3140 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003141 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003142}
3143/* END_CASE */
3144
itayzafrir58028322018-10-25 10:22:01 +03003145/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003146void mac_operation_init( )
3147{
Jaeden Amero252ef282019-02-15 14:05:35 +00003148 const uint8_t input[1] = { 0 };
3149
Jaeden Amero769ce272019-01-04 11:48:03 +00003150 /* Test each valid way of initializing the object, except for `= {0}`, as
3151 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3152 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003153 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003154 psa_mac_operation_t func = psa_mac_operation_init( );
3155 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3156 psa_mac_operation_t zero;
3157
3158 memset( &zero, 0, sizeof( zero ) );
3159
Jaeden Amero252ef282019-02-15 14:05:35 +00003160 /* A freshly-initialized MAC operation should not be usable. */
3161 TEST_EQUAL( psa_mac_update( &func,
3162 input, sizeof( input ) ),
3163 PSA_ERROR_BAD_STATE );
3164 TEST_EQUAL( psa_mac_update( &init,
3165 input, sizeof( input ) ),
3166 PSA_ERROR_BAD_STATE );
3167 TEST_EQUAL( psa_mac_update( &zero,
3168 input, sizeof( input ) ),
3169 PSA_ERROR_BAD_STATE );
3170
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003171 /* A default MAC operation should be abortable without error. */
3172 PSA_ASSERT( psa_mac_abort( &func ) );
3173 PSA_ASSERT( psa_mac_abort( &init ) );
3174 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003175}
3176/* END_CASE */
3177
3178/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003179void mac_setup( int key_type_arg,
3180 data_t *key,
3181 int alg_arg,
3182 int expected_status_arg )
3183{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003184 psa_key_type_t key_type = key_type_arg;
3185 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003186 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003187 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003188 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3189#if defined(KNOWN_SUPPORTED_MAC_ALG)
3190 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3191#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003192
Gilles Peskine8817f612018-12-18 00:18:46 +01003193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003194
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003195 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3196 &operation, &status ) )
3197 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003198 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003199
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003200 /* The operation object should be reusable. */
3201#if defined(KNOWN_SUPPORTED_MAC_ALG)
3202 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3203 smoke_test_key_data,
3204 sizeof( smoke_test_key_data ),
3205 KNOWN_SUPPORTED_MAC_ALG,
3206 &operation, &status ) )
3207 goto exit;
3208 TEST_EQUAL( status, PSA_SUCCESS );
3209#endif
3210
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003211exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003212 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003213}
3214/* END_CASE */
3215
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003216/* 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 +00003217void mac_bad_order( )
3218{
Ronald Cron5425a212020-08-04 14:58:35 +02003219 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003220 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3221 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003222 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003223 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3224 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3225 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003227 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3228 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3229 size_t sign_mac_length = 0;
3230 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3231 const uint8_t verify_mac[] = {
3232 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3233 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3234 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3235
3236 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003237 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003238 psa_set_key_algorithm( &attributes, alg );
3239 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003240
Ronald Cron5425a212020-08-04 14:58:35 +02003241 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3242 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003243
Jaeden Amero252ef282019-02-15 14:05:35 +00003244 /* Call update without calling setup beforehand. */
3245 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3246 PSA_ERROR_BAD_STATE );
3247 PSA_ASSERT( psa_mac_abort( &operation ) );
3248
3249 /* Call sign finish without calling setup beforehand. */
3250 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3251 &sign_mac_length),
3252 PSA_ERROR_BAD_STATE );
3253 PSA_ASSERT( psa_mac_abort( &operation ) );
3254
3255 /* Call verify finish without calling setup beforehand. */
3256 TEST_EQUAL( psa_mac_verify_finish( &operation,
3257 verify_mac, sizeof( verify_mac ) ),
3258 PSA_ERROR_BAD_STATE );
3259 PSA_ASSERT( psa_mac_abort( &operation ) );
3260
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003261 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003262 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003263 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003264 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003265 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003266 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003267 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003268 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003269
Jaeden Amero252ef282019-02-15 14:05:35 +00003270 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003271 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003272 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3273 PSA_ASSERT( psa_mac_sign_finish( &operation,
3274 sign_mac, sizeof( sign_mac ),
3275 &sign_mac_length ) );
3276 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3277 PSA_ERROR_BAD_STATE );
3278 PSA_ASSERT( psa_mac_abort( &operation ) );
3279
3280 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003281 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003282 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3283 PSA_ASSERT( psa_mac_verify_finish( &operation,
3284 verify_mac, sizeof( verify_mac ) ) );
3285 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3286 PSA_ERROR_BAD_STATE );
3287 PSA_ASSERT( psa_mac_abort( &operation ) );
3288
3289 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003290 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003291 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3292 PSA_ASSERT( psa_mac_sign_finish( &operation,
3293 sign_mac, sizeof( sign_mac ),
3294 &sign_mac_length ) );
3295 TEST_EQUAL( psa_mac_sign_finish( &operation,
3296 sign_mac, sizeof( sign_mac ),
3297 &sign_mac_length ),
3298 PSA_ERROR_BAD_STATE );
3299 PSA_ASSERT( psa_mac_abort( &operation ) );
3300
3301 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003302 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003303 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3304 PSA_ASSERT( psa_mac_verify_finish( &operation,
3305 verify_mac, sizeof( verify_mac ) ) );
3306 TEST_EQUAL( psa_mac_verify_finish( &operation,
3307 verify_mac, sizeof( verify_mac ) ),
3308 PSA_ERROR_BAD_STATE );
3309 PSA_ASSERT( psa_mac_abort( &operation ) );
3310
3311 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003312 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003313 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003314 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003315 TEST_EQUAL( psa_mac_verify_finish( &operation,
3316 verify_mac, sizeof( verify_mac ) ),
3317 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003318 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003319 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003320 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003321
3322 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003323 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003324 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003325 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003326 TEST_EQUAL( psa_mac_sign_finish( &operation,
3327 sign_mac, sizeof( sign_mac ),
3328 &sign_mac_length ),
3329 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003330 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003331 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003332 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003333
Ronald Cron5425a212020-08-04 14:58:35 +02003334 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003335
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003336exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003337 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003338}
3339/* END_CASE */
3340
3341/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003342void mac_sign_verify_multi( int key_type_arg,
3343 data_t *key_data,
3344 int alg_arg,
3345 data_t *input,
3346 int is_verify,
3347 data_t *expected_mac )
3348{
3349 size_t data_part_len = 0;
3350
3351 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3352 {
3353 /* Split data into length(data_part_len) parts. */
3354 mbedtls_test_set_step( 2000 + data_part_len );
3355
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003356 if( mac_multipart_internal_func( key_type_arg, key_data,
3357 alg_arg,
3358 input, data_part_len,
3359 expected_mac,
3360 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003361 break;
3362
3363 /* length(0) part, length(data_part_len) part, length(0) part... */
3364 mbedtls_test_set_step( 3000 + data_part_len );
3365
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003366 if( mac_multipart_internal_func( key_type_arg, key_data,
3367 alg_arg,
3368 input, data_part_len,
3369 expected_mac,
3370 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003371 break;
3372 }
3373
3374 /* Goto is required to silence warnings about unused labels, as we
3375 * don't actually do any test assertions in this function. */
3376 goto exit;
3377}
3378/* END_CASE */
3379
3380/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003381void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003382 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003383 int alg_arg,
3384 data_t *input,
3385 data_t *expected_mac )
3386{
Ronald Cron5425a212020-08-04 14:58:35 +02003387 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003388 psa_key_type_t key_type = key_type_arg;
3389 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003390 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003392 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003393 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003394 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003395 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003396 const size_t output_sizes_to_test[] = {
3397 0,
3398 1,
3399 expected_mac->len - 1,
3400 expected_mac->len,
3401 expected_mac->len + 1,
3402 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003403
Gilles Peskine7be11a72022-04-14 00:12:57 +02003404 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003405 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003406 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003407
Gilles Peskine8817f612018-12-18 00:18:46 +01003408 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003409
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003410 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003411 psa_set_key_algorithm( &attributes, alg );
3412 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003413
Ronald Cron5425a212020-08-04 14:58:35 +02003414 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3415 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003416
Gilles Peskine8b356b52020-08-25 23:44:59 +02003417 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3418 {
3419 const size_t output_size = output_sizes_to_test[i];
3420 psa_status_t expected_status =
3421 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3422 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003423
Chris Jones9634bb12021-01-20 15:56:42 +00003424 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003425 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003426
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003427 /* Calculate the MAC, one-shot case. */
3428 TEST_EQUAL( psa_mac_compute( key, alg,
3429 input->x, input->len,
3430 actual_mac, output_size, &mac_length ),
3431 expected_status );
3432 if( expected_status == PSA_SUCCESS )
3433 {
3434 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3435 actual_mac, mac_length );
3436 }
3437
3438 if( output_size > 0 )
3439 memset( actual_mac, 0, output_size );
3440
3441 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003442 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003443 PSA_ASSERT( psa_mac_update( &operation,
3444 input->x, input->len ) );
3445 TEST_EQUAL( psa_mac_sign_finish( &operation,
3446 actual_mac, output_size,
3447 &mac_length ),
3448 expected_status );
3449 PSA_ASSERT( psa_mac_abort( &operation ) );
3450
3451 if( expected_status == PSA_SUCCESS )
3452 {
3453 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3454 actual_mac, mac_length );
3455 }
3456 mbedtls_free( actual_mac );
3457 actual_mac = NULL;
3458 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003459
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003460exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003461 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003462 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003463 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003464 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003465}
3466/* END_CASE */
3467
3468/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003469void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003470 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003471 int alg_arg,
3472 data_t *input,
3473 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003474{
Ronald Cron5425a212020-08-04 14:58:35 +02003475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003476 psa_key_type_t key_type = key_type_arg;
3477 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003478 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003480 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003481
Gilles Peskine7be11a72022-04-14 00:12:57 +02003482 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003483
Gilles Peskine8817f612018-12-18 00:18:46 +01003484 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003485
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003486 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003487 psa_set_key_algorithm( &attributes, alg );
3488 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003489
Ronald Cron5425a212020-08-04 14:58:35 +02003490 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3491 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003492
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003493 /* Verify correct MAC, one-shot case. */
3494 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3495 expected_mac->x, expected_mac->len ) );
3496
3497 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003498 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003499 PSA_ASSERT( psa_mac_update( &operation,
3500 input->x, input->len ) );
3501 PSA_ASSERT( psa_mac_verify_finish( &operation,
3502 expected_mac->x,
3503 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003504
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003505 /* Test a MAC that's too short, one-shot case. */
3506 TEST_EQUAL( psa_mac_verify( key, alg,
3507 input->x, input->len,
3508 expected_mac->x,
3509 expected_mac->len - 1 ),
3510 PSA_ERROR_INVALID_SIGNATURE );
3511
3512 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003513 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003514 PSA_ASSERT( psa_mac_update( &operation,
3515 input->x, input->len ) );
3516 TEST_EQUAL( psa_mac_verify_finish( &operation,
3517 expected_mac->x,
3518 expected_mac->len - 1 ),
3519 PSA_ERROR_INVALID_SIGNATURE );
3520
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003521 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003522 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3523 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003524 TEST_EQUAL( psa_mac_verify( key, alg,
3525 input->x, input->len,
3526 perturbed_mac, expected_mac->len + 1 ),
3527 PSA_ERROR_INVALID_SIGNATURE );
3528
3529 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003530 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003531 PSA_ASSERT( psa_mac_update( &operation,
3532 input->x, input->len ) );
3533 TEST_EQUAL( psa_mac_verify_finish( &operation,
3534 perturbed_mac,
3535 expected_mac->len + 1 ),
3536 PSA_ERROR_INVALID_SIGNATURE );
3537
3538 /* Test changing one byte. */
3539 for( size_t i = 0; i < expected_mac->len; i++ )
3540 {
Chris Jones9634bb12021-01-20 15:56:42 +00003541 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003542 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003543
3544 TEST_EQUAL( psa_mac_verify( key, alg,
3545 input->x, input->len,
3546 perturbed_mac, expected_mac->len ),
3547 PSA_ERROR_INVALID_SIGNATURE );
3548
Ronald Cron5425a212020-08-04 14:58:35 +02003549 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003550 PSA_ASSERT( psa_mac_update( &operation,
3551 input->x, input->len ) );
3552 TEST_EQUAL( psa_mac_verify_finish( &operation,
3553 perturbed_mac,
3554 expected_mac->len ),
3555 PSA_ERROR_INVALID_SIGNATURE );
3556 perturbed_mac[i] ^= 1;
3557 }
3558
Gilles Peskine8c9def32018-02-08 10:02:12 +01003559exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003560 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003561 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003562 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003563 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003564}
3565/* END_CASE */
3566
3567/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003568void cipher_operation_init( )
3569{
Jaeden Ameroab439972019-02-15 14:12:05 +00003570 const uint8_t input[1] = { 0 };
3571 unsigned char output[1] = { 0 };
3572 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003573 /* Test each valid way of initializing the object, except for `= {0}`, as
3574 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3575 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003576 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003577 psa_cipher_operation_t func = psa_cipher_operation_init( );
3578 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3579 psa_cipher_operation_t zero;
3580
3581 memset( &zero, 0, sizeof( zero ) );
3582
Jaeden Ameroab439972019-02-15 14:12:05 +00003583 /* A freshly-initialized cipher operation should not be usable. */
3584 TEST_EQUAL( psa_cipher_update( &func,
3585 input, sizeof( input ),
3586 output, sizeof( output ),
3587 &output_length ),
3588 PSA_ERROR_BAD_STATE );
3589 TEST_EQUAL( psa_cipher_update( &init,
3590 input, sizeof( input ),
3591 output, sizeof( output ),
3592 &output_length ),
3593 PSA_ERROR_BAD_STATE );
3594 TEST_EQUAL( psa_cipher_update( &zero,
3595 input, sizeof( input ),
3596 output, sizeof( output ),
3597 &output_length ),
3598 PSA_ERROR_BAD_STATE );
3599
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003600 /* A default cipher operation should be abortable without error. */
3601 PSA_ASSERT( psa_cipher_abort( &func ) );
3602 PSA_ASSERT( psa_cipher_abort( &init ) );
3603 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003604}
3605/* END_CASE */
3606
3607/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003608void cipher_setup( int key_type_arg,
3609 data_t *key,
3610 int alg_arg,
3611 int expected_status_arg )
3612{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003613 psa_key_type_t key_type = key_type_arg;
3614 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003615 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003616 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003617 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003618#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003619 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3620#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003621
Gilles Peskine8817f612018-12-18 00:18:46 +01003622 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003623
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003624 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3625 &operation, &status ) )
3626 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003627 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003628
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003629 /* The operation object should be reusable. */
3630#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3631 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3632 smoke_test_key_data,
3633 sizeof( smoke_test_key_data ),
3634 KNOWN_SUPPORTED_CIPHER_ALG,
3635 &operation, &status ) )
3636 goto exit;
3637 TEST_EQUAL( status, PSA_SUCCESS );
3638#endif
3639
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003640exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003641 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003642 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003643}
3644/* END_CASE */
3645
Ronald Cronee414c72021-03-18 18:50:08 +01003646/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003647void cipher_bad_order( )
3648{
Ronald Cron5425a212020-08-04 14:58:35 +02003649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003650 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3651 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003653 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003654 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003655 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003656 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3657 0xaa, 0xaa, 0xaa, 0xaa };
3658 const uint8_t text[] = {
3659 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3660 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003661 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003662 size_t length = 0;
3663
3664 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003665 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3666 psa_set_key_algorithm( &attributes, alg );
3667 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003668 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3669 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003670
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003671 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003672 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003673 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003674 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003675 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003676 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003677 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003678 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003679
3680 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003681 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003682 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003683 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003684 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003685 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003686 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003687 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003688
Jaeden Ameroab439972019-02-15 14:12:05 +00003689 /* Generate an IV without calling setup beforehand. */
3690 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3691 buffer, sizeof( buffer ),
3692 &length ),
3693 PSA_ERROR_BAD_STATE );
3694 PSA_ASSERT( psa_cipher_abort( &operation ) );
3695
3696 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003697 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003698 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3699 buffer, sizeof( buffer ),
3700 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003701 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003702 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3703 buffer, sizeof( buffer ),
3704 &length ),
3705 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003706 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003707 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003708 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003709
3710 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003711 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003712 PSA_ASSERT( psa_cipher_set_iv( &operation,
3713 iv, sizeof( iv ) ) );
3714 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3715 buffer, sizeof( buffer ),
3716 &length ),
3717 PSA_ERROR_BAD_STATE );
3718 PSA_ASSERT( psa_cipher_abort( &operation ) );
3719
3720 /* Set an IV without calling setup beforehand. */
3721 TEST_EQUAL( psa_cipher_set_iv( &operation,
3722 iv, sizeof( iv ) ),
3723 PSA_ERROR_BAD_STATE );
3724 PSA_ASSERT( psa_cipher_abort( &operation ) );
3725
3726 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003727 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003728 PSA_ASSERT( psa_cipher_set_iv( &operation,
3729 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003730 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003731 TEST_EQUAL( psa_cipher_set_iv( &operation,
3732 iv, sizeof( iv ) ),
3733 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003734 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003735 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003736 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003737
3738 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003739 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003740 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3741 buffer, sizeof( buffer ),
3742 &length ) );
3743 TEST_EQUAL( psa_cipher_set_iv( &operation,
3744 iv, sizeof( iv ) ),
3745 PSA_ERROR_BAD_STATE );
3746 PSA_ASSERT( psa_cipher_abort( &operation ) );
3747
3748 /* Call update without calling setup beforehand. */
3749 TEST_EQUAL( psa_cipher_update( &operation,
3750 text, sizeof( text ),
3751 buffer, sizeof( buffer ),
3752 &length ),
3753 PSA_ERROR_BAD_STATE );
3754 PSA_ASSERT( psa_cipher_abort( &operation ) );
3755
3756 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003757 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003758 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003759 TEST_EQUAL( psa_cipher_update( &operation,
3760 text, sizeof( text ),
3761 buffer, sizeof( buffer ),
3762 &length ),
3763 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003764 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003765 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003766 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003767
3768 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003769 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003770 PSA_ASSERT( psa_cipher_set_iv( &operation,
3771 iv, sizeof( iv ) ) );
3772 PSA_ASSERT( psa_cipher_finish( &operation,
3773 buffer, sizeof( buffer ), &length ) );
3774 TEST_EQUAL( psa_cipher_update( &operation,
3775 text, sizeof( text ),
3776 buffer, sizeof( buffer ),
3777 &length ),
3778 PSA_ERROR_BAD_STATE );
3779 PSA_ASSERT( psa_cipher_abort( &operation ) );
3780
3781 /* Call finish without calling setup beforehand. */
3782 TEST_EQUAL( psa_cipher_finish( &operation,
3783 buffer, sizeof( buffer ), &length ),
3784 PSA_ERROR_BAD_STATE );
3785 PSA_ASSERT( psa_cipher_abort( &operation ) );
3786
3787 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003788 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003789 /* Not calling update means we are encrypting an empty buffer, which is OK
3790 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003791 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003792 TEST_EQUAL( psa_cipher_finish( &operation,
3793 buffer, sizeof( buffer ), &length ),
3794 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003795 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003796 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003797 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003798
3799 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003800 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003801 PSA_ASSERT( psa_cipher_set_iv( &operation,
3802 iv, sizeof( iv ) ) );
3803 PSA_ASSERT( psa_cipher_finish( &operation,
3804 buffer, sizeof( buffer ), &length ) );
3805 TEST_EQUAL( psa_cipher_finish( &operation,
3806 buffer, sizeof( buffer ), &length ),
3807 PSA_ERROR_BAD_STATE );
3808 PSA_ASSERT( psa_cipher_abort( &operation ) );
3809
Ronald Cron5425a212020-08-04 14:58:35 +02003810 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003811
Jaeden Ameroab439972019-02-15 14:12:05 +00003812exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003813 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003814 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003815}
3816/* END_CASE */
3817
3818/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003819void cipher_encrypt_fail( int alg_arg,
3820 int key_type_arg,
3821 data_t *key_data,
3822 data_t *input,
3823 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003824{
Ronald Cron5425a212020-08-04 14:58:35 +02003825 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003826 psa_status_t status;
3827 psa_key_type_t key_type = key_type_arg;
3828 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003829 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003830 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3831 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3832 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003833 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003834 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003835 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003836 size_t function_output_length;
3837 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003838 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3839
3840 if ( PSA_ERROR_BAD_STATE != expected_status )
3841 {
3842 PSA_ASSERT( psa_crypto_init( ) );
3843
3844 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3845 psa_set_key_algorithm( &attributes, alg );
3846 psa_set_key_type( &attributes, key_type );
3847
3848 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3849 input->len );
3850 ASSERT_ALLOC( output, output_buffer_size );
3851
3852 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3853 &key ) );
3854 }
3855
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003856 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003857 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3858 output_buffer_size, &output_length );
3859
3860 TEST_EQUAL( status, expected_status );
3861
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003862 /* Encrypt, multi-part */
3863 status = psa_cipher_encrypt_setup( &operation, key, alg );
3864 if( status == PSA_SUCCESS )
3865 {
3866 if( alg != PSA_ALG_ECB_NO_PADDING )
3867 {
3868 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3869 iv, iv_size,
3870 &iv_length ) );
3871 }
3872
3873 status = psa_cipher_update( &operation, input->x, input->len,
3874 output, output_buffer_size,
3875 &function_output_length );
3876 if( status == PSA_SUCCESS )
3877 {
3878 output_length += function_output_length;
3879
3880 status = psa_cipher_finish( &operation, output + output_length,
3881 output_buffer_size - output_length,
3882 &function_output_length );
3883
3884 TEST_EQUAL( status, expected_status );
3885 }
3886 else
3887 {
3888 TEST_EQUAL( status, expected_status );
3889 }
3890 }
3891 else
3892 {
3893 TEST_EQUAL( status, expected_status );
3894 }
3895
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003896exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003897 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003898 mbedtls_free( output );
3899 psa_destroy_key( key );
3900 PSA_DONE( );
3901}
3902/* END_CASE */
3903
3904/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003905void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3906 data_t *input, int iv_length,
3907 int expected_result )
3908{
3909 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3910 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3912 size_t output_buffer_size = 0;
3913 unsigned char *output = NULL;
3914
3915 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3916 ASSERT_ALLOC( output, output_buffer_size );
3917
3918 PSA_ASSERT( psa_crypto_init( ) );
3919
3920 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3921 psa_set_key_algorithm( &attributes, alg );
3922 psa_set_key_type( &attributes, key_type );
3923
3924 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3925 &key ) );
3926 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3927 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3928 iv_length ) );
3929
3930exit:
3931 psa_cipher_abort( &operation );
3932 mbedtls_free( output );
3933 psa_destroy_key( key );
3934 PSA_DONE( );
3935}
3936/* END_CASE */
3937
3938/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003939void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3940 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003941{
3942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3943 psa_key_type_t key_type = key_type_arg;
3944 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003945 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3946 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003947 unsigned char *output = NULL;
3948 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003949 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3951
3952 PSA_ASSERT( psa_crypto_init( ) );
3953
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003954 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003955 TEST_LE_U( ciphertext->len,
3956 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3957 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003958 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003959 TEST_LE_U( plaintext->len,
3960 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3961 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3962 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003963
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003964
3965 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003966 psa_set_key_usage_flags( &attributes,
3967 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003968 psa_set_key_algorithm( &attributes, alg );
3969 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003970 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3971 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003972 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3973 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003974 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003975
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003976 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003977 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3978 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3979 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003980 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3981 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003982 PSA_ERROR_BAD_STATE );
3983
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003984 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003985 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3986 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003987 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003988 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003989 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3990 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003991 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003992 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003993
Gilles Peskine286c3142022-04-20 17:09:38 +02003994 /* Multipart encryption */
3995 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3996 output_length = 0;
3997 length = ~0;
3998 PSA_ASSERT( psa_cipher_update( &operation,
3999 plaintext->x, plaintext->len,
4000 output, output_buffer_size,
4001 &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( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004010 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004011
Gilles Peskine286c3142022-04-20 17:09:38 +02004012 /* Multipart encryption */
4013 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
4014 output_length = 0;
4015 length = ~0;
4016 PSA_ASSERT( psa_cipher_update( &operation,
4017 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004018 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02004019 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004020 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02004021 output_length += length;
4022 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine42649d92022-11-23 14:15:57 +01004023 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine286c3142022-04-20 17:09:38 +02004024 output_buffer_size - output_length,
4025 &length ) );
4026 output_length += length;
4027 ASSERT_COMPARE( plaintext->x, plaintext->len,
4028 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004029
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004030 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004031 output_length = ~0;
4032 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
4033 output, output_buffer_size,
4034 &output_length ) );
4035 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
4036 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004037
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004038 /* One-shot decryption */
4039 output_length = ~0;
4040 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4041 output, output_buffer_size,
4042 &output_length ) );
4043 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004044 output, output_length );
4045
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004046exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004047 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004048 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004049 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004050 psa_destroy_key( key );
4051 PSA_DONE( );
4052}
4053/* END_CASE */
4054
4055/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004056void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4057{
4058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4059 psa_algorithm_t alg = alg_arg;
4060 psa_key_type_t key_type = key_type_arg;
4061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4062 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4063 psa_status_t status;
4064
4065 PSA_ASSERT( psa_crypto_init( ) );
4066
4067 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4068 psa_set_key_algorithm( &attributes, alg );
4069 psa_set_key_type( &attributes, key_type );
4070
4071 /* Usage of either of these two size macros would cause divide by zero
4072 * with incorrect key types previously. Input length should be irrelevant
4073 * here. */
4074 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4075 0 );
4076 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4077
4078
4079 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4080 &key ) );
4081
4082 /* Should fail due to invalid alg type (to support invalid key type).
4083 * Encrypt or decrypt will end up in the same place. */
4084 status = psa_cipher_encrypt_setup( &operation, key, alg );
4085
4086 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4087
4088exit:
4089 psa_cipher_abort( &operation );
4090 psa_destroy_key( key );
4091 PSA_DONE( );
4092}
4093/* END_CASE */
4094
4095/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004096void cipher_encrypt_validation( int alg_arg,
4097 int key_type_arg,
4098 data_t *key_data,
4099 data_t *input )
4100{
4101 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4102 psa_key_type_t key_type = key_type_arg;
4103 psa_algorithm_t alg = alg_arg;
4104 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4105 unsigned char *output1 = NULL;
4106 size_t output1_buffer_size = 0;
4107 size_t output1_length = 0;
4108 unsigned char *output2 = NULL;
4109 size_t output2_buffer_size = 0;
4110 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004111 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004112 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004114
Gilles Peskine8817f612018-12-18 00:18:46 +01004115 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004116
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004117 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4118 psa_set_key_algorithm( &attributes, alg );
4119 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004120
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004121 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4122 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4123 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4124 ASSERT_ALLOC( output1, output1_buffer_size );
4125 ASSERT_ALLOC( output2, output2_buffer_size );
4126
Ronald Cron5425a212020-08-04 14:58:35 +02004127 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4128 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004129
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004130 /* The one-shot cipher encryption uses generated iv so validating
4131 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004132 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4133 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004134 TEST_LE_U( output1_length,
4135 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4136 TEST_LE_U( output1_length,
4137 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004138
4139 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4140 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004141
Gilles Peskine8817f612018-12-18 00:18:46 +01004142 PSA_ASSERT( psa_cipher_update( &operation,
4143 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004144 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004145 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004146 TEST_LE_U( function_output_length,
4147 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4148 TEST_LE_U( function_output_length,
4149 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004150 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004151
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004152 PSA_ASSERT( psa_cipher_finish( &operation,
4153 output2 + output2_length,
4154 output2_buffer_size - output2_length,
4155 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004156 TEST_LE_U( function_output_length,
4157 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4158 TEST_LE_U( function_output_length,
4159 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004160 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004161
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004162 PSA_ASSERT( psa_cipher_abort( &operation ) );
4163 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4164 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004165
Gilles Peskine50e586b2018-06-08 14:28:46 +02004166exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004167 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004168 mbedtls_free( output1 );
4169 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004170 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004171 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004172}
4173/* END_CASE */
4174
4175/* BEGIN_CASE */
4176void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004177 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004178 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004179 int first_part_size_arg,
4180 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004181 data_t *expected_output,
4182 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004183{
Ronald Cron5425a212020-08-04 14:58:35 +02004184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004185 psa_key_type_t key_type = key_type_arg;
4186 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004187 psa_status_t status;
4188 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004189 size_t first_part_size = first_part_size_arg;
4190 size_t output1_length = output1_length_arg;
4191 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004192 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004193 size_t output_buffer_size = 0;
4194 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004195 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004196 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004198
Gilles Peskine8817f612018-12-18 00:18:46 +01004199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004200
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004201 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4202 psa_set_key_algorithm( &attributes, alg );
4203 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004204
Ronald Cron5425a212020-08-04 14:58:35 +02004205 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4206 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004207
Ronald Cron5425a212020-08-04 14:58:35 +02004208 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004209
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004210 if( iv->len > 0 )
4211 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004212 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004213 }
4214
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004215 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4216 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004217 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004218
Gilles Peskine7be11a72022-04-14 00:12:57 +02004219 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004220 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4221 output, output_buffer_size,
4222 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004223 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004224 TEST_LE_U( function_output_length,
4225 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4226 TEST_LE_U( function_output_length,
4227 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004228 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004229
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004230 if( first_part_size < input->len )
4231 {
4232 PSA_ASSERT( psa_cipher_update( &operation,
4233 input->x + first_part_size,
4234 input->len - first_part_size,
4235 ( output_buffer_size == 0 ? NULL :
4236 output + total_output_length ),
4237 output_buffer_size - total_output_length,
4238 &function_output_length ) );
4239 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004240 TEST_LE_U( function_output_length,
4241 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4242 alg,
4243 input->len - first_part_size ) );
4244 TEST_LE_U( function_output_length,
4245 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004246 total_output_length += function_output_length;
4247 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004248
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004249 status = psa_cipher_finish( &operation,
4250 ( output_buffer_size == 0 ? NULL :
4251 output + total_output_length ),
4252 output_buffer_size - total_output_length,
4253 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004254 TEST_LE_U( function_output_length,
4255 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4256 TEST_LE_U( function_output_length,
4257 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004258 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004259 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004260
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004261 if( expected_status == PSA_SUCCESS )
4262 {
4263 PSA_ASSERT( psa_cipher_abort( &operation ) );
4264
4265 ASSERT_COMPARE( expected_output->x, expected_output->len,
4266 output, total_output_length );
4267 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268
4269exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004270 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004271 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004272 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004273 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004274}
4275/* END_CASE */
4276
4277/* BEGIN_CASE */
4278void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004279 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004280 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004281 int first_part_size_arg,
4282 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004283 data_t *expected_output,
4284 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004285{
Ronald Cron5425a212020-08-04 14:58:35 +02004286 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004287 psa_key_type_t key_type = key_type_arg;
4288 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004289 psa_status_t status;
4290 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004291 size_t first_part_size = first_part_size_arg;
4292 size_t output1_length = output1_length_arg;
4293 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004294 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004295 size_t output_buffer_size = 0;
4296 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004297 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004298 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004300
Gilles Peskine8817f612018-12-18 00:18:46 +01004301 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004302
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004303 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4304 psa_set_key_algorithm( &attributes, alg );
4305 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004306
Ronald Cron5425a212020-08-04 14:58:35 +02004307 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4308 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004309
Ronald Cron5425a212020-08-04 14:58:35 +02004310 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004311
Steven Cooreman177deba2020-09-07 17:14:14 +02004312 if( iv->len > 0 )
4313 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004314 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004315 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004317 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4318 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004319 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320
Gilles Peskine7be11a72022-04-14 00:12:57 +02004321 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004322 PSA_ASSERT( psa_cipher_update( &operation,
4323 input->x, first_part_size,
4324 output, output_buffer_size,
4325 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004326 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004327 TEST_LE_U( function_output_length,
4328 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4329 TEST_LE_U( function_output_length,
4330 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004331 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004332
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004333 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004334 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004335 PSA_ASSERT( psa_cipher_update( &operation,
4336 input->x + first_part_size,
4337 input->len - first_part_size,
4338 ( output_buffer_size == 0 ? NULL :
4339 output + total_output_length ),
4340 output_buffer_size - total_output_length,
4341 &function_output_length ) );
4342 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004343 TEST_LE_U( function_output_length,
4344 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4345 alg,
4346 input->len - first_part_size ) );
4347 TEST_LE_U( function_output_length,
4348 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004349 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004350 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004351
Gilles Peskine50e586b2018-06-08 14:28:46 +02004352 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004353 ( output_buffer_size == 0 ? NULL :
4354 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004355 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004356 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004357 TEST_LE_U( function_output_length,
4358 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4359 TEST_LE_U( function_output_length,
4360 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004361 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004362 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004363
4364 if( expected_status == PSA_SUCCESS )
4365 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004366 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004367
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004368 ASSERT_COMPARE( expected_output->x, expected_output->len,
4369 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004370 }
4371
Gilles Peskine50e586b2018-06-08 14:28:46 +02004372exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004373 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004374 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004375 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004376 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004377}
4378/* END_CASE */
4379
Gilles Peskine50e586b2018-06-08 14:28:46 +02004380/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004381void cipher_decrypt_fail( int alg_arg,
4382 int key_type_arg,
4383 data_t *key_data,
4384 data_t *iv,
4385 data_t *input_arg,
4386 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004387{
4388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4389 psa_status_t status;
4390 psa_key_type_t key_type = key_type_arg;
4391 psa_algorithm_t alg = alg_arg;
4392 psa_status_t expected_status = expected_status_arg;
4393 unsigned char *input = NULL;
4394 size_t input_buffer_size = 0;
4395 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004396 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004397 size_t output_buffer_size = 0;
4398 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004399 size_t function_output_length;
4400 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4402
4403 if ( PSA_ERROR_BAD_STATE != expected_status )
4404 {
4405 PSA_ASSERT( psa_crypto_init( ) );
4406
4407 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4408 psa_set_key_algorithm( &attributes, alg );
4409 psa_set_key_type( &attributes, key_type );
4410
4411 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4412 &key ) );
4413 }
4414
4415 /* Allocate input buffer and copy the iv and the plaintext */
4416 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4417 if ( input_buffer_size > 0 )
4418 {
4419 ASSERT_ALLOC( input, input_buffer_size );
4420 memcpy( input, iv->x, iv->len );
4421 memcpy( input + iv->len, input_arg->x, input_arg->len );
4422 }
4423
4424 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4425 ASSERT_ALLOC( output, output_buffer_size );
4426
Neil Armstrong66a479f2022-02-07 15:41:19 +01004427 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004428 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4429 output_buffer_size, &output_length );
4430 TEST_EQUAL( status, expected_status );
4431
Neil Armstrong66a479f2022-02-07 15:41:19 +01004432 /* Decrypt, multi-part */
4433 status = psa_cipher_decrypt_setup( &operation, key, alg );
4434 if( status == PSA_SUCCESS )
4435 {
4436 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4437 input_arg->len ) +
4438 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4439 ASSERT_ALLOC( output_multi, output_buffer_size );
4440
4441 if( iv->len > 0 )
4442 {
4443 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4444
4445 if( status != PSA_SUCCESS )
4446 TEST_EQUAL( status, expected_status );
4447 }
4448
4449 if( status == PSA_SUCCESS )
4450 {
4451 status = psa_cipher_update( &operation,
4452 input_arg->x, input_arg->len,
4453 output_multi, output_buffer_size,
4454 &function_output_length );
4455 if( status == PSA_SUCCESS )
4456 {
4457 output_length = function_output_length;
4458
4459 status = psa_cipher_finish( &operation,
4460 output_multi + output_length,
4461 output_buffer_size - output_length,
4462 &function_output_length );
4463
4464 TEST_EQUAL( status, expected_status );
4465 }
4466 else
4467 {
4468 TEST_EQUAL( status, expected_status );
4469 }
4470 }
4471 else
4472 {
4473 TEST_EQUAL( status, expected_status );
4474 }
4475 }
4476 else
4477 {
4478 TEST_EQUAL( status, expected_status );
4479 }
4480
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004481exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004482 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004483 mbedtls_free( input );
4484 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004485 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004486 psa_destroy_key( key );
4487 PSA_DONE( );
4488}
4489/* END_CASE */
4490
4491/* BEGIN_CASE */
4492void cipher_decrypt( int alg_arg,
4493 int key_type_arg,
4494 data_t *key_data,
4495 data_t *iv,
4496 data_t *input_arg,
4497 data_t *expected_output )
4498{
4499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4500 psa_key_type_t key_type = key_type_arg;
4501 psa_algorithm_t alg = alg_arg;
4502 unsigned char *input = NULL;
4503 size_t input_buffer_size = 0;
4504 unsigned char *output = NULL;
4505 size_t output_buffer_size = 0;
4506 size_t output_length = 0;
4507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4508
4509 PSA_ASSERT( psa_crypto_init( ) );
4510
4511 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4512 psa_set_key_algorithm( &attributes, alg );
4513 psa_set_key_type( &attributes, key_type );
4514
4515 /* Allocate input buffer and copy the iv and the plaintext */
4516 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4517 if ( input_buffer_size > 0 )
4518 {
4519 ASSERT_ALLOC( input, input_buffer_size );
4520 memcpy( input, iv->x, iv->len );
4521 memcpy( input + iv->len, input_arg->x, input_arg->len );
4522 }
4523
4524 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4525 ASSERT_ALLOC( output, output_buffer_size );
4526
4527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4528 &key ) );
4529
4530 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4531 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004532 TEST_LE_U( output_length,
4533 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4534 TEST_LE_U( output_length,
4535 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004536
4537 ASSERT_COMPARE( expected_output->x, expected_output->len,
4538 output, output_length );
4539exit:
4540 mbedtls_free( input );
4541 mbedtls_free( output );
4542 psa_destroy_key( key );
4543 PSA_DONE( );
4544}
4545/* END_CASE */
4546
4547/* BEGIN_CASE */
4548void cipher_verify_output( int alg_arg,
4549 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004550 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004551 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004552{
Ronald Cron5425a212020-08-04 14:58:35 +02004553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004554 psa_key_type_t key_type = key_type_arg;
4555 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004556 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004557 size_t output1_size = 0;
4558 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004559 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004560 size_t output2_size = 0;
4561 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004562 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004563
Gilles Peskine8817f612018-12-18 00:18:46 +01004564 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004565
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004566 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4567 psa_set_key_algorithm( &attributes, alg );
4568 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004569
Ronald Cron5425a212020-08-04 14:58:35 +02004570 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4571 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004572 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004573 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004574
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004575 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4576 output1, output1_size,
4577 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004578 TEST_LE_U( output1_length,
4579 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4580 TEST_LE_U( output1_length,
4581 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004582
4583 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004584 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004585
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004586 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4587 output2, output2_size,
4588 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004589 TEST_LE_U( output2_length,
4590 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4591 TEST_LE_U( output2_length,
4592 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004593
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004594 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004595
4596exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004597 mbedtls_free( output1 );
4598 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004599 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004600 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004601}
4602/* END_CASE */
4603
4604/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004605void cipher_verify_output_multipart( int alg_arg,
4606 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004607 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004608 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004609 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004610{
Ronald Cron5425a212020-08-04 14:58:35 +02004611 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004612 psa_key_type_t key_type = key_type_arg;
4613 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004614 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004615 unsigned char iv[16] = {0};
4616 size_t iv_size = 16;
4617 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004618 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004619 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004620 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004621 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004622 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004623 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004624 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004625 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4626 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004628
Gilles Peskine8817f612018-12-18 00:18:46 +01004629 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004630
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4632 psa_set_key_algorithm( &attributes, alg );
4633 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004634
Ronald Cron5425a212020-08-04 14:58:35 +02004635 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4636 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004637
Ronald Cron5425a212020-08-04 14:58:35 +02004638 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4639 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004640
Steven Cooreman177deba2020-09-07 17:14:14 +02004641 if( alg != PSA_ALG_ECB_NO_PADDING )
4642 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004643 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4644 iv, iv_size,
4645 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004646 }
4647
gabor-mezei-armceface22021-01-21 12:26:17 +01004648 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004649 TEST_LE_U( output1_buffer_size,
4650 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004651 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004652
Gilles Peskine7be11a72022-04-14 00:12:57 +02004653 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004654
Gilles Peskine8817f612018-12-18 00:18:46 +01004655 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4656 output1, output1_buffer_size,
4657 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004658 TEST_LE_U( function_output_length,
4659 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4660 TEST_LE_U( function_output_length,
4661 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004662 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004663
Gilles Peskine8817f612018-12-18 00:18:46 +01004664 PSA_ASSERT( psa_cipher_update( &operation1,
4665 input->x + first_part_size,
4666 input->len - first_part_size,
4667 output1, output1_buffer_size,
4668 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004669 TEST_LE_U( function_output_length,
4670 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4671 alg,
4672 input->len - first_part_size ) );
4673 TEST_LE_U( function_output_length,
4674 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004675 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004676
Gilles Peskine8817f612018-12-18 00:18:46 +01004677 PSA_ASSERT( psa_cipher_finish( &operation1,
4678 output1 + output1_length,
4679 output1_buffer_size - output1_length,
4680 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004681 TEST_LE_U( function_output_length,
4682 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4683 TEST_LE_U( function_output_length,
4684 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004685 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004686
Gilles Peskine8817f612018-12-18 00:18:46 +01004687 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004688
Gilles Peskine048b7f02018-06-08 14:20:49 +02004689 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004690 TEST_LE_U( output2_buffer_size,
4691 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4692 TEST_LE_U( output2_buffer_size,
4693 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004694 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004695
Steven Cooreman177deba2020-09-07 17:14:14 +02004696 if( iv_length > 0 )
4697 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004698 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4699 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004700 }
Moran Pekerded84402018-06-06 16:36:50 +03004701
Gilles Peskine8817f612018-12-18 00:18:46 +01004702 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4703 output2, output2_buffer_size,
4704 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004705 TEST_LE_U( function_output_length,
4706 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4707 TEST_LE_U( function_output_length,
4708 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004709 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004710
Gilles Peskine8817f612018-12-18 00:18:46 +01004711 PSA_ASSERT( psa_cipher_update( &operation2,
4712 output1 + first_part_size,
4713 output1_length - first_part_size,
4714 output2, output2_buffer_size,
4715 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004716 TEST_LE_U( function_output_length,
4717 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4718 alg,
4719 output1_length - first_part_size ) );
4720 TEST_LE_U( function_output_length,
4721 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004722 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004723
Gilles Peskine8817f612018-12-18 00:18:46 +01004724 PSA_ASSERT( psa_cipher_finish( &operation2,
4725 output2 + output2_length,
4726 output2_buffer_size - output2_length,
4727 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004728 TEST_LE_U( function_output_length,
4729 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4730 TEST_LE_U( function_output_length,
4731 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004732 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004733
Gilles Peskine8817f612018-12-18 00:18:46 +01004734 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004735
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004736 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004737
4738exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004739 psa_cipher_abort( &operation1 );
4740 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004741 mbedtls_free( output1 );
4742 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004743 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004744 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004745}
4746/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004747
Gilles Peskine20035e32018-02-03 22:44:14 +01004748/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004749void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004750 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004751 data_t *nonce,
4752 data_t *additional_data,
4753 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004754 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004755{
Ronald Cron5425a212020-08-04 14:58:35 +02004756 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004757 psa_key_type_t key_type = key_type_arg;
4758 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004759 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004760 unsigned char *output_data = NULL;
4761 size_t output_size = 0;
4762 size_t output_length = 0;
4763 unsigned char *output_data2 = NULL;
4764 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004765 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004766 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004768
Gilles Peskine8817f612018-12-18 00:18:46 +01004769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004770
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004771 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4772 psa_set_key_algorithm( &attributes, alg );
4773 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004774
Gilles Peskine049c7532019-05-15 20:22:09 +02004775 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004776 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004777 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4778 key_bits = psa_get_key_bits( &attributes );
4779
4780 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4781 alg );
4782 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4783 * should be exact. */
4784 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4785 expected_result != PSA_ERROR_NOT_SUPPORTED )
4786 {
4787 TEST_EQUAL( output_size,
4788 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004789 TEST_LE_U( output_size,
4790 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004791 }
4792 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004793
Steven Cooremanf49478b2021-02-15 15:19:25 +01004794 status = psa_aead_encrypt( key, alg,
4795 nonce->x, nonce->len,
4796 additional_data->x,
4797 additional_data->len,
4798 input_data->x, input_data->len,
4799 output_data, output_size,
4800 &output_length );
4801
4802 /* If the operation is not supported, just skip and not fail in case the
4803 * encryption involves a common limitation of cryptography hardwares and
4804 * an alternative implementation. */
4805 if( status == PSA_ERROR_NOT_SUPPORTED )
4806 {
4807 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4808 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4809 }
4810
4811 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004812
4813 if( PSA_SUCCESS == expected_result )
4814 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004815 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004816
Gilles Peskine003a4a92019-05-14 16:09:40 +02004817 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4818 * should be exact. */
4819 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004820 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004821
Gilles Peskine7be11a72022-04-14 00:12:57 +02004822 TEST_LE_U( input_data->len,
4823 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004824
Ronald Cron5425a212020-08-04 14:58:35 +02004825 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004826 nonce->x, nonce->len,
4827 additional_data->x,
4828 additional_data->len,
4829 output_data, output_length,
4830 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004831 &output_length2 ),
4832 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004833
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004834 ASSERT_COMPARE( input_data->x, input_data->len,
4835 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004836 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004837
Gilles Peskinea1cac842018-06-11 19:33:02 +02004838exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004839 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004840 mbedtls_free( output_data );
4841 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004842 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843}
4844/* END_CASE */
4845
4846/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004847void aead_encrypt( int key_type_arg, data_t *key_data,
4848 int alg_arg,
4849 data_t *nonce,
4850 data_t *additional_data,
4851 data_t *input_data,
4852 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004853{
Ronald Cron5425a212020-08-04 14:58:35 +02004854 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004855 psa_key_type_t key_type = key_type_arg;
4856 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004857 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004858 unsigned char *output_data = NULL;
4859 size_t output_size = 0;
4860 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004862 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863
Gilles Peskine8817f612018-12-18 00:18:46 +01004864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4867 psa_set_key_algorithm( &attributes, alg );
4868 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869
Gilles Peskine049c7532019-05-15 20:22:09 +02004870 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004871 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004872 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4873 key_bits = psa_get_key_bits( &attributes );
4874
4875 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4876 alg );
4877 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4878 * should be exact. */
4879 TEST_EQUAL( output_size,
4880 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004881 TEST_LE_U( output_size,
4882 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004883 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884
Steven Cooremand588ea12021-01-11 19:36:04 +01004885 status = psa_aead_encrypt( key, alg,
4886 nonce->x, nonce->len,
4887 additional_data->x, additional_data->len,
4888 input_data->x, input_data->len,
4889 output_data, output_size,
4890 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004891
Ronald Cron28a45ed2021-02-09 20:35:42 +01004892 /* If the operation is not supported, just skip and not fail in case the
4893 * encryption involves a common limitation of cryptography hardwares and
4894 * an alternative implementation. */
4895 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004896 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004897 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4898 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004899 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004900
4901 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004902 ASSERT_COMPARE( expected_result->x, expected_result->len,
4903 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004904
Gilles Peskinea1cac842018-06-11 19:33:02 +02004905exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004906 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004907 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004908 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004909}
4910/* END_CASE */
4911
4912/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004913void aead_decrypt( int key_type_arg, data_t *key_data,
4914 int alg_arg,
4915 data_t *nonce,
4916 data_t *additional_data,
4917 data_t *input_data,
4918 data_t *expected_data,
4919 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004920{
Ronald Cron5425a212020-08-04 14:58:35 +02004921 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004922 psa_key_type_t key_type = key_type_arg;
4923 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004924 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004925 unsigned char *output_data = NULL;
4926 size_t output_size = 0;
4927 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004929 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004930 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004931
Gilles Peskine8817f612018-12-18 00:18:46 +01004932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004933
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4935 psa_set_key_algorithm( &attributes, alg );
4936 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004937
Gilles Peskine049c7532019-05-15 20:22:09 +02004938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004939 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004940 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4941 key_bits = psa_get_key_bits( &attributes );
4942
4943 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4944 alg );
4945 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4946 expected_result != PSA_ERROR_NOT_SUPPORTED )
4947 {
4948 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4949 * should be exact. */
4950 TEST_EQUAL( output_size,
4951 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004952 TEST_LE_U( output_size,
4953 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004954 }
4955 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004956
Steven Cooremand588ea12021-01-11 19:36:04 +01004957 status = psa_aead_decrypt( key, alg,
4958 nonce->x, nonce->len,
4959 additional_data->x,
4960 additional_data->len,
4961 input_data->x, input_data->len,
4962 output_data, output_size,
4963 &output_length );
4964
Ronald Cron28a45ed2021-02-09 20:35:42 +01004965 /* If the operation is not supported, just skip and not fail in case the
4966 * decryption involves a common limitation of cryptography hardwares and
4967 * an alternative implementation. */
4968 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004969 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004970 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4971 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004972 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004973
4974 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004975
Gilles Peskine2d277862018-06-18 15:41:12 +02004976 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004977 ASSERT_COMPARE( expected_data->x, expected_data->len,
4978 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004979
Gilles Peskinea1cac842018-06-11 19:33:02 +02004980exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004981 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004982 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004983 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004984}
4985/* END_CASE */
4986
4987/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004988void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4989 int alg_arg,
4990 data_t *nonce,
4991 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004992 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004993 int do_set_lengths,
4994 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004995{
Paul Elliottd3f82412021-06-16 16:52:21 +01004996 size_t ad_part_len = 0;
4997 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004998 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004999
Paul Elliott32f46ba2021-09-23 18:24:36 +01005000 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005001 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005002 mbedtls_test_set_step( ad_part_len );
5003
5004 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005005 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005006 if( ad_part_len & 0x01 )
5007 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5008 else
5009 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005010 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005011
5012 /* Split ad into length(ad_part_len) parts. */
5013 if( !aead_multipart_internal_func( key_type_arg, key_data,
5014 alg_arg, nonce,
5015 additional_data,
5016 ad_part_len,
5017 input_data, -1,
5018 set_lengths_method,
5019 expected_output,
5020 1, 0 ) )
5021 break;
5022
5023 /* length(0) part, length(ad_part_len) part, length(0) part... */
5024 mbedtls_test_set_step( 1000 + ad_part_len );
5025
5026 if( !aead_multipart_internal_func( key_type_arg, key_data,
5027 alg_arg, nonce,
5028 additional_data,
5029 ad_part_len,
5030 input_data, -1,
5031 set_lengths_method,
5032 expected_output,
5033 1, 1 ) )
5034 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005035 }
Paul Elliottd3f82412021-06-16 16:52:21 +01005036
Paul Elliott32f46ba2021-09-23 18:24:36 +01005037 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005039 /* Split data into length(data_part_len) parts. */
5040 mbedtls_test_set_step( 2000 + data_part_len );
5041
5042 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005043 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044 if( data_part_len & 0x01 )
5045 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5046 else
5047 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005048 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005049
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050 if( !aead_multipart_internal_func( key_type_arg, key_data,
5051 alg_arg, nonce,
5052 additional_data, -1,
5053 input_data, data_part_len,
5054 set_lengths_method,
5055 expected_output,
5056 1, 0 ) )
5057 break;
5058
5059 /* length(0) part, length(data_part_len) part, length(0) part... */
5060 mbedtls_test_set_step( 3000 + data_part_len );
5061
5062 if( !aead_multipart_internal_func( key_type_arg, key_data,
5063 alg_arg, nonce,
5064 additional_data, -1,
5065 input_data, data_part_len,
5066 set_lengths_method,
5067 expected_output,
5068 1, 1 ) )
5069 break;
5070 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005071
Paul Elliott8fc45162021-06-23 16:06:01 +01005072 /* Goto is required to silence warnings about unused labels, as we
5073 * don't actually do any test assertions in this function. */
5074 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005075}
5076/* END_CASE */
5077
5078/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005079void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5080 int alg_arg,
5081 data_t *nonce,
5082 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005083 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005084 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005085 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005086{
Paul Elliottd3f82412021-06-16 16:52:21 +01005087 size_t ad_part_len = 0;
5088 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005089 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005090
Paul Elliott32f46ba2021-09-23 18:24:36 +01005091 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005092 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005093 /* Split ad into length(ad_part_len) parts. */
5094 mbedtls_test_set_step( ad_part_len );
5095
5096 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005097 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005098 if( ad_part_len & 0x01 )
5099 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5100 else
5101 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005102 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005103
5104 if( !aead_multipart_internal_func( key_type_arg, key_data,
5105 alg_arg, nonce,
5106 additional_data,
5107 ad_part_len,
5108 input_data, -1,
5109 set_lengths_method,
5110 expected_output,
5111 0, 0 ) )
5112 break;
5113
5114 /* length(0) part, length(ad_part_len) part, length(0) part... */
5115 mbedtls_test_set_step( 1000 + ad_part_len );
5116
5117 if( !aead_multipart_internal_func( key_type_arg, key_data,
5118 alg_arg, nonce,
5119 additional_data,
5120 ad_part_len,
5121 input_data, -1,
5122 set_lengths_method,
5123 expected_output,
5124 0, 1 ) )
5125 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005126 }
5127
Paul Elliott32f46ba2021-09-23 18:24:36 +01005128 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005129 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005130 /* Split data into length(data_part_len) parts. */
5131 mbedtls_test_set_step( 2000 + data_part_len );
5132
5133 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005134 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005135 if( data_part_len & 0x01 )
5136 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5137 else
5138 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005139 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005140
5141 if( !aead_multipart_internal_func( key_type_arg, key_data,
5142 alg_arg, nonce,
5143 additional_data, -1,
5144 input_data, data_part_len,
5145 set_lengths_method,
5146 expected_output,
5147 0, 0 ) )
5148 break;
5149
5150 /* length(0) part, length(data_part_len) part, length(0) part... */
5151 mbedtls_test_set_step( 3000 + data_part_len );
5152
5153 if( !aead_multipart_internal_func( key_type_arg, key_data,
5154 alg_arg, nonce,
5155 additional_data, -1,
5156 input_data, data_part_len,
5157 set_lengths_method,
5158 expected_output,
5159 0, 1 ) )
5160 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005161 }
5162
Paul Elliott8fc45162021-06-23 16:06:01 +01005163 /* Goto is required to silence warnings about unused labels, as we
5164 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005165 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005166}
5167/* END_CASE */
5168
5169/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005170void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5171 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005172 int nonce_length,
5173 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005174 data_t *additional_data,
5175 data_t *input_data,
5176 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005177{
5178
5179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5180 psa_key_type_t key_type = key_type_arg;
5181 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005182 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005183 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5185 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005186 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005187 size_t actual_nonce_length = 0;
5188 size_t expected_nonce_length = expected_nonce_length_arg;
5189 unsigned char *output = NULL;
5190 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005191 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005192 size_t ciphertext_size = 0;
5193 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005194 size_t tag_length = 0;
5195 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005196
5197 PSA_ASSERT( psa_crypto_init( ) );
5198
5199 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5200 psa_set_key_algorithm( & attributes, alg );
5201 psa_set_key_type( & attributes, key_type );
5202
5203 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5204 &key ) );
5205
5206 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5207
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005208 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5209
Paul Elliottf1277632021-08-24 18:11:37 +01005210 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005211
Paul Elliottf1277632021-08-24 18:11:37 +01005212 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005213
Gilles Peskine7be11a72022-04-14 00:12:57 +02005214 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005215
Paul Elliottf1277632021-08-24 18:11:37 +01005216 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005217
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005218 status = psa_aead_encrypt_setup( &operation, key, alg );
5219
5220 /* If the operation is not supported, just skip and not fail in case the
5221 * encryption involves a common limitation of cryptography hardwares and
5222 * an alternative implementation. */
5223 if( status == PSA_ERROR_NOT_SUPPORTED )
5224 {
5225 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005226 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005227 }
5228
5229 PSA_ASSERT( status );
5230
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005231 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005232 nonce_length,
5233 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005234
Paul Elliott693bf312021-07-23 17:40:41 +01005235 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005236
Paul Elliottf1277632021-08-24 18:11:37 +01005237 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005238
Paul Elliott88ecbe12021-09-22 17:23:03 +01005239 if( expected_status == PSA_SUCCESS )
5240 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5241 alg ) );
5242
Gilles Peskine7be11a72022-04-14 00:12:57 +02005243 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005244
Paul Elliott693bf312021-07-23 17:40:41 +01005245 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005246 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005247 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005248 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5249 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005250
5251 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5252 additional_data->len ) );
5253
5254 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005255 output, output_size,
5256 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005257
Paul Elliottf1277632021-08-24 18:11:37 +01005258 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5259 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005260 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5261 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005262
5263exit:
5264 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005265 mbedtls_free( output );
5266 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005267 psa_aead_abort( &operation );
5268 PSA_DONE( );
5269}
5270/* END_CASE */
5271
5272/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005273void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5274 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005275 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005276 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005277 data_t *additional_data,
5278 data_t *input_data,
5279 int expected_status_arg )
5280{
5281
5282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5283 psa_key_type_t key_type = key_type_arg;
5284 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005285 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005286 uint8_t *nonce_buffer = NULL;
5287 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5288 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5289 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005290 unsigned char *output = NULL;
5291 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005292 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005293 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005294 size_t ciphertext_size = 0;
5295 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005296 size_t tag_length = 0;
5297 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005298 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005299 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005300
5301 PSA_ASSERT( psa_crypto_init( ) );
5302
5303 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5304 psa_set_key_algorithm( &attributes, alg );
5305 psa_set_key_type( &attributes, key_type );
5306
5307 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5308 &key ) );
5309
5310 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5311
5312 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5313
Paul Elliott6f0e7202021-08-25 12:57:18 +01005314 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005315
Paul Elliott6f0e7202021-08-25 12:57:18 +01005316 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005317
Gilles Peskine7be11a72022-04-14 00:12:57 +02005318 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005319
Paul Elliott6f0e7202021-08-25 12:57:18 +01005320 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005321
Paul Elliott863864a2021-07-23 17:28:31 +01005322 status = psa_aead_encrypt_setup( &operation, key, alg );
5323
5324 /* If the operation is not supported, just skip and not fail in case the
5325 * encryption involves a common limitation of cryptography hardwares and
5326 * an alternative implementation. */
5327 if( status == PSA_ERROR_NOT_SUPPORTED )
5328 {
5329 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005330 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005331 }
5332
5333 PSA_ASSERT( status );
5334
Paul Elliott4023ffd2021-09-10 16:21:22 +01005335 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5336 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005337 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005338 /* Arbitrary size buffer, to test zero length valid buffer. */
5339 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005340 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005341 }
5342 else
5343 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005344 /* If length is zero, then this will return NULL. */
5345 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005346 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005347
Paul Elliott4023ffd2021-09-10 16:21:22 +01005348 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005349 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005350 for( index = 0; index < nonce_length - 1; ++index )
5351 {
5352 nonce_buffer[index] = 'a' + index;
5353 }
Paul Elliott66696b52021-08-16 18:42:41 +01005354 }
Paul Elliott863864a2021-07-23 17:28:31 +01005355 }
5356
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005357 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5358 {
5359 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5360 input_data->len ) );
5361 }
5362
Paul Elliott6f0e7202021-08-25 12:57:18 +01005363 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005364
Paul Elliott693bf312021-07-23 17:40:41 +01005365 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005366
5367 if( expected_status == PSA_SUCCESS )
5368 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005369 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5370 {
5371 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5372 input_data->len ) );
5373 }
5374 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5375 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005376
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005377 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5378 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5379 additional_data->len ),
5380 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005381
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005382 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005383 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005384 &ciphertext_length ),
5385 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005386
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005387 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005388 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005389 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5390 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005391 }
5392
5393exit:
5394 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005395 mbedtls_free( output );
5396 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005397 mbedtls_free( nonce_buffer );
5398 psa_aead_abort( &operation );
5399 PSA_DONE( );
5400}
5401/* END_CASE */
5402
5403/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005404void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5405 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005406 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005407 data_t *nonce,
5408 data_t *additional_data,
5409 data_t *input_data,
5410 int expected_status_arg )
5411{
5412
5413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5414 psa_key_type_t key_type = key_type_arg;
5415 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005416 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5418 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5419 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005420 unsigned char *output = NULL;
5421 unsigned char *ciphertext = NULL;
5422 size_t output_size = output_size_arg;
5423 size_t ciphertext_size = 0;
5424 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005425 size_t tag_length = 0;
5426 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5427
5428 PSA_ASSERT( psa_crypto_init( ) );
5429
5430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5431 psa_set_key_algorithm( &attributes, alg );
5432 psa_set_key_type( &attributes, key_type );
5433
5434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5435 &key ) );
5436
5437 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5438
Paul Elliottc6d11d02021-09-01 12:04:23 +01005439 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005440
Paul Elliottc6d11d02021-09-01 12:04:23 +01005441 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005442
Paul Elliottc6d11d02021-09-01 12:04:23 +01005443 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005444
Paul Elliott43fbda62021-07-23 18:30:59 +01005445 status = psa_aead_encrypt_setup( &operation, key, alg );
5446
5447 /* If the operation is not supported, just skip and not fail in case the
5448 * encryption involves a common limitation of cryptography hardwares and
5449 * an alternative implementation. */
5450 if( status == PSA_ERROR_NOT_SUPPORTED )
5451 {
5452 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5453 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5454 }
5455
5456 PSA_ASSERT( status );
5457
Paul Elliott47b9a142021-10-07 15:04:57 +01005458 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5459 input_data->len ) );
5460
Paul Elliott43fbda62021-07-23 18:30:59 +01005461 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5462
5463 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5464 additional_data->len ) );
5465
5466 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005467 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005468
5469 TEST_EQUAL( status, expected_status );
5470
5471 if( expected_status == PSA_SUCCESS )
5472 {
5473 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005474 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5475 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005476 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5477 }
5478
5479exit:
5480 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005481 mbedtls_free( output );
5482 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005483 psa_aead_abort( &operation );
5484 PSA_DONE( );
5485}
5486/* END_CASE */
5487
Paul Elliott91b021e2021-07-23 18:52:31 +01005488/* BEGIN_CASE */
5489void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5490 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005491 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005492 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005493 data_t *nonce,
5494 data_t *additional_data,
5495 data_t *input_data,
5496 int expected_status_arg )
5497{
5498
5499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5500 psa_key_type_t key_type = key_type_arg;
5501 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005502 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005503 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5504 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5505 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005506 unsigned char *ciphertext = NULL;
5507 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005508 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005509 size_t ciphertext_size = 0;
5510 size_t ciphertext_length = 0;
5511 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005512 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005513 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005514
5515 PSA_ASSERT( psa_crypto_init( ) );
5516
5517 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5518 psa_set_key_algorithm( &attributes, alg );
5519 psa_set_key_type( &attributes, key_type );
5520
5521 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5522 &key ) );
5523
5524 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5525
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005526 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005527
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005528 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005529
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005530 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005531
Paul Elliott719c1322021-09-13 18:27:22 +01005532 ASSERT_ALLOC( tag_buffer, tag_size );
5533
Paul Elliott91b021e2021-07-23 18:52:31 +01005534 status = psa_aead_encrypt_setup( &operation, key, alg );
5535
5536 /* If the operation is not supported, just skip and not fail in case the
5537 * encryption involves a common limitation of cryptography hardwares and
5538 * an alternative implementation. */
5539 if( status == PSA_ERROR_NOT_SUPPORTED )
5540 {
5541 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5542 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5543 }
5544
5545 PSA_ASSERT( status );
5546
5547 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5548
Paul Elliott76bda482021-10-07 17:07:23 +01005549 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5550 input_data->len ) );
5551
Paul Elliott91b021e2021-07-23 18:52:31 +01005552 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5553 additional_data->len ) );
5554
5555 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005556 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005557
5558 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005559 status = psa_aead_finish( &operation, finish_ciphertext,
5560 finish_ciphertext_size,
5561 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005562 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005563
5564 TEST_EQUAL( status, expected_status );
5565
5566exit:
5567 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005568 mbedtls_free( ciphertext );
5569 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005570 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005571 psa_aead_abort( &operation );
5572 PSA_DONE( );
5573}
5574/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005575
5576/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005577void aead_multipart_verify( int key_type_arg, data_t *key_data,
5578 int alg_arg,
5579 data_t *nonce,
5580 data_t *additional_data,
5581 data_t *input_data,
5582 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005583 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005584 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005585 int expected_status_arg )
5586{
5587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5588 psa_key_type_t key_type = key_type_arg;
5589 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005590 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5592 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5593 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005594 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005595 unsigned char *plaintext = NULL;
5596 unsigned char *finish_plaintext = NULL;
5597 size_t plaintext_size = 0;
5598 size_t plaintext_length = 0;
5599 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005600 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005601 unsigned char *tag_buffer = NULL;
5602 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005603
5604 PSA_ASSERT( psa_crypto_init( ) );
5605
5606 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5607 psa_set_key_algorithm( &attributes, alg );
5608 psa_set_key_type( &attributes, key_type );
5609
5610 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5611 &key ) );
5612
5613 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5614
5615 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5616 input_data->len );
5617
5618 ASSERT_ALLOC( plaintext, plaintext_size );
5619
5620 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5621
5622 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5623
Paul Elliott9961a662021-09-17 19:19:02 +01005624 status = psa_aead_decrypt_setup( &operation, key, alg );
5625
5626 /* If the operation is not supported, just skip and not fail in case the
5627 * encryption involves a common limitation of cryptography hardwares and
5628 * an alternative implementation. */
5629 if( status == PSA_ERROR_NOT_SUPPORTED )
5630 {
5631 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5632 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5633 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005634 TEST_EQUAL( status, expected_setup_status );
5635
5636 if( status != PSA_SUCCESS )
5637 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005638
5639 PSA_ASSERT( status );
5640
5641 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5642
Paul Elliottfec6f372021-10-06 17:15:02 +01005643 status = psa_aead_set_lengths( &operation, additional_data->len,
5644 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005645 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005646
Paul Elliott9961a662021-09-17 19:19:02 +01005647 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5648 additional_data->len ) );
5649
5650 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5651 input_data->len,
5652 plaintext, plaintext_size,
5653 &plaintext_length ) );
5654
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005655 if( tag_usage == USE_GIVEN_TAG )
5656 {
5657 tag_buffer = tag->x;
5658 tag_size = tag->len;
5659 }
5660
Paul Elliott9961a662021-09-17 19:19:02 +01005661 status = psa_aead_verify( &operation, finish_plaintext,
5662 verify_plaintext_size,
5663 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005664 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005665
5666 TEST_EQUAL( status, expected_status );
5667
5668exit:
5669 psa_destroy_key( key );
5670 mbedtls_free( plaintext );
5671 mbedtls_free( finish_plaintext );
5672 psa_aead_abort( &operation );
5673 PSA_DONE( );
5674}
5675/* END_CASE */
5676
Paul Elliott9961a662021-09-17 19:19:02 +01005677/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005678void aead_multipart_setup( int key_type_arg, data_t *key_data,
5679 int alg_arg, int expected_status_arg )
5680{
5681 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5682 psa_key_type_t key_type = key_type_arg;
5683 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005684 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5686 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5687 psa_status_t expected_status = expected_status_arg;
5688
5689 PSA_ASSERT( psa_crypto_init( ) );
5690
5691 psa_set_key_usage_flags( &attributes,
5692 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5693 psa_set_key_algorithm( &attributes, alg );
5694 psa_set_key_type( &attributes, key_type );
5695
5696 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5697 &key ) );
5698
Paul Elliott5221ef62021-09-19 17:33:03 +01005699 status = psa_aead_encrypt_setup( &operation, key, alg );
5700
5701 TEST_EQUAL( status, expected_status );
5702
5703 psa_aead_abort( &operation );
5704
Paul Elliott5221ef62021-09-19 17:33:03 +01005705 status = psa_aead_decrypt_setup( &operation, key, alg );
5706
5707 TEST_EQUAL(status, expected_status );
5708
5709exit:
5710 psa_destroy_key( key );
5711 psa_aead_abort( &operation );
5712 PSA_DONE( );
5713}
5714/* END_CASE */
5715
5716/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5718 int alg_arg,
5719 data_t *nonce,
5720 data_t *additional_data,
5721 data_t *input_data )
5722{
5723 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5724 psa_key_type_t key_type = key_type_arg;
5725 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005726 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005727 unsigned char *output_data = NULL;
5728 unsigned char *final_data = NULL;
5729 size_t output_size = 0;
5730 size_t finish_output_size = 0;
5731 size_t output_length = 0;
5732 size_t key_bits = 0;
5733 size_t tag_length = 0;
5734 size_t tag_size = 0;
5735 size_t nonce_length = 0;
5736 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5737 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5738 size_t output_part_length = 0;
5739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5740
5741 PSA_ASSERT( psa_crypto_init( ) );
5742
5743 psa_set_key_usage_flags( & attributes,
5744 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5745 psa_set_key_algorithm( & attributes, alg );
5746 psa_set_key_type( & attributes, key_type );
5747
5748 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5749 &key ) );
5750
5751 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5752 key_bits = psa_get_key_bits( &attributes );
5753
5754 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5755
Gilles Peskine7be11a72022-04-14 00:12:57 +02005756 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757
5758 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5759
5760 ASSERT_ALLOC( output_data, output_size );
5761
5762 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5763
Gilles Peskine7be11a72022-04-14 00:12:57 +02005764 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765
5766 ASSERT_ALLOC( final_data, finish_output_size );
5767
5768 /* Test all operations error without calling setup first. */
5769
Paul Elliottc23a9a02021-06-21 18:32:46 +01005770 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5771 PSA_ERROR_BAD_STATE );
5772
5773 psa_aead_abort( &operation );
5774
Paul Elliottc23a9a02021-06-21 18:32:46 +01005775 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5776 PSA_AEAD_NONCE_MAX_SIZE,
5777 &nonce_length ),
5778 PSA_ERROR_BAD_STATE );
5779
5780 psa_aead_abort( &operation );
5781
Paul Elliott481be342021-07-16 17:38:47 +01005782 /* ------------------------------------------------------- */
5783
Paul Elliottc23a9a02021-06-21 18:32:46 +01005784 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5785 input_data->len ),
5786 PSA_ERROR_BAD_STATE );
5787
5788 psa_aead_abort( &operation );
5789
Paul Elliott481be342021-07-16 17:38:47 +01005790 /* ------------------------------------------------------- */
5791
Paul Elliottc23a9a02021-06-21 18:32:46 +01005792 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5793 additional_data->len ),
5794 PSA_ERROR_BAD_STATE );
5795
5796 psa_aead_abort( &operation );
5797
Paul Elliott481be342021-07-16 17:38:47 +01005798 /* ------------------------------------------------------- */
5799
Paul Elliottc23a9a02021-06-21 18:32:46 +01005800 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5801 input_data->len, output_data,
5802 output_size, &output_length ),
5803 PSA_ERROR_BAD_STATE );
5804
5805 psa_aead_abort( &operation );
5806
Paul Elliott481be342021-07-16 17:38:47 +01005807 /* ------------------------------------------------------- */
5808
Paul Elliottc23a9a02021-06-21 18:32:46 +01005809 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5810 finish_output_size,
5811 &output_part_length,
5812 tag_buffer, tag_length,
5813 &tag_size ),
5814 PSA_ERROR_BAD_STATE );
5815
5816 psa_aead_abort( &operation );
5817
Paul Elliott481be342021-07-16 17:38:47 +01005818 /* ------------------------------------------------------- */
5819
Paul Elliottc23a9a02021-06-21 18:32:46 +01005820 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5821 finish_output_size,
5822 &output_part_length,
5823 tag_buffer,
5824 tag_length ),
5825 PSA_ERROR_BAD_STATE );
5826
5827 psa_aead_abort( &operation );
5828
5829 /* Test for double setups. */
5830
Paul Elliottc23a9a02021-06-21 18:32:46 +01005831 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5832
5833 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5834 PSA_ERROR_BAD_STATE );
5835
5836 psa_aead_abort( &operation );
5837
Paul Elliott481be342021-07-16 17:38:47 +01005838 /* ------------------------------------------------------- */
5839
Paul Elliottc23a9a02021-06-21 18:32:46 +01005840 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5841
5842 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5843 PSA_ERROR_BAD_STATE );
5844
5845 psa_aead_abort( &operation );
5846
Paul Elliott374a2be2021-07-16 17:53:40 +01005847 /* ------------------------------------------------------- */
5848
Paul Elliott374a2be2021-07-16 17:53:40 +01005849 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5850
5851 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5852 PSA_ERROR_BAD_STATE );
5853
5854 psa_aead_abort( &operation );
5855
5856 /* ------------------------------------------------------- */
5857
Paul Elliott374a2be2021-07-16 17:53:40 +01005858 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5859
5860 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5861 PSA_ERROR_BAD_STATE );
5862
5863 psa_aead_abort( &operation );
5864
Paul Elliottc23a9a02021-06-21 18:32:46 +01005865 /* Test for not setting a nonce. */
5866
Paul Elliottc23a9a02021-06-21 18:32:46 +01005867 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5868
5869 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5870 additional_data->len ),
5871 PSA_ERROR_BAD_STATE );
5872
5873 psa_aead_abort( &operation );
5874
Paul Elliott7f628422021-09-01 12:08:29 +01005875 /* ------------------------------------------------------- */
5876
Paul Elliott7f628422021-09-01 12:08:29 +01005877 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5878
5879 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5880 input_data->len, output_data,
5881 output_size, &output_length ),
5882 PSA_ERROR_BAD_STATE );
5883
5884 psa_aead_abort( &operation );
5885
Paul Elliottbdc2c682021-09-21 18:37:10 +01005886 /* ------------------------------------------------------- */
5887
Paul Elliottbdc2c682021-09-21 18:37:10 +01005888 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5889
5890 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5891 finish_output_size,
5892 &output_part_length,
5893 tag_buffer, tag_length,
5894 &tag_size ),
5895 PSA_ERROR_BAD_STATE );
5896
5897 psa_aead_abort( &operation );
5898
5899 /* ------------------------------------------------------- */
5900
Paul Elliottbdc2c682021-09-21 18:37:10 +01005901 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5902
5903 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5904 finish_output_size,
5905 &output_part_length,
5906 tag_buffer,
5907 tag_length ),
5908 PSA_ERROR_BAD_STATE );
5909
5910 psa_aead_abort( &operation );
5911
Paul Elliottc23a9a02021-06-21 18:32:46 +01005912 /* Test for double setting nonce. */
5913
Paul Elliottc23a9a02021-06-21 18:32:46 +01005914 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5915
5916 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5917
5918 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5919 PSA_ERROR_BAD_STATE );
5920
5921 psa_aead_abort( &operation );
5922
Paul Elliott374a2be2021-07-16 17:53:40 +01005923 /* Test for double generating nonce. */
5924
Paul Elliott374a2be2021-07-16 17:53:40 +01005925 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5926
5927 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5928 PSA_AEAD_NONCE_MAX_SIZE,
5929 &nonce_length ) );
5930
5931 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5932 PSA_AEAD_NONCE_MAX_SIZE,
5933 &nonce_length ),
5934 PSA_ERROR_BAD_STATE );
5935
5936
5937 psa_aead_abort( &operation );
5938
5939 /* Test for generate nonce then set and vice versa */
5940
Paul Elliott374a2be2021-07-16 17:53:40 +01005941 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5942
5943 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5944 PSA_AEAD_NONCE_MAX_SIZE,
5945 &nonce_length ) );
5946
5947 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5948 PSA_ERROR_BAD_STATE );
5949
5950 psa_aead_abort( &operation );
5951
Andrzej Kurekad837522021-12-15 15:28:49 +01005952 /* Test for generating nonce after calling set lengths */
5953
5954 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5955
5956 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5957 input_data->len ) );
5958
5959 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5960 PSA_AEAD_NONCE_MAX_SIZE,
5961 &nonce_length ) );
5962
5963 psa_aead_abort( &operation );
5964
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005965 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005966
5967 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5968
5969 if( operation.alg == PSA_ALG_CCM )
5970 {
5971 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5972 input_data->len ),
5973 PSA_ERROR_INVALID_ARGUMENT );
5974 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5975 PSA_AEAD_NONCE_MAX_SIZE,
5976 &nonce_length ),
5977 PSA_ERROR_BAD_STATE );
5978 }
5979 else
5980 {
5981 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5982 input_data->len ) );
5983 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5984 PSA_AEAD_NONCE_MAX_SIZE,
5985 &nonce_length ) );
5986 }
5987
5988 psa_aead_abort( &operation );
5989
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005990 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005991#if SIZE_MAX > UINT32_MAX
5992 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5993
5994 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5995 {
5996 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5997 input_data->len ),
5998 PSA_ERROR_INVALID_ARGUMENT );
5999 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6000 PSA_AEAD_NONCE_MAX_SIZE,
6001 &nonce_length ),
6002 PSA_ERROR_BAD_STATE );
6003 }
6004 else
6005 {
6006 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6007 input_data->len ) );
6008 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6009 PSA_AEAD_NONCE_MAX_SIZE,
6010 &nonce_length ) );
6011 }
6012
6013 psa_aead_abort( &operation );
6014#endif
6015
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006016 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006017
6018 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6019
6020 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6021 PSA_AEAD_NONCE_MAX_SIZE,
6022 &nonce_length ) );
6023
6024 if( operation.alg == PSA_ALG_CCM )
6025 {
6026 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6027 input_data->len ),
6028 PSA_ERROR_INVALID_ARGUMENT );
6029 }
6030 else
6031 {
6032 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6033 input_data->len ) );
6034 }
6035
6036 psa_aead_abort( &operation );
6037
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006038 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006039 /* Test for setting nonce after calling set lengths */
6040
6041 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6042
6043 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6044 input_data->len ) );
6045
6046 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6047
6048 psa_aead_abort( &operation );
6049
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006050 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006051
6052 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6053
6054 if( operation.alg == PSA_ALG_CCM )
6055 {
6056 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6057 input_data->len ),
6058 PSA_ERROR_INVALID_ARGUMENT );
6059 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6060 PSA_ERROR_BAD_STATE );
6061 }
6062 else
6063 {
6064 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6065 input_data->len ) );
6066 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6067 }
6068
6069 psa_aead_abort( &operation );
6070
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006071 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006072#if SIZE_MAX > UINT32_MAX
6073 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6074
6075 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6076 {
6077 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6078 input_data->len ),
6079 PSA_ERROR_INVALID_ARGUMENT );
6080 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6081 PSA_ERROR_BAD_STATE );
6082 }
6083 else
6084 {
6085 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6086 input_data->len ) );
6087 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6088 }
6089
6090 psa_aead_abort( &operation );
6091#endif
6092
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006093 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006094
6095 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6096
6097 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6098
6099 if( operation.alg == PSA_ALG_CCM )
6100 {
6101 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6102 input_data->len ),
6103 PSA_ERROR_INVALID_ARGUMENT );
6104 }
6105 else
6106 {
6107 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6108 input_data->len ) );
6109 }
6110
6111 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006112
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006113 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006114#if SIZE_MAX > UINT32_MAX
6115 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6116
6117 if( operation.alg == PSA_ALG_GCM )
6118 {
6119 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6120 SIZE_MAX ),
6121 PSA_ERROR_INVALID_ARGUMENT );
6122 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6123 PSA_ERROR_BAD_STATE );
6124 }
6125 else if ( operation.alg != PSA_ALG_CCM )
6126 {
6127 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6128 SIZE_MAX ) );
6129 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6130 }
6131
6132 psa_aead_abort( &operation );
6133
Tom Cosgrove1797b052022-12-04 17:19:59 +00006134 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006135 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6136
6137 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6138
6139 if( operation.alg == PSA_ALG_GCM )
6140 {
6141 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6142 SIZE_MAX ),
6143 PSA_ERROR_INVALID_ARGUMENT );
6144 }
6145 else if ( operation.alg != PSA_ALG_CCM )
6146 {
6147 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6148 SIZE_MAX ) );
6149 }
6150
6151 psa_aead_abort( &operation );
6152#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006153
6154 /* ------------------------------------------------------- */
6155
Paul Elliott374a2be2021-07-16 17:53:40 +01006156 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6157
6158 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6159
6160 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6161 PSA_AEAD_NONCE_MAX_SIZE,
6162 &nonce_length ),
6163 PSA_ERROR_BAD_STATE );
6164
6165 psa_aead_abort( &operation );
6166
Paul Elliott7220cae2021-06-22 17:25:57 +01006167 /* Test for generating nonce in decrypt setup. */
6168
Paul Elliott7220cae2021-06-22 17:25:57 +01006169 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6170
6171 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6172 PSA_AEAD_NONCE_MAX_SIZE,
6173 &nonce_length ),
6174 PSA_ERROR_BAD_STATE );
6175
6176 psa_aead_abort( &operation );
6177
Paul Elliottc23a9a02021-06-21 18:32:46 +01006178 /* Test for setting lengths twice. */
6179
Paul Elliottc23a9a02021-06-21 18:32:46 +01006180 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6181
6182 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6183
6184 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6185 input_data->len ) );
6186
6187 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6188 input_data->len ),
6189 PSA_ERROR_BAD_STATE );
6190
6191 psa_aead_abort( &operation );
6192
Andrzej Kurekad837522021-12-15 15:28:49 +01006193 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006194
Paul Elliottc23a9a02021-06-21 18:32:46 +01006195 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6196
6197 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6198
Andrzej Kurekad837522021-12-15 15:28:49 +01006199 if( operation.alg == PSA_ALG_CCM )
6200 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006201
Andrzej Kurekad837522021-12-15 15:28:49 +01006202 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6203 additional_data->len ),
6204 PSA_ERROR_BAD_STATE );
6205 }
6206 else
6207 {
6208 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6209 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006210
Andrzej Kurekad837522021-12-15 15:28:49 +01006211 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6212 input_data->len ),
6213 PSA_ERROR_BAD_STATE );
6214 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006215 psa_aead_abort( &operation );
6216
6217 /* ------------------------------------------------------- */
6218
Paul Elliottf94bd992021-09-19 18:15:59 +01006219 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6220
6221 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6222
Andrzej Kurekad837522021-12-15 15:28:49 +01006223 if( operation.alg == PSA_ALG_CCM )
6224 {
6225 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6226 input_data->len, output_data,
6227 output_size, &output_length ),
6228 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006229
Andrzej Kurekad837522021-12-15 15:28:49 +01006230 }
6231 else
6232 {
6233 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6234 input_data->len, output_data,
6235 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Andrzej Kurekad837522021-12-15 15:28:49 +01006237 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6238 input_data->len ),
6239 PSA_ERROR_BAD_STATE );
6240 }
6241 psa_aead_abort( &operation );
6242
6243 /* ------------------------------------------------------- */
6244
6245 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6246
6247 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6248
6249 if( operation.alg == PSA_ALG_CCM )
6250 {
6251 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6252 finish_output_size,
6253 &output_part_length,
6254 tag_buffer, tag_length,
6255 &tag_size ) );
6256 }
6257 else
6258 {
6259 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6260 finish_output_size,
6261 &output_part_length,
6262 tag_buffer, tag_length,
6263 &tag_size ) );
6264
6265 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6266 input_data->len ),
6267 PSA_ERROR_BAD_STATE );
6268 }
6269 psa_aead_abort( &operation );
6270
6271 /* Test for setting lengths after generating nonce + already starting data. */
6272
6273 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6274
6275 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6276 PSA_AEAD_NONCE_MAX_SIZE,
6277 &nonce_length ) );
6278 if( operation.alg == PSA_ALG_CCM )
6279 {
6280
6281 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6282 additional_data->len ),
6283 PSA_ERROR_BAD_STATE );
6284 }
6285 else
6286 {
6287 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6288 additional_data->len ) );
6289
6290 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6291 input_data->len ),
6292 PSA_ERROR_BAD_STATE );
6293 }
6294 psa_aead_abort( &operation );
6295
6296 /* ------------------------------------------------------- */
6297
6298 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6299
6300 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6301 PSA_AEAD_NONCE_MAX_SIZE,
6302 &nonce_length ) );
6303 if( operation.alg == PSA_ALG_CCM )
6304 {
6305 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6306 input_data->len, output_data,
6307 output_size, &output_length ),
6308 PSA_ERROR_BAD_STATE );
6309
6310 }
6311 else
6312 {
6313 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6314 input_data->len, output_data,
6315 output_size, &output_length ) );
6316
6317 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6318 input_data->len ),
6319 PSA_ERROR_BAD_STATE );
6320 }
6321 psa_aead_abort( &operation );
6322
6323 /* ------------------------------------------------------- */
6324
6325 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6326
6327 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6328 PSA_AEAD_NONCE_MAX_SIZE,
6329 &nonce_length ) );
6330 if( operation.alg == PSA_ALG_CCM )
6331 {
6332 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6333 finish_output_size,
6334 &output_part_length,
6335 tag_buffer, tag_length,
6336 &tag_size ) );
6337 }
6338 else
6339 {
6340 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6341 finish_output_size,
6342 &output_part_length,
6343 tag_buffer, tag_length,
6344 &tag_size ) );
6345
6346 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6347 input_data->len ),
6348 PSA_ERROR_BAD_STATE );
6349 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006350 psa_aead_abort( &operation );
6351
Paul Elliott243080c2021-07-21 19:01:17 +01006352 /* Test for not sending any additional data or data after setting non zero
6353 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006354
Paul Elliottc23a9a02021-06-21 18:32:46 +01006355 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6356
6357 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6358
6359 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6360 input_data->len ) );
6361
6362 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6363 finish_output_size,
6364 &output_part_length,
6365 tag_buffer, tag_length,
6366 &tag_size ),
6367 PSA_ERROR_INVALID_ARGUMENT );
6368
6369 psa_aead_abort( &operation );
6370
Paul Elliott243080c2021-07-21 19:01:17 +01006371 /* Test for not sending any additional data or data after setting non-zero
6372 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006373
Paul Elliottc23a9a02021-06-21 18:32:46 +01006374 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6375
6376 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6377
6378 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6379 input_data->len ) );
6380
6381 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6382 finish_output_size,
6383 &output_part_length,
6384 tag_buffer,
6385 tag_length ),
6386 PSA_ERROR_INVALID_ARGUMENT );
6387
6388 psa_aead_abort( &operation );
6389
Paul Elliott243080c2021-07-21 19:01:17 +01006390 /* Test for not sending any additional data after setting a non-zero length
6391 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006392
Paul Elliottc23a9a02021-06-21 18:32:46 +01006393 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6394
6395 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6396
6397 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6398 input_data->len ) );
6399
6400 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6401 input_data->len, output_data,
6402 output_size, &output_length ),
6403 PSA_ERROR_INVALID_ARGUMENT );
6404
6405 psa_aead_abort( &operation );
6406
Paul Elliottf94bd992021-09-19 18:15:59 +01006407 /* Test for not sending any data after setting a non-zero length for it.*/
6408
Paul Elliottf94bd992021-09-19 18:15:59 +01006409 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6410
6411 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6412
6413 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6414 input_data->len ) );
6415
6416 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6417 additional_data->len ) );
6418
6419 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6420 finish_output_size,
6421 &output_part_length,
6422 tag_buffer, tag_length,
6423 &tag_size ),
6424 PSA_ERROR_INVALID_ARGUMENT );
6425
6426 psa_aead_abort( &operation );
6427
Paul Elliottb0450fe2021-09-01 15:06:26 +01006428 /* Test for sending too much additional data after setting lengths. */
6429
Paul Elliottb0450fe2021-09-01 15:06:26 +01006430 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6431
6432 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6433
6434 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6435
6436
6437 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6438 additional_data->len ),
6439 PSA_ERROR_INVALID_ARGUMENT );
6440
6441 psa_aead_abort( &operation );
6442
Paul Elliotta2a09b02021-09-22 14:56:40 +01006443 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006444
6445 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, additional_data->len,
6450 input_data->len ) );
6451
6452 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6453 additional_data->len ) );
6454
6455 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6456 1 ),
6457 PSA_ERROR_INVALID_ARGUMENT );
6458
6459 psa_aead_abort( &operation );
6460
Paul Elliottb0450fe2021-09-01 15:06:26 +01006461 /* Test for sending too much data after setting lengths. */
6462
Paul Elliottb0450fe2021-09-01 15:06:26 +01006463 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6464
6465 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6466
6467 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6468
6469 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6470 input_data->len, output_data,
6471 output_size, &output_length ),
6472 PSA_ERROR_INVALID_ARGUMENT );
6473
6474 psa_aead_abort( &operation );
6475
Paul Elliotta2a09b02021-09-22 14:56:40 +01006476 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006477
6478 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6479
6480 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6481
6482 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6483 input_data->len ) );
6484
6485 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6486 additional_data->len ) );
6487
6488 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6489 input_data->len, output_data,
6490 output_size, &output_length ) );
6491
6492 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6493 1, output_data,
6494 output_size, &output_length ),
6495 PSA_ERROR_INVALID_ARGUMENT );
6496
6497 psa_aead_abort( &operation );
6498
Paul Elliottc23a9a02021-06-21 18:32:46 +01006499 /* Test sending additional data after data. */
6500
Paul Elliottc23a9a02021-06-21 18:32:46 +01006501 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6502
6503 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6504
Andrzej Kurekad837522021-12-15 15:28:49 +01006505 if( operation.alg != PSA_ALG_CCM )
6506 {
6507 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6508 input_data->len, output_data,
6509 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006510
Andrzej Kurekad837522021-12-15 15:28:49 +01006511 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6512 additional_data->len ),
6513 PSA_ERROR_BAD_STATE );
6514 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006515 psa_aead_abort( &operation );
6516
Paul Elliott534d0b42021-06-22 19:15:20 +01006517 /* Test calling finish on decryption. */
6518
Paul Elliott534d0b42021-06-22 19:15:20 +01006519 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6520
6521 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6522
6523 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6524 finish_output_size,
6525 &output_part_length,
6526 tag_buffer, tag_length,
6527 &tag_size ),
6528 PSA_ERROR_BAD_STATE );
6529
6530 psa_aead_abort( &operation );
6531
6532 /* Test calling verify on encryption. */
6533
Paul Elliott534d0b42021-06-22 19:15:20 +01006534 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6535
6536 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6537
6538 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6539 finish_output_size,
6540 &output_part_length,
6541 tag_buffer,
6542 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006543 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006544
6545 psa_aead_abort( &operation );
6546
6547
Paul Elliottc23a9a02021-06-21 18:32:46 +01006548exit:
6549 psa_destroy_key( key );
6550 psa_aead_abort( &operation );
6551 mbedtls_free( output_data );
6552 mbedtls_free( final_data );
6553 PSA_DONE( );
6554}
6555/* END_CASE */
6556
6557/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006558void signature_size( int type_arg,
6559 int bits,
6560 int alg_arg,
6561 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006562{
6563 psa_key_type_t type = type_arg;
6564 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006565 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006566
Gilles Peskinefe11b722018-12-18 00:24:04 +01006567 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006568
Gilles Peskinee59236f2018-01-27 23:32:46 +01006569exit:
6570 ;
6571}
6572/* END_CASE */
6573
6574/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006575void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6576 int alg_arg, data_t *input_data,
6577 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006578{
Ronald Cron5425a212020-08-04 14:58:35 +02006579 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006580 psa_key_type_t key_type = key_type_arg;
6581 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006582 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006583 unsigned char *signature = NULL;
6584 size_t signature_size;
6585 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006587
Gilles Peskine8817f612018-12-18 00:18:46 +01006588 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006589
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006590 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006591 psa_set_key_algorithm( &attributes, alg );
6592 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006593
Gilles Peskine049c7532019-05-15 20:22:09 +02006594 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006595 &key ) );
6596 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006597 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006598
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006599 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006600 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006601 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006602 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006603 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006604 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006605 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006606
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006607 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006608 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006609 input_data->x, input_data->len,
6610 signature, signature_size,
6611 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006612 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006613 ASSERT_COMPARE( output_data->x, output_data->len,
6614 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006615
6616exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006617 /*
6618 * Key attributes may have been returned by psa_get_key_attributes()
6619 * thus reset them as required.
6620 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006621 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006622
Ronald Cron5425a212020-08-04 14:58:35 +02006623 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006624 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006625 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006626}
6627/* END_CASE */
6628
6629/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006630void sign_hash_fail( int key_type_arg, data_t *key_data,
6631 int alg_arg, data_t *input_data,
6632 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006633{
Ronald Cron5425a212020-08-04 14:58:35 +02006634 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006635 psa_key_type_t key_type = key_type_arg;
6636 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006637 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006638 psa_status_t actual_status;
6639 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006640 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006641 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006643
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006644 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006645
Gilles Peskine8817f612018-12-18 00:18:46 +01006646 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006647
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006648 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006649 psa_set_key_algorithm( &attributes, alg );
6650 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006651
Gilles Peskine049c7532019-05-15 20:22:09 +02006652 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006653 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006654
Ronald Cron5425a212020-08-04 14:58:35 +02006655 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006656 input_data->x, input_data->len,
6657 signature, signature_size,
6658 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006659 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006660 /* The value of *signature_length is unspecified on error, but
6661 * whatever it is, it should be less than signature_size, so that
6662 * if the caller tries to read *signature_length bytes without
6663 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006664 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006665
6666exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006667 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006668 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006669 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006670 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006671}
6672/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006673
6674/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006675void sign_verify_hash( int key_type_arg, data_t *key_data,
6676 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006677{
Ronald Cron5425a212020-08-04 14:58:35 +02006678 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006679 psa_key_type_t key_type = key_type_arg;
6680 psa_algorithm_t alg = alg_arg;
6681 size_t key_bits;
6682 unsigned char *signature = NULL;
6683 size_t signature_size;
6684 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006686
Gilles Peskine8817f612018-12-18 00:18:46 +01006687 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006688
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006689 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006690 psa_set_key_algorithm( &attributes, alg );
6691 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006692
Gilles Peskine049c7532019-05-15 20:22:09 +02006693 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006694 &key ) );
6695 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006696 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006697
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006698 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006699 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006700 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006701 key_bits, alg );
6702 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006703 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006704 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006705
6706 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006707 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006708 input_data->x, input_data->len,
6709 signature, signature_size,
6710 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006711 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006712 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006713 TEST_ASSERT( signature_length > 0 );
6714
6715 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006716 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006717 input_data->x, input_data->len,
6718 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006719
6720 if( input_data->len != 0 )
6721 {
6722 /* Flip a bit in the input and verify that the signature is now
6723 * detected as invalid. Flip a bit at the beginning, not at the end,
6724 * because ECDSA may ignore the last few bits of the input. */
6725 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006726 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006727 input_data->x, input_data->len,
6728 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006729 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006730 }
6731
6732exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006733 /*
6734 * Key attributes may have been returned by psa_get_key_attributes()
6735 * thus reset them as required.
6736 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006737 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006738
Ronald Cron5425a212020-08-04 14:58:35 +02006739 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006740 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006741 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006742}
6743/* END_CASE */
6744
6745/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006746void verify_hash( int key_type_arg, data_t *key_data,
6747 int alg_arg, data_t *hash_data,
6748 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006749{
Ronald Cron5425a212020-08-04 14:58:35 +02006750 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006751 psa_key_type_t key_type = key_type_arg;
6752 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006754
Gilles Peskine7be11a72022-04-14 00:12:57 +02006755 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006756
Gilles Peskine8817f612018-12-18 00:18:46 +01006757 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006758
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006759 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006760 psa_set_key_algorithm( &attributes, alg );
6761 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006762
Gilles Peskine049c7532019-05-15 20:22:09 +02006763 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006764 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006765
Ronald Cron5425a212020-08-04 14:58:35 +02006766 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006767 hash_data->x, hash_data->len,
6768 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006769
itayzafrir5c753392018-05-08 11:18:38 +03006770exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006771 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006772 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006773 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006774}
6775/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006776
6777/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006778void verify_hash_fail( int key_type_arg, data_t *key_data,
6779 int alg_arg, data_t *hash_data,
6780 data_t *signature_data,
6781 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006782{
Ronald Cron5425a212020-08-04 14:58:35 +02006783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006784 psa_key_type_t key_type = key_type_arg;
6785 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006786 psa_status_t actual_status;
6787 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006788 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006789
Gilles Peskine8817f612018-12-18 00:18:46 +01006790 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006791
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006792 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006793 psa_set_key_algorithm( &attributes, alg );
6794 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006795
Gilles Peskine049c7532019-05-15 20:22:09 +02006796 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006797 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006798
Ronald Cron5425a212020-08-04 14:58:35 +02006799 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006800 hash_data->x, hash_data->len,
6801 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006802 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006803
6804exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006805 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006806 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006807 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006808}
6809/* END_CASE */
6810
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006811/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006812void sign_message_deterministic( int key_type_arg,
6813 data_t *key_data,
6814 int alg_arg,
6815 data_t *input_data,
6816 data_t *output_data )
6817{
6818 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6819 psa_key_type_t key_type = key_type_arg;
6820 psa_algorithm_t alg = alg_arg;
6821 size_t key_bits;
6822 unsigned char *signature = NULL;
6823 size_t signature_size;
6824 size_t signature_length = 0xdeadbeef;
6825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6826
6827 PSA_ASSERT( psa_crypto_init( ) );
6828
6829 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6830 psa_set_key_algorithm( &attributes, alg );
6831 psa_set_key_type( &attributes, key_type );
6832
6833 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6834 &key ) );
6835 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6836 key_bits = psa_get_key_bits( &attributes );
6837
6838 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6839 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006840 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006841 ASSERT_ALLOC( signature, signature_size );
6842
6843 PSA_ASSERT( psa_sign_message( key, alg,
6844 input_data->x, input_data->len,
6845 signature, signature_size,
6846 &signature_length ) );
6847
6848 ASSERT_COMPARE( output_data->x, output_data->len,
6849 signature, signature_length );
6850
6851exit:
6852 psa_reset_key_attributes( &attributes );
6853
6854 psa_destroy_key( key );
6855 mbedtls_free( signature );
6856 PSA_DONE( );
6857
6858}
6859/* END_CASE */
6860
6861/* BEGIN_CASE */
6862void sign_message_fail( int key_type_arg,
6863 data_t *key_data,
6864 int alg_arg,
6865 data_t *input_data,
6866 int signature_size_arg,
6867 int expected_status_arg )
6868{
6869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6870 psa_key_type_t key_type = key_type_arg;
6871 psa_algorithm_t alg = alg_arg;
6872 size_t signature_size = signature_size_arg;
6873 psa_status_t actual_status;
6874 psa_status_t expected_status = expected_status_arg;
6875 unsigned char *signature = NULL;
6876 size_t signature_length = 0xdeadbeef;
6877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6878
6879 ASSERT_ALLOC( signature, signature_size );
6880
6881 PSA_ASSERT( psa_crypto_init( ) );
6882
6883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6884 psa_set_key_algorithm( &attributes, alg );
6885 psa_set_key_type( &attributes, key_type );
6886
6887 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6888 &key ) );
6889
6890 actual_status = psa_sign_message( key, alg,
6891 input_data->x, input_data->len,
6892 signature, signature_size,
6893 &signature_length );
6894 TEST_EQUAL( actual_status, expected_status );
6895 /* The value of *signature_length is unspecified on error, but
6896 * whatever it is, it should be less than signature_size, so that
6897 * if the caller tries to read *signature_length bytes without
6898 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006899 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006900
6901exit:
6902 psa_reset_key_attributes( &attributes );
6903 psa_destroy_key( key );
6904 mbedtls_free( signature );
6905 PSA_DONE( );
6906}
6907/* END_CASE */
6908
6909/* BEGIN_CASE */
6910void sign_verify_message( int key_type_arg,
6911 data_t *key_data,
6912 int alg_arg,
6913 data_t *input_data )
6914{
6915 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6916 psa_key_type_t key_type = key_type_arg;
6917 psa_algorithm_t alg = alg_arg;
6918 size_t key_bits;
6919 unsigned char *signature = NULL;
6920 size_t signature_size;
6921 size_t signature_length = 0xdeadbeef;
6922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6923
6924 PSA_ASSERT( psa_crypto_init( ) );
6925
6926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6927 PSA_KEY_USAGE_VERIFY_MESSAGE );
6928 psa_set_key_algorithm( &attributes, alg );
6929 psa_set_key_type( &attributes, key_type );
6930
6931 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6932 &key ) );
6933 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6934 key_bits = psa_get_key_bits( &attributes );
6935
6936 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6937 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006938 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006939 ASSERT_ALLOC( signature, signature_size );
6940
6941 PSA_ASSERT( psa_sign_message( key, alg,
6942 input_data->x, input_data->len,
6943 signature, signature_size,
6944 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006945 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006946 TEST_ASSERT( signature_length > 0 );
6947
6948 PSA_ASSERT( psa_verify_message( key, alg,
6949 input_data->x, input_data->len,
6950 signature, signature_length ) );
6951
6952 if( input_data->len != 0 )
6953 {
6954 /* Flip a bit in the input and verify that the signature is now
6955 * detected as invalid. Flip a bit at the beginning, not at the end,
6956 * because ECDSA may ignore the last few bits of the input. */
6957 input_data->x[0] ^= 1;
6958 TEST_EQUAL( psa_verify_message( key, alg,
6959 input_data->x, input_data->len,
6960 signature, signature_length ),
6961 PSA_ERROR_INVALID_SIGNATURE );
6962 }
6963
6964exit:
6965 psa_reset_key_attributes( &attributes );
6966
6967 psa_destroy_key( key );
6968 mbedtls_free( signature );
6969 PSA_DONE( );
6970}
6971/* END_CASE */
6972
6973/* BEGIN_CASE */
6974void verify_message( int key_type_arg,
6975 data_t *key_data,
6976 int alg_arg,
6977 data_t *input_data,
6978 data_t *signature_data )
6979{
6980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6981 psa_key_type_t key_type = key_type_arg;
6982 psa_algorithm_t alg = alg_arg;
6983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6984
Gilles Peskine7be11a72022-04-14 00:12:57 +02006985 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006986
6987 PSA_ASSERT( psa_crypto_init( ) );
6988
6989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6990 psa_set_key_algorithm( &attributes, alg );
6991 psa_set_key_type( &attributes, key_type );
6992
6993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6994 &key ) );
6995
6996 PSA_ASSERT( psa_verify_message( key, alg,
6997 input_data->x, input_data->len,
6998 signature_data->x, signature_data->len ) );
6999
7000exit:
7001 psa_reset_key_attributes( &attributes );
7002 psa_destroy_key( key );
7003 PSA_DONE( );
7004}
7005/* END_CASE */
7006
7007/* BEGIN_CASE */
7008void verify_message_fail( int key_type_arg,
7009 data_t *key_data,
7010 int alg_arg,
7011 data_t *hash_data,
7012 data_t *signature_data,
7013 int expected_status_arg )
7014{
7015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7016 psa_key_type_t key_type = key_type_arg;
7017 psa_algorithm_t alg = alg_arg;
7018 psa_status_t actual_status;
7019 psa_status_t expected_status = expected_status_arg;
7020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7021
7022 PSA_ASSERT( psa_crypto_init( ) );
7023
7024 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
7025 psa_set_key_algorithm( &attributes, alg );
7026 psa_set_key_type( &attributes, key_type );
7027
7028 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
7029 &key ) );
7030
7031 actual_status = psa_verify_message( key, alg,
7032 hash_data->x, hash_data->len,
7033 signature_data->x,
7034 signature_data->len );
7035 TEST_EQUAL( actual_status, expected_status );
7036
7037exit:
7038 psa_reset_key_attributes( &attributes );
7039 psa_destroy_key( key );
7040 PSA_DONE( );
7041}
7042/* END_CASE */
7043
7044/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007045void asymmetric_encrypt( int key_type_arg,
7046 data_t *key_data,
7047 int alg_arg,
7048 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007049 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007050 int expected_output_length_arg,
7051 int expected_status_arg )
7052{
Ronald Cron5425a212020-08-04 14:58:35 +02007053 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007054 psa_key_type_t key_type = key_type_arg;
7055 psa_algorithm_t alg = alg_arg;
7056 size_t expected_output_length = expected_output_length_arg;
7057 size_t key_bits;
7058 unsigned char *output = NULL;
7059 size_t output_size;
7060 size_t output_length = ~0;
7061 psa_status_t actual_status;
7062 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007064
Gilles Peskine8817f612018-12-18 00:18:46 +01007065 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007066
Gilles Peskine656896e2018-06-29 19:12:28 +02007067 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007068 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7069 psa_set_key_algorithm( &attributes, alg );
7070 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007071 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007072 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007073
7074 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007075 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007076 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007077
Gilles Peskine656896e2018-06-29 19:12:28 +02007078 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007079 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007080 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007081
7082 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007083 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007084 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007085 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007086 output, output_size,
7087 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007088 TEST_EQUAL( actual_status, expected_status );
7089 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007090
Gilles Peskine68428122018-06-30 18:42:41 +02007091 /* If the label is empty, the test framework puts a non-null pointer
7092 * in label->x. Test that a null pointer works as well. */
7093 if( label->len == 0 )
7094 {
7095 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007096 if( output_size != 0 )
7097 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007098 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007099 input_data->x, input_data->len,
7100 NULL, label->len,
7101 output, output_size,
7102 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007103 TEST_EQUAL( actual_status, expected_status );
7104 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007105 }
7106
Gilles Peskine656896e2018-06-29 19:12:28 +02007107exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007108 /*
7109 * Key attributes may have been returned by psa_get_key_attributes()
7110 * thus reset them as required.
7111 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007112 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007113
Ronald Cron5425a212020-08-04 14:58:35 +02007114 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007115 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007116 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007117}
7118/* END_CASE */
7119
7120/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007121void asymmetric_encrypt_decrypt( int key_type_arg,
7122 data_t *key_data,
7123 int alg_arg,
7124 data_t *input_data,
7125 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007126{
Ronald Cron5425a212020-08-04 14:58:35 +02007127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007128 psa_key_type_t key_type = key_type_arg;
7129 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007130 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007131 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007132 size_t output_size;
7133 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007134 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007135 size_t output2_size;
7136 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007137 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007138
Gilles Peskine8817f612018-12-18 00:18:46 +01007139 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007140
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7142 psa_set_key_algorithm( &attributes, alg );
7143 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007144
Gilles Peskine049c7532019-05-15 20:22:09 +02007145 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007146 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007147
7148 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007149 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007150 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007151
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007152 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007153 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007154 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007155
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007156 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007157 TEST_LE_U( output2_size,
7158 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7159 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007160 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007161
Gilles Peskineeebd7382018-06-08 18:11:54 +02007162 /* We test encryption by checking that encrypt-then-decrypt gives back
7163 * the original plaintext because of the non-optional random
7164 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007165 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007166 input_data->x, input_data->len,
7167 label->x, label->len,
7168 output, output_size,
7169 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007170 /* We don't know what ciphertext length to expect, but check that
7171 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007172 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007173
Ronald Cron5425a212020-08-04 14:58:35 +02007174 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007175 output, output_length,
7176 label->x, label->len,
7177 output2, output2_size,
7178 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007179 ASSERT_COMPARE( input_data->x, input_data->len,
7180 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007181
7182exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007183 /*
7184 * Key attributes may have been returned by psa_get_key_attributes()
7185 * thus reset them as required.
7186 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007187 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007188
Ronald Cron5425a212020-08-04 14:58:35 +02007189 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007190 mbedtls_free( output );
7191 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007192 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007193}
7194/* END_CASE */
7195
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007196/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007197void asymmetric_decrypt( int key_type_arg,
7198 data_t *key_data,
7199 int alg_arg,
7200 data_t *input_data,
7201 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007202 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007203{
Ronald Cron5425a212020-08-04 14:58:35 +02007204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007205 psa_key_type_t key_type = key_type_arg;
7206 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007207 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007208 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007209 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007210 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007212
Gilles Peskine8817f612018-12-18 00:18:46 +01007213 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007214
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007215 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7216 psa_set_key_algorithm( &attributes, alg );
7217 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007218
Gilles Peskine049c7532019-05-15 20:22:09 +02007219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007220 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007221
gabor-mezei-armceface22021-01-21 12:26:17 +01007222 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7223 key_bits = psa_get_key_bits( &attributes );
7224
7225 /* Determine the maximum ciphertext length */
7226 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007227 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007228 ASSERT_ALLOC( output, output_size );
7229
Ronald Cron5425a212020-08-04 14:58:35 +02007230 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007231 input_data->x, input_data->len,
7232 label->x, label->len,
7233 output,
7234 output_size,
7235 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007236 ASSERT_COMPARE( expected_data->x, expected_data->len,
7237 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007238
Gilles Peskine68428122018-06-30 18:42:41 +02007239 /* If the label is empty, the test framework puts a non-null pointer
7240 * in label->x. Test that a null pointer works as well. */
7241 if( label->len == 0 )
7242 {
7243 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007244 if( output_size != 0 )
7245 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007246 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007247 input_data->x, input_data->len,
7248 NULL, label->len,
7249 output,
7250 output_size,
7251 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007252 ASSERT_COMPARE( expected_data->x, expected_data->len,
7253 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007254 }
7255
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007257 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007258 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007259 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007260 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261}
7262/* END_CASE */
7263
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007264/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007265void asymmetric_decrypt_fail( int key_type_arg,
7266 data_t *key_data,
7267 int alg_arg,
7268 data_t *input_data,
7269 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007270 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007271 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007272{
Ronald Cron5425a212020-08-04 14:58:35 +02007273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007274 psa_key_type_t key_type = key_type_arg;
7275 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007276 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007277 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007278 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007279 psa_status_t actual_status;
7280 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007282
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007283 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007284
Gilles Peskine8817f612018-12-18 00:18:46 +01007285 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007286
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007287 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7288 psa_set_key_algorithm( &attributes, alg );
7289 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007290
Gilles Peskine049c7532019-05-15 20:22:09 +02007291 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007292 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007293
Ronald Cron5425a212020-08-04 14:58:35 +02007294 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007295 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007296 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007297 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007298 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007299 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007300 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007301
Gilles Peskine68428122018-06-30 18:42:41 +02007302 /* If the label is empty, the test framework puts a non-null pointer
7303 * in label->x. Test that a null pointer works as well. */
7304 if( label->len == 0 )
7305 {
7306 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007307 if( output_size != 0 )
7308 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007309 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007310 input_data->x, input_data->len,
7311 NULL, label->len,
7312 output, output_size,
7313 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007314 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007315 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007316 }
7317
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007318exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007319 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007320 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007321 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007322 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007323}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007324/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007325
7326/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007327void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007328{
7329 /* Test each valid way of initializing the object, except for `= {0}`, as
7330 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7331 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007332 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007333 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007334 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7335 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7336 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007337
7338 memset( &zero, 0, sizeof( zero ) );
7339
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007340 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007341 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007342 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007343 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007344 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007345 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007346 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007347
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007348 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007349 PSA_ASSERT( psa_key_derivation_abort(&func) );
7350 PSA_ASSERT( psa_key_derivation_abort(&init) );
7351 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007352}
7353/* END_CASE */
7354
Janos Follath16de4a42019-06-13 16:32:24 +01007355/* BEGIN_CASE */
7356void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007357{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007358 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007359 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007360 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007361
Gilles Peskine8817f612018-12-18 00:18:46 +01007362 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007363
Janos Follath16de4a42019-06-13 16:32:24 +01007364 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007365 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007366
7367exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007368 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007369 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007370}
7371/* END_CASE */
7372
Janos Follathaf3c2a02019-06-12 12:34:34 +01007373/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007374void derive_set_capacity( int alg_arg, int capacity_arg,
7375 int expected_status_arg )
7376{
7377 psa_algorithm_t alg = alg_arg;
7378 size_t capacity = capacity_arg;
7379 psa_status_t expected_status = expected_status_arg;
7380 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7381
7382 PSA_ASSERT( psa_crypto_init( ) );
7383
7384 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7385
7386 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7387 expected_status );
7388
7389exit:
7390 psa_key_derivation_abort( &operation );
7391 PSA_DONE( );
7392}
7393/* END_CASE */
7394
7395/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007396void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007397 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007398 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007399 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007400 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007401 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007402 int expected_status_arg3,
7403 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007404{
7405 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007406 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7407 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007408 psa_status_t expected_statuses[] = {expected_status_arg1,
7409 expected_status_arg2,
7410 expected_status_arg3};
7411 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007412 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7413 MBEDTLS_SVC_KEY_ID_INIT,
7414 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007415 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7417 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007418 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007419 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007420 psa_status_t expected_output_status = expected_output_status_arg;
7421 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007422
7423 PSA_ASSERT( psa_crypto_init( ) );
7424
7425 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7426 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007427
7428 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7429
7430 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7431 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007432 mbedtls_test_set_step( i );
7433 if( steps[i] == 0 )
7434 {
7435 /* Skip this step */
7436 }
7437 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007438 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007439 psa_set_key_type( &attributes, key_types[i] );
7440 PSA_ASSERT( psa_import_key( &attributes,
7441 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007442 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007443 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7444 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7445 {
7446 // When taking a private key as secret input, use key agreement
7447 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007448 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7449 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007450 expected_statuses[i] );
7451 }
7452 else
7453 {
7454 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007455 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007456 expected_statuses[i] );
7457 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007458 }
7459 else
7460 {
7461 TEST_EQUAL( psa_key_derivation_input_bytes(
7462 &operation, steps[i],
7463 inputs[i]->x, inputs[i]->len ),
7464 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007465 }
7466 }
7467
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007468 if( output_key_type != PSA_KEY_TYPE_NONE )
7469 {
7470 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007471 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007472 psa_set_key_bits( &attributes, 8 );
7473 actual_output_status =
7474 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007475 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007476 }
7477 else
7478 {
7479 uint8_t buffer[1];
7480 actual_output_status =
7481 psa_key_derivation_output_bytes( &operation,
7482 buffer, sizeof( buffer ) );
7483 }
7484 TEST_EQUAL( actual_output_status, expected_output_status );
7485
Janos Follathaf3c2a02019-06-12 12:34:34 +01007486exit:
7487 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007488 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7489 psa_destroy_key( keys[i] );
7490 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007491 PSA_DONE( );
7492}
7493/* END_CASE */
7494
Janos Follathd958bb72019-07-03 15:02:16 +01007495/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007496void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007497{
Janos Follathd958bb72019-07-03 15:02:16 +01007498 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007500 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007501 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007502 unsigned char input1[] = "Input 1";
7503 size_t input1_length = sizeof( input1 );
7504 unsigned char input2[] = "Input 2";
7505 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007506 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007507 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007508 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7509 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7510 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007512
Gilles Peskine8817f612018-12-18 00:18:46 +01007513 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007514
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007515 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7516 psa_set_key_algorithm( &attributes, alg );
7517 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007518
Gilles Peskine73676cb2019-05-15 20:15:10 +02007519 PSA_ASSERT( psa_import_key( &attributes,
7520 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007521 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007522
7523 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007524 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7525 input1, input1_length,
7526 input2, input2_length,
7527 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007528 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007529
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007530 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007531 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007532 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007533
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007534 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007535
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007536 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007537 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007538
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007539exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007540 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007541 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007542 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007543}
7544/* END_CASE */
7545
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007546/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007547void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007548{
7549 uint8_t output_buffer[16];
7550 size_t buffer_size = 16;
7551 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007552 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007553
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007554 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7555 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007556 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007557
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007558 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007559 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007560
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007561 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007562
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007563 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7564 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007565 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007566
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007567 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007568 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007569
7570exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007571 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007572}
7573/* END_CASE */
7574
7575/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007576void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007577 int step1_arg, data_t *input1, int expected_status_arg1,
7578 int step2_arg, data_t *input2, int expected_status_arg2,
7579 int step3_arg, data_t *input3, int expected_status_arg3,
7580 int step4_arg, data_t *input4, int expected_status_arg4,
7581 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007582 int requested_capacity_arg,
7583 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007584 data_t *expected_output2,
7585 int other_key_input_type,
7586 int key_input_type,
7587 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007588{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007589 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007590 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7591 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007592 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7593 MBEDTLS_SVC_KEY_ID_INIT,
7594 MBEDTLS_SVC_KEY_ID_INIT,
7595 MBEDTLS_SVC_KEY_ID_INIT};
7596 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7597 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007598 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007599 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007600 uint8_t *expected_outputs[2] =
7601 {expected_output1->x, expected_output2->x};
7602 size_t output_sizes[2] =
7603 {expected_output1->len, expected_output2->len};
7604 size_t output_buffer_size = 0;
7605 uint8_t *output_buffer = NULL;
7606 size_t expected_capacity;
7607 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007608 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7609 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7610 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7611 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007612 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007613 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007614 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007615
7616 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7617 {
7618 if( output_sizes[i] > output_buffer_size )
7619 output_buffer_size = output_sizes[i];
7620 if( output_sizes[i] == 0 )
7621 expected_outputs[i] = NULL;
7622 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007623 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007624 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007625
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007626 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007627 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7628 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7629 requested_capacity ) );
7630 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007631 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007632 switch( steps[i] )
7633 {
7634 case 0:
7635 break;
7636 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007637 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007638 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007639 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007640 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007641 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007642 inputs[i]->x, inputs[i]->len ),
7643 statuses[i] );
7644
7645 if( statuses[i] != PSA_SUCCESS )
7646 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007647 break;
7648 case 1: // input key
7649 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7650 psa_set_key_algorithm( &attributes1, alg );
7651 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7652
7653 PSA_ASSERT( psa_import_key( &attributes1,
7654 inputs[i]->x, inputs[i]->len,
7655 &keys[i] ) );
7656
Przemek Stekiel38647de2022-04-19 13:27:47 +02007657 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007658 {
7659 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007660 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7661 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007662 }
7663
Przemek Stekiel38647de2022-04-19 13:27:47 +02007664 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7665 steps[i],
7666 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007667 break;
7668 default:
7669 TEST_ASSERT( ! "default case not supported" );
7670 break;
7671 }
7672 break;
7673 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007674 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007675 {
7676 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007677 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7678 steps[i],
7679 inputs[i]->x,
7680 inputs[i]->len ),
7681 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007682 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007683 case 1: // input key, type DERIVE
7684 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007685 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7686 psa_set_key_algorithm( &attributes2, alg );
7687 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7688
7689 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007690 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007691 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7692
7693 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007694 inputs[i]->x, inputs[i]->len,
7695 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007696
Przemek Stekiel38647de2022-04-19 13:27:47 +02007697 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7698 steps[i],
7699 keys[i] ),
7700 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007701 break;
7702 case 2: // key agreement
7703 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7704 psa_set_key_algorithm( &attributes3, alg );
7705 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7706
7707 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007708 inputs[i]->x, inputs[i]->len,
7709 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007710
7711 TEST_EQUAL( psa_key_derivation_key_agreement(
7712 &operation,
7713 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7714 keys[i], key_agreement_peer_key->x,
7715 key_agreement_peer_key->len ), statuses[i] );
7716 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007717 default:
7718 TEST_ASSERT( ! "default case not supported" );
7719 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007720 }
7721
Przemek Stekiel38647de2022-04-19 13:27:47 +02007722 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007723 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007724 break;
7725 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007726 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007727 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007728 inputs[i]->x, inputs[i]->len ), statuses[i] );
7729
7730 if( statuses[i] != PSA_SUCCESS )
7731 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007732 break;
7733 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007734 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007735
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007736 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007737 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007738 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007739 expected_capacity = requested_capacity;
7740
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007741 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007742 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007743 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7744
7745 /* For output key derivation secret must be provided using
7746 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007747 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007748 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007749
7750 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7751 psa_set_key_algorithm( &attributes4, alg );
7752 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007753 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007754
7755 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007756 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007757 }
7758 else // output bytes
7759 {
7760 /* Expansion phase. */
7761 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007762 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007763 /* Read some bytes. */
7764 status = psa_key_derivation_output_bytes( &operation,
7765 output_buffer, output_sizes[i] );
7766 if( expected_capacity == 0 && output_sizes[i] == 0 )
7767 {
7768 /* Reading 0 bytes when 0 bytes are available can go either way. */
7769 TEST_ASSERT( status == PSA_SUCCESS ||
7770 status == PSA_ERROR_INSUFFICIENT_DATA );
7771 continue;
7772 }
7773 else if( expected_capacity == 0 ||
7774 output_sizes[i] > expected_capacity )
7775 {
7776 /* Capacity exceeded. */
7777 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7778 expected_capacity = 0;
7779 continue;
7780 }
7781 /* Success. Check the read data. */
7782 PSA_ASSERT( status );
7783 if( output_sizes[i] != 0 )
7784 ASSERT_COMPARE( output_buffer, output_sizes[i],
7785 expected_outputs[i], output_sizes[i] );
7786 /* Check the operation status. */
7787 expected_capacity -= output_sizes[i];
7788 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7789 &current_capacity ) );
7790 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007791 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007792 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007793 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007794
7795exit:
7796 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007797 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007798 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7799 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007800 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007801 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007802}
7803/* END_CASE */
7804
7805/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007806void derive_full( int alg_arg,
7807 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007808 data_t *input1,
7809 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007810 int requested_capacity_arg )
7811{
Ronald Cron5425a212020-08-04 14:58:35 +02007812 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007813 psa_algorithm_t alg = alg_arg;
7814 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007815 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007816 unsigned char output_buffer[16];
7817 size_t expected_capacity = requested_capacity;
7818 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007820
Gilles Peskine8817f612018-12-18 00:18:46 +01007821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007822
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007823 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7824 psa_set_key_algorithm( &attributes, alg );
7825 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007826
Gilles Peskine049c7532019-05-15 20:22:09 +02007827 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007828 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007829
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007830 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7831 input1->x, input1->len,
7832 input2->x, input2->len,
7833 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007834 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007835
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007836 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007837 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007838 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007839
7840 /* Expansion phase. */
7841 while( current_capacity > 0 )
7842 {
7843 size_t read_size = sizeof( output_buffer );
7844 if( read_size > current_capacity )
7845 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007846 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007847 output_buffer,
7848 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007849 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007850 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007851 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007852 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007853 }
7854
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007855 /* Check that the operation refuses to go over capacity. */
7856 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007857 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007858
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007859 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007860
7861exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007862 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007863 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007864 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007865}
7866/* END_CASE */
7867
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007868/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007869void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007870 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007871 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007872 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007873 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007874{
7875 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7876 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007877 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007878 uint8_t *output_buffer = NULL;
7879 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007880 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7881 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7882 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007883
7884 ASSERT_ALLOC( output_buffer, expected_output->len );
7885 PSA_ASSERT( psa_crypto_init() );
7886
7887 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007888 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007889 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007890
7891 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007892 step, input->x, input->len ),
7893 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007894
7895 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7896 goto exit;
7897
7898 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7899 expected_output->len );
7900
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007901 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007902 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7903 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7904 expected_output->len );
7905
7906exit:
7907 mbedtls_free( output_buffer );
7908 psa_key_derivation_abort( &operation );
7909 PSA_DONE();
7910}
7911/* END_CASE */
7912
Janos Follathe60c9052019-07-03 13:51:30 +01007913/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007914void derive_key_exercise( int alg_arg,
7915 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007916 data_t *input1,
7917 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007918 int derived_type_arg,
7919 int derived_bits_arg,
7920 int derived_usage_arg,
7921 int derived_alg_arg )
7922{
Ronald Cron5425a212020-08-04 14:58:35 +02007923 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7924 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007925 psa_algorithm_t alg = alg_arg;
7926 psa_key_type_t derived_type = derived_type_arg;
7927 size_t derived_bits = derived_bits_arg;
7928 psa_key_usage_t derived_usage = derived_usage_arg;
7929 psa_algorithm_t derived_alg = derived_alg_arg;
7930 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007931 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007933 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007934
Gilles Peskine8817f612018-12-18 00:18:46 +01007935 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007936
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007937 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7938 psa_set_key_algorithm( &attributes, alg );
7939 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007940 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007941 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007942
7943 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007944 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7945 input1->x, input1->len,
7946 input2->x, input2->len,
7947 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007948 goto exit;
7949
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007950 psa_set_key_usage_flags( &attributes, derived_usage );
7951 psa_set_key_algorithm( &attributes, derived_alg );
7952 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007953 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007954 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007955 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007956
7957 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007958 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007959 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7960 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007961
7962 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007963 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007964 goto exit;
7965
7966exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007967 /*
7968 * Key attributes may have been returned by psa_get_key_attributes()
7969 * thus reset them as required.
7970 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007971 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007972
7973 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007974 psa_destroy_key( base_key );
7975 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007976 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007977}
7978/* END_CASE */
7979
Janos Follath42fd8882019-07-03 14:17:09 +01007980/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007981void derive_key_export( int alg_arg,
7982 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007983 data_t *input1,
7984 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007985 int bytes1_arg,
7986 int bytes2_arg )
7987{
Ronald Cron5425a212020-08-04 14:58:35 +02007988 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7989 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007990 psa_algorithm_t alg = alg_arg;
7991 size_t bytes1 = bytes1_arg;
7992 size_t bytes2 = bytes2_arg;
7993 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007994 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007995 uint8_t *output_buffer = NULL;
7996 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007997 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7998 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007999 size_t length;
8000
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008001 ASSERT_ALLOC( output_buffer, capacity );
8002 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01008003 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008004
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008005 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8006 psa_set_key_algorithm( &base_attributes, alg );
8007 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008008 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008009 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008010
8011 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008012 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8013 input1->x, input1->len,
8014 input2->x, input2->len,
8015 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008016 goto exit;
8017
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008018 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008019 output_buffer,
8020 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008021 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008022
8023 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008024 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8025 input1->x, input1->len,
8026 input2->x, input2->len,
8027 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008028 goto exit;
8029
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008030 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8031 psa_set_key_algorithm( &derived_attributes, 0 );
8032 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008033 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008034 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008035 &derived_key ) );
8036 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008037 export_buffer, bytes1,
8038 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008039 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02008040 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008041 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008042 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008043 &derived_key ) );
8044 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008045 export_buffer + bytes1, bytes2,
8046 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008047 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008048
8049 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008050 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8051 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008052
8053exit:
8054 mbedtls_free( output_buffer );
8055 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008056 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008057 psa_destroy_key( base_key );
8058 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008059 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008060}
8061/* END_CASE */
8062
8063/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008064void derive_key_type( int alg_arg,
8065 data_t *key_data,
8066 data_t *input1,
8067 data_t *input2,
8068 int key_type_arg, int bits_arg,
8069 data_t *expected_export )
8070{
8071 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8072 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8073 const psa_algorithm_t alg = alg_arg;
8074 const psa_key_type_t key_type = key_type_arg;
8075 const size_t bits = bits_arg;
8076 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8077 const size_t export_buffer_size =
8078 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8079 uint8_t *export_buffer = NULL;
8080 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8081 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8082 size_t export_length;
8083
8084 ASSERT_ALLOC( export_buffer, export_buffer_size );
8085 PSA_ASSERT( psa_crypto_init( ) );
8086
8087 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8088 psa_set_key_algorithm( &base_attributes, alg );
8089 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8090 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8091 &base_key ) );
8092
Przemek Stekielc85f0912022-03-08 11:37:54 +01008093 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008094 &operation, base_key, alg,
8095 input1->x, input1->len,
8096 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008097 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008098 goto exit;
8099
8100 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8101 psa_set_key_algorithm( &derived_attributes, 0 );
8102 psa_set_key_type( &derived_attributes, key_type );
8103 psa_set_key_bits( &derived_attributes, bits );
8104 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8105 &derived_key ) );
8106
8107 PSA_ASSERT( psa_export_key( derived_key,
8108 export_buffer, export_buffer_size,
8109 &export_length ) );
8110 ASSERT_COMPARE( export_buffer, export_length,
8111 expected_export->x, expected_export->len );
8112
8113exit:
8114 mbedtls_free( export_buffer );
8115 psa_key_derivation_abort( &operation );
8116 psa_destroy_key( base_key );
8117 psa_destroy_key( derived_key );
8118 PSA_DONE( );
8119}
8120/* END_CASE */
8121
8122/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008123void derive_key( int alg_arg,
8124 data_t *key_data, data_t *input1, data_t *input2,
8125 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008126 int expected_status_arg,
8127 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008128{
Ronald Cron5425a212020-08-04 14:58:35 +02008129 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8130 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008131 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008132 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008133 size_t bits = bits_arg;
8134 psa_status_t expected_status = expected_status_arg;
8135 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8136 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8137 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8138
8139 PSA_ASSERT( psa_crypto_init( ) );
8140
8141 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8142 psa_set_key_algorithm( &base_attributes, alg );
8143 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8144 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008145 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008146
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008147 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8148 input1->x, input1->len,
8149 input2->x, input2->len,
8150 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008151 goto exit;
8152
8153 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8154 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008155 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008156 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008157
8158 psa_status_t status =
8159 psa_key_derivation_output_key( &derived_attributes,
8160 &operation,
8161 &derived_key );
8162 if( is_large_output > 0 )
8163 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8164 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008165
8166exit:
8167 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008168 psa_destroy_key( base_key );
8169 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008170 PSA_DONE( );
8171}
8172/* END_CASE */
8173
8174/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008175void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008176 int our_key_type_arg, int our_key_alg_arg,
8177 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008178 int expected_status_arg )
8179{
Ronald Cron5425a212020-08-04 14:58:35 +02008180 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008181 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008182 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008183 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008184 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008186 psa_status_t expected_status = expected_status_arg;
8187 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008188
Gilles Peskine8817f612018-12-18 00:18:46 +01008189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008190
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008191 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008192 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008193 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008194 PSA_ASSERT( psa_import_key( &attributes,
8195 our_key_data->x, our_key_data->len,
8196 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008197
Gilles Peskine77f40d82019-04-11 21:27:06 +02008198 /* The tests currently include inputs that should fail at either step.
8199 * Test cases that fail at the setup step should be changed to call
8200 * key_derivation_setup instead, and this function should be renamed
8201 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008202 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008203 if( status == PSA_SUCCESS )
8204 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008205 TEST_EQUAL( psa_key_derivation_key_agreement(
8206 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8207 our_key,
8208 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008209 expected_status );
8210 }
8211 else
8212 {
8213 TEST_ASSERT( status == expected_status );
8214 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008215
8216exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008217 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008218 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008219 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008220}
8221/* END_CASE */
8222
8223/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008224void raw_key_agreement( int alg_arg,
8225 int our_key_type_arg, data_t *our_key_data,
8226 data_t *peer_key_data,
8227 data_t *expected_output )
8228{
Ronald Cron5425a212020-08-04 14:58:35 +02008229 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008230 psa_algorithm_t alg = alg_arg;
8231 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008233 unsigned char *output = NULL;
8234 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008235 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008236
Gilles Peskinef0cba732019-04-11 22:12:38 +02008237 PSA_ASSERT( psa_crypto_init( ) );
8238
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008239 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8240 psa_set_key_algorithm( &attributes, alg );
8241 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008242 PSA_ASSERT( psa_import_key( &attributes,
8243 our_key_data->x, our_key_data->len,
8244 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008245
gabor-mezei-armceface22021-01-21 12:26:17 +01008246 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8247 key_bits = psa_get_key_bits( &attributes );
8248
Gilles Peskine992bee82022-04-13 23:25:52 +02008249 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008250 TEST_LE_U( expected_output->len,
8251 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8252 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8253 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008254
8255 /* Good case with exact output size */
8256 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008257 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8258 peer_key_data->x, peer_key_data->len,
8259 output, expected_output->len,
8260 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008261 ASSERT_COMPARE( output, output_length,
8262 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008263 mbedtls_free( output );
8264 output = NULL;
8265 output_length = ~0;
8266
8267 /* Larger buffer */
8268 ASSERT_ALLOC( output, expected_output->len + 1 );
8269 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8270 peer_key_data->x, peer_key_data->len,
8271 output, expected_output->len + 1,
8272 &output_length ) );
8273 ASSERT_COMPARE( output, output_length,
8274 expected_output->x, expected_output->len );
8275 mbedtls_free( output );
8276 output = NULL;
8277 output_length = ~0;
8278
8279 /* Buffer too small */
8280 ASSERT_ALLOC( output, expected_output->len - 1 );
8281 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8282 peer_key_data->x, peer_key_data->len,
8283 output, expected_output->len - 1,
8284 &output_length ),
8285 PSA_ERROR_BUFFER_TOO_SMALL );
8286 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008287 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008288 mbedtls_free( output );
8289 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008290
8291exit:
8292 mbedtls_free( output );
8293 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008294 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008295}
8296/* END_CASE */
8297
8298/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008299void key_agreement_capacity( int alg_arg,
8300 int our_key_type_arg, data_t *our_key_data,
8301 data_t *peer_key_data,
8302 int expected_capacity_arg )
8303{
Ronald Cron5425a212020-08-04 14:58:35 +02008304 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008305 psa_algorithm_t alg = alg_arg;
8306 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008307 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008309 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008310 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008311
Gilles Peskine8817f612018-12-18 00:18:46 +01008312 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008313
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008314 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8315 psa_set_key_algorithm( &attributes, alg );
8316 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008317 PSA_ASSERT( psa_import_key( &attributes,
8318 our_key_data->x, our_key_data->len,
8319 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008320
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008321 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008322 PSA_ASSERT( psa_key_derivation_key_agreement(
8323 &operation,
8324 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8325 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008326 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8327 {
8328 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008329 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008330 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008331 NULL, 0 ) );
8332 }
Gilles Peskine59685592018-09-18 12:11:34 +02008333
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008334 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008335 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008336 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008337 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008338
Gilles Peskinebf491972018-10-25 22:36:12 +02008339 /* Test the actual capacity by reading the output. */
8340 while( actual_capacity > sizeof( output ) )
8341 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008342 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008343 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008344 actual_capacity -= sizeof( output );
8345 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008346 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008347 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008348 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008349 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008350
Gilles Peskine59685592018-09-18 12:11:34 +02008351exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008352 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008353 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008354 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008355}
8356/* END_CASE */
8357
8358/* BEGIN_CASE */
8359void key_agreement_output( int alg_arg,
8360 int our_key_type_arg, data_t *our_key_data,
8361 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008362 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008363{
Ronald Cron5425a212020-08-04 14:58:35 +02008364 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008365 psa_algorithm_t alg = alg_arg;
8366 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008367 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008369 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008370
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008371 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8372 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008373
Gilles Peskine8817f612018-12-18 00:18:46 +01008374 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008375
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008376 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8377 psa_set_key_algorithm( &attributes, alg );
8378 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008379 PSA_ASSERT( psa_import_key( &attributes,
8380 our_key_data->x, our_key_data->len,
8381 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008382
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008383 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008384 PSA_ASSERT( psa_key_derivation_key_agreement(
8385 &operation,
8386 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8387 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008388 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8389 {
8390 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008391 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008392 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008393 NULL, 0 ) );
8394 }
Gilles Peskine59685592018-09-18 12:11:34 +02008395
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008396 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008397 actual_output,
8398 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008399 ASSERT_COMPARE( actual_output, expected_output1->len,
8400 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008401 if( expected_output2->len != 0 )
8402 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008403 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008404 actual_output,
8405 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008406 ASSERT_COMPARE( actual_output, expected_output2->len,
8407 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008408 }
Gilles Peskine59685592018-09-18 12:11:34 +02008409
8410exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008411 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008412 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008413 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008414 mbedtls_free( actual_output );
8415}
8416/* END_CASE */
8417
8418/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008419void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008420{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008421 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008422 unsigned char *output = NULL;
8423 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008424 size_t i;
8425 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008426
Simon Butcher49f8e312020-03-03 15:51:50 +00008427 TEST_ASSERT( bytes_arg >= 0 );
8428
Gilles Peskine91892022021-02-08 19:50:26 +01008429 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008430 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008431
Gilles Peskine8817f612018-12-18 00:18:46 +01008432 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008433
Gilles Peskinea50d7392018-06-21 10:22:13 +02008434 /* Run several times, to ensure that every output byte will be
8435 * nonzero at least once with overwhelming probability
8436 * (2^(-8*number_of_runs)). */
8437 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008438 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008439 if( bytes != 0 )
8440 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008441 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008442
Gilles Peskinea50d7392018-06-21 10:22:13 +02008443 for( i = 0; i < bytes; i++ )
8444 {
8445 if( output[i] != 0 )
8446 ++changed[i];
8447 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008448 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008449
8450 /* Check that every byte was changed to nonzero at least once. This
8451 * validates that psa_generate_random is overwriting every byte of
8452 * the output buffer. */
8453 for( i = 0; i < bytes; i++ )
8454 {
8455 TEST_ASSERT( changed[i] != 0 );
8456 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008457
8458exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008459 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008460 mbedtls_free( output );
8461 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008462}
8463/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008464
8465/* BEGIN_CASE */
8466void generate_key( int type_arg,
8467 int bits_arg,
8468 int usage_arg,
8469 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008470 int expected_status_arg,
8471 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008472{
Ronald Cron5425a212020-08-04 14:58:35 +02008473 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008474 psa_key_type_t type = type_arg;
8475 psa_key_usage_t usage = usage_arg;
8476 size_t bits = bits_arg;
8477 psa_algorithm_t alg = alg_arg;
8478 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008480 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008481
Gilles Peskine8817f612018-12-18 00:18:46 +01008482 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008483
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008484 psa_set_key_usage_flags( &attributes, usage );
8485 psa_set_key_algorithm( &attributes, alg );
8486 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008487 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008488
8489 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008490 psa_status_t status = psa_generate_key( &attributes, &key );
8491
8492 if( is_large_key > 0 )
8493 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8494 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008495 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008496 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008497
8498 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008499 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008500 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8501 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008502
Gilles Peskine818ca122018-06-20 18:16:48 +02008503 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008504 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008505 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008506
8507exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008508 /*
8509 * Key attributes may have been returned by psa_get_key_attributes()
8510 * thus reset them as required.
8511 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008512 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008513
Ronald Cron5425a212020-08-04 14:58:35 +02008514 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008515 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008516}
8517/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008518
Ronald Cronee414c72021-03-18 18:50:08 +01008519/* 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 +02008520void generate_key_rsa( int bits_arg,
8521 data_t *e_arg,
8522 int expected_status_arg )
8523{
Ronald Cron5425a212020-08-04 14:58:35 +02008524 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008525 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008526 size_t bits = bits_arg;
8527 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8528 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8529 psa_status_t expected_status = expected_status_arg;
8530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8531 uint8_t *exported = NULL;
8532 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008533 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008534 size_t exported_length = SIZE_MAX;
8535 uint8_t *e_read_buffer = NULL;
8536 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008537 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008538 size_t e_read_length = SIZE_MAX;
8539
8540 if( e_arg->len == 0 ||
8541 ( e_arg->len == 3 &&
8542 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8543 {
8544 is_default_public_exponent = 1;
8545 e_read_size = 0;
8546 }
8547 ASSERT_ALLOC( e_read_buffer, e_read_size );
8548 ASSERT_ALLOC( exported, exported_size );
8549
8550 PSA_ASSERT( psa_crypto_init( ) );
8551
8552 psa_set_key_usage_flags( &attributes, usage );
8553 psa_set_key_algorithm( &attributes, alg );
8554 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8555 e_arg->x, e_arg->len ) );
8556 psa_set_key_bits( &attributes, bits );
8557
8558 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008559 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008560 if( expected_status != PSA_SUCCESS )
8561 goto exit;
8562
8563 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008564 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008565 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8566 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8567 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8568 e_read_buffer, e_read_size,
8569 &e_read_length ) );
8570 if( is_default_public_exponent )
8571 TEST_EQUAL( e_read_length, 0 );
8572 else
8573 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8574
8575 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008576 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008577 goto exit;
8578
8579 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008580 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008581 exported, exported_size,
8582 &exported_length ) );
8583 {
8584 uint8_t *p = exported;
8585 uint8_t *end = exported + exported_length;
8586 size_t len;
8587 /* RSAPublicKey ::= SEQUENCE {
8588 * modulus INTEGER, -- n
8589 * publicExponent INTEGER } -- e
8590 */
8591 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008592 MBEDTLS_ASN1_SEQUENCE |
8593 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008594 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008595 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8596 MBEDTLS_ASN1_INTEGER ) );
8597 if( len >= 1 && p[0] == 0 )
8598 {
8599 ++p;
8600 --len;
8601 }
8602 if( e_arg->len == 0 )
8603 {
8604 TEST_EQUAL( len, 3 );
8605 TEST_EQUAL( p[0], 1 );
8606 TEST_EQUAL( p[1], 0 );
8607 TEST_EQUAL( p[2], 1 );
8608 }
8609 else
8610 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8611 }
8612
8613exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008614 /*
8615 * Key attributes may have been returned by psa_get_key_attributes() or
8616 * set by psa_set_key_domain_parameters() thus reset them as required.
8617 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008618 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008619
Ronald Cron5425a212020-08-04 14:58:35 +02008620 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008621 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008622 mbedtls_free( e_read_buffer );
8623 mbedtls_free( exported );
8624}
8625/* END_CASE */
8626
Darryl Greend49a4992018-06-18 17:27:26 +01008627/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008628void persistent_key_load_key_from_storage( data_t *data,
8629 int type_arg, int bits_arg,
8630 int usage_flags_arg, int alg_arg,
8631 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008632{
Ronald Cron71016a92020-08-28 19:01:50 +02008633 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8636 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008637 psa_key_type_t type = type_arg;
8638 size_t bits = bits_arg;
8639 psa_key_usage_t usage_flags = usage_flags_arg;
8640 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008641 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008642 unsigned char *first_export = NULL;
8643 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008644 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008645 size_t first_exported_length;
8646 size_t second_exported_length;
8647
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008648 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8649 {
8650 ASSERT_ALLOC( first_export, export_size );
8651 ASSERT_ALLOC( second_export, export_size );
8652 }
Darryl Greend49a4992018-06-18 17:27:26 +01008653
Gilles Peskine8817f612018-12-18 00:18:46 +01008654 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008655
Gilles Peskinec87af662019-05-15 16:12:22 +02008656 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008657 psa_set_key_usage_flags( &attributes, usage_flags );
8658 psa_set_key_algorithm( &attributes, alg );
8659 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008660 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008661
Darryl Green0c6575a2018-11-07 16:05:30 +00008662 switch( generation_method )
8663 {
8664 case IMPORT_KEY:
8665 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008666 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008667 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008668 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008669
Darryl Green0c6575a2018-11-07 16:05:30 +00008670 case GENERATE_KEY:
8671 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008672 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008673 break;
8674
8675 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008676#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008677 {
8678 /* Create base key */
8679 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8680 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8681 psa_set_key_usage_flags( &base_attributes,
8682 PSA_KEY_USAGE_DERIVE );
8683 psa_set_key_algorithm( &base_attributes, derive_alg );
8684 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008685 PSA_ASSERT( psa_import_key( &base_attributes,
8686 data->x, data->len,
8687 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008688 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008689 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008690 PSA_ASSERT( psa_key_derivation_input_key(
8691 &operation,
8692 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008693 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008694 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008695 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008696 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8697 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008698 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008699 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008700 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008701 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008702 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008703#else
8704 TEST_ASSUME( ! "KDF not supported in this configuration" );
8705#endif
8706 break;
8707
8708 default:
8709 TEST_ASSERT( ! "generation_method not implemented in test" );
8710 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008711 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008712 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008713
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008714 /* Export the key if permitted by the key policy. */
8715 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8716 {
Ronald Cron5425a212020-08-04 14:58:35 +02008717 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008718 first_export, export_size,
8719 &first_exported_length ) );
8720 if( generation_method == IMPORT_KEY )
8721 ASSERT_COMPARE( data->x, data->len,
8722 first_export, first_exported_length );
8723 }
Darryl Greend49a4992018-06-18 17:27:26 +01008724
8725 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008726 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008727 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008728 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008729
Darryl Greend49a4992018-06-18 17:27:26 +01008730 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008731 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008732 TEST_ASSERT( mbedtls_svc_key_id_equal(
8733 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008734 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8735 PSA_KEY_LIFETIME_PERSISTENT );
8736 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8737 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008738 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008739 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008740 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008741
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008742 /* Export the key again if permitted by the key policy. */
8743 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008744 {
Ronald Cron5425a212020-08-04 14:58:35 +02008745 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008746 second_export, export_size,
8747 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008748 ASSERT_COMPARE( first_export, first_exported_length,
8749 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008750 }
8751
8752 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008753 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008754 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008755
8756exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008757 /*
8758 * Key attributes may have been returned by psa_get_key_attributes()
8759 * thus reset them as required.
8760 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008761 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008762
Darryl Greend49a4992018-06-18 17:27:26 +01008763 mbedtls_free( first_export );
8764 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008765 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008766 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008767 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008768 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008769}
8770/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008771
Neil Armstronga557cb82022-06-10 08:58:32 +02008772/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008773void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8774 int primitive_arg, int hash_arg, int role_arg,
Valerio Setti1070aed2022-11-11 19:37:31 +01008775 int test_input, data_t *pw_data,
8776 int inj_err_type_arg,
8777 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008778{
8779 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8780 psa_pake_operation_t operation = psa_pake_operation_init();
8781 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008782 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008783 psa_key_type_t key_type_pw = key_type_pw_arg;
8784 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008785 psa_algorithm_t hash_alg = hash_arg;
8786 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8788 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01008789 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
8790 psa_status_t expected_error = expected_error_arg;
8791 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008792 unsigned char *output_buffer = NULL;
8793 size_t output_len = 0;
8794
8795 PSA_INIT( );
8796
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008797 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8798 PSA_PAKE_STEP_KEY_SHARE);
8799 ASSERT_ALLOC( output_buffer, buf_size );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008800
8801 if( pw_data->len > 0 )
8802 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008803 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008804 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008805 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008806 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8807 &key ) );
8808 }
8809
8810 psa_pake_cs_set_algorithm( &cipher_suite, alg );
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008811 psa_pake_cs_set_primitive( &cipher_suite, primitive );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008812 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8813
Neil Armstrong645cccd2022-06-08 17:36:23 +02008814 PSA_ASSERT( psa_pake_abort( &operation ) );
8815
Valerio Setti1070aed2022-11-11 19:37:31 +01008816 if ( inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS )
8817 {
8818 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8819 expected_error );
8820 PSA_ASSERT( psa_pake_abort( &operation ) );
8821 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8822 expected_error );
8823 PSA_ASSERT( psa_pake_abort( &operation ) );
8824 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8825 expected_error );
8826 PSA_ASSERT( psa_pake_abort( &operation ) );
8827 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8828 expected_error );
8829 PSA_ASSERT( psa_pake_abort( &operation ) );
8830 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8831 NULL, 0, NULL ),
8832 expected_error );
8833 PSA_ASSERT( psa_pake_abort( &operation ) );
8834 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
8835 expected_error );
8836 PSA_ASSERT( psa_pake_abort( &operation ) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008837 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008838 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008839
Valerio Setti1070aed2022-11-11 19:37:31 +01008840 status = psa_pake_setup( &operation, &cipher_suite );
8841 if (status != PSA_SUCCESS)
8842 {
8843 TEST_EQUAL( status, expected_error );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008844 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008845 }
8846
8847 if( inj_err_type == INJECT_ERR_DUPLICATE_SETUP )
8848 {
8849 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8850 expected_error );
8851 goto exit;
8852 }
8853
8854 status = psa_pake_set_role( &operation, role);
8855 if ( status != PSA_SUCCESS )
8856 {
8857 TEST_EQUAL( status, expected_error );
8858 goto exit;
8859 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008860
8861 if( pw_data->len > 0 )
8862 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008863 status = psa_pake_set_password_key( &operation, key );
8864 if ( status != PSA_SUCCESS )
8865 {
8866 TEST_EQUAL( status, expected_error );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008867 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008868 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008869 }
8870
Valerio Setti1070aed2022-11-11 19:37:31 +01008871 if ( inj_err_type == INJECT_ERR_INVALID_USER )
8872 {
8873 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8874 PSA_ERROR_INVALID_ARGUMENT );
8875 goto exit;
8876 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008877
Valerio Setti1070aed2022-11-11 19:37:31 +01008878 if ( inj_err_type == INJECT_ERR_INVALID_PEER )
8879 {
8880 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8881 PSA_ERROR_INVALID_ARGUMENT );
8882 goto exit;
8883 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008884
Valerio Setti1070aed2022-11-11 19:37:31 +01008885 if ( inj_err_type == INJECT_ERR_SET_USER )
8886 {
8887 const uint8_t unsupported_id[] = "abcd";
8888 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8889 PSA_ERROR_NOT_SUPPORTED );
8890 goto exit;
8891 }
8892
8893 if ( inj_err_type == INJECT_ERR_SET_PEER )
8894 {
8895 const uint8_t unsupported_id[] = "abcd";
8896 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8897 PSA_ERROR_NOT_SUPPORTED );
8898 goto exit;
8899 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008900
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008901 const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive,
8902 PSA_PAKE_STEP_KEY_SHARE );
8903 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive,
8904 PSA_PAKE_STEP_ZK_PUBLIC );
8905 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive,
8906 PSA_PAKE_STEP_ZK_PROOF );
8907
Valerio Setti1070aed2022-11-11 19:37:31 +01008908 if ( test_input )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008909 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008910 if ( inj_err_type == INJECT_EMPTY_IO_BUFFER )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008911 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008912 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0 ),
8913 PSA_ERROR_INVALID_ARGUMENT );
8914 goto exit;
8915 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008916
Valerio Setti1070aed2022-11-11 19:37:31 +01008917 if ( inj_err_type == INJECT_UNKNOWN_STEP )
8918 {
8919 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8920 output_buffer, size_zk_proof ),
8921 PSA_ERROR_INVALID_ARGUMENT );
8922 goto exit;
8923 }
8924
8925 if ( inj_err_type == INJECT_INVALID_FIRST_STEP )
8926 {
8927 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8928 output_buffer, size_zk_proof ),
8929 PSA_ERROR_BAD_STATE );
8930 goto exit;
8931 }
8932
8933 status = psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8934 output_buffer, size_key_share );
8935 if ( status != PSA_SUCCESS )
8936 {
8937 TEST_EQUAL( status, expected_error);
8938 goto exit;
8939 }
8940
8941 if ( inj_err_type == INJECT_WRONG_BUFFER_SIZE )
8942 {
8943 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8944 output_buffer, size_zk_public + 1 ),
8945 PSA_ERROR_INVALID_ARGUMENT );
8946 goto exit;
8947 }
8948
8949 if ( inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE )
8950 {
8951 // Just trigger any kind of error. We don't care about the result here
8952 psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8953 output_buffer, size_zk_public + 1 );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008954 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008955 output_buffer, size_zk_public ),
Valerio Setti1070aed2022-11-11 19:37:31 +01008956 PSA_ERROR_BAD_STATE );
8957 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008958 }
Valerio Setti1070aed2022-11-11 19:37:31 +01008959 } else {
8960 if ( inj_err_type == INJECT_EMPTY_IO_BUFFER )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008961 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008962 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8963 NULL, 0, NULL ),
8964 PSA_ERROR_INVALID_ARGUMENT );
8965 goto exit;
8966 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008967
Valerio Setti1070aed2022-11-11 19:37:31 +01008968 if ( inj_err_type == INJECT_UNKNOWN_STEP )
8969 {
8970 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8971 output_buffer, buf_size, &output_len ),
8972 PSA_ERROR_INVALID_ARGUMENT );
8973 goto exit;
8974 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008975
Valerio Setti1070aed2022-11-11 19:37:31 +01008976 if ( inj_err_type == INJECT_INVALID_FIRST_STEP )
8977 {
8978 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8979 output_buffer, buf_size, &output_len ),
8980 PSA_ERROR_BAD_STATE );
8981 goto exit;
8982 }
8983
8984 status = psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8985 output_buffer, buf_size, &output_len );
8986 if ( status != PSA_SUCCESS )
8987 {
8988 TEST_EQUAL( status, expected_error);
8989 goto exit;
8990 }
8991
8992 TEST_ASSERT( output_len > 0 );
8993
8994 if ( inj_err_type == INJECT_WRONG_BUFFER_SIZE )
8995 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008996 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Valerio Setti1070aed2022-11-11 19:37:31 +01008997 output_buffer, size_zk_public - 1, &output_len ),
8998 PSA_ERROR_BUFFER_TOO_SMALL );
8999 goto exit;
9000 }
9001
9002 if ( inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE )
9003 {
9004 // Just trigger any kind of error. We don't care about the result here
9005 psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
9006 output_buffer, size_zk_public - 1, &output_len );
9007 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
9008 output_buffer, buf_size, &output_len ),
9009 PSA_ERROR_BAD_STATE );
9010 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009011 }
9012 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009013
9014exit:
9015 PSA_ASSERT( psa_destroy_key( key ) );
9016 PSA_ASSERT( psa_pake_abort( &operation ) );
9017 mbedtls_free( output_buffer );
9018 PSA_DONE( );
9019}
9020/* END_CASE */
9021
Neil Armstronga557cb82022-06-10 08:58:32 +02009022/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009023void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
9024 int client_input_first, int inject_error,
9025 data_t *pw_data )
9026{
9027 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9028 psa_pake_operation_t server = psa_pake_operation_init();
9029 psa_pake_operation_t client = psa_pake_operation_init();
9030 psa_algorithm_t alg = alg_arg;
9031 psa_algorithm_t hash_alg = hash_arg;
9032 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9034
9035 PSA_INIT( );
9036
9037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9038 psa_set_key_algorithm( &attributes, alg );
9039 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9040 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9041 &key ) );
9042
9043 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9044 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9045 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9046
9047
9048 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9049 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9050
9051 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9052 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9053
9054 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9055 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9056
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009057 ecjpake_do_round( alg, primitive_arg, &server, &client,
9058 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009059
9060 if( inject_error == 1 || inject_error == 2 )
9061 goto exit;
9062
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009063 ecjpake_do_round( alg, primitive_arg, &server, &client,
9064 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009065
9066exit:
9067 psa_destroy_key( key );
9068 psa_pake_abort( &server );
9069 psa_pake_abort( &client );
9070 PSA_DONE( );
9071}
9072/* END_CASE */
9073
9074/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009075void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02009076 int derive_alg_arg, data_t *pw_data,
Valerio Setti1070aed2022-11-11 19:37:31 +01009077 int client_input_first, int inj_err_type_arg )
Neil Armstrongd597bc72022-05-25 11:28:39 +02009078{
9079 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9080 psa_pake_operation_t server = psa_pake_operation_init();
9081 psa_pake_operation_t client = psa_pake_operation_init();
9082 psa_algorithm_t alg = alg_arg;
9083 psa_algorithm_t hash_alg = hash_arg;
9084 psa_algorithm_t derive_alg = derive_alg_arg;
9085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9087 psa_key_derivation_operation_t server_derive =
9088 PSA_KEY_DERIVATION_OPERATION_INIT;
9089 psa_key_derivation_operation_t client_derive =
9090 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009091 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009092
9093 PSA_INIT( );
9094
Neil Armstrongd597bc72022-05-25 11:28:39 +02009095 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9096 psa_set_key_algorithm( &attributes, alg );
9097 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9098 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9099 &key ) );
9100
9101 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9102 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9103 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9104
Neil Armstrong1e855602022-06-15 11:32:11 +02009105 /* Get shared key */
9106 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
9107 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
9108
9109 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
9110 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
9111 {
9112 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
9113 PSA_KEY_DERIVATION_INPUT_SEED,
9114 (const uint8_t*) "", 0) );
9115 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
9116 PSA_KEY_DERIVATION_INPUT_SEED,
9117 (const uint8_t*) "", 0) );
9118 }
9119
Neil Armstrongd597bc72022-05-25 11:28:39 +02009120 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9121 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9122
9123 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9124 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9125
9126 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9127 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9128
Valerio Setti1070aed2022-11-11 19:37:31 +01009129 if( inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1 )
9130 {
9131 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009132 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009133 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009134 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009135 goto exit;
9136 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009137
Neil Armstrongf983caf2022-06-15 15:27:48 +02009138 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009139 ecjpake_do_round( alg, primitive_arg, &server, &client,
9140 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009141
Valerio Setti1070aed2022-11-11 19:37:31 +01009142 if ( inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2 )
9143 {
9144 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009145 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009146 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009147 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009148 goto exit;
9149 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009150
Neil Armstrongf983caf2022-06-15 15:27:48 +02009151 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009152 ecjpake_do_round( alg, primitive_arg, &server, &client,
9153 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009154
Neil Armstrongd597bc72022-05-25 11:28:39 +02009155 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9156 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9157
9158exit:
9159 psa_key_derivation_abort( &server_derive );
9160 psa_key_derivation_abort( &client_derive );
9161 psa_destroy_key( key );
9162 psa_pake_abort( &server );
9163 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009164 PSA_DONE( );
9165}
9166/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009167
9168/* BEGIN_CASE */
9169void ecjpake_size_macros( )
9170{
9171 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9172 const size_t bits = 256;
9173 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9174 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9175 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9176 PSA_ECC_FAMILY_SECP_R1 );
9177
9178 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9179 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9180 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9181 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9182 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9183 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9184 /* The output for ZK_PROOF is the same bitsize as the curve */
9185 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9186 PSA_BITS_TO_BYTES( bits ) );
9187
9188 /* Input sizes are the same as output sizes */
9189 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9190 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9191 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9192 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9193 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9194 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9195
9196 /* These inequalities will always hold even when other PAKEs are added */
9197 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9198 PSA_PAKE_OUTPUT_MAX_SIZE );
9199 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9200 PSA_PAKE_OUTPUT_MAX_SIZE );
9201 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9202 PSA_PAKE_OUTPUT_MAX_SIZE );
9203 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9204 PSA_PAKE_INPUT_MAX_SIZE );
9205 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9206 PSA_PAKE_INPUT_MAX_SIZE );
9207 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9208 PSA_PAKE_INPUT_MAX_SIZE );
9209}
9210/* END_CASE */